1 ! Copyright (C) 2007 Doug Coleman, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays combinators concurrency.mailboxes io
4 io.encodings.8-bit io.sockets kernel namespaces sequences
5 sequences.lib splitting threads calendar classes.tuple
6 classes ascii assocs accessors destructors continuations ;
9 ! ======================================
10 ! Setup and running objects
11 ! ======================================
13 SYMBOL: current-irc-client
15 : irc-port 6667 ; ! Default irc port
18 TUPLE: irc-profile server port nickname password ;
19 C: <irc-profile> irc-profile
21 TUPLE: irc-channel-profile name password ;
22 : <irc-channel-profile> ( -- irc-channel-profile ) irc-channel-profile new ;
25 TUPLE: nick name channels log ;
28 TUPLE: irc-client profile nick stream in-messages out-messages join-messages
29 listeners is-running connect reconnect-time ;
30 : <irc-client> ( profile -- irc-client )
31 f V{ } clone V{ } clone <nick>
32 f <mailbox> <mailbox> <mailbox> H{ } clone f
33 [ <inet> latin1 <client> ] 15 seconds irc-client boa ;
35 TUPLE: irc-listener in-messages out-messages ;
36 TUPLE: irc-server-listener < irc-listener ;
37 TUPLE: irc-channel-listener < irc-listener name password timeout ;
38 TUPLE: irc-nick-listener < irc-listener name ;
39 UNION: irc-named-listener irc-nick-listener irc-channel-listener ;
41 : <irc-listener> ( -- irc-listener ) <mailbox> <mailbox> irc-listener boa ;
43 : <irc-server-listener> ( -- irc-server-listener )
44 <mailbox> <mailbox> irc-server-listener boa ;
46 : <irc-channel-listener> ( name -- irc-channel-listener )
47 <mailbox> <mailbox> rot f 60 seconds irc-channel-listener boa ;
49 : <irc-nick-listener> ( name -- irc-nick-listener )
50 <mailbox> <mailbox> rot irc-nick-listener boa ;
52 ! ======================================
54 ! ======================================
56 SINGLETON: irc-end ! sent when the client isn't running anymore
57 SINGLETON: irc-disconnected ! sent when connection is lost
58 SINGLETON: irc-connected ! sent when connection is instantiated
59 UNION: irc-broadcasted-message irc-end irc-disconnected irc-connected ;
61 TUPLE: irc-message line prefix command parameters trailing timestamp ;
62 TUPLE: logged-in < irc-message name ;
63 TUPLE: ping < irc-message ;
64 TUPLE: join < irc-message ;
65 TUPLE: part < irc-message name channel ;
66 TUPLE: quit < irc-message ;
67 TUPLE: privmsg < irc-message name ;
68 TUPLE: kick < irc-message channel who ;
69 TUPLE: roomlist < irc-message channel names ;
70 TUPLE: nick-in-use < irc-message asterisk name ;
71 TUPLE: notice < irc-message type ;
72 TUPLE: mode < irc-message name channel mode ;
73 TUPLE: unhandled < irc-message ;
75 : terminate-irc ( irc-client -- )
77 [ in-messages>> irc-end swap mailbox-put ]
78 [ f >>is-running drop ]
83 ! ======================================
85 ! ======================================
87 : irc> ( -- irc-client ) current-irc-client get ;
88 : irc-stream> ( -- stream ) irc> stream>> ;
89 : irc-write ( s -- ) irc-stream> stream-write ;
90 : irc-print ( s -- ) irc-stream> [ stream-print ] keep stream-flush ;
92 ! ======================================
94 ! ======================================
97 "NICK " irc-write irc-print ;
101 "USER " irc-write irc-write
102 " hostname servername :irc.factor" irc-print ;
104 : /CONNECT ( server port -- stream )
105 irc> connect>> call drop ;
107 : /JOIN ( channel password -- )
109 [ " :" swap 3append ] when* irc-print ;
111 : /PART ( channel text -- )
112 [ "PART " irc-write irc-write ] dip
113 " :" irc-write irc-print ;
115 : /KICK ( channel who -- )
116 [ "KICK " irc-write irc-write ] dip
117 " " irc-write irc-print ;
119 : /PRIVMSG ( nick line -- )
120 [ "PRIVMSG " irc-write irc-write ] dip
121 " :" irc-write irc-print ;
123 : /ACTION ( nick line -- )
124 [ 1 , "ACTION " % % 1 , ] "" make /PRIVMSG ;
127 "QUIT :" irc-write irc-print ;
130 "PONG " irc-write irc-print ;
132 ! ======================================
134 ! ======================================
136 : split-at-first ( seq separators -- before after )
137 dupd [ member? ] curry find
142 : remove-heading-: ( seq -- seq ) dup ":" head? [ 1 tail ] when ;
144 : parse-name ( string -- string )
145 remove-heading-: "!" split-at-first drop ;
147 : split-prefix ( string -- string/f string )
149 [ remove-heading-: " " split1 ]
153 : split-trailing ( string -- string string/f )
156 : string>irc-message ( string -- object )
157 dup split-prefix split-trailing
158 [ [ blank? ] trim " " split unclip swap ] dip
159 now irc-message boa ;
161 : parse-irc-line ( string -- message )
164 { "PING" [ \ ping ] }
165 { "NOTICE" [ \ notice ] }
166 { "001" [ \ logged-in ] }
167 { "433" [ \ nick-in-use ] }
168 { "JOIN" [ \ join ] }
169 { "PART" [ \ part ] }
170 { "PRIVMSG" [ \ privmsg ] }
171 { "QUIT" [ \ quit ] }
172 { "MODE" [ \ mode ] }
173 { "KICK" [ \ kick ] }
176 [ [ tuple-slots ] [ parameters>> ] bi append ] dip prefix >tuple ;
178 ! ======================================
179 ! Server message handling
180 ! ======================================
182 : me? ( string -- ? )
183 irc> nick>> name>> = ;
185 : irc-message-origin ( irc-message -- name )
186 dup name>> me? [ prefix>> parse-name ] [ name>> ] if ;
188 : broadcast-message-to-listeners ( message -- )
189 irc> listeners>> values [ in-messages>> mailbox-put ] with each ;
191 GENERIC: handle-incoming-irc ( irc-message -- )
193 M: irc-message handle-incoming-irc ( irc-message -- )
196 M: logged-in handle-incoming-irc ( logged-in -- )
197 name>> irc> nick>> (>>name) ;
199 M: ping handle-incoming-irc ( ping -- )
202 M: nick-in-use handle-incoming-irc ( nick-in-use -- )
203 name>> "_" append /NICK ;
205 M: privmsg handle-incoming-irc ( privmsg -- )
206 dup irc-message-origin irc> listeners>> at
207 [ in-messages>> mailbox-put ] [ drop ] if* ;
209 M: join handle-incoming-irc ( join -- )
210 irc> join-messages>> mailbox-put ;
212 M: irc-broadcasted-message handle-incoming-irc ( irc-broadcasted-message -- )
213 broadcast-message-to-listeners ;
215 ! ======================================
216 ! Client message handling
217 ! ======================================
219 GENERIC: handle-outgoing-irc ( obj -- )
221 M: privmsg handle-outgoing-irc ( privmsg -- )
222 [ name>> ] [ trailing>> ] bi /PRIVMSG ;
224 ! ======================================
226 ! ======================================
228 : irc-mailbox-get ( mailbox quot -- )
229 swap 5 seconds [ mailbox-get-timeout swap call ] 3curry [ drop ] recover ;
231 : stream-readln-or-close ( stream -- str/f )
232 dup stream-readln [ nip ] [ dispose f ] if* ;
234 : handle-reader-message ( irc-message -- )
235 irc> in-messages>> mailbox-put ;
238 : handle-disconnect ( error -- )
240 [ in-messages>> irc-disconnected swap mailbox-put ]
241 [ reconnect-time>> sleep (connect-irc) ]
242 [ profile>> nickname>> /LOGIN ]
245 : (reader-loop) ( -- )
247 |dispose stream-readln [
248 parse-irc-line handle-reader-message
255 [ (reader-loop) ] [ handle-disconnect ] recover ;
258 irc> out-messages>> [ handle-outgoing-irc ] irc-mailbox-get ;
260 ! ======================================
262 ! ======================================
264 : in-multiplexer-loop ( -- )
265 irc> in-messages>> [ handle-incoming-irc ] irc-mailbox-get ;
267 : maybe-annotate-with-name ( name obj -- obj )
268 dup privmsg instance? [ swap >>name ] [ nip ] if ;
270 : listener-loop ( name listener -- )
271 out-messages>> mailbox-get maybe-annotate-with-name
272 irc> out-messages>> mailbox-put ;
274 : spawn-irc-loop ( quot name -- )
275 [ [ irc> is-running>> ] compose ] dip
279 [ reader-loop ] "irc-reader-loop" spawn-irc-loop
280 [ writer-loop ] "irc-writer-loop" spawn-irc-loop
281 [ in-multiplexer-loop ] "in-multiplexer-loop" spawn-irc-loop ;
283 ! ======================================
284 ! Listener join request handling
285 ! ======================================
287 : set+run-listener ( name irc-listener -- )
288 [ [ listener-loop ] 2curry "listener" spawn-irc-loop ]
289 [ swap irc> listeners>> set-at ]
292 GENERIC: (add-listener) ( irc-listener -- )
293 M: irc-channel-listener (add-listener) ( irc-channel-listener -- )
294 [ [ name>> ] [ password>> ] bi /JOIN ]
295 [ [ [ drop irc> join-messages>> ]
297 [ name>> [ swap trailing>> = ] curry ]
298 tri mailbox-get-timeout? trailing>> ] keep set+run-listener
301 M: irc-nick-listener (add-listener) ( irc-nick-listener -- )
302 [ name>> ] keep set+run-listener ;
304 : (connect-irc) ( irc-client -- )
305 [ profile>> [ server>> ] keep port>> /CONNECT ] keep
308 in-messages>> irc-connected swap mailbox-put ;
312 : connect-irc ( irc-client -- )
313 dup current-irc-client [
314 [ (connect-irc) ] [ profile>> nickname>> /LOGIN ] bi
318 GENERIC: add-listener ( irc-client irc-listener -- )
319 M: irc-listener add-listener ( irc-client irc-listener -- )
320 current-irc-client swap [ (add-listener) ] curry with-variable ;