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
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 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 -- )
77 [ is-running>> ] keep and [
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 ;
108 : unregister-chat ( name -- )
110 [ at [ irc-chat-end ] dip to-chat ]
114 : (remove-participant) ( nick chat -- )
115 [ participants>> delete-at ]
116 [ [ +part+ f <participant-changed> ] dip to-chat ] 2bi ;
118 : remove-participant ( nick channel -- )
119 chat> [ (remove-participant) ] [ drop ] if* ;
121 : chats-with-participant ( nick -- seq )
123 [ [ irc-channel-chat? ] keep and [ participants>> key? ] [ drop f ] if* ]
126 : to-chats-with-participant ( message nickname -- )
127 chats-with-participant [ to-chat ] with each ;
129 : remove-participant-from-all ( nick -- )
130 dup chats-with-participant [ (remove-participant) ] with each ;
132 : notify-rename ( newnick oldnick chat -- )
133 [ participant-changed new +nick+ >>action
134 [ (>>nick) ] [ (>>parameter) ] [ ] tri ] dip to-chat ;
136 : rename-participant ( newnick oldnick chat -- )
137 [ participants>> [ delete-at* drop ] [ swapd set-at ] bi ]
138 [ notify-rename ] 3bi ;
140 : rename-participant-in-all ( oldnick newnick -- )
141 swap dup chats-with-participant [ rename-participant ] with with each ;
143 : add-participant ( mode nick channel -- )
145 [ participants>> set-at ]
146 [ [ +join+ f <participant-changed> ] dip to-chat ] 2bi ;
148 : change-participant-mode ( channel mode nick -- )
150 [ participants>> set-at ]
151 [ [ participant-changed new
152 [ (>>nick) ] [ (>>parameter) ] [ +mode+ >>action ] tri ] dip to-chat ]
157 ! ======================================
158 ! IRC client messages
159 ! ======================================
162 "NICK " irc-write irc-print ;
166 "USER " irc-write irc-write
167 " hostname servername :irc.factor" irc-print ;
169 : /CONNECT ( server port -- stream )
170 irc> connect>> call drop ;
172 : /JOIN ( channel password -- )
174 [ [ " :" ] dip 3append ] when* irc-print ;
177 "PONG " irc-write irc-print ;
179 ! ======================================
180 ! Server message handling
181 ! ======================================
183 GENERIC: initialize-chat ( chat -- )
184 M: irc-chat initialize-chat drop ;
185 M: irc-channel-chat initialize-chat [ name>> ] [ password>> ] bi /JOIN ;
187 GENERIC: forward-name ( irc-message -- name )
188 M: join forward-name trailing>> ;
189 M: part forward-name channel>> ;
190 M: kick forward-name channel>> ;
191 M: mode forward-name name>> ;
192 M: privmsg forward-name dup name>> me? [ irc-message-sender ] [ name>> ] if ;
194 UNION: single-forward join part kick mode privmsg ;
195 UNION: multiple-forward nick quit ;
196 UNION: broadcast-forward irc-end irc-disconnected irc-connected ;
197 GENERIC: forward-message ( irc-message -- )
199 M: irc-message forward-message
200 +server-chat+ chat> [ to-chat ] [ drop ] if* ;
202 M: single-forward forward-message dup forward-name to-chat ;
204 M: multiple-forward forward-message
205 dup irc-message-sender to-chats-with-participant ;
207 M: broadcast-forward forward-message
208 irc> chats>> values [ to-chat ] with each ;
210 GENERIC: process-message ( irc-message -- )
211 M: object process-message drop ;
212 M: logged-in process-message
213 name>> t irc> [ (>>is-ready) ] [ (>>nick) ] [ chats>> ] tri
214 values [ initialize-chat ] each ;
215 M: ping process-message trailing>> /PONG ;
216 M: nick-in-use process-message name>> "_" append /NICK ;
218 M: join process-message
219 [ drop +normal+ ] [ irc-message-sender ] [ trailing>> ] tri
220 dup chat> [ add-participant ] [ 3drop ] if ;
222 M: part process-message
223 [ irc-message-sender ] [ channel>> ] bi remove-participant ;
225 M: kick process-message
226 [ [ who>> ] [ channel>> ] bi remove-participant ]
227 [ dup who>> me? [ unregister-chat ] [ drop ] if ]
230 M: quit process-message
231 irc-message-sender remove-participant-from-all ;
233 M: nick process-message
234 [ irc-message-sender ] [ trailing>> ] bi rename-participant-in-all ;
236 M: mode process-message ( mode -- )
237 [ channel-mode? ] keep and [
238 [ name>> ] [ mode>> ] [ parameter>> ] tri
239 [ change-participant-mode ] [ 2drop ] if*
242 : >nick/mode ( string -- nick mode )
243 dup first "+@" member? [ unclip ] [ 0 ] if participant-mode ;
245 : names-reply>participants ( names-reply -- participants )
246 trailing>> [ blank? ] trim " " split
247 [ >nick/mode 2array ] map >hashtable ;
249 : maybe-clean-participants ( channel-chat -- )
250 dup clean-participants>> [
251 H{ } clone >>participants f >>clean-participants
254 M: names-reply process-message
255 [ names-reply>participants ] [ channel>> chat> ] bi [
256 [ maybe-clean-participants ]
257 [ participants>> 2array assoc-combine ]
258 [ (>>participants) ] tri
261 M: end-of-names process-message
263 t >>clean-participants
264 [ f f f <participant-changed> ] dip name>> to-chat
267 ! ======================================
268 ! Client message handling
269 ! ======================================
271 GENERIC: handle-outgoing-irc ( irc-message -- ? )
272 M: irc-end handle-outgoing-irc drop f ;
273 M: irc-message handle-outgoing-irc irc-message>client-line irc-print t ;
275 ! ======================================
277 ! ======================================
279 : handle-reader-message ( irc-message -- )
280 irc> in-messages>> mailbox-put ;
284 : (handle-disconnect) ( -- )
286 [ [ irc-disconnected ] dip in-messages>> mailbox-put ]
287 [ dup reconnect-time>> sleep (connect-irc) ]
291 ! FIXME: do something with the exception, store somewhere to help debugging
292 : handle-disconnect ( error -- ? )
293 drop irc> is-running>> [ (handle-disconnect) t ] [ f ] if ;
295 : (reader-loop) ( -- ? )
297 |dispose stream-readln [
298 parse-irc-line handle-reader-message t
304 : reader-loop ( -- ? )
305 [ (reader-loop) ] [ handle-disconnect ] recover ;
307 : writer-loop ( -- ? )
308 irc> out-messages>> mailbox-get handle-outgoing-irc ;
310 ! ======================================
312 ! ======================================
314 : in-multiplexer-loop ( -- ? )
315 irc> in-messages>> mailbox-get
316 [ forward-message ] [ process-message ] [ irc-end? not ] tri ;
318 : strings>privmsg ( name string -- privmsg )
319 privmsg new [ (>>trailing) ] keep [ (>>name) ] keep ;
321 : maybe-annotate-with-name ( name obj -- obj )
322 { { [ dup string? ] [ strings>privmsg ] }
323 { [ dup privmsg instance? ] [ swap >>name ] }
327 GENERIC: annotate-message ( chat object -- object )
328 M: object annotate-message nip ;
329 M: part annotate-message swap name>> >>channel ;
330 M: privmsg annotate-message swap name>> >>name ;
331 M: string annotate-message [ name>> ] dip strings>privmsg ;
334 [ reader-loop ] "irc-reader-loop" spawn-server
335 [ writer-loop ] "irc-writer-loop" spawn-server
336 [ in-multiplexer-loop ] "in-multiplexer-loop" spawn-server
339 GENERIC: (attach-chat) ( irc-chat -- )
341 M: irc-chat (attach-chat)
342 [ [ irc> >>client ] [ name>> ] bi irc> chats>> set-at ]
343 [ [ irc> is-ready>> ] dip and [ initialize-chat ] when* ]
346 M: irc-server-chat (attach-chat)
347 irc> >>client +server-chat+ irc> chats>> set-at ;
349 GENERIC: (remove-chat) ( irc-chat -- )
351 M: irc-nick-chat (remove-chat)
352 name>> unregister-chat ;
354 M: irc-channel-chat (remove-chat)
355 [ part new annotate-message irc> out-messages>> mailbox-put ] keep
356 name>> unregister-chat ;
358 M: irc-server-chat (remove-chat)
359 drop +server-chat+ unregister-chat ;
361 : (connect-irc) ( irc-client -- )
363 [ profile>> [ server>> ] [ port>> ] bi /CONNECT ]
365 [ t swap (>>is-running) ]
366 [ in-messages>> [ irc-connected ] dip mailbox-put ]
369 : with-irc-client ( irc-client quot: ( -- ) -- )
370 [ \ current-irc-client ] dip with-variable ; inline
374 : connect-irc ( irc-client -- )
375 dup [ [ (connect-irc) ] [ nick>> /LOGIN ] bi spawn-irc ] with-irc-client ;
377 : attach-chat ( irc-chat irc-client -- ) [ (attach-chat) ] with-irc-client ;
379 : detach-chat ( irc-chat -- )
380 [ client>> ] keep '[ _ (remove-chat) ] with-irc-client ;
382 : speak ( message irc-chat -- )
383 [ swap annotate-message ] [ client>> out-messages>> mailbox-put ] bi ;
385 : hear ( irc-chat -- message ) in-messages>> mailbox-get ;