1 ! Copyright (C) 2009 Bruno Deferrari
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs arrays concurrency.mailboxes continuations destructors
4 hashtables io irc.client.base irc.client.chats irc.messages kernel namespaces
5 strings words.symbol irc.messages.base irc.client.participants fry threads
6 combinators irc.messages.parser math ;
7 EXCLUDE: sequences => join ;
8 IN: irc.client.internals
10 : do-connect ( server port quot: ( host port -- stream ) attempts -- stream/f )
12 [ drop call( host port -- stream ) ]
13 [ drop 15 sleep 1- do-connect ]
15 ] [ 2drop 2drop f ] if ;
17 : /NICK ( nick -- ) "NICK " prepend irc-print ;
18 : /PONG ( text -- ) "PONG " prepend irc-print ;
19 : /PASS ( password -- ) "PASS " prepend irc-print ;
23 "USER " prepend " hostname servername :irc.factor" append irc-print ;
25 : /CONNECT ( server port -- stream )
26 irc> [ connect>> ] [ reconnect-attempts>> ] bi do-connect ;
28 : /JOIN ( channel password -- )
29 [ " :" swap 3append ] when* "JOIN " prepend irc-print ;
31 : try-connect ( -- stream/f )
32 irc> profile>> [ server>> ] [ port>> ] bi /CONNECT ;
34 : (terminate-irc) ( -- )
35 irc> dup is-running>> [
37 [ stream>> dispose ] keep
38 [ in-messages>> ] [ out-messages>> ] bi 2array
39 [ irc-end swap mailbox-put ] each
42 : (connect-irc) ( -- )
44 [ irc> ] dip >>stream t >>is-running
45 in-messages>> [ irc-connected ] dip mailbox-put
46 ] [ (terminate-irc) ] if* ;
50 [ profile>> password>> [ /PASS ] when* ]
54 GENERIC: initialize-chat ( chat -- )
55 M: irc-chat initialize-chat drop ;
56 M: irc-channel-chat initialize-chat [ name>> ] [ password>> ] bi /JOIN ;
58 GENERIC: chat-put ( message obj -- )
59 M: irc-chat chat-put in-messages>> mailbox-put ;
60 M: symbol chat-put chat> [ chat-put ] [ drop ] if* ;
61 M: string chat-put chat> +server-chat+ or chat-put ;
62 M: sequence chat-put [ chat-put ] with each ;
64 : delete-chat ( name -- ) irc> chats>> delete-at ;
65 : unregister-chat ( name -- ) [ irc-chat-end chat-put ] [ delete-chat ] bi ;
67 ! Server message handling
69 GENERIC: message-forwards ( irc-message -- seq )
70 M: irc-message message-forwards drop +server-chat+ ;
71 M: to-one-chat message-forwards chat> ;
72 M: to-all-chats message-forwards drop chats> ;
73 M: to-many-chats message-forwards sender>> participant-chats ;
75 GENERIC: process-message ( irc-message -- )
76 M: object process-message drop ;
77 M: ping process-message trailing>> /PONG ;
78 ! FIXME: it shouldn't be checking for the presence of chat here...
79 M: join process-message [ sender>> ] [ chat> ] bi [ join-participant ] [ drop ] if* ;
80 M: part process-message [ sender>> ] [ chat> ] bi [ part-participant ] [ drop ] if* ;
81 M: quit process-message sender>> quit-participant ;
82 M: nick process-message [ trailing>> ] [ sender>> ] bi rename-participant* ;
83 M: rpl-nickname-in-use process-message name>> "_" append /NICK ;
85 M: rpl-welcome process-message
87 swap nickname>> >>nick
89 chats>> values [ initialize-chat ] each ;
91 M: kick process-message
92 [ [ user>> ] [ chat> ] bi part-participant ]
93 [ dup user>> me? [ unregister-chat ] [ drop ] if ]
96 M: participant-mode process-message ( participant-mode -- )
97 [ mode>> ] [ name>> ] [ parameter>> ] tri change-participant-mode ;
99 M: rpl-names process-message
100 [ nicks>> ] [ chat> ] bi dup ?clear-participants
101 '[ _ join-participant ] each ;
103 M: rpl-names-end process-message chat> t >>clear-participants drop ;
105 ! Client message handling
107 GENERIC: handle-outgoing-irc ( irc-message -- ? )
108 M: irc-end handle-outgoing-irc drop f ;
109 M: irc-message handle-outgoing-irc irc-message>string irc-print t ;
113 : handle-reader-message ( irc-message -- ) irc> in-messages>> mailbox-put ;
115 : (handle-disconnect) ( -- )
116 irc-disconnected irc> in-messages>> mailbox-put
117 (connect-irc) (do-login) ;
119 : handle-disconnect ( error -- ? )
120 [ irc> exceptions>> push ] when*
121 irc> is-running>> [ (handle-disconnect) t ] [ f ] if ;
123 GENERIC: handle-input ( line/f -- ? )
124 M: string handle-input string>irc-message handle-reader-message t ;
125 M: f handle-input handle-disconnect ;
127 : (reader-loop) ( -- ? )
128 stream> [ |dispose stream-readln handle-input ] with-destructors ;
130 : reader-loop ( -- ? ) [ (reader-loop) ] [ handle-disconnect ] recover ;
131 : writer-loop ( -- ? ) irc> out-messages>> mailbox-get handle-outgoing-irc ;
135 : in-multiplexer-loop ( -- ? )
136 irc> in-messages>> mailbox-get {
143 : strings>privmsg ( name string -- privmsg )
144 " :" prepend append "PRIVMSG " prepend string>irc-message ;
146 GENERIC: annotate-message ( chat object -- object )
147 M: object annotate-message nip ;
148 M: to-channel annotate-message swap name>> >>channel ;
149 M: to-target annotate-message swap name>> >>target ;
150 M: mode annotate-message swap name>> >>name ;
151 M: string annotate-message [ name>> ] dip strings>privmsg ;
154 [ reader-loop ] "irc-reader-loop" spawn-server
155 [ writer-loop ] "irc-writer-loop" spawn-server
156 [ in-multiplexer-loop ] "in-multiplexer-loop" spawn-server
159 GENERIC: (attach-chat) ( irc-chat -- )
161 M: irc-chat (attach-chat)
163 [ [ chats>> ] [ >>client name>> swap ] 2bi set-at ]
164 [ is-ready>> [ initialize-chat ] [ drop ] if ]
167 M: irc-server-chat (attach-chat)
168 irc> [ (>>client) ] [ chats>> +server-chat+ set-at ] 2bi ;
170 GENERIC: remove-chat ( irc-chat -- )
171 M: irc-nick-chat remove-chat name>> unregister-chat ;
172 M: irc-server-chat remove-chat drop +server-chat+ unregister-chat ;
174 M: irc-channel-chat remove-chat
175 [ part new annotate-message irc-send ]
176 [ name>> unregister-chat ] bi ;
178 : (speak) ( message irc-chat -- ) swap annotate-message irc-send ;