<commit_before>! Copyright (C) 2008 <NAME>
! See http://factorcode.org/license.txt for BSD license.
USING: kernel fry splitting ascii calendar accessors combinators qualified
       arrays classes.tuple math.order quotations ;
RENAME: join sequences => sjoin
EXCLUDE: sequences => join ;
IN: irc.messages

TUPLE: irc-message line prefix command parameters trailing timestamp ;
TUPLE: logged-in < irc-message name ;
TUPLE: ping < irc-message ;
TUPLE: join < irc-message ;
TUPLE: part < irc-message channel ;
TUPLE: quit < irc-message ;
TUPLE: nick < irc-message ;
TUPLE: privmsg < irc-message name ;
TUPLE: kick < irc-message channel who ;
TUPLE: roomlist < irc-message channel names ;
TUPLE: nick-in-use < irc-message asterisk name ;
TUPLE: notice < irc-message type ;
TUPLE: mode < irc-message channel mode nickname parameter ;
TUPLE: names-reply < irc-message who channel ;
TUPLE: unhandled < irc-message ;

: <irc-client-message> ( command parameters trailing -- irc-message )
    irc-message new now >>timestamp
    [ [ (>>trailing) ] [ (>>parameters) ] [ (>>command) ] tri ] keep ;

<PRIVATE

: channel? ( string -- ? )
    first "#&" member? ;

GENERIC: command-string>> ( irc-message -- string )

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" ;

GENERIC: command-parameters>> ( irc-message -- seq )

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) ( params irc-message -- )

M: irc-message (>>command-parameters) ( params irc-message -- ) 2drop ;
M: logged-in (>>command-parameters) ( params part -- )  >r first r> (>>name) ;
M: part    (>>command-parameters) ( params part -- )    >r first r> (>>channel) ;
M: privmsg (>>command-parameters) ( params privmsg -- ) >r first r> (>>name) ;
M: notice  (>>command-parameters) ( params notice -- )  >r first r> (>>type) ;
M: kick    (>>command-parameters) ( params kick -- )
    >r first2 r> [ (>>who) ] [ (>>channel) ] bi ;
M: names-reply (>>command-parameters) ( params names-reply -- )
    [ >r first r> (>>who) ] [ >r third r> (>>channel) ] 2bi ;
M: mode    (>>command-parameters) ( params mode -- )
    over first channel? [
        over length 3 = [
            >r first3 r> [ (>>parameter) ] [ (>>mode) ] [ (>>channel) ] tri
        ] [
            >r first2 r>                   [ (>>mode) ] [ (>>channel) ] bi
        ] if
    ] [
        >r first2 r> [ (>>mode) ] [ (>>nickname) ] bi
    ] if ;

PRIVATE>

GENERIC: irc-message>client-line ( irc-message -- string )

M: irc-message irc-message>client-line ( irc-message -- string )
    [ command-string>> ]
    [ command-parameters>> " " sjoin ]
    [ trailing>> [ CHAR: : prefix ] [ "" ] if* ]
    tri 3array " " sjoin ;

GENERIC: irc-message>server-line ( irc-message -- string )

M: irc-message irc-message>server-line ( irc-message -- string )
   drop "not implemented yet" ;

<PRIVATE
! ======================================
! Message parsing<commit_msg>irc.messages: handle mode parameters using inverse's switch<commit_after>! Copyright (C) 2008 <NAME>
! See http://factorcode.org/license.txt for BSD license.
USING: kernel fry splitting ascii calendar accessors combinators qualified
       arrays classes.tuple math.order inverse ;
RENAME: join sequences => sjoin
EXCLUDE: sequences => join ;
IN: irc.messages

TUPLE: irc-message line prefix command parameters trailing timestamp ;
TUPLE: logged-in < irc-message name ;
TUPLE: ping < irc-message ;
TUPLE: join < irc-message ;
TUPLE: part < irc-message channel ;
TUPLE: quit < irc-message ;
TUPLE: nick < irc-message ;
TUPLE: privmsg < irc-message name ;
TUPLE: kick < irc-message channel who ;
TUPLE: roomlist < irc-message channel names ;
TUPLE: nick-in-use < irc-message asterisk name ;
TUPLE: notice < irc-message type ;
TUPLE: mode < irc-message channel mode nickname parameter ;
TUPLE: names-reply < irc-message who channel ;
TUPLE: unhandled < irc-message ;

: <irc-client-message> ( command parameters trailing -- irc-message )
    irc-message new now >>timestamp
    [ [ (>>trailing) ] [ (>>parameters) ] [ (>>command) ] tri ] keep ;

<PRIVATE

: channel? ( string -- ? )
    first "#&" member? ;

GENERIC: command-string>> ( irc-message -- string )

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" ;

GENERIC: command-parameters>> ( irc-message -- seq )

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 ;

: (>>channel|nickname) ( string mode -- )
    over channel? [ (>>channel) ] [ (>>nickname) ] if ;

GENERIC: (>>command-parameters) ( params irc-message -- )

M: irc-message (>>command-parameters) ( params irc-message -- ) 2drop ;
M: logged-in (>>command-parameters) ( params part -- )  >r first r> (>>name) ;
M: part    (>>command-parameters) ( params part -- )    >r first r> (>>channel) ;
M: privmsg (>>command-parameters) ( params privmsg -- ) >r first r> (>>name) ;
M: notice  (>>command-parameters) ( params notice -- )  >r first r> (>>type) ;
M: kick    (>>command-parameters) ( params kick -- )
    >r first2 r> [ (>>who) ] [ (>>channel) ] bi ;
M: names-reply (>>command-parameters) ( params names-reply -- )
    [ >r first r> (>>who) ] [ >r third r> (>>channel) ] 2bi ;
M: mode    (>>command-parameters) ( params mode -- )
    { { [ >r 2array r> ] [ [ (>>mode) ] [ (>>channel|nickname) ] bi ] }
      { [ >r 3array r> ] [ [ (>>parameter) ] [ (>>mode) ] [ (>>channel) ] tri ] }
    } switch ;

PRIVATE>

GENERIC: irc-message>client-line ( irc-message -- string )

M: irc-message irc-message>client-line ( irc-message -- string )
    [ command-string>> ]
    [ command-parameters>> " " sjoin ]
    [ trailing>> [ CHAR: : prefix ] [ "" ] if* ]
    tri 3array " " sjoin ;

GENERIC: irc-message>server-line ( irc-message -- string )

M: irc-message irc-message>server-line ( irc-message -- string )
   drop "not implemented yet" ;

<PRIVATE
! ======================================
! Message parsing
{'fraction_non_alphanumeric': 0.1732766761095373, 'fraction_numerical': 0.003777148253068933, 'mean_word_length': 2.820108205590622, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 313, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4918992', 'n_tokens_mistral': 2798, 'n_tokens_neox': 2736, 'n_words': 806}