1 ! Copyright (C) 2007 Doug Coleman, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays calendar io io.sockets kernel match namespaces
4 sequences splitting strings continuations threads ;
8 TUPLE: profile server port nickname password default-channels ;
11 TUPLE: channel-profile name password auto-rejoin ;
12 C: <channel-profile> channel-profile
15 TUPLE: irc-client profile nick stream stream-process controller-process ;
16 C: <irc-client> irc-client
18 TUPLE: nick name channels log ;
21 TUPLE: channel name topic members log attributes ;
24 ! the delegate of all irc messages
25 TUPLE: irc-message timestamp ;
26 C: <irc-message> irc-message
28 ! "irc message" objects
29 TUPLE: logged-in name text ;
30 C: <logged-in> logged-in
35 TUPLE: join name channel ;
38 TUPLE: part name channel text ;
44 TUPLE: privmsg name text ;
47 TUPLE: kick channel er ee text ;
50 TUPLE: roomlist channel names ;
51 C: <roomlist> roomlist
53 TUPLE: nick-in-use name ;
54 C: <nick-in-use> nick-in-use
56 TUPLE: notice type text ;
59 TUPLE: mode name channel mode text ;
63 TUPLE: unhandled text ;
64 C: <unhandled> unhandled
66 ! "control message" objects
67 TUPLE: command sender ;
68 TUPLE: service predicate quot enabled? ;
69 TUPLE: chat-command from to text ;
70 TUPLE: join-command channel password ;
71 TUPLE: part-command channel text ;
74 : irc-stream> ( -- stream ) irc-client get irc-client-stream ;
75 : trim-: ( seq -- seq ) [ CHAR: : = ] ltrim ;
76 : parse-name ( string -- string )
77 trim-: "!" split first ;
78 : irc-split ( string -- seq )
79 1 swap [ [ CHAR: : = ] find* ] keep
80 swap [ cut trim-: ] [ nip f ] if >r [ blank? ] trim trim-:
81 " " split r> [ 1array append ] when* ;
83 irc-client get irc-client-nick nick-name = ;
86 irc-stream> stream-write ;
89 irc-stream> [ stream-print ] keep stream-flush ;
92 "NICK " irc-write irc-print ;
96 "USER " irc-write irc-write
97 " hostname servername :irc.factor" irc-print ;
99 : connect* ( server port -- )
100 <inet> <client> irc-client get set-irc-client-stream ;
102 : connect ( server -- ) 6667 connect* ;
104 : join ( channel password -- )
106 [ >r " :" r> 3append ] when* irc-print ;
108 : part ( channel text -- )
109 >r "PART " irc-write irc-write r>
110 " :" irc-write irc-print ;
112 : say ( line nick -- )
113 "PRIVMSG " irc-write irc-write " :" irc-write irc-print ;
116 "QUIT :" irc-write irc-print ;
119 GENERIC: handle-irc ( obj -- )
121 M: object handle-irc ( obj -- )
122 "Unhandled irc object" print drop ;
124 M: logged-in handle-irc ( obj -- )
125 logged-in-name irc-client get [ irc-client-nick set-nick-name ] keep
127 irc-client-profile profile-default-channels
129 [ channel-profile-name ] keep
130 channel-profile-password join
133 M: ping handle-irc ( obj -- )
135 ping-name irc-print ;
137 M: nick-in-use handle-irc ( obj -- )
138 nick-in-use-name "_" append nick ;
140 : delegate-timestamp ( obj -- obj )
141 now <irc-message> over set-delegate ;
143 MATCH-VARS: ?name ?name2 ?channel ?text ?mode ;
145 : match-irc ( string -- )
152 { { ?name "001" ?name2 ?text }
153 [ ?name2 ?text <logged-in> ] }
154 { { ?name "433" _ ?name2 "Nickname is already in use." }
155 [ ?name2 <nick-in-use> ] }
157 { { ?name "JOIN" ?channel }
158 [ ?name ?channel <join> ] }
159 { { ?name "PART" ?channel ?text }
160 [ ?name ?channel ?text <part> ] }
161 { { ?name "PRIVMSG" ?channel ?text }
162 [ ?name ?channel ?text <privmsg> ] }
163 { { ?name "QUIT" ?text }
164 [ ?name ?text <quit> ] }
166 { { "NOTICE" ?name ?text }
167 [ ?name ?text <notice> ] }
168 { { ?name "MODE" ?channel ?mode ?text }
169 [ ?name ?channel ?mode ?text <mode> ] }
170 { { ?name "KICK" ?channel ?name2 ?text }
171 [ ?channel ?name ?name2 ?text <kick> ] }
173 ! { { ?name "353" ?name2 _ ?channel ?text }
174 ! [ ?text ?channel ?name2 make-member-list ] }
175 { _ [ line get <unhandled> ] }
177 delegate-timestamp handle-irc flush ;
180 irc-stream> stream-readln
181 [ match-irc irc-loop ] when* ;
183 : do-irc ( irc-client -- )
185 dup irc-client-profile profile-server
186 over irc-client-profile profile-port connect*
187 dup irc-client-profile profile-nickname login
188 [ irc-loop ] [ irc-stream> stream-close ] [ ] cleanup ;
190 : with-infinite-loop ( quot timeout -- quot timeout )
191 "looping" print flush
192 over catch drop dup sleep with-infinite-loop ;
194 : start-irc ( irc-client -- )
195 ! [ [ do-irc ] curry 3000 with-infinite-loop ] with-scope ;
196 [ do-irc ] curry 3000 with-infinite-loop ;
201 "irc.freenode.org" 6667 "factorbot" f
203 "#concatenative-flood" f f <channel-profile> ,
205 f V{ } clone V{ } clone <nick>
209 make-factorbot start-irc ;