]> 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 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 CONSTANT: 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 ! ======================================
156 ! IRC client messages
157 ! ======================================
158
159 : /NICK ( nick -- )
160     "NICK " irc-write irc-print ;
161
162 : /LOGIN ( nick -- )
163     dup /NICK
164     "USER " irc-write irc-write
165     " hostname servername :irc.factor" irc-print ;
166
167 : /CONNECT ( server port -- stream )
168     irc> connect>> call drop ; inline
169
170 : /JOIN ( channel password -- )
171     "JOIN " irc-write
172     [ [ " :" ] dip 3append ] when* irc-print ;
173
174 : /PONG ( text -- )
175     "PONG " irc-write irc-print ;
176
177 ! ======================================
178 ! Server message handling
179 ! ======================================
180
181 GENERIC: initialize-chat ( chat -- )
182 M: irc-chat initialize-chat drop ;
183 M: irc-channel-chat initialize-chat [ name>> ] [ password>> ] bi /JOIN ;
184
185 GENERIC: forward-name ( irc-message -- name )
186 M: join forward-name trailing>> ;
187 M: part forward-name channel>> ;
188 M: kick forward-name channel>> ;
189 M: mode forward-name name>> ;
190 M: privmsg forward-name dup name>> me? [ irc-message-sender ] [ name>> ] if ;
191
192 UNION: single-forward join part kick mode privmsg ;
193 UNION: multiple-forward nick quit ;
194 UNION: broadcast-forward irc-end irc-disconnected irc-connected ;
195 GENERIC: forward-message ( irc-message -- )
196
197 M: irc-message forward-message
198     +server-chat+ chat> [ to-chat ] [ drop ] if* ;
199
200 M: single-forward forward-message dup forward-name to-chat ;
201
202 M: multiple-forward forward-message
203     dup irc-message-sender to-chats-with-participant ;
204   
205 M: broadcast-forward forward-message
206     irc> chats>> values [ to-chat ] with each ;
207
208 GENERIC: process-message ( irc-message -- )
209 M: object      process-message drop ; 
210 M: logged-in   process-message
211     name>> t irc> [ (>>is-ready) ] [ (>>nick) ] [ chats>> ] tri
212     values [ initialize-chat ] each ;
213 M: ping        process-message trailing>> /PONG ;
214 M: nick-in-use process-message name>> "_" append /NICK ;
215
216 M: join process-message
217     [ drop +normal+ ] [ irc-message-sender ] [ trailing>> ] tri
218     dup chat> [ add-participant ] [ 3drop ] if ;
219
220 M: part process-message
221     [ irc-message-sender ] [ channel>> ] bi remove-participant ;
222
223 M: kick process-message
224     [ [ who>> ] [ channel>> ] bi remove-participant ]
225     [ dup who>> me? [ unregister-chat ] [ drop ] if ]
226     bi ;
227
228 M: quit process-message
229     irc-message-sender remove-participant-from-all ;
230
231 M: nick process-message
232     [ irc-message-sender ] [ trailing>> ] bi rename-participant-in-all ;
233
234 M: mode process-message ( mode -- )
235     [ channel-mode? ] keep and [
236         [ name>> ] [ mode>> ] [ parameter>> ] tri
237         [ change-participant-mode ] [ 2drop ] if*
238     ] when* ;
239
240 : >nick/mode ( string -- nick mode )
241     dup first "+@" member? [ unclip ] [ 0 ] if participant-mode ;
242
243 : names-reply>participants ( names-reply -- participants )
244     trailing>> [ blank? ] trim " " split
245     [ >nick/mode 2array ] map >hashtable ;
246
247 : maybe-clean-participants ( channel-chat -- )
248     dup clean-participants>> [
249         H{ } clone >>participants f >>clean-participants
250     ] when drop ;
251
252 M: names-reply process-message
253     [ names-reply>participants ] [ channel>> chat> ] bi [
254         [ maybe-clean-participants ] 
255         [ participants>> 2array assoc-combine ]
256         [ (>>participants) ] tri
257     ] [ drop ] if* ;
258
259 M: end-of-names process-message
260     channel>> chat> [
261         t >>clean-participants
262         [ f f f <participant-changed> ] dip name>> to-chat
263     ] when* ;
264
265 ! ======================================
266 ! Client message handling
267 ! ======================================
268
269 GENERIC: handle-outgoing-irc ( irc-message -- ? )
270 M: irc-end     handle-outgoing-irc drop f ;
271 M: irc-message handle-outgoing-irc irc-message>client-line irc-print t ;
272
273 ! ======================================
274 ! Reader/Writer
275 ! ======================================
276
277 : handle-reader-message ( irc-message -- )
278     irc> in-messages>> mailbox-put ;
279
280 DEFER: (connect-irc)
281
282 : (handle-disconnect) ( -- )
283     irc>
284         [ [ irc-disconnected ] dip in-messages>> mailbox-put ]
285         [ dup reconnect-time>> sleep (connect-irc) ]
286         [ nick>> /LOGIN ]
287     tri ;
288
289 ! FIXME: do something with the exception, store somewhere to help debugging
290 : handle-disconnect ( error -- ? )
291     drop irc> is-running>> [ (handle-disconnect) t ] [ f ] if ;
292
293 : (reader-loop) ( -- ? )
294     irc> stream>> [
295         |dispose stream-readln [
296             parse-irc-line handle-reader-message t
297         ] [
298             handle-disconnect
299         ] if*
300     ] with-destructors ;
301
302 : reader-loop ( -- ? )
303     [ (reader-loop) ] [ handle-disconnect ] recover ;
304
305 : writer-loop ( -- ? )
306     irc> out-messages>> mailbox-get handle-outgoing-irc ;
307
308 ! ======================================
309 ! Processing loops
310 ! ======================================
311
312 : in-multiplexer-loop ( -- ? )
313     irc> in-messages>> mailbox-get
314     [ forward-message ] [ process-message ] [ irc-end? not ] tri ;
315
316 : strings>privmsg ( name string -- privmsg )
317     privmsg new [ (>>trailing) ] keep [ (>>name) ] keep ;
318
319 : maybe-annotate-with-name ( name obj -- obj )
320     { { [ dup string? ] [ strings>privmsg ] }
321       { [ dup privmsg instance? ] [ swap >>name ] }
322       [ nip ]
323     } cond ;
324
325 GENERIC: annotate-message ( chat object -- object )
326 M: object  annotate-message nip ;
327 M: part    annotate-message swap name>> >>channel ;
328 M: privmsg annotate-message swap name>> >>name ;
329 M: string  annotate-message [ name>> ] dip strings>privmsg ;
330
331 : spawn-irc ( -- )
332     [ reader-loop ] "irc-reader-loop" spawn-server
333     [ writer-loop ] "irc-writer-loop" spawn-server
334     [ in-multiplexer-loop ] "in-multiplexer-loop" spawn-server
335     3drop ;
336
337 GENERIC: (attach-chat) ( irc-chat -- )
338 USE: prettyprint
339 M: irc-chat (attach-chat)
340     [ [ irc> >>client ] [ name>> ] bi irc> chats>> set-at ]
341     [ [ irc> is-ready>> ] dip and [ initialize-chat ] when* ]
342     bi ;
343
344 M: irc-server-chat (attach-chat)
345     irc> >>client +server-chat+ irc> chats>> set-at ;
346
347 GENERIC: (remove-chat) ( irc-chat -- )
348
349 M: irc-nick-chat (remove-chat)
350     name>> unregister-chat ;
351
352 M: irc-channel-chat (remove-chat)
353     [ part new annotate-message irc> out-messages>> mailbox-put  ] keep
354     name>> unregister-chat ;
355
356 M: irc-server-chat (remove-chat)
357    drop +server-chat+ unregister-chat ;
358
359 : (connect-irc) ( irc-client -- )
360     {
361         [ profile>> [ server>> ] [ port>> ] bi /CONNECT ]
362         [ (>>stream) ]
363         [ t swap (>>is-running) ]
364         [ in-messages>> [ irc-connected ] dip mailbox-put ]
365     } cleave ;
366
367 : with-irc-client ( irc-client quot: ( -- ) -- )
368     [ \ current-irc-client ] dip with-variable ; inline
369
370 PRIVATE>
371
372 : connect-irc ( irc-client -- )
373     dup [ [ (connect-irc) ] [ nick>> /LOGIN ] bi spawn-irc ] with-irc-client ;
374
375 : attach-chat ( irc-chat irc-client -- ) [ (attach-chat) ] with-irc-client ;
376
377 : detach-chat ( irc-chat -- )
378     [ client>> ] keep '[ _ (remove-chat) ] with-irc-client ;
379
380 : speak ( message irc-chat -- )
381     [ swap annotate-message ] [ client>> out-messages>> mailbox-put ] bi ;
382
383 : hear ( irc-chat -- message ) in-messages>> mailbox-get ;