1 ! Copyright (C) 2008 Bruno Deferrari, Doug Coleman, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: concurrency.mailboxes kernel io.sockets io.encodings.8-bit calendar
4 accessors destructors namespaces io assocs arrays qualified fry
5 continuations threads strings classes combinators splitting hashtables
7 RENAME: join sequences => sjoin
8 EXCLUDE: sequences => join ;
11 ! ======================================
12 ! Setup and running objects
13 ! ======================================
15 : irc-port 6667 ; ! Default irc port
17 TUPLE: irc-profile server port nickname password ;
18 C: <irc-profile> irc-profile
20 TUPLE: irc-client profile stream in-messages out-messages
21 listeners is-running nick connect reconnect-time ;
22 : <irc-client> ( profile -- irc-client )
23 [ f <mailbox> <mailbox> H{ } clone f ] keep nickname>>
24 [ <inet> latin1 <client> ] 15 seconds irc-client boa ;
26 TUPLE: irc-listener in-messages client ;
27 TUPLE: irc-server-listener < irc-listener ;
28 TUPLE: irc-channel-listener < irc-listener name password timeout participants ;
29 TUPLE: irc-nick-listener < irc-listener name ;
30 SYMBOL: +server-listener+
37 : participant-mode ( n -- mode )
38 H{ { 64 +operator+ } { 43 +voice+ } { 0 +normal+ } } at ;
40 ! participant changed actions
47 : <irc-listener> ( -- irc-listener ) <mailbox> <mailbox> irc-listener boa ;
49 : <irc-server-listener> ( -- irc-server-listener )
50 <mailbox> f irc-server-listener boa ;
52 : <irc-channel-listener> ( name -- irc-channel-listener )
53 [ <mailbox> f ] dip f 60 seconds H{ } clone
54 irc-channel-listener boa ;
56 : <irc-nick-listener> ( name -- irc-nick-listener )
57 [ <mailbox> f ] dip irc-nick-listener boa ;
59 ! ======================================
61 ! ======================================
63 TUPLE: participant-changed nick action parameter ;
64 C: <participant-changed> participant-changed
66 SINGLETON: irc-listener-end ! send to a listener to stop its execution
67 SINGLETON: irc-end ! sent when the client isn't running anymore
68 SINGLETON: irc-disconnected ! sent when connection is lost
69 SINGLETON: irc-connected ! sent when connection is established
70 SINGLETON: irc-ready ! sent after the client is logged in
72 : terminate-irc ( irc-client -- )
73 [ is-running>> ] keep and [
75 [ in-messages>> ] [ out-messages>> ] bi 2array
76 [ irc-end swap mailbox-put ] each
81 SYMBOL: current-irc-client
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 ;
91 : irc-send ( irc-message -- ) irc> out-messages>> mailbox-put ;
92 : listener> ( name -- listener/f ) irc> listeners>> at ;
93 : channel-mode? ( mode -- ? ) name>> first "#&" member? ;
94 : me? ( string -- ? ) irc> nick>> = ;
96 GENERIC: to-listener ( message obj -- )
99 listener> [ +server-listener+ listener> ] unless*
100 [ to-listener ] [ drop ] if* ;
102 M: irc-listener to-listener in-messages>> mailbox-put ;
104 : unregister-listener ( name -- )
106 [ at [ irc-listener-end ] dip to-listener ]
110 : (remove-participant) ( nick listener -- )
111 [ participants>> delete-at ]
112 [ [ +part+ f <participant-changed> ] dip to-listener ] 2bi ;
114 : remove-participant ( nick channel -- )
115 listener> [ (remove-participant) ] [ drop ] if* ;
117 : listeners-with-participant ( nick -- seq )
118 irc> listeners>> values
119 [ [ irc-channel-listener? ] keep and [ participants>> key? ] [ drop f ] if* ]
122 : to-listeners-with-participant ( message nickname -- )
123 listeners-with-participant [ to-listener ] with each ;
125 : remove-participant-from-all ( nick -- )
126 dup listeners-with-participant [ (remove-participant) ] with each ;
128 : notify-rename ( newnick oldnick listener -- )
129 [ participant-changed new +nick+ >>action
130 [ (>>nick) ] [ (>>parameter) ] [ ] tri ] dip to-listener ;
132 : rename-participant ( newnick oldnick listener -- )
133 [ participants>> [ delete-at* drop ] [ [ swap ] dip set-at ] bi ]
134 [ notify-rename ] 3bi ;
136 : rename-participant-in-all ( oldnick newnick -- )
137 swap dup listeners-with-participant [ rename-participant ] with with each ;
139 : add-participant ( mode nick channel -- )
141 [ participants>> set-at ]
142 [ [ +join+ f <participant-changed> ] dip to-listener ] 2bi ;
144 : change-participant-mode ( channel mode nick -- )
146 [ participants>> set-at ]
147 [ [ [ +mode+ ] dip <participant-changed> ] dip to-listener ] 3bi ; ! FIXME
151 ! ======================================
152 ! IRC client messages
153 ! ======================================
156 "NICK " irc-write irc-print ;
160 "USER " irc-write irc-write
161 " hostname servername :irc.factor" irc-print ;
163 : /CONNECT ( server port -- stream )
164 irc> connect>> call drop ;
166 : /JOIN ( channel password -- )
168 [ [ " :" ] dip 3append ] when* irc-print ;
171 "PONG " irc-write irc-print ;
173 ! ======================================
174 ! Server message handling
175 ! ======================================
177 GENERIC: forward-name ( irc-message -- name )
178 M: join forward-name trailing>> ;
179 M: part forward-name channel>> ;
180 M: kick forward-name channel>> ;
181 M: mode forward-name name>> ;
182 M: privmsg forward-name dup name>> me? [ irc-message-sender ] [ name>> ] if ;
184 UNION: single-forward join part kick mode privmsg ;
185 UNION: multiple-forward nick quit ;
186 UNION: broadcast-forward irc-end irc-disconnected irc-connected ;
187 GENERIC: forward-message ( irc-message -- )
189 M: irc-message forward-message
190 +server-listener+ listener> [ to-listener ] [ drop ] if* ;
192 M: single-forward forward-message dup forward-name to-listener ;
194 M: multiple-forward forward-message
195 dup irc-message-sender to-listeners-with-participant ;
197 M: broadcast-forward forward-message
198 irc> listeners>> values [ to-listener ] with each ;
200 GENERIC: process-message ( irc-message -- )
201 M: object process-message drop ;
202 M: logged-in process-message name>> irc> (>>nick) ;
203 M: ping process-message trailing>> /PONG ;
204 M: nick-in-use process-message name>> "_" append /NICK ;
206 M: join process-message
207 [ drop +normal+ ] [ irc-message-sender ] [ trailing>> ] tri
208 dup listener> [ add-participant ] [ 3drop ] if ;
210 M: part process-message
211 [ irc-message-sender ] [ channel>> ] bi remove-participant ;
213 M: kick process-message
214 [ [ who>> ] [ channel>> ] bi remove-participant ]
215 [ dup who>> me? [ unregister-listener ] [ drop ] if ]
218 M: quit process-message
219 irc-message-sender remove-participant-from-all ;
221 M: nick process-message
222 [ irc-message-sender ] [ trailing>> ] bi rename-participant-in-all ;
224 ! M: mode process-message ( mode -- )
225 ! [ channel-mode? ] keep and [
226 ! [ name>> ] [ mode>> ] [ parameter>> ] tri
227 ! [ change-participant-mode ] [ 2drop ] if*
230 : >nick/mode ( string -- nick mode )
231 dup first "+@" member? [ unclip ] [ 0 ] if participant-mode ;
233 : names-reply>participants ( names-reply -- participants )
234 trailing>> [ blank? ] trim " " split
235 [ >nick/mode 2array ] map >hashtable ;
237 M: names-reply process-message
238 [ names-reply>participants ] [ channel>> listener> ] bi [
240 [ [ f f f <participant-changed> ] dip name>> to-listener ] bi
243 ! ======================================
244 ! Client message handling
245 ! ======================================
247 GENERIC: handle-outgoing-irc ( irc-message -- ? )
248 M: irc-end handle-outgoing-irc drop f ;
249 M: irc-message handle-outgoing-irc irc-message>client-line irc-print t ;
251 ! ======================================
253 ! ======================================
255 : handle-reader-message ( irc-message -- )
256 irc> in-messages>> mailbox-put ;
260 : (handle-disconnect) ( -- )
262 [ [ irc-disconnected ] dip in-messages>> mailbox-put ]
263 [ dup reconnect-time>> sleep (connect-irc) ]
267 ! FIXME: do something with the exception, store somewhere to help debugging
268 : handle-disconnect ( error -- )
269 drop irc> is-running>> [ (handle-disconnect) ] when ;
271 : (reader-loop) ( -- ? )
273 |dispose stream-readln [
274 parse-irc-line handle-reader-message t
280 : reader-loop ( -- ? )
281 [ (reader-loop) ] [ handle-disconnect t ] recover ;
283 : writer-loop ( -- ? )
284 irc> out-messages>> mailbox-get handle-outgoing-irc ;
286 ! ======================================
288 ! ======================================
290 : in-multiplexer-loop ( -- ? )
291 irc> in-messages>> mailbox-get
292 [ forward-message ] [ process-message ] [ irc-end? not ] tri ;
294 : strings>privmsg ( name string -- privmsg )
295 privmsg new [ (>>trailing) ] keep [ (>>name) ] keep ;
297 : maybe-annotate-with-name ( name obj -- obj )
298 { { [ dup string? ] [ strings>privmsg ] }
299 { [ dup privmsg instance? ] [ swap >>name ] }
303 GENERIC: annotate-message ( listener object -- object )
304 M: object annotate-message nip ;
305 M: part annotate-message swap name>> >>channel ;
306 M: privmsg annotate-message swap name>> >>name ;
307 M: string annotate-message [ name>> ] dip strings>privmsg ;
310 [ reader-loop ] "irc-reader-loop" spawn-server
311 [ writer-loop ] "irc-writer-loop" spawn-server
312 [ in-multiplexer-loop ] "in-multiplexer-loop" spawn-server
315 GENERIC: (add-listener) ( irc-listener -- )
317 M: irc-listener (add-listener)
318 [ irc> >>client ] [ name>> ] bi irc> listeners>> set-at ;
320 M: irc-server-listener (add-listener)
321 irc> >>client +server-listener+ irc> listeners>> set-at ;
323 GENERIC: (remove-listener) ( irc-listener -- )
325 M: irc-nick-listener (remove-listener)
326 name>> unregister-listener ;
328 M: irc-channel-listener (remove-listener)
329 [ part new annotate-message irc> out-messages>> mailbox-put ] keep
330 name>> unregister-listener ;
332 M: irc-server-listener (remove-listener)
333 drop +server-listener+ unregister-listener ;
335 : (connect-irc) ( irc-client -- )
337 [ profile>> [ server>> ] [ port>> ] bi /CONNECT ]
339 [ t swap (>>is-running) ]
340 [ in-messages>> [ irc-connected ] dip mailbox-put ]
343 : with-irc-client ( irc-client quot: ( -- ) -- )
344 [ \ current-irc-client ] dip with-variable ; inline
348 : connect-irc ( irc-client -- )
349 dup [ [ (connect-irc) ] [ nick>> /LOGIN ] bi spawn-irc ] with-irc-client ;
351 : add-listener ( irc-listener irc-client -- )
352 swap '[ _ (add-listener) ] with-irc-client ;
354 : remove-listener ( irc-listener -- )
355 [ client>> ] keep '[ _ (remove-listener) ] with-irc-client ;
357 : join-irc-channel ( irc-channel-listener -- )
358 dup client>> [ [ name>> ] [ password>> ] bi /JOIN ] with-irc-client ;
360 : write-message ( message irc-listener -- )
361 [ swap annotate-message ] [ client>> out-messages>> mailbox-put ] bi ;
363 : read-message ( irc-listener -- message ) in-messages>> mailbox-get ;