factor/examples/factorbot.factor

115 lines
2.6 KiB
Factor
Raw Normal View History

! Simple IRC bot written in Factor.
2006-09-06 17:19:41 -04:00
REQUIRES: contrib/httpd ;
2005-10-31 20:39:38 -05:00
2006-06-26 04:05:06 -04:00
USING: errors generic hashtables help html http io kernel math
memory namespaces parser prettyprint sequences strings threads
words ;
2005-07-26 16:39:14 -04:00
IN: factorbot
SYMBOL: irc-stream
SYMBOL: nickname
SYMBOL: speaker
SYMBOL: receiver
: irc-write ( s -- ) irc-stream get stream-write ;
: irc-print ( s -- )
irc-stream get stream-print
irc-stream get stream-flush ;
: nick ( nick -- )
dup nickname set "NICK " irc-write irc-print ;
: login ( nick -- )
dup nick
"USER " irc-write irc-write
" hostname servername :irc.factor" irc-print ;
: connect ( server -- ) 6667 <client> irc-stream set ;
: disconnect ( -- ) irc-stream get stream-close ;
: join ( chan -- )
"JOIN " irc-write irc-print ;
GENERIC: handle-irc
2005-07-24 19:08:32 -04:00
PREDICATE: string privmsg " " split1 nip "PRIVMSG" head? ;
PREDICATE: string ping "PING" head? ;
2005-07-24 20:17:51 -04:00
M: object handle-irc ( line -- )
2005-07-23 18:27:29 -04:00
drop ;
: parse-privmsg ( line -- text )
2005-07-24 19:08:32 -04:00
" " split1 nip
"PRIVMSG " ?head drop
" " split1 swap receiver set
":" ?head drop ;
M: privmsg handle-irc ( line -- )
parse-privmsg
" " split1 swap
"factorbot-commands" lookup dup
2005-09-24 15:21:17 -04:00
[ execute ] [ 2drop ] if ;
2005-07-26 16:39:14 -04:00
M: ping handle-irc ( line -- )
"PING " ?head drop "PONG " swap append irc-print ;
2005-07-24 19:08:32 -04:00
: parse-irc ( line -- )
":" ?head [ "!" split1 swap speaker set ] when handle-irc ;
: say ( line nick -- )
"PRIVMSG " irc-write irc-write " :" irc-write irc-print ;
: respond ( line -- )
receiver get nickname get = speaker receiver ? get say ;
: irc-loop ( -- )
2006-01-31 15:41:26 -05:00
irc-stream get stream-readln
[ dup print flush parse-irc irc-loop ] when* ;
: factorbot
"irc.freenode.net" connect
"factorbot" login
"#concatenative" join
2006-01-31 15:41:26 -05:00
[ irc-loop ] [ irc-stream get stream-close ] cleanup ;
2006-01-31 15:41:26 -05:00
: factorbot-loop [ factorbot ] try 30000 sleep factorbot-loop ;
: multiline-respond ( string -- )
<string-reader> lines [ respond ] each ;
2006-06-26 04:05:06 -04:00
: object-href
"http://factorcode.org" swap browser-link-href append ;
: not-found ( str -- )
"Sorry, I couldn't find anything for " swap append respond ;
IN: factorbot-commands
: see ( text -- )
2006-09-05 18:15:51 -04:00
dup words-named dup empty? [
drop
2006-06-26 04:05:06 -04:00
not-found
] [
nip [
dup summary " -- "
2006-06-26 04:05:06 -04:00
rot object-href append3 respond
2005-07-23 18:27:29 -04:00
] each
2005-09-24 15:21:17 -04:00
] if ;
: memory ( text -- )
drop [ room. ] string-out multiline-respond ;
2006-06-26 04:05:06 -04:00
: search ( text -- )
search-help dup empty? [
not-found
] [
first first dup article-title
" -- " rot <link> object-href append3 respond
] if ;
: quit ( text -- )
drop speaker get "slava" = [ disconnect ] when ;
2006-09-06 17:19:41 -04:00
PROVIDE: examples/factorbot ;