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