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