Jump to content

Frankenstein UDF v1.2 - IRC Functions, Time Conversion Functions, Token String Functions and other Misc Functions


coderusa
 Share

Recommended Posts

Latest Version: 1.2

11-26-2023

This is a UDF I've put together over my time using Autoit. Built on needs that I've come across while making scripts, and figure other people could maybe find use of it. Also contains _FileCreate and _IsPressed.

Version 1.2 Additions

Added some new IRC Functions, IRC_Set_ChanTopic, IRC_Chanserv, IRC_Nickserv, IRC_Change_Nick

Added some various time conversion functions, a little more singular than using _TicksToTime. Converts milliseconds, seconds, minutes. It does convert options into hours, but didn't come across a need to convert hours backwards, but will probably be a thing in future versions of this UDF.

Made some small refining changes to Token String Functions. No script breakage.

I am, currently, always adding new stuff and changes, and will post updates as changes are made or new functions added. It's not quite perfect, but it does work well. Some code could probably be refined, but as I stated, I'm always playing with this UDF.

Current Functions

Spoiler
; #CURRENT IRC Functions# =======================================================================================================
; IRC_Change_Nick
; IRC_Chanserv
; IRC_Command
; IRC_Join_Channel
; IRC_Kick_User
; IRC_Nickserv
; IRC_Send_Action
; IRC_Send_CTCP
; IRC_Send_Notice
; IRC_Send_PrivMsg
; IRC_Server_Connect
; IRC_Server_Disconnect
; IRC_Server_Ping
; IRC_Set_ChanMode
; IRC_Set_ChanTopic
; IRC_Set_UserMode
; IRC_Part_Channel
; ===============================================================================================================================
; #CURRENT Time Conversion Functions# ===========================================================================================
; MilSecConvertMsg
; MilSecToHour
; MilSecToMin
; MilSecToSec
; MinToHour
; SecToHour
; SecToMilSec
; SecToMin
;================================================================================================================================
; #CURRENT Token String Functions# ==============================================================================================
; AddTok
; DelTok
; GetTok
; NumTok
; PutTok
; RemTok
; RepTok
; ===============================================================================================================================
; #CURRENT Misc Functions# ======================================================================================================
; Cons
; _FileCreate
; Ini_Prc
; _IsPressed
; Parity
; UxTheme
; ===============================================================================================================================

 

Examples (UDF contains documentation on every function and how to use it.)

Time Conversion

Spoiler
#include <Frankenstein.au3>

Cons("MilSecConvertMsg - Converts 86388000 milliseconds to a statement of: " & MilSecConvertMsg(86388000))
Cons("MilSecToSec - Converts 8640000 milliseconds to: " & MilSecToSec(86400000) & " seconds")
Cons("MilSecToMin - Converts 60000 millseconds to " & MilSecToMin(60000) & " minutes")
Cons("MilSecToHour - Converts 5400000 milliseconds to " & MilSecToHour(5400000) & " hours")
Cons("SecToMin - Converts 86400 seconds to " & SecToMin(86400) & " minutes")
Cons("MinToHour - Converts 30 minutes to " & MinToHour(30) & " hours")
Cons("SecToMilSec - Converts 86400 seconds to " & SecToMilSec(86400) & " milliseconds")
Cons("SecToHour - Converts 3600 seconds to " & SecToHour(3600) & " hours")

 

Token Strings

Spoiler
#include <Frankenstein.au3>

;Token String Example
TokenExample("A,one,two,three,A,four,five,six,seven,eight,nine")

Func TokenExample($String)

    Cons("Example String: " & $String)

    $TS = AddTok($String, "A", "44")
    Cons("(AddTok) Add another 'A' to end of string: " & $TS)

    $TS = DelTok($TS, "A", "44")
    Cons("(DelTok) Delete all occurances of 'A' from string: " & $TS)

    Cons("(GetTok) Return 3rd token from string: " & GetTok($TS, "3", "44"))

    Cons("(NumTok) Total number of tokens in the string: " & NumTok($TS, "44"))

    $TS = PutTok($TS, "B", "44", "5")
    Cons("(PutTok) Put a 'B' into the 5th section of the string: " & $TS)

    $TS = RemTok($TS, "5", "44")
    Cons("(RemTok) Remove the 'B' from the 5th section of the string: " & $TS)

    $TS = RepTok($TS, "P", "44", "7")
    Cons("(RepTok) Replaces the 7th section of the string with 'P': " & $TS)
EndFunc

