]> gitweb.factorcode.org Git - factor.git/blob - extra/irc/client/client.factor
Merge branch 'master' of git://factorcode.org/git/factor
[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        chats is-running nick connect reconnect-time is-ready ;
22
23 : <irc-client> ( profile -- irc-client )
24     irc-client new
25         swap >>profile
26         <mailbox> >>in-messages
27         <mailbox> >>out-messages
28         H{ } clone >>chats
29         dup profile>> nickname>> >>nick
30         [ <inet> latin1 <client> ] >>connect
31         15 seconds >>reconnect-time ;
32
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 ;
36 TUPLE: irc-nick-chat < irc-chat name ;
37 SYMBOL: +server-chat+
38
39 ! participant modes
40 SYMBOL: +operator+
41 SYMBOL: +voice+
42 SYMBOL: +normal+
43
44 : participant-mode ( n -- mode )
45     H{ { 64 +operator+ } { 43 +voice+ } { 0 +normal+ } } at ;
46
47 ! participant changed actions
48 SYMBOL: +join+
49 SYMBOL: +part+
50 SYMBOL: +mode+
51 SYMBOL: +nick+
52
53 ! chat objects
54 : <irc-server-chat> ( -- irc-server-chat )
55      <mailbox> f irc-server-chat boa ;
56
57 : <irc-channel-chat> ( name -- irc-channel-chat )
58      [ <mailbox> f ] dip f 60 seconds H{ } clone
59      irc-channel-chat boa ;
60
61 : <irc-nick-chat> ( name -- irc-nick-chat )
62      [ <mailbox> f ] dip irc-nick-chat boa ;
63
64 ! ======================================
65 ! Message objects
66 ! ======================================
67
68 TUPLE: participant-changed nick action parameter ;
69 C: <participant-changed> participant-changed
70
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
75
76 : terminate-irc ( irc-client -- )
77     [ is-running>> ] keep and [
78         f >>is-running
79         [ stream>> dispose ] keep
80         [ in-messages>> ] [ out-messages>> ] bi 2array
81         [ irc-end swap mailbox-put ] each
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-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>> = ;
99
100 GENERIC: to-chat ( message obj -- )
101
102 M: string to-chat
103     chat> [ +server-chat+ chat> ] unless*
104     [ to-chat ] [ drop ] if* ;
105
106 M: irc-chat to-chat in-messages>> mailbox-put ;
107
108 : unregister-chat ( name -- )
109     irc> chats>>
110         [ at [ irc-chat-end ] dip to-chat ]
111         [ delete-at ]
112     2bi ;
113
114 : (remove-participant) ( nick chat -- )
115     [ participants>> delete-at ]
116     [ [ +part+ f <participant-changed> ] dip to-chat ] 2bi ;
117
118 : remove-participant ( nick channel -- )
119     chat> [ (remove-participant) ] [ drop ] if* ;
120
121 : chats-with-participant ( nick -- seq )
122     irc> chats>> values
123     [ [ irc-channel-chat? ] keep and [ participants>> key? ] [ drop f ] if* ]
124     with filter ;
125
126 : to-chats-with-participant ( message nickname -- )
127     chats-with-participant [ to-chat ] with each ;
128
129 : remove-participant-from-all ( nick -- )
130     dup chats-with-participant [ (remove-participant) ] with each ;
131
132 : notify-rename ( newnick oldnick chat -- )
133     [ participant-changed new +nick+ >>action
134       [ (>>nick) ] [ (>>parameter) ] [ ] tri ] dip to-chat ;
135
136 : rename-participant ( newnick oldnick chat -- )
137     [ participants>> [ delete-at* drop ] [ swapd set-at ] bi ]
138     [ notify-rename ] 3bi ;
139
140 : rename-participant-in-all ( oldnick newnick -- )
141     swap dup chats-with-participant [ rename-participant ] with with each ;
142
143 : add-participant ( mode nick channel -- )
144     chat>
145     [ participants>> set-at ]
146     [ [ +join+ f <participant-changed> ] dip to-chat ] 2bi ;
147
148 : change-participant-mode ( channel mode nick -- )
149     rot chat>
150     [ participants>> set-at ]
151     [ [ participant-changed new
152         [ (>>nick) ] [ (>>parameter) ] [ +mode+ >>action ] tri ] dip to-chat ]
153     3bi ; ! FIXME
154
155 DEFER: me?
156
157 ! ======================================
158 ! IRC client messages
159 ! ======================================
160
161 : /NICK ( nick -- )
162     "NICK " irc-write irc-print ;
163
164 : /LOGIN ( nick -- )
165     dup /NICK
166     "USER " irc-write irc-write
167     " hostname servername :irc.factor" irc-print ;
168
169 : /CONNECT ( server port -- stream )
170     irc> connect>> call drop ;
171
172 : /JOIN ( channel password -- )
173     "JOIN " irc-write
174     [ [ " :" ] dip 3append ] when* irc-print ;
175
176 : /PONG ( text -- )
177     "PONG " irc-write irc-print ;
178
179 ! ======================================
180 ! Server message handling
181 ! ======================================
182
183 GENERIC: initialize-chat ( chat -- )
184 M: irc-chat initialize-chat drop ;
185 M: irc-channel-chat initialize-chat [ name>> ] [ password>> ] bi /JOIN ;
186
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 ;
193
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 -- )
198
199 M: irc-message forward-message
200     +server-chat+ chat> [ to-chat ] [ drop ] if* ;
201
202 M: single-forward forward-message dup forward-name to-chat ;
203
204 M: multiple-forward forward-message
205     dup irc-message-sender to-chats-with-participant ;
206   
207 M: broadcast-forward forward-message
208     irc> chats>> values [ to-chat ] with each ;
209
210 GENERIC: process-message ( irc-message -- )
211 M: object      process-message drop ; 
212 M: logged-in   process-message
213     name>> f 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 ;
217
218 M: join process-message
219     [ drop +normal+ ] [ irc-message-sender ] [ trailing>> ] tri
220     dup chat> [ add-participant ] [ 3drop ] if ;
221
222 M: part process-message
223     [ irc-message-sender ] [ channel>> ] bi remove-participant ;
224
225 M: kick process-message
226     [ [ who>> ] [ channel>> ] bi remove-participant ]
227     [ dup who>> me? [ unregister-chat ] [ drop ] if ]
228     bi ;
229
230 M: quit process-message
231     irc-message-sender remove-participant-from-all ;
232
233 M: nick process-message
234     [ irc-message-sender ] [ trailing>> ] bi rename-participant-in-all ;
235
236 M: mode process-message ( mode -- )
237     [ channel-mode? ] keep and [
238         [ name>> ] [ mode>> ] [ parameter>> ] tri
239         [ change-participant-mode ] [ 2drop ] if*
240     ] when* ;
241
242 : >nick/mode ( string -- nick mode )
243     dup first "+@" member? [ unclip ] [ 0 ] if participant-mode ;
244
245 : names-reply>participants ( names-reply -- participants )
246     trailing>> [ blank? ] trim " " split
247     [ >nick/mode 2array ] map >hashtable ;
248
249 M: names-reply process-message
250     [ names-reply>participants ] [ channel>> chat> ] bi [
251         [ (>>participants) ]
252         [ [ f f f <participant-changed> ] dip name>> to-chat ] bi
253     ] [ drop ] if* ;
254
255 ! ======================================
256 ! Client message handling
257 ! ======================================
258
259 GENERIC: handle-outgoing-irc ( irc-message -- ? )
260 M: irc-end     handle-outgoing-irc drop f ;
261 M: irc-message handle-outgoing-irc irc-message>client-line irc-print t ;
262
263 ! ======================================
264 ! Reader/Writer
265 ! ======================================
266
267 : handle-reader-message ( irc-message -- )
268     irc> in-messages>> mailbox-put ;
269
270 DEFER: (connect-irc)
271
272 : (handle-disconnect) ( -- )
273     irc>
274         [ [ irc-disconnected ] dip in-messages>> mailbox-put ]
275         [ dup reconnect-time>> sleep (connect-irc) ]
276         [ nick>> /LOGIN ]
277     tri ;
278
279 ! FIXME: do something with the exception, store somewhere to help debugging
280 : handle-disconnect ( error -- ? )
281     drop irc> is-running>> [ (handle-disconnect) t ] [ f ] if ;
282
283 : (reader-loop) ( -- ? )
284     irc> stream>> [
285         |dispose stream-readln [
286             parse-irc-line handle-reader-message t
287         ] [
288             irc> terminate-irc f
289         ] if*
290     ] with-destructors ;
291
292 : reader-loop ( -- ? )
293     [ (reader-loop) ] [ handle-disconnect ] recover ;
294
295 : writer-loop ( -- ? )
296     irc> out-messages>> mailbox-get handle-outgoing-irc ;
297
298 ! ======================================
299 ! Processing loops
300 ! ======================================
301
302 : in-multiplexer-loop ( -- ? )
303     irc> in-messages>> mailbox-get
304     [ forward-message ] [ process-message ] [ irc-end? not ] tri ;
305
306 : strings>privmsg ( name string -- privmsg )
307     privmsg new [ (>>trailing) ] keep [ (>>name) ] keep ;
308
309 : maybe-annotate-with-name ( name obj -- obj )
310     { { [ dup string? ] [ strings>privmsg ] }
311       { [ dup privmsg instance? ] [ swap >>name ] }
312       [ nip ]
313     } cond ;
314
315 GENERIC: annotate-message ( chat object -- object )
316 M: object  annotate-message nip ;
317 M: part    annotate-message swap name>> >>channel ;
318 M: privmsg annotate-message swap name>> >>name ;
319 M: string  annotate-message [ name>> ] dip strings>privmsg ;
320
321 : spawn-irc ( -- )
322     [ reader-loop ] "irc-reader-loop" spawn-server
323     [ writer-loop ] "irc-writer-loop" spawn-server
324     [ in-multiplexer-loop ] "in-multiplexer-loop" spawn-server
325     3drop ;
326
327 GENERIC: (attach-chat) ( irc-chat -- )
328 USE: prettyprint
329 M: irc-chat (attach-chat)
330     [ [ irc> >>client ] [ name>> ] bi irc> chats>> set-at ]
331     [ [ irc> is-ready>> ] dip and [ initialize-chat ] when* ]
332     bi ;
333
334 M: irc-server-chat (attach-chat)
335     irc> >>client +server-chat+ irc> chats>> set-at ;
336
337 GENERIC: (remove-chat) ( irc-chat -- )
338
339 M: irc-nick-chat (remove-chat)
340     name>> unregister-chat ;
341
342 M: irc-channel-chat (remove-chat)
343     [ part new annotate-message irc> out-messages>> mailbox-put  ] keep
344     name>> unregister-chat ;
345
346 M: irc-server-chat (remove-chat)
347    drop +server-chat+ unregister-chat ;
348
349 : (connect-irc) ( irc-client -- )
350     {
351         [ profile>> [ server>> ] [ port>> ] bi /CONNECT ]
352         [ (>>stream) ]
353         [ t swap (>>is-running) ]
354         [ in-messages>> [ irc-connected ] dip mailbox-put ]
355     } cleave ;
356
357 : with-irc-client ( irc-client quot: ( -- ) -- )
358     [ \ current-irc-client ] dip with-variable ; inline
359
360 PRIVATE>
361
362 : connect-irc ( irc-client -- )
363     dup [ [ (connect-irc) ] [ nick>> /LOGIN ] bi spawn-irc ] with-irc-client ;
364
365 : attach-chat ( irc-chat irc-client -- ) [ (attach-chat) ] with-irc-client ;
366
367 : detach-chat ( irc-chat -- )
368     [ client>> ] keep '[ _ (remove-chat) ] with-irc-client ;
369
370 : speak ( message irc-chat -- )
371     [ swap annotate-message ] [ client>> out-messages>> mailbox-put ] bi ;
372
373 : hear ( irc-chat -- message ) in-messages>> mailbox-get ;