2008-07-09 21:48:17 -04:00
|
|
|
! Copyright (C) 2008 Bruno Deferrari
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-07-11 19:23:31 -04:00
|
|
|
USING: kernel fry splitting ascii calendar accessors combinators qualified
|
2008-09-19 21:14:00 -04:00
|
|
|
arrays classes.tuple math.order ;
|
2008-07-11 19:23:31 -04:00
|
|
|
RENAME: join sequences => sjoin
|
|
|
|
EXCLUDE: sequences => join ;
|
2008-07-09 21:48:17 -04:00
|
|
|
IN: irc.messages
|
|
|
|
|
|
|
|
TUPLE: irc-message line prefix command parameters trailing timestamp ;
|
|
|
|
TUPLE: logged-in < irc-message name ;
|
|
|
|
TUPLE: ping < irc-message ;
|
2008-07-11 00:16:15 -04:00
|
|
|
TUPLE: join < irc-message ;
|
2008-07-09 21:48:17 -04:00
|
|
|
TUPLE: part < irc-message channel ;
|
|
|
|
TUPLE: quit < irc-message ;
|
2008-07-31 20:35:09 -04:00
|
|
|
TUPLE: nick < irc-message ;
|
2008-07-09 21:48:17 -04:00
|
|
|
TUPLE: privmsg < irc-message name ;
|
|
|
|
TUPLE: kick < irc-message channel who ;
|
|
|
|
TUPLE: roomlist < irc-message channel names ;
|
2008-10-06 15:54:27 -04:00
|
|
|
TUPLE: nick-in-use < irc-message asterisk name ;
|
2008-07-09 21:48:17 -04:00
|
|
|
TUPLE: notice < irc-message type ;
|
2008-09-06 23:14:51 -04:00
|
|
|
TUPLE: mode < irc-message name mode parameter ;
|
2008-09-05 01:16:38 -04:00
|
|
|
TUPLE: names-reply < irc-message who channel ;
|
2008-07-09 21:48:17 -04:00
|
|
|
TUPLE: unhandled < irc-message ;
|
|
|
|
|
2008-07-14 20:43:42 -04:00
|
|
|
: <irc-client-message> ( command parameters trailing -- irc-message )
|
2008-10-06 15:54:27 -04:00
|
|
|
irc-message new
|
|
|
|
now >>timestamp
|
|
|
|
swap >>trailing
|
|
|
|
swap >>parameters
|
|
|
|
swap >>command ;
|
2008-07-14 20:39:52 -04:00
|
|
|
|
2008-07-26 14:32:16 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2008-09-05 01:16:38 -04:00
|
|
|
GENERIC: command-string>> ( irc-message -- string )
|
|
|
|
|
2008-10-06 15:54:27 -04:00
|
|
|
M: irc-message command-string>> ( irc-message -- string ) command>> ;
|
|
|
|
M: ping command-string>> ( ping -- string ) drop "PING" ;
|
|
|
|
M: join command-string>> ( join -- string ) drop "JOIN" ;
|
|
|
|
M: part command-string>> ( part -- string ) drop "PART" ;
|
|
|
|
M: quit command-string>> ( quit -- string ) drop "QUIT" ;
|
|
|
|
M: nick command-string>> ( nick -- string ) drop "NICK" ;
|
|
|
|
M: privmsg command-string>> ( privmsg -- string ) drop "PRIVMSG" ;
|
|
|
|
M: notice command-string>> ( notice -- string ) drop "NOTICE" ;
|
|
|
|
M: mode command-string>> ( mode -- string ) drop "MODE" ;
|
|
|
|
M: kick command-string>> ( kick -- string ) drop "KICK" ;
|
2008-09-05 01:16:38 -04:00
|
|
|
|
|
|
|
GENERIC: command-parameters>> ( irc-message -- seq )
|
|
|
|
|
2008-10-06 15:54:27 -04:00
|
|
|
M: irc-message command-parameters>> ( irc-message -- seq ) parameters>> ;
|
|
|
|
M: ping command-parameters>> ( ping -- seq ) drop { } ;
|
|
|
|
M: join command-parameters>> ( join -- seq ) drop { } ;
|
|
|
|
M: part command-parameters>> ( part -- seq ) channel>> 1array ;
|
|
|
|
M: quit command-parameters>> ( quit -- seq ) drop { } ;
|
|
|
|
M: nick command-parameters>> ( nick -- seq ) drop { } ;
|
|
|
|
M: privmsg command-parameters>> ( privmsg -- seq ) name>> 1array ;
|
|
|
|
M: notice command-parameters>> ( norice -- seq ) type>> 1array ;
|
|
|
|
M: kick command-parameters>> ( kick -- seq )
|
|
|
|
[ channel>> ] [ who>> ] bi 2array ;
|
|
|
|
M: mode command-parameters>> ( mode -- seq )
|
|
|
|
[ name>> ] [ channel>> ] [ mode>> ] tri 3array ;
|
|
|
|
|
|
|
|
GENERIC# >>command-parameters 1 ( irc-message params -- irc-message )
|
|
|
|
|
|
|
|
M: irc-message >>command-parameters ( irc-message params -- irc-message )
|
|
|
|
drop ;
|
|
|
|
|
|
|
|
M: logged-in >>command-parameters ( part params -- part )
|
|
|
|
first >>name ;
|
|
|
|
|
|
|
|
M: privmsg >>command-parameters ( privmsg params -- privmsg )
|
|
|
|
first >>name ;
|
|
|
|
|
|
|
|
M: notice >>command-parameters ( notice params -- notice )
|
|
|
|
first >>type ;
|
|
|
|
|
|
|
|
M: part >>command-parameters ( part params -- part )
|
|
|
|
first >>channel ;
|
|
|
|
|
|
|
|
M: kick >>command-parameters ( kick params -- kick )
|
|
|
|
first2 [ >>channel ] [ >>who ] bi* ;
|
|
|
|
|
|
|
|
M: nick-in-use >>command-parameters ( nick-in-use params -- nick-in-use )
|
|
|
|
second >>name ;
|
|
|
|
|
|
|
|
M: names-reply >>command-parameters ( names-reply params -- names-reply )
|
|
|
|
first3 nip [ >>who ] [ >>channel ] bi* ;
|
|
|
|
|
|
|
|
M: mode >>command-parameters ( mode params -- mode )
|
|
|
|
dup length 3 = [
|
|
|
|
first3 [ >>name ] [ >>mode ] [ >>parameter ] tri*
|
|
|
|
] [
|
|
|
|
first2 [ >>name ] [ >>mode ] bi*
|
|
|
|
] if ;
|
2008-09-05 01:16:38 -04:00
|
|
|
|
2008-07-26 14:32:16 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
2008-07-11 19:23:31 -04:00
|
|
|
GENERIC: irc-message>client-line ( irc-message -- string )
|
|
|
|
|
2008-10-06 15:54:27 -04:00
|
|
|
M: irc-message irc-message>client-line ( irc-message -- string )
|
2008-09-05 01:16:38 -04:00
|
|
|
[ command-string>> ]
|
|
|
|
[ command-parameters>> " " sjoin ]
|
2008-07-26 14:32:16 -04:00
|
|
|
[ trailing>> [ CHAR: : prefix ] [ "" ] if* ]
|
2008-07-11 19:23:31 -04:00
|
|
|
tri 3array " " sjoin ;
|
|
|
|
|
|
|
|
GENERIC: irc-message>server-line ( irc-message -- string )
|
2008-10-06 15:54:27 -04:00
|
|
|
|
|
|
|
M: irc-message irc-message>server-line ( irc-message -- string )
|
|
|
|
drop "not implemented yet" ;
|
2008-07-11 19:23:31 -04:00
|
|
|
|
2008-07-09 21:48:17 -04:00
|
|
|
<PRIVATE
|
2008-10-06 15:54:27 -04:00
|
|
|
|
2008-07-09 21:48:17 -04:00
|
|
|
! ======================================
|
|
|
|
! Message parsing
|
|
|
|
! ======================================
|
|
|
|
|
|
|
|
: split-at-first ( seq separators -- before after )
|
2008-09-19 21:14:00 -04:00
|
|
|
dupd '[ _ member? ] find [ cut 1 tail ] [ swap ] if ;
|
2008-07-09 21:48:17 -04:00
|
|
|
|
2008-10-06 15:54:27 -04:00
|
|
|
: remove-heading-: ( seq -- seq )
|
|
|
|
":" ?head drop ;
|
2008-07-09 21:48:17 -04:00
|
|
|
|
|
|
|
: parse-name ( string -- string )
|
|
|
|
remove-heading-: "!" split-at-first drop ;
|
|
|
|
|
|
|
|
: split-prefix ( string -- string/f string )
|
|
|
|
dup ":" head?
|
2008-10-06 15:54:27 -04:00
|
|
|
[ remove-heading-: " " split1 ] [ f swap ] if ;
|
2008-07-09 21:48:17 -04:00
|
|
|
|
|
|
|
: split-trailing ( string -- string string/f )
|
|
|
|
":" split1 ;
|
|
|
|
|
2008-10-06 15:54:27 -04:00
|
|
|
: copy-message-in ( command irc-message -- command )
|
|
|
|
{
|
|
|
|
[ parameters>> [ >>parameters ] [ >>command-parameters ] bi ]
|
|
|
|
[ line>> >>line ]
|
|
|
|
[ prefix>> >>prefix ]
|
|
|
|
[ command>> >>command ]
|
|
|
|
[ trailing>> >>trailing ]
|
|
|
|
[ timestamp>> >>timestamp ]
|
|
|
|
} cleave ;
|
2008-09-05 01:16:38 -04:00
|
|
|
|
2008-07-14 20:39:52 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
2008-08-12 01:58:12 -04:00
|
|
|
UNION: sender-in-prefix privmsg join part quit kick mode nick ;
|
|
|
|
GENERIC: irc-message-sender ( irc-message -- sender )
|
2008-10-06 15:54:27 -04:00
|
|
|
M: sender-in-prefix irc-message-sender ( sender-in-prefix -- sender )
|
|
|
|
prefix>> parse-name ;
|
2008-08-12 01:58:12 -04:00
|
|
|
|
2008-07-09 21:48:17 -04:00
|
|
|
: string>irc-message ( string -- object )
|
|
|
|
dup split-prefix split-trailing
|
|
|
|
[ [ blank? ] trim " " split unclip swap ] dip
|
|
|
|
now irc-message boa ;
|
|
|
|
|
2008-10-06 15:54:27 -04:00
|
|
|
: irc-message>command ( irc-message -- command )
|
|
|
|
[
|
|
|
|
command>> {
|
|
|
|
{ "PING" [ ping ] }
|
|
|
|
{ "NOTICE" [ notice ] }
|
|
|
|
{ "001" [ logged-in ] }
|
|
|
|
{ "433" [ nick-in-use ] }
|
|
|
|
{ "353" [ names-reply ] }
|
|
|
|
{ "JOIN" [ join ] }
|
|
|
|
{ "PART" [ part ] }
|
|
|
|
{ "NICK" [ nick ] }
|
|
|
|
{ "PRIVMSG" [ privmsg ] }
|
|
|
|
{ "QUIT" [ quit ] }
|
|
|
|
{ "MODE" [ mode ] }
|
|
|
|
{ "KICK" [ kick ] }
|
|
|
|
[ drop unhandled ]
|
|
|
|
} case new
|
|
|
|
] keep copy-message-in ;
|
|
|
|
|
2008-07-09 21:48:17 -04:00
|
|
|
: parse-irc-line ( string -- message )
|
2008-10-06 15:54:27 -04:00
|
|
|
string>irc-message irc-message>command ;
|