IRC Functions Example  (uses various functions from Frankenstein.au3)

Spoiler
; #SCRIPT# ======================================================================================================================
; Title .........: Basic IRC-Bot
; AutoIt Version : v3.3.16.1
; Script Version.: v1.0.0
; Language ......: English
; Description ...: Basic IRC bot script for building into full bot
; Functions .....: Bot functions listed at bottom of script.
; Includes ......: Requires Frankenstein UDF
; Author(s) .....: Neo_ (aka coderusa)
; ===============================================================================================================================
#include <Frankenstein.au3>

; #CONFIGURATION# ===============================================================================================================
; Description ...: For each constant input the server info, channels, BotMaster (your name), and BotAccess (ops)
; ===============================================================================================================================
Global Const $BotServer = "" ;irc server address (plain text)
Global Const $BotPort = "" ;irc server port (6667 is best, no SSL/SASL)
Global Const $BotName = "" ;Bot username
Global Const $ChanList = "" ;Bot Channels (#Channel1,#Channel2,#Channel3... etc) can be single or multiple channels
Global Const $BotMaster = "" ;Bot Masters (Username1,Username2,Username3... etc) can be single or multiple users
Global Const $BotAccess = "" ;Bot Access list (Username1,Username2,Username4... etc) can be single or multiple users
; ===============================================================================================================================

;Assigns UserList global variables based on $ChanList
;Every channel in $ChanList is assigned to $UserList#ChannelName
;for handling user names and OPs on channels
For $x = 1 To NumTok($ChanList, "44") Step 1
    $varname = "UserList" & StringTrimLeft(GetTok($ChanList, $x, "44"), 1)
    Assign($varname, " ", 2)
Next

;Set this to 1500ms or higher, otherwise closes socket too soon and fails to connect.
Opt("TCPTimeout", 1500)

TCPStartUp()

$sock = IRC_Server_Connect($BotServer, $BotPort, $BotName)

;Main Loop
While 1
    $recv = TCPRecv($sock, 9999)
    If @error = -1 Then
        Cons("ERROR: " & @error)
        Exit
    Else
        ConsoleWrite($recv)
    EndIf

    If $recv Then
        $sData = StringSplit($recv, @CRLF)
        For $i = 1 To $sData[0] Step 1
            $sTemp = StringSplit($sData[$i], Chr(32))

            If $sTemp[1] = "" Then
                ContinueLoop
            EndIf

            ;Server Ping? Pong!
            If $sTemp[1] = "PING" Then
                IRC_Server_Ping($sock, $sTemp[2])
                ContinueLoop
            EndIf

            If $sTemp[0] >= 2 Then

                ;End of MOTD --> Join bot channel(s)
                If $sTemp[2] = "376" Then
                    For $a = 1 To NumTok($ChanList, "44") Step 1
                        IRC_Join_Channel($sock, GetTok($ChanList, $a, "44"))
                        Sleep(500)
                        ContinueLoop
                    Next
                EndIf

                ;--------------------------------------------------------------------
                ;User name storage/update
                ;Each channel is stored in a global variable, $UserList#ChannelName
                ;Declared and set by Assign and Eval.
                ;--------------------------------------------------------------------

                ;When NAMES data is received (numeric 353 end of /Names list)
                If $sTemp[2] = "353" And IsBotChan($sTemp[5]) = True Then
                    $varname = "UserList" & StringTrimLeft($sTemp[5], 1)
                    Assign($varname, GetTok($sData[$i], "3", "58"), 2)
                    ContinueLoop
                EndIf

                ;Sends /NAMES request for updating user lists. Updates on MODE to update for Ops
                If $sTemp[2] = "MODE" And IsBotChan($sTemp[3]) = True Then
                    TCPSend($sock, "NAMES :" & $sTemp[3] & @CRLF)
                    Sleep(10)
                    ContinueLoop
                EndIf

                ;Sends /NAMES request for updating user lists. Updates on JOIN, PART and QUIT.
                If $sTemp[2] = "JOIN" Or $sTemp[2] = "PART" Or $sTemp[2] = "QUIT" Then
                    TCPSend($sock, "NAMES :" & $sTemp[3] & @CRLF)
                    Sleep(10)
                    ContinueLoop
                EndIf
            EndIf

            If $sTemp[0] >= 4 Then

                ;Text and PRIVMSG commands
                ;Parses data into 3 variables, $UserName, $MessageRecv and $BotChannel
                If $sTemp[2] = "PRIVMSG" And IsBotChan($sTemp[3]) = True Then
                    $UserName = StringTrimLeft(GetTok($sTemp[1], "1", "33"), 1)
                    $MessageRecv = RemTok(StringTrimLeft($sData[$i], 1), "1", "58")
                    $BotChannel = $sTemp[3]

                    ;Channel PRIVMSG Commands (Regular user commands)
                    If IsBotChan($BotChannel) = True Then

                        ;!help
                        If GetTok($MessageRecv, "1", "32") = "!help" Then
                            IRC_Send_PrivMsg($sock, $BotChannel, "Commands: !help, !slap, !op, !deop, !voice, !devoice, !kick, !ban, !kickban, !topic, !shutdown")
                            ContinueLoop
                        EndIf

                        ;!slap <username>
                        If GetTok($MessageRecv, "1", "32") = "!slap" And $sTemp[0] >= 3 Then
                            IRC_Send_Action($sock, $BotChannel, "slaps " & GetTok($MessageRecv, "2", "32") & " around a bit with a large trout.")
                            ContinueLoop
                        EndIf
                    EndIf

                    ;Channel PRIVMSG Commands (OP Commands)
                    If IsBotChan($BotChannel) = True And IsOp($UserName, $BotChannel) = True Or IsBotMaster($UserName) = True Then

                        ;Op commands ignored if bot is not OP on channel
                        If IsOp($BotName, $BotChannel) = False Then
                            ContinueLoop
                        EndIf

                        ;!op
                        If GetTok($MessageRecv, "1", "32") = "!op" Then
                            ;!op
                            If NumTok($MessageRecv, "32") = 1 Then
                                IRC_Set_UserMode($sock, $BotChannel, "+o", $UserName)
                                ContinueLoop
                            ;!op <username>
                            ElseIf NumTok($MessageRecv, "32") > 1 Then
                                IRC_Set_UserMode($sock, $BotChannel, "+o", GetTok($MessageRecv, "2", "32"))
                                ContinueLoop
                            EndIf
                        EndIf

                        ;!deop
                        If GetTok($MessageRecv, "1", "32") = "!deop" Then
                            ;!deop
                            If NumTok($MessageRecv, "32") = 1 Then
                                IRC_Set_UserMode($sock, $BotChannel, "-o", $UserName)
                                ContinueLoop
                            ;!deop <username>
                            ElseIf NumTok($MessageRecv, "32") > 1 Then
                                IRC_Set_UserMode($sock, $BotChannel, "-o", GetTok($MessageRecv, "2", "32"))
                                ContinueLoop
                            EndIf
                        EndIf

                        ;!voice
                        If GetTok($MessageRecv, "1", "32") = "!voice" And IsOp($UserName, $BotChannel) = True Then
                            ;!voice
                            If NumTok($MessageRecv, "32") = 1 Then
                                IRC_Set_UserMode($sock, $BotChannel, "+v", $UserName)
                                ContinueLoop
                            ;!voice <username>
                            ElseIf NumTok($MessageRecv, "32") > 1 Then
                                IRC_Set_UserMode($sock, $BotChannel, "+v", GetTok($MessageRecv, "2", "32"))
                                ContinueLoop
                            EndIf
                        EndIf

                        ;!devoice
                        If GetTok($MessageRecv, "1", "32") = "!devoice" Then
                            ;!devoice
                            If NumTok($MessageRecv, "32") = 1 Then
                                IRC_Set_UserMode($sock, $BotChannel, "-v", $UserName)
                                ContinueLoop
                            ;!devoice <username>
                            ElseIf NumTok($MessageRecv, "32") > 1 Then
                                IRC_Set_UserMode($sock, $BotChannel, "-v", GetTok($MessageRecv, "2", "32"))
                                ContinueLoop
                            EndIf
                        EndIf

                        ;!kick
                        If GetTok($MessageRecv, "1", "32") = "!kick" And NumTok($MessageRecv, "32") >= 2 Then
                            ;!kick
                            If NumTok($MessageRecv, "32") = 2 Then
                                IRC_Kick_User($sock, $BotChannel, GetTok($MessageRecv, "2", "32"), "KICK request by " & $UserName)
                                ContinueLoop
                            ;!kick <kick message>
                            ElseIf NumTok($MessageRecv, "32") > 2 Then
                                $KickMsg = RemTok($MessageRecv, "1", "32")
                                IRC_Kick_User($sock, $BotChannel, GetTok($MessageRecv, "2", "32"), RemTok($KickMsg, "1", "32"))
                                ContinueLoop
                            EndIf
                        EndIf

                        ;!ban <username or mask>
                        If GetTok($MessageRecv, "1", "32") = "!ban" And NumTok($MessageRecv, "32") >= 2 Then
                            IRC_Set_UserMode($sock, $BotChannel, "+b", GetTok($MessageRecv, "2", "32"))
                            ContinueLoop
                        EndIf

                        ;!unban <username or mask>
                        If GetTok($MessageRecv, "1", "32") = "!unban" And NumTok($MessageRecv, "32") >= 2 Then
                            IRC_Set_UserMode($sock, $BotChannel, "-b", GetTok($MessageRecv, "2", "32"))
                            ContinueLoop
                        EndIf

                        ;!kickban <username>
                        If GetTok($MessageRecv, "1", "32") = "!kickban" Or GetTok($MessageRecv, "1", "32") = "!kb" Then
                            IRC_Set_UserMode($sock, $BotChannel, "+b", GetTok($MessageRecv, "2", "32"))
                            IRC_Kick_User($sock, $BotChannel, GetTok($MessageRecv, "2", "32"), "KICK-BAN requested by " & $UserName & @CRLF)
                            ContinueLoop
                        EndIf

                        ;!topic <topic message> - Sets channel topic to <topic message>
                        If GetTok($MessageRecv, "1", "32") = "!topic" Then
                            TCPSend($sock, "TOPIC " & $BotChannel & " :" & RemTok($MessageRecv, "1", "32") & @CRLF)
                            ContinueLoop
                        EndIf

                        ;!shutdown - (BotMaster Only)
                        If GetTok($MessageRecv, "1", "32") = "!shutdown" And IsBotMaster($UserName) = True Then
                            IRC_Send_PrivMsg($sock, $BotChannel, "Shutting down!")
                            Sleep(500)
                            IRC_Server_Disconnect($sock, "Shut down request by " & $UserName & @CRLF)
                            Sleep(1000)
                            TCPShutDown()
                            Exit
                        EndIf
                    EndIf
                EndIf

                ;CTCP Stuff
                If $sTemp[2] = "PRIVMSG" And $sTemp[3] = $BotName Then
                    $UserName = StringTrimLeft(GetTok($sTemp[1], "1", "33"), 1)

                    ;CTCP Version/Finger
                    If $sTemp[4] = ":�VERSION�" Then
                        IRC_Send_CTCP($sock, $UserName, "VERSION IRC-Bot " & BotVersion() & " by Neo_")
                        ContinueLoop
                    EndIf

                    ;CTCP Finger
                    If $sTemp[4] = ":�FINGER�" Then
                        IRC_Send_CTCP($sock, $UserName, "FINGER IRC-Bot " & BotVersion() & " by Neo_")
                        ContinueLoop
                    EndIf

                    ;CTCP Ping
                    If $sTemp[4] = ":�PING" Then
                        IRC_Send_CTCP($sock, $UserName, "PONG " & StringTrimRight($sTemp[5], 1))
                        ContinueLoop
                    EndIf
                    ContinueLoop
                EndIf
            EndIf
        Next
    EndIf
WEnd

TCPShutdown()

Exit

; #BOT FUNCTIONS# ================================================================================================================
; BotVersion
; IsAccess
; IsBotChan
; IsBotMaster
; IsOp
; =================================================================================================================================

; #FUNCTION# ======================================================================================================================
; Name............: BotVersion
; Description.....: Version hard code
; Syntax..........: BotVersion()
; Parameters......: None
; Return Values...: Returns version number
; Author..........: Neo_ (aka coderusa)
; Modified........:
; =================================================================================================================================
Func BotVersion()
    Return "1.0.0"
EndFunc ;===> BotVersion

; #FUNCTION# ======================================================================================================================
; Name............: IsAccess
; Description.....: Determines if $name is listed in $BotAccess constant token string
; Syntax..........: IsAccess($name)
; Parameters......: $name - Username to be checked
; Return Values...: Returns TRUE if user is listed in $BotAccess
;                   Returns FALSE if user is not listed in $BotAccess
; Author..........: Neo_ (aka coderusa)
; Modified........:
; =================================================================================================================================
Func IsAccess($name)
    $Users = StringSplit($BotAccess, Chr(44))
    For $x = 1 To $Users[0] Step 1
        If $name = $Users[$x] Then
            Return True
        Else
            ContinueLoop
        EndIf
    Next
    Return False
EndFunc ;===> IsAccess

; #FUNCTION# ======================================================================================================================
; Name............: IsBotChan
; Description.....: Determines if $ChannelName is specified in $ChanList constant token string
; Syntax..........: IsBotChan($ChannelName)
; Parameters......: $ChannelName - Channel name to be checked
; Return Values...: Returns TRUE if channel is listed in $ChanList
;                   Returns FALSE if channel is not listed in $ChanList
; Author..........: Neo_ (aka coderusa)
; Modified........:
; =================================================================================================================================
Func IsBotChan($ChannelName)
    $Channels = StringSplit($ChanList, Chr(44))
    For $x = 1 To $Channels[0] Step 1
        If $ChannelName = $Channels[$x] Then
            Return True
        Else
            ContinueLoop
        EndIf
    Next
    Return False
EndFunc ;===> IsBotChan

; #FUNCTION# ======================================================================================================================
; Name............: IsBotMaster
; Description.....: Checks $BotMaster constant for specified username
; Syntax..........: IsBotMaster($name)
; Parameters......: $name - Username to be checked
; Return Values...: Returns TRUE if user is in botmaster list
;                   Returns FALSE if user is not in botmaster list.
; Author..........: Neo_ (aka coderusa)
; Modified........:
; =================================================================================================================================
Func IsBotMaster($name)
    $User = StringSplit($BotMaster, Chr(44))
    For $x = 1 To $User[0] Step 1
        If $User[$x] = $name Then
            Return True
        Else
            ContinueLoop
        EndIf
    Next
    Return False
EndFunc ;===> IsBotMaster

; #FUNCTION# ======================================================================================================================
; Name............: IsOp
; Description.....: Determines if $name is an OP on $channel via $UserList#ChannelName
; Syntax..........: IsOp($name, $channel)
; Parameters......: $name - Username to be checked
;                   $channel - Channel to be checked
; Return Values...: Returns TRUE if user has +o (@), +a (&) or +q (~) on channel
;                   Returns FALSE if user is not an OP on channel.
; Author..........: Neo_ (aka coderusa)
; Modified........:
; =================================================================================================================================
Func IsOp($name, $channel)
    $Users = StringSplit(Eval("UserList" & StringTrimLeft($channel, 1)), Chr(32))
    For $x = 1 to $Users[0] Step 1
        If Chr(64) & $name = $Users[$x] Then
            Return True
        ElseIf Chr(38) & $name = $Users[$x] Then
            Return True
        ElseIf Chr(126) & $name = $Users[$x] Then
            Return True
        Else
            ContinueLoop
        EndIf
    Next
    Return False
EndFunc ;===> IsOp

 

 

Frankenstein UDF v1.2: Frankenstein.au3

If, for some reason, you would like or need an older version of Frankenstein UDF, feel free to send me a PM.

Edited by coderusa
Link to comment
Share on other sites

Func _FileCreate($sFilePath, $data = "", $mode = 2)
    Local $hOpenFile = FileOpen($sFilePath, $mode)
    If $hOpenFile = -1 Then Return SetError(1, 0, 0)

    Local $hWriteFile = FileWrite($hOpenFile, $data)
    FileClose($hOpenFile)
    If $hWriteFile = -1 Then Return SetError(2, 0, 0)
    Return 1
 EndFunc ;===> _FileCreate

..is a good expansion/flexibility, for something that's already there.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

29 minutes ago, argumentum said:
Func _FileCreate($sFilePath, $data = "", $mode = 2)
    Local $hOpenFile = FileOpen($sFilePath, $mode)
    If $hOpenFile = -1 Then Return SetError(1, 0, 0)

    Local $hWriteFile = FileWrite($hOpenFile, $data)
    FileClose($hOpenFile)
    If $hWriteFile = -1 Then Return SetError(2, 0, 0)
    Return 1
 EndFunc ;===> _FileCreate

..is a good expansion/flexibility, for something that's already there.

When I was using this function, I felt that including all the extra stuff from File.au3 just to use the one function was a bit overkill. After looking it over, I saw that the only thing that kept it from being universal was the $FO_OVERWRITE constant, which to me seemed unnecessary to use a constant variable here, so I just switched it to the raw value of $FO_OVERWRITE.

Edited by coderusa
Link to comment
Share on other sites

  • coderusa changed the title to Frankenstein UDF v1.2 - IRC Functions, Time Conversion Functions, Token String Functions and other Misc Functions

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...