]> gitweb.factorcode.org Git - factor.git/blob - extra/irc/client/internals/internals.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / extra / irc / client / internals / internals.factor
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
9
10 : do-connect ( server port quot: ( host port -- stream ) attempts -- stream/f )
11     dup 0 > [
12         [ drop call( host port -- stream ) ]
13         [ drop 15 sleep 1 - do-connect ]
14         recover
15     ] [ 2drop 2drop f ] if ;
16
17 : /NICK ( nick -- ) "NICK " prepend irc-print ;
18 : /PONG ( text -- ) "PONG " prepend irc-print ;
19 : /PASS ( password -- ) "PASS " prepend irc-print ;
20
21 : /LOGIN ( nick -- )
22     dup /NICK
23     "USER " prepend " hostname servername :irc.factor" append irc-print ;
24
25 : /CONNECT ( server port -- stream )
26     irc> [ connect>> ] [ reconnect-attempts>> ] bi do-connect ;
27
28 : /JOIN ( channel password -- )
29     [ " :" swap 3append ] when* "JOIN " prepend irc-print ;
30
31 : try-connect ( -- stream/f )
32     irc> profile>> [ server>> ] [ port>> ] bi /CONNECT ;
33
34 : (terminate-irc) ( -- )
35     irc> dup is-running>> [
36         f >>is-running
37         [ stream>> dispose ] keep
38         [ in-messages>> ] [ out-messages>> ] bi 2array
39         [ irc-end swap mailbox-put ] each
40     ] [ drop ] if ;
41
42 : (connect-irc) ( -- )
43     try-connect [
44         [ irc> ] dip >>stream t >>is-running
45         in-messages>> [ irc-connected ] dip mailbox-put
46     ] [ (terminate-irc) ] if* ;
47
48 : (do-login) ( -- )
49      irc>
50      [ profile>> password>> [ /PASS ] when* ]
51      [ nick>> /LOGIN ]
52      bi ;
53
54 GENERIC: initialize-chat ( chat -- )
55 M: irc-chat         initialize-chat drop ;
56 M: irc-channel-chat initialize-chat [ name>> ] [ password>> ] bi /JOIN ;
57
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 ;
63
64 : delete-chat ( name -- ) irc> chats>> delete-at ;
65 : unregister-chat ( name -- ) [ irc-chat-end chat-put ] [ delete-chat ] bi ;
66
67 ! Server message handling
68
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 ;
74
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 ;
84
85 M: rpl-welcome process-message
86     irc>
87         swap nickname>> >>nick
88         t >>is-ready
89     chats>> values [ initialize-chat ] each ;
90
91 M: kick process-message
92     [ [ user>> ] [ chat> ] bi part-participant ]
93     [ dup user>> me? [ unregister-chat ] [ drop ] if ]
94     bi ;
95
96 M: participant-mode process-message ( participant-mode -- )
97     [ mode>> ] [ name>> ] [ parameter>> ] tri change-participant-mode ;
98
99 M: rpl-names process-message
100     [ nicks>> ] [ chat> ] bi dup ?clear-participants
101     '[ _ join-participant ] each ;
102
103 M: rpl-names-end process-message chat> t >>clear-participants drop ;
104
105 ! Client message handling
106
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 ;
110
111 ! Reader/Writer
112
113 : handle-reader-message ( irc-message -- ) irc> in-messages>> mailbox-put ;
114
115 : (handle-disconnect) ( -- )
116     irc-disconnected irc> in-messages>> mailbox-put
117     (connect-irc) (do-login) ;
118
119 : handle-disconnect ( error -- ? )
120     [ irc> exceptions>> push ] when*
121     irc> is-running>> [ (handle-disconnect) t ] [ f ] if ;
122
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 ;
126
127 : (reader-loop) ( -- ? )
128     stream> [ |dispose stream-readln handle-input ] with-destructors ;
129
130 : reader-loop ( -- ? ) [ (reader-loop) ] [ handle-disconnect ] recover ;
131 : writer-loop ( -- ? ) irc> out-messages>> mailbox-get handle-outgoing-irc ;
132
133 ! Processing loops
134
135 : in-multiplexer-loop ( -- ? )
136     irc> in-messages>> mailbox-get {
137         [ message-forwards ]
138         [ process-message ]
139         [ swap chat-put ]
140         [ irc-end? not ]
141     } cleave ;
142
143 : strings>privmsg ( name string -- privmsg )
144     " :" prepend append "PRIVMSG " prepend string>irc-message ;
145
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 ;
152
153 : spawn-irc ( -- )
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
157     3drop ;
158
159 GENERIC: (attach-chat) ( irc-chat -- )
160
161 M: irc-chat (attach-chat)
162     irc>
163     [ [ chats>> ] [ >>client name>> swap ] 2bi set-at ]
164     [ is-ready>> [ initialize-chat ] [ drop ] if ]
165     2bi ;
166
167 M: irc-server-chat (attach-chat)
168     irc> [ (>>client) ] [ chats>> +server-chat+ set-at ] 2bi ;
169
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 ;
173
174 M: irc-channel-chat remove-chat
175     [ part new annotate-message irc-send ]
176     [ name>> unregister-chat ] bi ;
177
178 : (speak) ( message irc-chat -- ) swap annotate-message irc-send ;