! Copyright (C) 2009 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: accessors calendar calendar.format combinators.smart constructors io io.encodings.utf8 io.sockets irc.client.messages irc.client.messages.base irc.client.messages.parser kernel namespaces nested-comments prettyprint sequences ; QUALIFIED-WITH: multi-methods mm QUALIFIED-WITH: sequences s IN: irc-client : global. ( object -- ) global [ . flush ] bind ; SYMBOL: current-irc-client TUPLE: irc-client duplex-stream ; CONSTRUCTOR: irc-client ( duplex-stream -- obj ) ; : print-irc ( string -- ) current-irc-client get duplex-stream>> [ stream-write ] [ "\r\n" swap stream-write ] [ stream-flush ] tri ; : read-irc-message ( -- string ) current-irc-client get duplex-stream>> stream-readln string>irc-message ; ! Hooks that the bot implementor can override for each kind of irc message mm:GENERIC: handle-message ( irc-message -- ) mm:METHOD: handle-message { unhandled { current-irc-client irc-client } } drop ; mm:METHOD: handle-message { irc-message { current-irc-client irc-client } } global. ; : /PONG ( string -- ) "PONG " prepend print-irc ; mm:METHOD: handle-message { ping { current-irc-client irc-client } } trailing>> /PONG ; mm:METHOD: handle-message { rpl-welcome { current-irc-client irc-client } } drop "JOIN #factor22" print-irc ; : irc-loop ( -- ) [ read-irc-message [ handle-message ] [ ] bi ] loop ; TUPLE: log-bot < irc-client ; CONSTRUCTOR: log-bot ( duplex-stream -- obj ) ; ! Log pings for some reason mm:METHOD: handle-message { ping { current-irc-client log-bot } } [ timestamp>> timestamp>rfc822 "Ping received at " prepend global. ] [ trailing>> /PONG ] bi ; mm:METHOD: handle-message { privmsg { current-irc-client log-bot } } [ [ target>> ] [ sender>> ] [ text>> ] tri ] output>array " " s:join global. ; : startbot ( -- ) "irc.freenode.net" 6667 utf8 drop current-irc-client [ "NICK fbot122" print-irc "USER fbot122 fbot irc.freenode.net :lol" print-irc irc-loop ] with-variable ;