]> gitweb.factorcode.org Git - factor.git/blob - extra/irc/client/client.factor
569f6c4bf76b1832f588606d85fd5a20ca2adf3a
[factor.git] / extra / irc / client / client.factor
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
6        ascii irc.messages ;
7 RENAME: join sequences => sjoin
8 EXCLUDE: sequences => join ;
9 IN: irc.client
10
11 ! ======================================
12 ! Setup and running objects
13 ! ======================================
14
15 : irc-port 6667 ; ! Default irc port
16
17 TUPLE: irc-profile server port nickname password ;
18 C: <irc-profile> irc-profile
19
20 TUPLE: irc-client profile stream in-messages out-messages join-messages
21        listeners is-running connect reconnect-time ;
22 : <irc-client> ( profile -- irc-client )
23     f <mailbox> <mailbox> <mailbox> H{ } clone f
24     [ <inet> latin1 <client> ] 15 seconds irc-client boa ;
25
26 TUPLE: irc-listener in-messages out-messages ;
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+
31
32 ! participant modes
33 SYMBOL: +operator+
34 SYMBOL: +voice+
35 SYMBOL: +normal+
36
37 : participant-mode ( n -- mode )
38     H{ { 64 +operator+ } { 43 +voice+ } { 0 +normal+ } } at ;
39
40 ! participant changed actions
41 SYMBOL: +join+
42 SYMBOL: +part+
43 SYMBOL: +mode+
44 SYMBOL: +nick+
45
46 ! listener objects
47 : <irc-listener> ( -- irc-listener ) <mailbox> <mailbox> irc-listener boa ;
48
49 : <irc-server-listener> ( -- irc-server-listener )
50      <mailbox> <mailbox> irc-server-listener boa ;
51
52 : <irc-channel-listener> ( name -- irc-channel-listener )
53      [ <mailbox> <mailbox> ] dip f 60 seconds H{ } clone
54      irc-channel-listener boa ;
55
56 : <irc-nick-listener> ( name -- irc-nick-listener )
57      [ <mailbox> <mailbox> ] dip irc-nick-listener boa ;
58
59 ! ======================================
60 ! Message objects
61 ! ======================================
62
63 TUPLE: participant-changed nick action parameter ;
64 C: <participant-changed> participant-changed
65
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
71 <PRIVATE
72 : end-loops ( irc-client -- )
73      [ listeners>> values [ out-messages>> ] map ]
74      [ in-messages>> ]
75      [ out-messages>> ] tri 2array prepend
76      [ irc-end swap mailbox-put ] each ;
77 PRIVATE>
78
79 : terminate-irc ( irc-client -- )
80     [ is-running>> ] keep and [
81         [ end-loops ] [ [ f ] dip (>>is-running) ] bi
82     ] when* ;
83
84 <PRIVATE
85
86 SYMBOL: current-irc-client
87
88 ! ======================================
89 ! Utils
90 ! ======================================
91
92 : irc> ( -- irc-client ) current-irc-client get ;
93 : irc-stream> ( -- stream ) irc> stream>> ;
94 : irc-write ( s -- ) irc-stream> stream-write ;
95 : irc-print ( s -- ) irc-stream> [ stream-print ] keep stream-flush ;
96 : irc-send ( irc-message -- ) irc> out-messages>> mailbox-put ;
97 : listener> ( name -- listener/f ) irc> listeners>> at ;
98 : channel-mode? ( mode -- ? ) name>> first "#&" member? ;
99 : me? ( string -- ? ) irc> profile>> nickname>> = ;
100
101 GENERIC: to-listener ( message obj -- )
102
103 M: string to-listener ( message string -- )
104     listener> [ +server-listener+ listener> ] unless*
105     [ to-listener ] [ drop ] if* ;
106
107 M: irc-listener to-listener ( message irc-listener -- )
108     in-messages>> mailbox-put ;
109
110 : unregister-listener ( name -- )
111     irc> listeners>>
112         [ at [ irc-listener-end ] dip to-listener ]
113         [ delete-at ]
114     2bi ;
115
116 : (remove-participant) ( nick listener -- )
117     [ participants>> delete-at ]
118     [ [ +part+ f <participant-changed> ] dip to-listener ] 2bi ;
119
120 : remove-participant ( nick channel -- )
121     listener> [ (remove-participant) ] [ drop ] if* ;
122
123 : listeners-with-participant ( nick -- seq )
124     irc> listeners>> values
125     [ dup irc-channel-listener? [ participants>> key? ] [ 2drop f ] if ]
126     with filter ;
127
128 : to-listeners-with-participant ( message nickname -- )
129     listeners-with-participant [ to-listener ] with each ;
130
131 : remove-participant-from-all ( nick -- )
132     dup listeners-with-participant [ (remove-participant) ] with each ;
133
134 : notify-rename ( newnick oldnick listener -- )
135     [ participant-changed new +nick+ >>action
136       [ (>>nick) ] [ (>>parameter) ] [ ] tri ] dip to-listener ;
137
138 : rename-participant ( newnick oldnick listener -- )
139     [ participants>> [ delete-at* drop ] [ [ swap ] dip set-at ] bi ]
140     [ notify-rename ] 3bi ;
141
142 : rename-participant-in-all ( oldnick newnick -- )
143     swap dup listeners-with-participant [ rename-participant ] with with each ;
144
145 : add-participant ( mode nick channel -- )
146     listener>
147     [ participants>> set-at ]
148     [ [ +join+ f <participant-changed> ] dip to-listener ] 2bi ;
149
150 : change-participant-mode ( channel mode nick -- )
151     rot listener>
152     [ participants>> set-at ]
153     [ [ [ +mode+ ] dip <participant-changed> ] dip to-listener ] 3bi ; ! FIXME
154
155 DEFER: me?
156
157 : maybe-forward-join ( join -- )
158     [ irc-message-sender me? ] keep and
159     [ irc> join-messages>> mailbox-put ] when* ;
160
161 ! ======================================
162 ! IRC client messages
163 ! ======================================
164
165 : /NICK ( nick -- )
166     "NICK " irc-write irc-print ;
167
168 : /LOGIN ( nick -- )
169     dup /NICK
170     "USER " irc-write irc-write
171     " hostname servername :irc.factor" irc-print ;
172
173 : /CONNECT ( server port -- stream )
174     irc> connect>> call drop ;
175
176 : /JOIN ( channel password -- )
177     "JOIN " irc-write
178     [ [ " :" ] dip 3append ] when* irc-print ;
179
180 : /PONG ( text -- )
181     "PONG " irc-write irc-print ;
182
183 ! ======================================
184 ! Server message handling
185 ! ======================================
186
187 GENERIC: forward-name ( irc-message -- name )
188 M: join forward-name ( join -- name ) trailing>> ;
189 M: part forward-name ( part -- name ) channel>> ;
190 M: kick forward-name ( kick -- name ) channel>> ;
191 M: mode forward-name ( mode -- name ) name>> ;
192 M: privmsg forward-name ( privmsg -- name )
193     dup name>> me? [ irc-message-sender ] [ name>> ] if ;
194
195 UNION: single-forward join part kick mode privmsg ;
196 UNION: multiple-forward nick quit ;
197 UNION: broadcast-forward irc-end irc-disconnected irc-connected ;
198 GENERIC: forward-message ( irc-message -- )
199
200 M: irc-message forward-message ( irc-message -- )
201     +server-listener+ listener> [ to-listener ] [ drop ] if* ;
202
203 M: single-forward forward-message ( forward-single -- )
204     dup forward-name to-listener ;
205
206 M: multiple-forward forward-message ( multiple-forward -- )
207     dup irc-message-sender to-listeners-with-participant ;
208
209 M: join forward-message ( join -- )
210     [ maybe-forward-join ] [ call-next-method ] bi ;
211     
212 M: broadcast-forward forward-message ( irc-broadcasted-message -- )
213     irc> listeners>> values [ to-listener ] with each ;
214
215 GENERIC: process-message ( irc-message -- )
216
217 M: object process-message ( object -- )
218     drop ;
219     
220 M: logged-in process-message ( logged-in -- )
221     name>> irc> profile>> (>>nickname) ;
222
223 M: ping process-message ( ping -- )
224     trailing>> /PONG ;
225
226 M: nick-in-use process-message ( nick-in-use -- )
227     name>> "_" append /NICK ;
228
229 M: join process-message ( join -- )
230     [ drop +normal+ ] [ irc-message-sender ] [ trailing>> ] tri
231     dup listener> [ add-participant ] [ 3drop ] if ;
232
233 M: part process-message ( part -- )
234     [ irc-message-sender ] [ channel>> ] bi remove-participant ;
235
236 M: kick process-message ( kick -- )
237     [ [ who>> ] [ channel>> ] bi remove-participant ]
238     [ dup who>> me? [ unregister-listener ] [ drop ] if ]
239     bi ;
240
241 M: quit process-message ( quit -- )
242     irc-message-sender remove-participant-from-all ;
243
244 M: nick process-message ( nick -- )
245     [ irc-message-sender ] [ trailing>> ] bi rename-participant-in-all ;
246
247 ! M: mode process-message ( mode -- )
248 !    [ channel-mode? ] keep and [
249 !        [ name>> ] [ mode>> ] [ parameter>> ] tri
250 !        [ change-participant-mode ] [ 2drop ] if*
251 !    ] when* ;
252
253 : >nick/mode ( string -- nick mode )
254     dup first "+@" member? [ unclip ] [ 0 ] if participant-mode ;
255
256 : names-reply>participants ( names-reply -- participants )
257     trailing>> [ blank? ] trim " " split
258     [ >nick/mode 2array ] map >hashtable ;
259
260 M: names-reply process-message ( names-reply -- )
261     [ names-reply>participants ] [ channel>> listener> ] bi [
262         [ (>>participants) ]
263         [ [ f f f <participant-changed> ] dip name>> to-listener ] bi
264     ] [ drop ] if* ;
265
266 ! ======================================
267 ! Client message handling
268 ! ======================================
269
270 GENERIC: handle-outgoing-irc ( irc-message -- ? )
271 M: irc-end handle-outgoing-irc ( irc-end -- ? ) drop f ;
272 M: irc-message handle-outgoing-irc ( irc-message -- ? )
273     irc-message>client-line irc-print t ;
274
275 ! ======================================
276 ! Reader/Writer
277 ! ======================================
278
279 : handle-reader-message ( irc-message -- )
280     irc> in-messages>> mailbox-put ;
281
282 DEFER: (connect-irc)
283
284 : (handle-disconnect) ( -- )
285     irc>
286         [ [ irc-disconnected ] dip in-messages>> mailbox-put ]
287         [ dup reconnect-time>> sleep (connect-irc) ]
288         [ profile>> nickname>> /LOGIN ]
289     tri ;
290
291 ! FIXME: do something with the exception, store somewhere to help debugging
292 : handle-disconnect ( error -- )
293     drop irc> is-running>> [ (handle-disconnect) ] when ;
294
295 : (reader-loop) ( -- ? )
296     irc> stream>> [
297         |dispose stream-readln [
298             parse-irc-line handle-reader-message t
299         ] [
300             irc> terminate-irc f
301         ] if*
302     ] with-destructors ;
303
304 : reader-loop ( -- ? )
305     [ (reader-loop) ] [ handle-disconnect t ] recover ;
306
307 : writer-loop ( -- ? )
308     irc> out-messages>> mailbox-get handle-outgoing-irc ;
309
310 ! ======================================
311 ! Processing loops
312 ! ======================================
313
314 : in-multiplexer-loop ( -- ? )
315     irc> in-messages>> mailbox-get
316     [ forward-message ] [ process-message ] [ irc-end? not ] tri ;
317
318 : strings>privmsg ( name string -- privmsg )
319     privmsg new [ (>>trailing) ] keep [ (>>name) ] keep ;
320
321 : maybe-annotate-with-name ( name obj -- obj )
322     { { [ dup string? ] [ strings>privmsg ] }
323       { [ dup privmsg instance? ] [ swap >>name ] }
324       [ nip ]
325     } cond ;
326
327 GENERIC: handle-listener-out ( irc-message -- ? )
328 M: irc-end handle-listener-out ( irc-end -- ? ) drop f ;
329 M: irc-message handle-listener-out ( irc-message -- ? )
330      irc> out-messages>> mailbox-put t ;
331     
332 : listener-loop ( name -- ? )
333     dup listener> [
334         out-messages>> mailbox-get
335         maybe-annotate-with-name handle-listener-out
336     ] [ drop f ] if* ;
337
338 : spawn-irc ( -- )
339     [ reader-loop ] "irc-reader-loop" spawn-server
340     [ writer-loop ] "irc-writer-loop" spawn-server
341     [ in-multiplexer-loop ] "in-multiplexer-loop" spawn-server
342     3drop ;
343
344 ! ======================================
345 ! Listener join request handling
346 ! ======================================
347
348 : set+run-listener ( name irc-listener -- )
349     over irc> listeners>> set-at
350     '[ _ listener-loop ] "irc-listener-loop" spawn-server drop ;
351
352 GENERIC: (add-listener) ( irc-listener -- )
353
354 M: irc-channel-listener (add-listener) ( irc-channel-listener -- )
355     [ [ name>> ] [ password>> ] bi /JOIN ]
356     [ [ [ drop irc> join-messages>> ]
357         [ timeout>> ]
358         [ name>> '[ trailing>> _ = ] ]
359         tri mailbox-get-timeout? trailing>> ] keep set+run-listener
360     ] bi ;
361
362 M: irc-nick-listener (add-listener) ( irc-nick-listener -- )
363     [ name>> ] keep set+run-listener ;
364
365 M: irc-server-listener (add-listener) ( irc-server-listener -- )
366     [ +server-listener+ ] dip set+run-listener ;
367
368 GENERIC: (remove-listener) ( irc-listener -- )
369
370 M: irc-nick-listener (remove-listener) ( irc-nick-listener -- )
371     name>> unregister-listener ;
372
373 M: irc-channel-listener (remove-listener) ( irc-channel-listener -- )
374     [ [ name>> ] [ out-messages>> ] bi
375       [ [ part new ] dip >>channel ] dip mailbox-put ] keep
376     name>> unregister-listener ;
377
378 M: irc-server-listener (remove-listener) ( irc-server-listener -- )
379    drop +server-listener+ unregister-listener ;
380
381 : (connect-irc) ( irc-client -- )
382     [ profile>> [ server>> ] [ port>> ] bi /CONNECT ] keep
383         swap >>stream
384         t >>is-running
385     in-messages>> [ irc-connected ] dip mailbox-put ;
386
387 : with-irc-client ( irc-client quot: ( -- ) -- )
388     [ \ current-irc-client ] dip with-variable ; inline
389
390 PRIVATE>
391
392 : connect-irc ( irc-client -- )
393     [ irc>
394       [ (connect-irc) ] [ profile>> nickname>> /LOGIN ] bi
395       spawn-irc ] with-irc-client ;
396
397 : add-listener ( irc-listener irc-client -- )
398     swap '[ _ (add-listener) ] with-irc-client ;
399
400 : remove-listener ( irc-listener irc-client -- )
401     swap '[ _ (remove-listener) ] with-irc-client ;
402
403 : write-message ( message irc-listener -- ) out-messages>> mailbox-put ;
404 : read-message ( irc-listener -- message ) in-messages>> mailbox-get ;