/
factorbot.factor
108 lines (79 loc) · 2.53 KB
/
factorbot.factor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
! Simple IRC bot written in Factor.
REQUIRES: apps/http-server ;
USING: errors generic hashtables help html http io kernel math
memory namespaces parser prettyprint sequences strings threads
words inspector network ;
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 <inet> <client> irc-stream set ;
: disconnect ( -- ) irc-stream get stream-close ;
: join ( chan -- )
"JOIN " irc-write irc-print ;
GENERIC: handle-irc ( line -- )
PREDICATE: string privmsg " " split1 nip "PRIVMSG" head? ;
PREDICATE: string ping "PING" head? ;
M: object handle-irc ( line -- )
drop ;
: parse-privmsg ( line -- text )
" " split1 nip
"PRIVMSG " ?head drop
" " split1 swap receiver set
":" ?head drop ;
M: privmsg handle-irc ( line -- )
parse-privmsg
" " split1 swap
"factorbot-commands" lookup dup
[ execute ] [ 2drop ] if ;
M: ping handle-irc ( line -- )
"PING " ?head drop "PONG " swap append irc-print ;
: 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 ( -- )
irc-stream get stream-readln
[ dup print flush parse-irc irc-loop ] when* ;
: factorbot
"irc.freenode.net" connect
"factorbot" login
"#concatenative" join
[ irc-loop ] [ irc-stream get stream-close ] cleanup ;
: factorbot-loop [ factorbot ] try 30000 sleep factorbot-loop ;
: multiline-respond ( string -- )
string-lines [ respond ] each ;
: 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 -- )
dup words-named dup empty? [
drop
not-found
] [
nip [
dup summary " -- "
rot object-href 3append respond
] each
] if ;
: memory ( text -- )
drop [ room. ] with-string-writer multiline-respond ;
: quit ( text -- )
drop speaker get "slava" = [ disconnect ] when ;
PROVIDE: apps/factorbot ;
MAIN: apps/factorbot factorbot ;