]> gitweb.factorcode.org Git - factor.git/blob - extra/irc/messages/messages.factor
bb78efd6800349c71cd3e2e777f00d35a7d1e7a9
[factor.git] / extra / irc / messages / messages.factor
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 ;
7 IN: irc.messages
8
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 ;
24
25 : <irc-client-message> ( command parameters trailing -- irc-message )
26     irc-message new now >>timestamp
27     [ [ (>>trailing) ] [ (>>parameters) ] [ (>>command) ] tri ] keep ;
28
29 <PRIVATE
30
31 GENERIC: command-string>> ( irc-message -- string )
32
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" ;
43
44 GENERIC: command-parameters>> ( irc-message -- seq )
45
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 ;
58
59 GENERIC: (>>command-parameters) ( params irc-message -- )
60
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 ] }
73     } switch ;
74
75 PRIVATE>
76
77 GENERIC: irc-message>client-line ( irc-message -- string )
78
79 M: irc-message irc-message>client-line ( irc-message -- string )
80     [ command-string>> ]
81     [ command-parameters>> " " sjoin ]
82     [ trailing>> [ CHAR: : prefix ] [ "" ] if* ]
83     tri 3array " " sjoin ;
84
85 GENERIC: irc-message>server-line ( irc-message -- string )
86
87 M: irc-message irc-message>server-line ( irc-message -- string )
88    drop "not implemented yet" ;
89
90 <PRIVATE
91 ! ======================================
92 ! Message parsing
93 ! ======================================
94
95 : split-at-first ( seq separators -- before after )
96     dupd '[ , member? ] find
97         [ cut 1 tail ]
98         [ swap ]
99     if ;
100
101 : remove-heading-: ( seq -- seq ) dup ":" head? [ 1 tail ] when ;
102
103 : parse-name ( string -- string )
104     remove-heading-: "!" split-at-first drop ;
105
106 : split-prefix ( string -- string/f string )
107     dup ":" head?
108         [ remove-heading-: " " split1 ]
109         [ f swap ]
110     if ;
111
112 : split-trailing ( string -- string string/f )
113     ":" split1 ;
114
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) ]
122     } 2cleave ;
123
124 PRIVATE>
125
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 ;
130
131 : string>irc-message ( string -- object )
132     dup split-prefix split-trailing
133     [ [ blank? ] trim " " split unclip swap ] dip
134     now irc-message boa ;
135
136 : parse-irc-line ( string -- message )
137     string>irc-message
138     dup command>> {
139         { "PING"    [ ping ] }
140         { "NOTICE"  [ notice ] }
141         { "001"     [ logged-in ] }
142         { "433"     [ nick-in-use ] }
143         { "353"     [ names-reply ] }
144         { "JOIN"    [ join ] }
145         { "PART"    [ part ] }
146         { "NICK"    [ nick ] }
147         { "PRIVMSG" [ privmsg ] }
148         { "QUIT"    [ quit ] }
149         { "MODE"    [ mode ] }
150         { "KICK"    [ kick ] }
151         [ drop unhandled ]
152     } case new [ copy-message-in ] keep ;