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 ;
7 EXCLUDE: sequences => join ;
8 IN: irc.client.internals
10 : /NICK ( nick -- ) "NICK " prepend irc-print ;
11 : /PONG ( text -- ) "PONG " prepend irc-print ;
15 "USER " prepend " hostname servername :irc.factor" append irc-print ;
17 : /CONNECT ( server port -- stream )
18 irc> connect>> call( host port -- stream local ) drop ;
20 : /JOIN ( channel password -- )
21 [ " :" swap 3append ] when* "JOIN " prepend irc-print ;
23 : (connect-irc) ( -- )
25 [ profile>> [ server>> ] [ port>> ] bi /CONNECT ]
27 [ t swap (>>is-running) ]
28 [ in-messages>> [ irc-connected ] dip mailbox-put ]
31 : (do-login) ( -- ) irc> nick>> /LOGIN ;
33 GENERIC: initialize-chat ( chat -- )
34 M: irc-chat initialize-chat drop ;
35 M: irc-channel-chat initialize-chat [ name>> ] [ password>> ] bi /JOIN ;
37 GENERIC: chat-put ( message obj -- )
38 M: irc-chat chat-put in-messages>> mailbox-put ;
39 M: symbol chat-put chat> [ chat-put ] [ drop ] if* ;
40 M: string chat-put chat> +server-chat+ or chat-put ;
41 M: sequence chat-put [ chat-put ] with each ;
43 : delete-chat ( name -- ) irc> chats>> delete-at ;
44 : unregister-chat ( name -- ) [ irc-chat-end chat-put ] [ delete-chat ] bi ;
46 ! Server message handling
48 GENERIC: message-forwards ( irc-message -- seq )
49 M: irc-message message-forwards drop +server-chat+ ;
50 M: to-one-chat message-forwards chat> ;
51 M: to-all-chats message-forwards drop chats> ;
52 M: to-many-chats message-forwards sender>> participant-chats ;
54 GENERIC: process-message ( irc-message -- )
55 M: object process-message drop ;
56 M: ping process-message trailing>> /PONG ;
57 M: join process-message [ sender>> ] [ chat> ] bi join-participant ;
58 M: part process-message [ sender>> ] [ chat> ] bi part-participant ;
59 M: quit process-message sender>> quit-participant ;
60 M: nick process-message [ trailing>> ] [ sender>> ] bi rename-participant* ;
61 M: rpl-nickname-in-use process-message name>> "_" append /NICK ;
63 M: rpl-welcome process-message
65 swap nickname>> >>nick
67 chats>> values [ initialize-chat ] each ;
69 M: kick process-message
70 [ [ user>> ] [ chat> ] bi part-participant ]
71 [ dup user>> me? [ unregister-chat ] [ drop ] if ]
74 M: participant-mode process-message ( participant-mode -- )
75 [ mode>> ] [ name>> ] [ parameter>> ] tri change-participant-mode ;
77 M: rpl-names process-message
78 [ nicks>> ] [ chat> ] bi dup ?clear-participants
79 '[ _ join-participant ] each ;
81 M: rpl-names-end process-message chat> t >>clear-participants drop ;
83 ! Client message handling
85 GENERIC: handle-outgoing-irc ( irc-message -- ? )
86 M: irc-end handle-outgoing-irc drop f ;
87 M: irc-message handle-outgoing-irc irc-message>string irc-print t ;
91 : handle-reader-message ( irc-message -- ) irc> in-messages>> mailbox-put ;
93 : (handle-disconnect) ( -- )
94 irc-disconnected irc> in-messages>> mailbox-put
95 irc> reconnect-time>> sleep
99 : handle-disconnect ( error -- ? )
100 [ irc> exceptions>> push ] when*
101 irc> is-running>> [ (handle-disconnect) t ] [ f ] if ;
103 GENERIC: handle-input ( line/f -- ? )
104 M: string handle-input string>irc-message handle-reader-message t ;
105 M: f handle-input handle-disconnect ;
107 : (reader-loop) ( -- ? )
108 stream> [ |dispose stream-readln handle-input ] with-destructors ;
110 : reader-loop ( -- ? ) [ (reader-loop) ] [ handle-disconnect ] recover ;
111 : writer-loop ( -- ? ) irc> out-messages>> mailbox-get handle-outgoing-irc ;
115 : in-multiplexer-loop ( -- ? )
116 irc> in-messages>> mailbox-get {
123 : strings>privmsg ( name string -- privmsg )
124 " :" prepend append "PRIVMSG " prepend string>irc-message ;
126 GENERIC: annotate-message ( chat object -- object )
127 M: object annotate-message nip ;
128 M: to-channel annotate-message swap name>> >>channel ;
129 M: to-target annotate-message swap name>> >>target ;
130 M: mode annotate-message swap name>> >>name ;
131 M: string annotate-message [ name>> ] dip strings>privmsg ;
134 [ reader-loop ] "irc-reader-loop" spawn-server
135 [ writer-loop ] "irc-writer-loop" spawn-server
136 [ in-multiplexer-loop ] "in-multiplexer-loop" spawn-server
139 GENERIC: (attach-chat) ( irc-chat -- )
141 M: irc-chat (attach-chat)
143 [ [ chats>> ] [ >>client name>> swap ] 2bi set-at ]
144 [ is-ready>> [ initialize-chat ] [ drop ] if ]
147 M: irc-server-chat (attach-chat)
148 irc> [ (>>client) ] [ chats>> +server-chat+ set-at ] 2bi ;
150 GENERIC: remove-chat ( irc-chat -- )
151 M: irc-nick-chat remove-chat name>> unregister-chat ;
152 M: irc-server-chat remove-chat drop +server-chat+ unregister-chat ;
154 M: irc-channel-chat remove-chat
155 [ part new annotate-message irc-send ]
156 [ name>> unregister-chat ] bi ;
158 : (terminate-irc) ( -- )
159 irc> dup is-running>> [
161 [ stream>> dispose ] keep
162 [ in-messages>> ] [ out-messages>> ] bi 2array
163 [ irc-end swap mailbox-put ] each
166 : (speak) ( message irc-chat -- ) swap annotate-message irc-send ;