Paste: FactorBot - IRC Bot
Author: | dom96 |
Mode: | factor |
Date: | Sun, 27 Dec 2009 21:18:55 |
Plain Text |
USING: kernel io.sockets io splitting sequences namespaces combinators
io.encodings.utf8 math generalizations eval arrays continuations debugger
io.streams.string ;
IN: irc-bot
: global-print ( msg -- )
global [ print flush ] bind ;
: privmsg ( splitmsg sendmsg -- )
"PRIVMSG " swap 2 swap nth " :" 3append swap
append
"\r\n" append write flush ;
: execute-string ( string -- output )
[
[ 1array [ parse-string call ] with-datastack drop ]
[ nip print-error ] recover
] with-string-writer ;
: handle-line ( line -- )
dup " " split
swap dup global-print
{
{ [ "PING" ?head ] [ "PONG" prepend "\r\n" append dup write flush global-print drop ] }
{ [ over 1 swap nth "001" = ] [ "001 received, joining channel." global-print "JOIN #Factor\r\n" write flush drop drop ] }
{
[
swap
dup length 4 >=
[
1 over nth
"PRIVMSG" =
]
[ f ]
if
]
[
3 over nth {
{ ":|about"
[ dup "FactorBot 0.1 | This took me"
" about 2 days to figure out | Thanks to the helpful"
" community at #concatenative\r\n" 3append swap privmsg ]
}
{ ":|quit"
[
0 over nth
"!" split
0 swap nth
":dom96" =
[ 0 over remove-nth 3 [ 0 over remove-nth swap drop ] times " " join "QUIT :" "\r\n" surround write flush ]
[ dup "Your not allowed to use this command" swap privmsg ]
if
]
}
{ ":|say"
[
dup 0 over remove-nth 3 [ 0 over remove-nth swap drop ] times " " join swap
privmsg
]
}
{ ":|eval"
[
dup 0 over remove-nth 3 [ 0 over remove-nth swap drop ] times " " join
execute-string
"\n" split
[ swap dup 2over drop swap privmsg swap drop ] each
drop
]
}
[
":|" head?
[ dup "Unknown command" swap privmsg ]
[ ]
if
]
} case
drop drop
]
}
[ drop drop ]
} cond ;
: readlns ( -- )
readln [ handle-line readlns ] when* ;
: startbot ( -- )
"irc.freenode.net" 6667 <inet> utf8
[
"NICK FactorBot\r\n" write
"USER FactorBot FactorBot irc.freenode.net :Someone\r\n" write
flush
readlns
]
with-client ;
MAIN: startbot
Author: | erg |
Mode: | factor |
Date: | Mon, 28 Dec 2009 16:39:24 |
Plain Text |
SYMBOL: current-irc-client
TUPLE: irc-client ;
TUPLE: log-bot < irc-client ;
TUPLE: irc-message raw timestamp ;
TUPLE: ping < irc-message sender ;
TUPLE: privmsg < irc-message sender receiver string ;
HOOK: raw current-irc-client ( irc-message -- )
HOOK: ping current-irc-client ( ping -- )
HOOK: privmsg current-irc-client ( privmsg -- )
M: irc-client ping
sender>> "PONG " prepend send-irc ;
M: log-bot ping
[ timestamp>> timestamp>rfc822 "Ping received at " prepend log-it ]
[ handle-next-method ] bi ;
M: log-bot privmsg
... ;
New Annotation