Author Topic: IRC chat protocol / ChatBot  (Read 3267 times)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10725
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
IRC chat protocol / ChatBot
« on: 2016-Oct-30 »
Here is a basic framework for an IRC chat bot. I hope to use this for a clinet/server application one day.... Just have to deal with "flood posting" errors.

Code: (glbasic) [Select]
// --------------------------------- //
// Project: IRC
// Start: Sunday, October 30, 2016
// IDE Version: 14.401


// SETCURRENTDIR("Media") // go to media files


IRC_connect("irc.freenode.org", "Chatbot", "glbasic")


IRC_send("I'm a bot ask me something")

WHILE TRUE
// LOCAL msg$ = STDIN$()
LOCAL what$, sender$, text$
IF IRC_recv(what$, sender$, text$)
STDOUT "From: "+sender$+": what:" + what$+" text:"+text$+"\n"
ENDIF
IF what$="NOTICE"
IRC_send(sender$ + ", ask me anything but " +text$)

ENDIF



WEND



GLOBAL gIRC_sock%=-1
GLOBAL gIRC_nick$
GLOBAL gIRC_channel$
GLOBAL gIRC_server$
FUNCTION IRC_connect%: server$, nick$, channel$

SOCK_INIT()
gIRC_sock% = SOCK_TCPCONNECT(server$, 6667)
IF gIRC_sock%=-1
STDOUT "Error - connect\n"
STDOUT NETGETLASTERROR$()+"\n"
ENDIF
SOCK_SETBLOCKING gIRC_sock%, TRUE

LOCAL what$, sender$, text$
gIRC_nick$ = nick$
gIRC_channel$ = channel$
gIRC_server$ = server$

// SOCK_TCPSEND(gIRC_sock%, "PASS *\r\n")
SOCK_TCPSEND(gIRC_sock%, "NICK " + nick$+"\r\n")
IRC_recv(what$, sender$, text$)
SLEEP 2000
IRC_recv(what$, sender$, text$)
SOCK_TCPSEND(gIRC_sock%, "USER " + nick$+" 8 * :"+nick$+" v1.0 (glbasic.com)\r\n")
IRC_recv(what$, sender$, text$)


STDOUT "**** JOIN *****\n"
IRC_recv(what$, sender$, text$)

// SOCK_TCPSEND(gIRC_sock%, "QUIT\r\n")

// SOCK_TCPSEND(gIRC_sock%, "PRIVMSG NickServ register password test@gmail.com\r\n")

SOCK_TCPSEND(gIRC_sock%, "JOIN #"+channel$+"\r\n")

SOCK_SETBLOCKING gIRC_sock%, FALSE
STDOUT "**** reading *****\n"

ENDFUNCTION


FUNCTION IRC_send%: text$

SOCK_TCPSEND(gIRC_sock%, "PRIVMSG #"+gIRC_channel$+" :"+text$+"\r\n")

ENDFUNCTION



FUNCTION IRC_parse%: BYREF msg$, BYREF what$, BYREF sender$, BYREF text$
LOCAL cmd$ = LEFT$(msg$, 4)
IF cmd$ = "PING"
STDOUT "pong:"+MID$(msg$, 5)+"\n"
SOCK_TCPSEND(gIRC_sock%, "PONG "+MID$(msg$, 5) + "\r\n")
ENDIF

// message
// :KungPhoo!~kungphoo@p548E4C59.dip0.t-ipconnect.de PRIVMSG #glbasic :test

// private message
// :KungPhoo!~kungphoo@p548E4C59.dip0.t-ipconnect.de NOTICE Chatbot :xxx

// KunhPhoo joined
// :KungPhoo!~kungphoo@p548E4C59.dip0.t-ipconnect.de JOIN #glbasic

// KungPhoo left
// :KungPhoo!~kungphoo@p548E4C59.dip0.t-ipconnect.de PART #glbasic

// Chatbot quit
// :Chatbot!~Chatbot@p548E4C59.dip0.t-ipconnect.de QUIT :Ping timeout: 268 seconds

// KungPhoo kicked Chatbot
// :KungPhoo5!~kungphoo@p548E4C59.dip0.t-ipconnect.de KICK #MinionsDomain Chatbot :KungPhoo5

// Excess flooding:
// ERROR :Closing Link: Chatbot by dreamhack.se.quakenet.org (Excess Flood)

IF LEN(msg$)
LOCAL tok$[]
SPLITSTR(msg$, tok$[], " ")

IF LEN(tok$[]) > 2
LOCAL who$[]
SPLITSTR(tok$[0], who$[], ":!")
IF LEN(who$[]) THEN sender$ = who$[0]

LOCAL pos% = INSTR(msg$, ":", 1) + 1

// Message
IF tok$[1] = "PRIVMSG"
what$ = "MSG"
text$ = MID$(msg$, pos)
RETURN TRUE
ENDIF

// whisper
IF tok$[1] = "NOTICE"
what$ = "NOTICE"
text$ = MID$(msg$, pos)
IF sender$ = gIRC_server$
STDOUT "Msg from server: "+msg$+"\n"
RETURN FALSE // skip them
ENDIF

RETURN TRUE
ENDIF

IF tok$[1] = "JOIN"
what$ = "JOIN"
text$ = MID$(msg$, pos)
RETURN TRUE
ENDIF

IF tok$[1] = "KICK" AND LEN(tok$[])>3 AND tok$[3]=gIRC_nick$
STDOUT "*** got kicked ***\n"
tok$[1]="QUIT"
sender$ = gIRC_nick$


ENDIF

IF tok$[1] = "PART" OR tok$[1] = "QUIT"
what$ = "QUIT"

IF sender$ = gIRC_nick$
STDOUT "*** RECONNECT ***\n"
SOCK_TCPSEND(gIRC_sock%, "JOIN #"+gIRC_channel$+"\r\n")
ENDIF

RETURN TRUE
ENDIF

ENDIF

ENDIF

ENDFUNCTION




FUNCTION IRC_recv%: BYREF what$, BYREF sender$, BYREF text$

what$=""
sender$=""
text$=""


IF gIRC_sock% =-1 THEN RETURN FALSE
STATIC msg$
msg$=""
SOCK_RECV(gIRC_sock%, msg$, 8*1024)
IF NOT LEN(msg$) THEN RETURN FALSE

LOCAL rv% = FALSE
LOCAL tok$[]
SPLITSTR(msg$, tok$[], "\r\n")
FOREACH t$ IN tok$[]
IF IRC_parse(t$, what$, sender$, text$)
rv%=TRUE
STDOUT t$+"\n"
ENDIF
NEXT

RETURN rv%
ENDFUNCTION


Offline UBERmonkeybot

  • Mr. Polyvector
  • ***
  • Posts: 243
    • View Profile
Re: IRC chat protocol / ChatBot
« Reply #1 on: 2016-Oct-30 »
Cool.
 :D

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5030
  • O Rly?
    • View Profile
Re: IRC chat protocol / ChatBot
« Reply #2 on: 2016-Nov-01 »
Have a old one too

Code: (glbasic) [Select]
// --------------------------------- //
// Project: IRC Bot RC1
// Start: Wednesday, August 05, 2009
// IDE Version: 7.072
AUTOPAUSE FALSE
 
LOCAL server$   = "rowling.freenode.net"
LOCAL port%             = 6667
LOCAL hostname$ = "localhost"
LOCAL chan$             = "#glbasic"
LOCAL name$             = "MrGLBasic"
LOCAL sock%
 
 
LOCAL bInit%
restart:
bInit% = FALSE
IF SOCK_INIT()
 
        sock% = SOCK_TCPCONNECT(server$, port%)
        hostname$ = SOCK_GETIP$(SOCK_GETIP("localhost"))
 
        SOCK_TCPSEND(sock%,"PASS password\r\n")
        SOCK_TCPSEND(sock%,"USER "+name$+" "+hostname$+" "+server$+" :"+name$+"\r\n")
        SOCK_TCPSEND(sock%,"NICK "+name$+"\r\n")
ELSE
 
        STDOUT "Fehler beim Initialisieren des Socket!"
        END
ENDIF
 
LOCAL user%
LOCAL user$
 
LOCAL friends$[]
DIMDATA friends$[], "KungPhoo", "Hiro", "Schranz0r"
 
 
WHILE TRUE
        LOCAL rcv%, full$
        LOCAL lines$[], spl$[]
 
        rcv% = SOCK_RECV(sock%,full$,256)
 
        IF rcv%<0
                SLEEP 500
                CONTINUE
        ENDIF
 
        IF rcv% = 0 // server quit!
                STDERR "server quit.\n"
                SOCK_CLOSE(sock%)
                SLEEP 5000
                GOTO restart
        ENDIF
 
        IF rcv%>0
                SPLITSTR(full$, lines$[], "\r\n")
                FOREACH msg$ IN lines$[]
                        STDOUT msg$ + "\n"
 
                        // Ping Pong
                        IF MID$(msg$,0,4) = "PING"
                                SOCK_TCPSEND(sock%,"PONG"+MID$(msg$,4))
                        ENDIF
 
                        // message
                        LOCAL old_user% = user%
                        IF MID$(msg$, 0,1) = ":"
                                SPLITSTR(msg$, spl$[], " ")
                                IF LEN(spl$[])>1
                                        SELECT spl$[1] // message
                                        CASE "PRIVMSG"
                                                // :KungPhoo!~kungphoo@ipconnect.de PRIVMSG MrGLBasic :Here is my text
                                                IF spl$[2] = name$ // someone directly talked to me
 
                                                ENDIF
 
                                        CASE "322" // list message
                                                // :irc.remakes.org 322 MrGLBasic #glbasic 3 :
                                                IF LEN(spl$[])>2 AND spl$[3] = chan$
                                                        user = INTEGER(spl$[4])
                                                ENDIF
 
                                        CASE "PART"
                                                // :MrGLBasic!MrGLBasic@ipconnect.de PART :#glbasic
                                                IF spl$[2] = ":" + chan$ THEN DEC user,1
                                        CASE "QUIT"
                                                // :MrGLBasic!MrGLBasic@ipconnect.de QUIT :#glbasic
                                                IF spl$[2] = ":" + chan$ THEN DEC user,1
                                        CASE "JOIN"
                                                IF spl$[2] = ":" + chan$ THEN INC user,1
                                                // :MrGLBasic!MrGLBasic@ipconnect.de JOIN :#glbasic
 
                                                // find name of the guy who joined
                                                LOCAL epos% = INSTR(spl$[0], "!")
                                                IF epos>1
                                                        LOCAL join$ = MID$(spl$[0], 1, epos-1)
                                                        STDOUT "name="+join$+"|\n"
 
                                                        // mode +o for friends
                                                        FOREACH frnd$ IN friends$[]
                                                                IF join$ = frnd$
                                                                        SOCK_TCPSEND(sock%,"MODE "+chan$+" +o "+join$+"\r\n")
                                                                        BREAK
                                                                ENDIF
                                                        NEXT
 
                                                ENDIF
 
                                        ENDSELECT
                                ENDIF
                        ENDIF
 
                        // something changed
                        IF old_user <> user
                                SOCK_TCPSEND(sock%,"PRIVMSG "+chan$+" :User im Chat: "+user+" mit mir\r\n")
                        ENDIF
                NEXT
        ENDIF
 
 
        // initalize and gather information
        IF bInit = FALSE
 
                bInit = TRUE
                SOCK_TCPSEND(sock%,"PRIVMSG NickServ :identify password\r\n")
                SOCK_TCPSEND(sock%,"JOIN "+chan$+"\r\n")
                SOCK_TCPSEND(sock%,"LIST "+chan$+"\r\n")
                SOCK_TCPSEND(sock%,"PART #remakes\r\n")
 
        ENDIF
 
 
 
WEND
 
 
I <3 DGArray's :D

PC:
AMD Ryzen 7 1700 @3.9GHz, 16GB HyperX Fury 3000MHz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline yaKC

  • Mc. Print
  • *
  • Posts: 17
    • View Profile
Re: IRC chat protocol / ChatBot
« Reply #3 on: 2016-Nov-02 »
These must have taken some work, nice one! Network coding looks scary, testing must be a nightmare unless you have two computers.

Sent from my 01v21_v89_gq3008s_89t_5g using Tapatalk


Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10725
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: IRC chat protocol / ChatBot
« Reply #4 on: 2016-Nov-03 »
IRC uses a server. You can start two clients on one PC.  Thx Schranzor. I totally forgot you did that....
I tried to use it for a network game but i get kicked too soon.

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5030
  • O Rly?
    • View Profile
Re: IRC chat protocol / ChatBot
« Reply #5 on: 2016-Nov-03 »
The Kicks a serverconfigs (Spamdetection)
You have a root Gernot, host your own one :)

Achja Gernot:
Kann es sein das dein Bot zu oft ne Abfrage macht?
Hab mir den Code jetzt nicht komplett angeschaut, k├Ânnte der Kick sein.
Ich glaub deshalb hab ich den SLEEP 500 bei mir drin...
« Last Edit: 2016-Nov-03 by Schranz0r »
I <3 DGArray's :D

PC:
AMD Ryzen 7 1700 @3.9GHz, 16GB HyperX Fury 3000MHz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard