Skip to content

Instantly share code, notes, and snippets.

@daimor
Created April 18, 2016 15:18
Show Gist options
  • Save daimor/3e127c302c5f1caf40e0550f12eff2d3 to your computer and use it in GitHub Desktop.
Save daimor/3e127c302c5f1caf40e0550f12eff2d3 to your computer and use it in GitHub Desktop.
#include %occInclude
kill
set host="chat.freenode.net"
set port="6667"
set sock=##class(%IO.Socket).%New()
set linepat=##class(%Regex.Matcher).%New("^\:([^\!\ ]+)\![^\ ]+\ (.+)$")
set privmsg=##class(%Regex.Matcher).%New("^PRIVMSG #([^\ ]+) \:(.+)")
set action=##class(%Regex.Matcher).%New("!(?:bot|BOT) (.+)")
try {
if sock.Open(host, port, 100, .tSC) {
do sock.WriteLine("USER cosbot 0 * :Cache Bot")
do sock.WriteLine("NICK cosbot")
do sock.WriteLine("JOIN ##cos")
do {
set line=sock.ReadLine(, .5, .tSC)
if $$$ISERR(tSC) {
do $system.OBJ.DisplayError(tSC)
quit
}
set line=$zstrip(line,"<>C")
if line'="" {
write $c(13),line,!
}
if $match(line, "^PING \:([a-zA-Z\-\.0-9]+)$") {
set response="PONG "_$extract(line, 7, *)
write $c(13),"RESPONSE: "_response,!
do sock.WriteLine(response)
} elseif linepat.Match(line) {
set nick=linepat.Group(1)
set line=linepat.Group(2)
if privmsg.Match(line) {
set channel=privmsg.Group(1)
set msg=privmsg.Group(2)
if action.Match(msg) {
set text=action.Group(1)
set response="PRIVMSG #"_channel_" :"_nick_", "_text
write $c(13),"RESPONSE: "_response,!
do sock.WriteLine(response)
}
continue
}
} else {
}
read test:0
if test'="" {
read msg
set msg=test_msg
quit:msg="q"
set response="PRIVMSG ##cos :"_msg
write $c(13),"RESPONSE: "_response,!
do sock.WriteLine(response)
}
} while 1
} else {
do $system.OBJ.DisplayError(tSC)
}
do sock.WriteLine("QUIT :bye-bye")
} catch {}
write !,sock.Close()
quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment