1 ! Copyright (C) 2008 Bruno Deferrari
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel fry splitting ascii calendar accessors combinators qualified
4 arrays classes.tuple math.order inverse ;
5 RENAME: join sequences => sjoin
6 EXCLUDE: sequences => join ;
9 TUPLE: irc-message line prefix command parameters trailing timestamp ;
10 TUPLE: logged-in < irc-message name ;
11 TUPLE: ping < irc-message ;
12 TUPLE: join < irc-message ;
13 TUPLE: part < irc-message channel ;
14 TUPLE: quit < irc-message ;
15 TUPLE: nick < irc-message ;
16 TUPLE: privmsg < irc-message name ;
17 TUPLE: kick < irc-message channel who ;
18 TUPLE: roomlist < irc-message channel names ;
19 TUPLE: nick-in-use < irc-message asterisk name ;
20 TUPLE: notice < irc-message type ;
21 TUPLE: mode < irc-message name mode parameter ;
22 TUPLE: names-reply < irc-message who channel ;
23 TUPLE: unhandled < irc-message ;
25 : <irc-client-message> ( command parameters trailing -- irc-message )
26 irc-message new now >>timestamp
27 [ [ (>>trailing) ] [ (>>parameters) ] [ (>>command) ] tri ] keep ;
31 GENERIC: command-string>> ( irc-message -- string )
33 M: irc-message command-string>> ( irc-message -- string ) command>> ;
34 M: ping command-string>> ( ping -- string ) drop "PING" ;
35 M: join command-string>> ( join -- string ) drop "JOIN" ;
36 M: part command-string>> ( part -- string ) drop "PART" ;
37 M: quit command-string>> ( quit -- string ) drop "QUIT" ;
38 M: nick command-string>> ( nick -- string ) drop "NICK" ;
39 M: privmsg command-string>> ( privmsg -- string ) drop "PRIVMSG" ;
40 M: notice command-string>> ( notice -- string ) drop "NOTICE" ;
41 M: mode command-string>> ( mode -- string ) drop "MODE" ;
42 M: kick command-string>> ( kick -- string ) drop "KICK" ;
44 GENERIC: command-parameters>> ( irc-message -- seq )
46 M: irc-message command-parameters>> ( irc-message -- seq ) parameters>> ;
47 M: ping command-parameters>> ( ping -- seq ) drop { } ;
48 M: join command-parameters>> ( join -- seq ) drop { } ;
49 M: part command-parameters>> ( part -- seq ) channel>> 1array ;
50 M: quit command-parameters>> ( quit -- seq ) drop { } ;
51 M: nick command-parameters>> ( nick -- seq ) drop { } ;
52 M: privmsg command-parameters>> ( privmsg -- seq ) name>> 1array ;
53 M: notice command-parameters>> ( norice -- seq ) type>> 1array ;
54 M: kick command-parameters>> ( kick -- seq )
55 [ channel>> ] [ who>> ] bi 2array ;
56 M: mode command-parameters>> ( mode -- seq )
57 [ name>> ] [ channel>> ] [ mode>> ] tri 3array ;
59 GENERIC: (>>command-parameters) ( params irc-message -- )
61 M: irc-message (>>command-parameters) ( params irc-message -- ) 2drop ;
62 M: logged-in (>>command-parameters) ( params part -- ) >r first r> (>>name) ;
63 M: part (>>command-parameters) ( params part -- ) >r first r> (>>channel) ;
64 M: privmsg (>>command-parameters) ( params privmsg -- ) >r first r> (>>name) ;
65 M: notice (>>command-parameters) ( params notice -- ) >r first r> (>>type) ;
66 M: kick (>>command-parameters) ( params kick -- )
67 >r first2 r> [ (>>who) ] [ (>>channel) ] bi ;
68 M: names-reply (>>command-parameters) ( params names-reply -- )
69 [ >r first r> (>>who) ] [ >r third r> (>>channel) ] 2bi ;
70 M: mode (>>command-parameters) ( params mode -- )
71 { { [ >r 2array r> ] [ [ (>>mode) ] [ (>>name) ] bi ] }
72 { [ >r 3array r> ] [ [ (>>parameter) ] [ (>>mode) ] [ (>>name) ] tri ] }
77 GENERIC: irc-message>client-line ( irc-message -- string )
79 M: irc-message irc-message>client-line ( irc-message -- string )
81 [ command-parameters>> " " sjoin ]
82 [ trailing>> [ CHAR: : prefix ] [ "" ] if* ]
83 tri 3array " " sjoin ;
85 GENERIC: irc-message>server-line ( irc-message -- string )
87 M: irc-message irc-message>server-line ( irc-message -- string )
88 drop "not implemented yet" ;
91 ! ======================================
93 ! ======================================
95 : split-at-first ( seq separators -- before after )
96 dupd '[ , member? ] find
101 : remove-heading-: ( seq -- seq ) dup ":" head? [ 1 tail ] when ;
103 : parse-name ( string -- string )
104 remove-heading-: "!" split-at-first drop ;
106 : split-prefix ( string -- string/f string )
108 [ remove-heading-: " " split1 ]
112 : split-trailing ( string -- string string/f )
115 : copy-message-in ( origin dest -- )
116 { [ >r parameters>> r> [ (>>command-parameters) ] [ (>>parameters) ] 2bi ]
117 [ >r line>> r> (>>line) ]
118 [ >r prefix>> r> (>>prefix) ]
119 [ >r command>> r> (>>command) ]
120 [ >r trailing>> r> (>>trailing) ]
121 [ >r timestamp>> r> (>>timestamp) ]
126 UNION: sender-in-prefix privmsg join part quit kick mode nick ;
127 GENERIC: irc-message-sender ( irc-message -- sender )
128 M: sender-in-prefix irc-message-sender ( sender-in-prefix -- sender )
129 prefix>> parse-name ;
131 : string>irc-message ( string -- object )
132 dup split-prefix split-trailing
133 [ [ blank? ] trim " " split unclip swap ] dip
134 now irc-message boa ;
136 : parse-irc-line ( string -- message )
140 { "NOTICE" [ notice ] }
141 { "001" [ logged-in ] }
142 { "433" [ nick-in-use ] }
143 { "353" [ names-reply ] }
147 { "PRIVMSG" [ privmsg ] }
152 } case new [ copy-message-in ] keep ;