Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;-------------------------
- ;IRC Chat Window Socket
- ;Script, for RPGenerator
- ;-------------------------
- #include <ButtonConstants.au3>
- #include <EditConstants.au3>
- #include <GUIConstantsEx.au3>
- #include <GUIListBox.au3>
- #include <WindowsConstants.au3>
- #include <IE.au3>
- #include <ListboxConstants.au3>
- ;Misc Startup
- _IEErrorHandlerRegister()
- OnAutoItExitRegister("CloseCon")
- ;IRC Variables
- #Region
- Global $ip = "irc.esper.net"
- Global $port = 6667
- Global $channel = "#DarkCastle"
- Global $nickname = "Testing_Console"
- Global $username = "Testing_Console"
- Global $realname = "Testing_Console"
- Global $sock
- Global $connected = False
- Global $mList
- Global $OnChannel = False
- Global $OldNick
- Global $NickInUse = False
- #EndRegion
- ;IE Chat window variables
- $HTML = "<HTML>" & @CR
- $HTML &= "<HEAD>" & @CR
- $HTML &= '<style type="text/css"><!-- body { margin: 4px; line-height: 16px;} --></style>'
- $HTML &= "</HEAD>" & @CR
- $HTML &= "<BODY>"
- $HTML &= "</BODY>"
- $HTML &= "</HTML>"
- $IEChatBox = _IECreateEmbedded()
- ;Chat window GUI
- #Region
- Opt("GUIOnEventMode", 1)
- $DCRPG = GUICreate("DarkCastle RPG - Chat", 850, 500, 192, 114)
- $ConButton = GUICtrlCreateButton("Connect", 9, 3, 80, 23)
- GUICtrlSetOnEvent($ConButton, "CheckConnection")
- $ChatBox = GUICtrlCreateObj($IEChatBox, 9, 29, 685, 380)
- $NickList = GUICtrlCreateList("", 700, 28, 140, 390, $LBS_SORT)
- $EditBox = GUICtrlCreateInput("", 8, 424, 545, 21)
- $SendText = GUICtrlCreateButton("Send", 560, 424, 65, 25, BitOR($WS_GROUP, $BS_DEFPUSHBUTTON))
- GUICtrlSetOnEvent($SendText, "SendText")
- GUISetOnEvent($GUI_EVENT_CLOSE, "CloseDown")
- GUISetState(@SW_SHOW)
- #EndRegion
- ;IE Chat window stuff
- _IENavigate($IEChatBox, "about:blank")
- _IEDocWriteHTML($IEChatBox, $HTML)
- _IEHeadInsertEventScript($IEChatBox, "document", "oncontextmenu", "return false")
- ;Main Loop
- While 1
- ;GUI Stuff
- ;$nMsg = GUIGetMsg()
- ;Switch $nMsg
- ; Case $GUI_EVENT_CLOSE
- ; Exit
- ; Case $NickList
- ; Case $SendText
- ;EndSwitch
- ;Sleep(1000)
- ;IRC Stuff - thanks to Kealper of EsperNET for helping with the IRC Section!
- If Not $connected Then
- Sleep(500)
- ContinueLoop
- EndIf
- $receive = TCPRecv($sock, 8192)
- If @error Then ErrorMsg("001")
- If Not $receive Then ContinueLoop
- $sData = StringSplit($receive, @CRLF, 1)
- ;ShowText("Normal", "" & $receive)
- ;If Not $sData[2] = "372" Then ShowText("Normal", "" & $receive)
- For $i = 1 To $sData[0]
- $sTemp = StringSplit($sData[$i], " ")
- If $sTemp[1] = "PING" Then TCPSend($sock, "PONG " & $sTemp[2] & @CRLF)
- If $sTemp[0] < 3 Then ContinueLoop
- Switch $sTemp[2]
- Case "372"
- $message = StringMid($sData[$i], StringInStr($sData[$i], ":", 0, 2) + 1)
- ShowText("Info", "" & $message)
- Case "PRIVMSG"
- $nick = StringMid($sTemp[1], 2, StringInStr($sTemp[1], "!") - 2)
- $message = StringMid($sData[$i], StringInStr($sData[$i], ":", 0, 2) + 1)
- $chan = $sTemp[3]
- $smsg = StringSplit($message, " ")
- If $smsg[1] = Chr(1) & "ACTION" Then
- $length = StringLen($message)
- $message1 = StringLeft($message, Number($length - 1))
- ShowText("Action", "* " & $nick & " " & StringRight($message1, Number($length - 9)))
- Else
- ShowText("Normal", "<" & $nick & "> " & $message)
- EndIf
- Case "NOTICE"
- $nick = StringMid($sTemp[1], 2, StringInStr($sTemp[1], "!") - 2)
- $message = StringMid($sData[$i], StringInStr($sData[$i], ":", 0, 2) + 1)
- $chan = $sTemp[3]
- ShowText("Notice", "<" & $nick & "> " & $message)
- Case "PART"
- $nick = StringMid($sTemp[1], 2, StringInStr($sTemp[1], "!") - 2)
- $chan = $sTemp[3]
- ShowText("Mode", "* " & $nick & " has left " & $chan)
- NickListRemove("" & $nick)
- Case "JOIN"
- $nick = StringMid($sTemp[1], 2, StringInStr($sTemp[1], "!") - 2)
- $chan = $sTemp[3]
- ShowText("Mode", "* " & $nick & " has joined " & $chan & ".")
- NickListAdd("" & $nick)
- Case "KICK"
- $nick = StringMid($sTemp[1], 2, StringInStr($sTemp[1], "!") - 2)
- $message = StringMid($sData[$i], StringInStr($sData[$i], ":", 0, 2) + 1)
- $chan = $sTemp[3]
- $knick = $sTemp[4]
- ShowText("Mode", "* " & $nick & " has kicked " & $knick & " from " & $chan & " (" & $message & ")")
- If $knick = $nickname Then
- Kicked()
- Else
- NickListRemove("" & $knick)
- EndIf
- Case "TOPIC"
- $nick = StringMid($sTemp[1], 2, StringInStr($sTemp[1], "!") - 2)
- $topic = StringMid($sData[$i], StringInStr($sData[$i], ":", 0, 2) + 1)
- $chan = $sTemp[3]
- ShowText("Mode", "* " & $nick & " changes the topic to: " & $topic)
- Case "MODE"
- $nick = StringMid($sTemp[1], 2, StringInStr($sTemp[1], "!") - 2)
- $chan = $sTemp[3]
- ShowText("Mode", "* " & $nick & " sets mode " & StringTrimLeft($sData[$i], StringInStr($sData[$i], " ", 0, 3)))
- If $sTemp[4] = "+o" Then NickListMode("@", "" & $sTemp[5])
- If $sTemp[4] = "+v" Then NickListMode("+", "" & $sTemp[5])
- If $sTemp[4] = "-o" Then NickListUnMode("@", "" & $sTemp[5])
- If $sTemp[4] = "-v" Then NickListUnMode("+", "" & $sTemp[5])
- Case "004"
- JoinChannel($channel)
- Case "332"
- $topic = ""
- $tData = StringSplit($sData[$i], " ")
- For $tN = 5 to $tData[0]
- If Not $topic AND $tn = 5 Then
- $topic = StringTrimLeft($tData[$tN], 1)
- ContinueLoop
- ElseIf $topic Then
- $topic = $topic & " " & $tData[$tN]
- ContinueLoop
- EndIf
- Next
- ShowText("Info", "* " & $topic)
- Case "353"
- NickListSet($sData[$i])
- $OnChannel = True
- Case "404"
- If $OnChannel = True Then
- ShowText("Notice", "Unable to send message, chat is on mute.")
- ElseIf $onChannel = False Then
- ShowText("Notice", "You are not in chat.")
- EndIf
- Case "433"
- ShowText("Notice", "The nickname " & $nickname & " is currently in use.")
- ChangeNick("" & $OldNick)
- Case "NICK"
- ;:Neo`Nemesis!Z31@67-135-195-93.dia.static.qwest.net NICK :Testing
- $nData = StringSplit($sData[$i], "!")
- $OldUser = StringTrimLeft($nData[1], 1)
- $nData = StringSplit($sData[$i], " ")
- $NewUser = StringTrimLeft($nData[3], 1)
- ShowText("Mode", "* " & $OldUser & " is now known as " & $NewUser)
- $nData = StringSplit($mList, " ")
- For $nN = 1 To $nData[0]
- If "@" & $OldUser = $nData[$nN] Then
- $NewUser = "@" & $NewUser
- ExitLoop
- ElseIf "+" & $OldUser = $nData[$nN] Then
- $NewUser = "+" & $NewUser
- ExitLoop
- EndIf
- Next
- NickListRemove($OldUser)
- NickListAdd($NewUser)
- Case "471"
- ShowText("Notice", "Unable to join, chat is full.")
- Case "473"
- ShowText("Notice", "Unable to join, chat is set to invite only.")
- Case "474"
- ShowText("Notice", "Unable to join, you are banned from chat.")
- Case "475"
- ShowText("Notice", "Unable to join, chat is locked.")
- Case "477"
- ShowText("Notice", "Unable to join, chat is set to registered users only.")
- EndSwitch
- Next
- WEnd
- ;-------------------------
- ;GUI Functions
- ;-------------------------
- ;Showing text in the IE Chat window
- Func ShowText($event, $text)
- $text = HTMLencode(UniConvert($text))
- $display = _IEBodyReadHTML($IEChatBox)
- If $display == 0 Then $display = ""
- If $event = "Mode" Then $color = "5AA05A"
- If $event = "Action" Then $color = "6C2DC7"
- If $event = "Notice" Then $color = "C11B17"
- If $event = "Normal" Then $color = "000000"
- If $event = "Info" Then $color = "2B65EC"
- _IEBodyWriteHTML($IEChatBox, $display & "<font face=""FixedSys"" color=""#" & $color & """ size=""1"">[" & @HOUR & ":" & @MIN & "]</font> <font face=""FixedSys"" color=""#" & $color & """ size=""-1""> " & $text & "</font><br>")
- Scroll()
- ;Flash()
- EndFunc ;==>ShowText
- ;Auto Scrolling in IE chat window
- Func Scroll()
- $scrollH = $IEChatBox.document.body.scrollHeight
- $IEChatBox.document.parentWindow.scrollTo(0, $scrollH)
- EndFunc ;==>Scroll
- ;HTML encoding for ShowText()
- Func HTMLencode($encode)
- $Len = StringLen($encode)
- Local $Num
- If $Len = 0 Then Return ''
- For $i = 1 To $Len
- $Char = StringMid($encode, $i, 1)
- $Num &= '&#' & AscW($Char) & ';'
- Next
- Return $Num
- EndFunc ;==>HTMLencode
- ;ANSII to Unicode converting for ShowText()
- Func UniConvert($convert)
- $bin = StringToBinary($convert)
- $uni = BinaryToString($bin, 4)
- Return $uni
- EndFunc ;==>UniConvert
- ;Exiting proggy
- Func CloseDown()
- Exit
- EndFunc ;==>CloseDown
- ;-------------------------
- ;IRC Functions
- ;-------------------------
- ;open Connection (CheckConnection(), ConnectToServer(), Connect())
- Func CheckConnection()
- If $connected = True Then
- CloseCon()
- ElseIf $connected = False Then
- ConnectToServer()
- EndIf
- EndFunc ;==>CheckConnection
- Func ConnectToServer()
- TCPStartup()
- $sock = Connect($ip, $port, $nickname, $username, $realname)
- $connected = True
- EndFunc ;==>ConnectToServer
- Func Connect($sIP, $iPort, $sNick, $sUser, $sReal)
- ShowText("Info", "* Connecting to server...")
- GUICtrlSetData($ConButton, "Disconnect")
- $sIP = TCPNameToIP($sIP) ;Resolve the given hostname to a real IP address
- Local $iSock = TCPConnect($sIP, $iPort) ;Connect to the given IP address and port
- TCPSend($iSock, "NICK " & $sNick & @CRLF) ;Request the requested nickname
- TCPSend($iSock, "USER " & $sUser & " 0 0 " & $sReal & @CRLF) ;Send info about the bot, the two zeros are just placeholders for optional data in this example.
- Return $iSock
- EndFunc ;==>Connect
- ;Close Connection
- Func CloseCon()
- TCPCloseSocket($sock)
- TCPShutdown()
- If $connected = True Then
- ShowText("Info", "* Disconnected...")
- GUICtrlSetData($ConButton, "Connect")
- GUICtrlSetData($NickList, "")
- $connected = False
- $mList = ""
- $OnChannel = False
- EndIf
- EndFunc ;==>CloseCon
- ;IRC ErrorMsg()
- Func ErrorMsg($eNum)
- If $eNum = "001" Then
- ShowText("Notice", "ERROR 001: Server not responding.")
- CloseCon()
- EndIf
- EndFunc ;==>ErrorMsg
- ;Join Channel
- Func JoinChannel($sChannel)
- TCPSend($sock, "JOIN " & $sChannel & @CRLF)
- EndFunc ;==>JoinChannel
- ;Leave Channel
- Func LeaveChannel($cChannel, $pMsg)
- TCPSend($sock, "PART " & $cChannel & "" & $pMsg & @CRLF)
- EndFunc ;==>LeaveChannel
- ;Change Nickname
- Func ChangeNick($newnick)
- $OldNick = $nickname
- If $sock Then TCPSend($sock, "NICK :" & $newnick & @CRLF)
- ShowText("Mode", "* You are now using the nickname " & $newnick)
- If $OnChannel = True Then
- NickListRemove($nickname)
- NickListAdd($newnick)
- EndIf
- $nickname = $newnick
- EndFunc ;==>ChangeNick
- ;Nick List Functions
- Func NickListUpdate($uList)
- GUICtrlSetData($NickList, "")
- $SuList = StringSplit($uList, " ")
- For $un = 1 To $SuList[0]
- If $SuList[$un] = ":" & $nickname Then
- GUICtrlSetData($NickList, $nickname)
- Else
- GUICtrlSetData($NickList, $SuList[$un])
- EndIf
- Next
- EndFunc ;==>NickListUpdate
- Func NickListSet($members)
- $MemberList = StringSplit($members, " ")
- For $Num = 6 To $MemberList[0]
- If Not $mList Then
- $mList = $MemberList[$Num]
- ElseIf $mList Then
- $mList = $mList & " " & $MemberList[$Num]
- EndIf
- Next
- NickListUpdate($mList)
- EndFunc ;==>NickListSet
- Func NickListAdd($aUser)
- If Not $mList Then
- $mList = $aUser
- ElseIf $mList Then
- $mList = $mList & " " & $aUser
- EndIf
- NickListUpdate($mList)
- EndFunc ;==>NickListAdd
- Func NickListRemove($rUser)
- $rList = StringSplit($mList, " ")
- $mList = ""
- For $n = 1 To $rList[0]
- If $rList[$n] = $rUser Or StringTrimLeft($rList[$n], 1) = $rUser Then
- $mList = $mList
- Else
- $mList = $mList & " " & $rList[$n]
- EndIf
- Next
- NickListUpdate($mList)
- EndFunc ;==>NickListRemove
- Func NickListMode($mode, $mUser)
- $RepNick = $mode & $mUser
- NickListRemove($mUser)
- NickListAdd($RepNick)
- EndFunc ;==>NickListMode
- Func NickListUnMode($unmode, $unUser)
- $RepUnNick = $unmode & $unUser
- NickListRemove($RepUnNick)
- NickListAdd($unUser)
- EndFunc ;==>NickListUnMode
- ;Kicked() function, for when you are kicked from chat!
- Func Kicked()
- GUICtrlSetData($NickList, "")
- ShowText("Mode", "* You have been kicked from chat, please use the /rejoin command to rejoin.")
- $OnChannel = False
- EndFunc ;==>Kicked
- ;Send message
- Func SendMsg($sChannel, $smsg)
- TCPSend($sock, "PRIVMSG " & $sChannel & " :" & $smsg & @CRLF)
- EndFunc ;==>SendMsg
- ;Sending text and command interpreter.
- Func SendText()
- If Not $sock And $OnChannel = False Then
- If Not $sock And Not GUICtrlRead($EditBox) Then
- Sleep(10)
- ElseIf Not $sock Then
- ShowText("Notice", "* You are not connected...")
- ElseIf $OnChannel = False Then
- ShowText("Notice", "* You are not in chat.")
- EndIf
- Else
- If Not GUICtrlRead($EditBox) Then
- Sleep(1000)
- Else
- $cText = StringSplit(GUICtrlRead($EditBox), " ")
- If StringLeft($cText[1], 1) = "/" Then
- SendCommand(GUICtrlRead($EditBox))
- Else
- $sText = "<" & $nickname & "> " & GUICtrlRead($EditBox)
- SendMsg($channel, "" & GUICtrlRead($EditBox))
- ShowText("Normal", "" & $sText)
- EndIf
- GUICtrlSetData($EditBox, "")
- EndIf
- EndIf
- EndFunc ;==>SendText
- Func SendCommand($cmd)
- $cmdText = StringSplit($cmd, " ")
- ;/me <action>
- If $cmdText[1] = "/me" Then
- If $cmdText[0] > 2 OR $cmdText[0] < 2 Then
- ShowText("Info", "" & $cmdText[1] & ": Invalid parameters")
- ElseIf $cmdText[0] = 2 Then
- ShowText("Action", "* " & $nickname & " " & StringRight("" & $cmd, Number(StringLen("" & $cmd) - 4)))
- TCPSend($sock, "PRIVMSG " & $channel & " :" & Chr(1) & "ACTION " & StringRight("" & $cmd, Number(StringLen("" & $cmd) - 4)) & Chr(1) & @CRLF)
- EndIf
- ;/quit <quit message>
- ElseIf $cmdText[1] = "/quit" Then
- If Not StringRight("" & $cmd, Number(StringLen("" & $cmd) - 6)) Then
- $qMsg = "Quit: DarkCastle RPG - User has logged out of chat."
- Else
- $qMsg = StringRight("" & $cmd, Number(StringLen("" & $cmd) - 6))
- EndIf
- TCPSend($sock, "QUIT :" & $qMsg & @CRLF)
- ShowText("Info", "* You have logged out of chat.")
- CloseCon()
- ;/rejoin
- ElseIf $cmdText[1] = "/rejoin" Then
- JoinChannel("" & $channel)
- ;/nickserv <paramter1 ... parameterN>
- ElseIf $cmdText[1] = "/nickserv" Then
- If Not $cmdText[2] Then
- ShowText("Info", "" & $cmdText[1] & ": Invalid parameters")
- Else
- $nsText = StringRight("" & $cmd, Number(StringLen("" & $cmd) - 10))
- TCPSend($sock, "PRIVMSG NickServ :" & $nsText & @CRLF)
- EndIf
- ;/chanserv <paramter1 ... parameterN>
- ElseIf $cmdText[1] = "/chanserv" Then
- If Not $cmdText[2] Then
- ShowText("Info", "" & $cmdText[1] & ": Invalid parameters")
- Else
- $csText = StringRight("" & $cmd, Number(StringLen("" & $cmd) - 10))
- TCPSend($sock, "PRIVMSG ChanServ :" & $csText & @CRLF)
- EndIf
- ;/nick <nickname> <password
- ElseIf $cmdText[1] = "/nick" Then
- If $cmdText[0] < 2 OR $cmdtext[0] > 2 Then
- ShowText("Info", "" & $cmdText[1] & ": Invalid parameters")
- ElseIf $cmdtext[0] = 2 Then
- If $cmdText[2] = $nickname Then
- ShowText("Info", "* You are already using that nickname.")
- Else
- $nListed = StringSplit($mList, " ")
- For $nL = 1 To $nListed[0]
- If $nListed[$nL] = $cmdtext[2] OR $nListed[$nL] = "@" & $cmdtext[2] OR $nListed[$nL] = "+" & $cmdtext[2] Then
- ShowText("Info", "The nickname " & $cmdtext[2] & " is currently in use.")
- $NickInUse = True
- ExitLoop
- EndIf
- Next
- If $NickInUse = False Then ChangeNick("" & $cmdText[2])
- EndIf
- $NickInUse = False
- EndIf
- ;/login <password>
- ElseIf $cmdText[1] = "/login" Then
- If $cmdText[0] > 2 Or $cmdText[0] < 2 Then
- ShowText("Info", "" & $cmdText[1] & ": Invalid parameters")
- ElseIf $cmdText[0] = 2 Then
- TCPSend($sock, "PRIVMSG Boris :LOGIN " & $cmdText[2] & @CRLF)
- EndIf
- ;/register <nickname> <password> <e-mail>
- ElseIf $cmdText[1] = "/register" Then
- If $cmdText[0] < 4 Or $cmdText[0] > 4 Then
- ShowText("Info", "" & $cmdText[1] & ": Invalid parameters")
- ElseIf $cmdText[0] = 4 Then
- If Not $nickname = $cmdText[2] Then
- ChangeNick("" & $cmdText[2])
- TCPSend($sock, "PRIVMSG Nickserv :REGISTER " & $cmdText[3] & " " & $cmdText[4] & @CRLF)
- TCPSend($sock, "PRIVMSG Boris :REGISTER " & $cmdText[3] & @CRLF)
- EndIf
- EndIf
- ;/connect
- ElseIf $cmdText[1] = "/connect" Then
- ConnectToServer()
- Else
- ShowText("Info", "* " & $cmdText[1] & ": Unknown command.")
- EndIf
- EndFunc ;==>SendCommand
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement