Paste: irc framework

Author: erg
Mode: factor
Date: Mon, 28 Dec 2009 20:08:49
Plain Text |
! 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 <inet> utf8 <client> drop <log-bot>
    current-irc-client
    [
        "NICK fbot122" print-irc
        "USER fbot122 fbot irc.freenode.net :lol" print-irc
        irc-loop
    ] with-variable ;

Annotation: logbot vocab

Author: erg
Mode: factor
Date: Mon, 28 Dec 2009 21:20:52
Plain Text |
! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors calendar.format combinators.smart constructors
io.encodings.utf8 io.sockets irc-client irc-client.messages
kernel namespaces sequences ;
QUALIFIED-WITH: multi-methods mm
QUALIFIED-WITH: sequences s
IN: irc-client.logbot

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 <inet> utf8 <client> drop <log-bot>
    current-irc-client
    [
        "NICK fbot122erg" print-irc
        "USER fbot122erg fbot irc.freenode.net :lol" print-irc
        irc-loop
    ] with-variable ;

New Annotation

Summary:
Author:
Mode:
Body: