Paste: anybot

Author: mnestic
Mode: factor
Date: Tue, 7 Sep 2010 03:53:00
Plain Text |
! Copyright (C) 2008 Slava Pestov, Adam Wearn.
! See http://factorcode.org/license.txt for BSD license.
USING:  accessors assocs compiler continuations debugger eval fry 
        io io.streams.string io.encodings.8-bit io.encodings.string 
        irc.messages irc.client irc.client.chats kernel namespaces math
        parser sequences splitting  threads  ;
RENAME: join sequences => seq:join
IN: irc.anybot

: bot-profile ( -- obj )  "localhost" 6667 "anybot" f <irc-profile> ;

: bot-channel ( -- seq )  "#concatenative" ;

: shorten ( str -- short-str )  dup length 256 > [ 256 short head "..." append ] when ; 

GENERIC: handle-message ( msg -- str )

M: privmsg handle-message 
    [ 
        [   [ text>> ] [ 2drop "" ] recover dup dup 
            length 2 > [ first2 [ CHAR: [ = ] [ CHAR: \s = ] bi* and ] [ drop f ] if
            [ parse-string '[ _ call call ]  { } swap with-datastack ] when 
            drop 
        ] 
        [ nip print-error ] recover 
    ] with-string-writer 
    { { CHAR: \n CHAR: \s } } substitute { { CHAR: \r CHAR: \s } } substitute latin1 decode " " split harvest " " seq:join ;

M: object handle-message ( msg -- str ) drop "" ;
    

  
: bot-loop ( chat -- )           
    dup dup hear '[ _ _ handle-message shorten swap speak stop ] in-thread bot-loop ;

    
: start-bot ( -- chat )
    t auto-use? set-global
    "quiet" on
    bot-profile <irc-client>
    [ connect-irc ]
    [
        [ bot-channel <irc-channel-chat> dup ] dip
        '[ _ [ _ attach-chat ] [ bot-loop ] bi ]
        "anybot" spawn drop
    ] bi ;

: bot ( -- )  start-bot drop ;

MAIN: bot



New Annotation

Summary:
Author:
Mode:
Body: