]> gitweb.factorcode.org Git - factor.git/blob - extra/irc/client/client.factor
8199347feb0c0a2b6529a8374b97044a9c3e99ca
[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 clean-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 t
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>> 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 ;
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 : maybe-clean-participants ( channel-chat -- )
250     dup clean-participants>> [
251         H{ } clone >>participants f >>clean-participants
252     ] when drop ;
253
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
259     ] [ drop ] if* ;
260
261 M: end-of-names process-message
262     channel>> chat> [
263         t >>clean-participants
264         [ f f f <participant-changed> ] dip name>> to-chat
265     ] when* ;
266
267 ! ======================================
268 ! Client message handling
269 ! ======================================
270
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 ;
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         [ nick>> /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) t ] [ f ] if ;
294
295 : (reader-loop) ( -- ? )
296     irc> stream>> [
297         |dispose stream-readln [
298             parse-irc-line handle-reader-message t
299         ] [
300             handle-disconnect
301         ] if*
302     ] with-destructors ;
303
304 : reader-loop ( -- ? )
305     [ (reader-loop) ] [ handle-disconnect ] 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: 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 ;
332
333 : spawn-irc ( -- )
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
337     3drop ;
338
339 GENERIC: (attach-chat) ( irc-chat -- )
340 USE: prettyprint
341 M: irc-chat (attach-chat)
342     [ [ irc> >>client ] [ name>> ] bi irc> chats>> set-at ]
343     [ [ irc> is-ready>> ] dip and [ initialize-chat ] when* ]
344     bi ;
345
346 M: irc-server-chat (attach-chat)
347     irc> >>client +server-chat+ irc> chats>> set-at ;
348
349 GENERIC: (remove-chat) ( irc-chat -- )
350
351 M: irc-nick-chat (remove-chat)
352     name>> unregister-chat ;
353
354 M: irc-channel-chat (remove-chat)
355     [ part new annotate-message irc> out-messages>> mailbox-put  ] keep
356     name>> unregister-chat ;
357
358 M: irc-server-chat (remove-chat)
359    drop +server-chat+ unregister-chat ;
360
361 : (connect-irc) ( irc-client -- )
362     {
363         [ profile>> [ server>> ] [ port>> ] bi /CONNECT ]
364         [ (>>stream) ]
365         [ t swap (>>is-running) ]
366         [ in-messages>> [ irc-connected ] dip mailbox-put ]
367     } cleave ;
368
369 : with-irc-client ( irc-client quot: ( -- ) -- )
370     [ \ current-irc-client ] dip with-variable ; inline
371
372 PRIVATE>
373
374 : connect-irc ( irc-client -- )
375     dup [ [ (connect-irc) ] [ nick>> /LOGIN ] bi spawn-irc ] with-irc-client ;
376
377 : attach-chat ( irc-chat irc-client -- ) [ (attach-chat) ] with-irc-client ;
378
379 : detach-chat ( irc-chat -- )
380     [ client>> ] keep '[ _ (remove-chat) ] with-irc-client ;
381
382 : speak ( message irc-chat -- )
383     [ swap annotate-message ] [ client>> out-messages>> mailbox-put ] bi ;
384
385 : hear ( irc-chat -- message ) in-messages>> mailbox-get ;