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 fry
5 continuations threads strings classes combinators splitting hashtables
6 ascii irc.messages irc.messages.base irc.messages.parser ;
7 RENAME: join sequences => sjoin
8 EXCLUDE: sequences => join ;
11 ! ======================================
12 ! Setup and running objects
13 ! ======================================
15 CONSTANT: 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 chats is-running nick connect reconnect-time is-ready ;
23 : <irc-client> ( profile -- irc-client )
26 <mailbox> >>in-messages
27 <mailbox> >>out-messages
29 dup profile>> nickname>> >>nick
30 [ <inet> latin1 <client> ] >>connect
31 15 seconds >>reconnect-time ;
33 TUPLE: irc-chat in-messages client ;
34 TUPLE: irc-server-chat < irc-chat ;
35 TUPLE: irc-channel-chat < irc-chat name password timeout participants clean-participants ;
36 TUPLE: irc-nick-chat < irc-chat name ;
44 : participant-mode ( n -- mode )
45 H{ { 64 +operator+ } { 43 +voice+ } { 0 +normal+ } } at ;
47 ! participant changed actions
54 : <irc-server-chat> ( -- irc-server-chat )
55 <mailbox> f irc-server-chat boa ;
57 : <irc-channel-chat> ( name -- irc-channel-chat )
58 [ <mailbox> f ] dip f 60 seconds H{ } clone t
59 irc-channel-chat boa ;
61 : <irc-nick-chat> ( name -- irc-nick-chat )
62 [ <mailbox> f ] dip irc-nick-chat boa ;
64 ! ======================================
66 ! ======================================
68 TUPLE: participant-changed nick action parameter ;
69 C: <participant-changed> participant-changed
71 SINGLETON: irc-chat-end ! sent to a chat to stop its execution
72 SINGLETON: irc-end ! sent when the client isn't running anymore
73 SINGLETON: irc-disconnected ! sent when connection is lost
74 SINGLETON: irc-connected ! sent when connection is established
76 : terminate-irc ( irc-client -- )
79 [ stream>> dispose ] keep
80 [ in-messages>> ] [ out-messages>> ] bi 2array
81 [ irc-end swap mailbox-put ] each
86 SYMBOL: current-irc-client
88 ! ======================================
90 ! ======================================
92 : irc> ( -- irc-client ) current-irc-client get ;
93 : irc-write ( s -- ) irc> stream>> stream-write ;
94 : irc-print ( s -- ) irc> stream>> [ stream-print ] keep stream-flush ;
95 : irc-send ( irc-message -- ) irc> out-messages>> mailbox-put ;
96 : chat> ( name -- chat/f ) irc> chats>> at ;
97 : channel-mode? ( mode -- ? ) name>> first "#&" member? ;
98 : me? ( string -- ? ) irc> nick>> = ;
100 GENERIC: to-chat ( message obj -- )
103 chat> [ +server-chat+ chat> ] unless*
104 [ to-chat ] [ drop ] if* ;
106 M: irc-chat to-chat in-messages>> mailbox-put ;
107 M: sequence to-chat [ to-chat ] with each ;
109 : unregister-chat ( name -- )
111 [ at [ irc-chat-end ] dip to-chat ]
115 : (remove-participant) ( nick chat -- )
116 [ participants>> delete-at ]
117 [ [ +part+ f <participant-changed> ] dip to-chat ] 2bi ;
119 : remove-participant ( nick channel -- )
120 chat> [ (remove-participant) ] [ drop ] if* ;
122 : chats-with-participant ( nick -- seq )
124 [ dup irc-channel-chat? [ participants>> key? ] [ 2drop f ] if ]
127 : remove-participant-from-all ( nick -- )
128 dup chats-with-participant [ (remove-participant) ] with each ;
130 : notify-rename ( newnick oldnick chat -- )
131 [ participant-changed new +nick+ >>action
132 [ (>>nick) ] [ (>>parameter) ] [ ] tri ] dip to-chat ;
134 : rename-participant ( newnick oldnick chat -- )
135 [ participants>> [ delete-at* drop ] [ swapd set-at ] bi ]
136 [ notify-rename ] 3bi ;
138 : rename-participant-in-all ( oldnick newnick -- )
139 swap dup chats-with-participant [ rename-participant ] with with each ;
141 : add-participant ( mode nick channel -- )
143 [ participants>> set-at ]
144 [ [ +join+ f <participant-changed> ] dip to-chat ] 2bi ;
146 : change-participant-mode ( channel mode nick -- )
148 [ participants>> set-at ]
149 [ [ participant-changed new
150 [ (>>nick) ] [ (>>parameter) ] [ +mode+ >>action ] tri ] dip to-chat ]
153 ! ======================================
154 ! IRC client messages
155 ! ======================================
158 "NICK " irc-write irc-print ;
162 "USER " irc-write irc-write
163 " hostname servername :irc.factor" irc-print ;
165 : /CONNECT ( server port -- stream )
166 irc> connect>> call( host port -- stream local ) drop ;
168 : /JOIN ( channel password -- )
169 "JOIN " irc-write [ " :" swap 3append ] when* irc-print ;
172 "PONG " irc-write irc-print ;
174 ! ======================================
175 ! Server message handling
176 ! ======================================
178 GENERIC: initialize-chat ( chat -- )
179 M: irc-chat initialize-chat drop ;
180 M: irc-channel-chat initialize-chat [ name>> ] [ password>> ] bi /JOIN ;
182 GENERIC: forward-name ( irc-message -- name )
183 M: join forward-name trailing>> ;
184 M: part forward-name channel>> ;
185 M: kick forward-name channel>> ;
186 M: mode forward-name name>> ;
187 M: privmsg forward-name dup target>> me? [ sender>> ] [ target>> ] if ;
189 UNION: single-forward join part kick mode privmsg ;
190 UNION: multiple-forward nick quit ;
191 UNION: broadcast-forward irc-end irc-disconnected irc-connected ;
192 GENERIC: forward-message ( irc-message -- )
194 M: irc-message forward-message
195 +server-chat+ chat> [ to-chat ] [ drop ] if* ;
197 M: single-forward forward-message dup forward-name to-chat ;
199 M: multiple-forward forward-message
200 dup sender>> chats-with-participant to-chat ;
202 M: broadcast-forward forward-message
203 irc> chats>> values [ to-chat ] with each ;
205 GENERIC: process-message ( irc-message -- )
206 M: object process-message drop ;
207 M: rpl-welcome process-message
208 nickname>> t irc> [ (>>is-ready) ] [ (>>nick) ] [ chats>> ] tri
209 values [ initialize-chat ] each ;
210 M: ping process-message trailing>> /PONG ;
211 M: rpl-nickname-in-use process-message name>> "_" append /NICK ;
213 M: join process-message
214 [ drop +normal+ ] [ sender>> ] [ trailing>> ] tri
215 dup chat> [ add-participant ] [ 3drop ] if ;
217 M: part process-message
218 [ sender>> ] [ channel>> ] bi remove-participant ;
220 M: kick process-message
221 [ [ user>> ] [ channel>> ] bi remove-participant ]
222 [ dup user>> me? [ unregister-chat ] [ drop ] if ]
225 M: quit process-message
226 sender>> remove-participant-from-all ;
228 M: nick process-message
229 [ sender>> ] [ trailing>> ] bi rename-participant-in-all ;
231 M: mode process-message ( mode -- )
233 [ name>> ] [ mode>> ] [ parameter>> ] tri
234 [ change-participant-mode ] [ 2drop ] if*
237 : >nick/mode ( string -- nick mode )
238 dup first "+@" member? [ unclip ] [ 0 ] if participant-mode ;
240 : names-reply>participants ( names-reply -- participants )
241 nicks>> [ blank? ] trim " " split
242 [ >nick/mode 2array ] map >hashtable ;
244 : maybe-clean-participants ( channel-chat -- )
245 dup clean-participants>> [
246 H{ } clone >>participants f >>clean-participants
249 M: rpl-names process-message
250 [ names-reply>participants ] [ channel>> chat> ] bi [
251 [ maybe-clean-participants ]
252 [ participants>> 2array assoc-combine ]
253 [ (>>participants) ] tri
256 M: rpl-names-end process-message
258 t >>clean-participants
259 [ f f f <participant-changed> ] dip name>> to-chat
262 ! ======================================
263 ! Client message handling
264 ! ======================================
266 GENERIC: handle-outgoing-irc ( irc-message -- ? )
267 M: irc-end handle-outgoing-irc drop f ;
268 M: irc-message handle-outgoing-irc irc-message>string irc-print t ;
270 ! ======================================
272 ! ======================================
274 : handle-reader-message ( irc-message -- )
275 irc> in-messages>> mailbox-put ;
279 : (handle-disconnect) ( -- )
281 [ [ irc-disconnected ] dip in-messages>> mailbox-put ]
282 [ dup reconnect-time>> sleep (connect-irc) ]
286 ! FIXME: do something with the exception, store somewhere to help debugging
287 : handle-disconnect ( error -- ? )
288 drop irc> is-running>> [ (handle-disconnect) t ] [ f ] if ;
290 : (reader-loop) ( -- ? )
292 |dispose stream-readln [
293 string>irc-message handle-reader-message t
299 : reader-loop ( -- ? )
300 [ (reader-loop) ] [ handle-disconnect ] recover ;
302 : writer-loop ( -- ? )
303 irc> out-messages>> mailbox-get handle-outgoing-irc ;
305 ! ======================================
307 ! ======================================
309 : in-multiplexer-loop ( -- ? )
310 irc> in-messages>> mailbox-get
311 [ forward-message ] [ process-message ] [ irc-end? not ] tri ;
313 : strings>privmsg ( name string -- privmsg )
314 " :" prepend append "PRIVMSG " prepend string>irc-message ;
316 : maybe-annotate-with-name ( name obj -- obj )
317 { { [ dup string? ] [ strings>privmsg ] }
318 { [ dup privmsg instance? ] [ swap >>name ] }
322 GENERIC: annotate-message ( chat object -- object )
323 M: object annotate-message nip ;
324 M: part annotate-message swap name>> >>channel ;
325 M: privmsg annotate-message swap name>> >>target ;
326 M: string annotate-message [ name>> ] dip strings>privmsg ;
329 [ reader-loop ] "irc-reader-loop" spawn-server
330 [ writer-loop ] "irc-writer-loop" spawn-server
331 [ in-multiplexer-loop ] "in-multiplexer-loop" spawn-server
334 GENERIC: (attach-chat) ( irc-chat -- )
336 M: irc-chat (attach-chat)
337 [ [ irc> >>client ] [ name>> ] bi irc> chats>> set-at ]
338 [ [ irc> is-ready>> ] dip and [ initialize-chat ] when* ]
341 M: irc-server-chat (attach-chat)
342 irc> >>client +server-chat+ irc> chats>> set-at ;
344 GENERIC: (remove-chat) ( irc-chat -- )
346 M: irc-nick-chat (remove-chat)
347 name>> unregister-chat ;
349 M: irc-channel-chat (remove-chat)
350 [ part new annotate-message irc> out-messages>> mailbox-put ] keep
351 name>> unregister-chat ;
353 M: irc-server-chat (remove-chat)
354 drop +server-chat+ unregister-chat ;
356 : (connect-irc) ( irc-client -- )
358 [ profile>> [ server>> ] [ port>> ] bi /CONNECT ]
360 [ t swap (>>is-running) ]
361 [ in-messages>> [ irc-connected ] dip mailbox-put ]
364 : with-irc-client ( irc-client quot: ( -- ) -- )
365 [ \ current-irc-client ] dip with-variable ; inline
369 : connect-irc ( irc-client -- )
370 dup [ [ (connect-irc) ] [ nick>> /LOGIN ] bi spawn-irc ] with-irc-client ;
372 : attach-chat ( irc-chat irc-client -- ) [ (attach-chat) ] with-irc-client ;
374 : detach-chat ( irc-chat -- )
375 [ client>> ] keep '[ _ (remove-chat) ] with-irc-client ;
377 : speak ( message irc-chat -- )
378 [ swap annotate-message ] [ client>> out-messages>> mailbox-put ] bi ;
380 : hear ( irc-chat -- message ) in-messages>> mailbox-get ;