factor/examples/irc.factor

106 lines
2.4 KiB
Factor
Raw Normal View History

2004-12-07 23:21:32 -05:00
! A simple IRC client written in Factor.
2004-11-09 12:25:13 -05:00
2004-12-14 02:57:40 -05:00
IN: irc
USE: generic
2004-11-09 12:25:13 -05:00
USE: stdio
2004-12-07 23:21:32 -05:00
USE: namespaces
2004-11-09 12:25:13 -05:00
USE: streams
2004-12-10 21:39:27 -05:00
USE: kernel
2004-12-07 23:21:32 -05:00
USE: threads
2004-12-14 02:57:40 -05:00
USE: lists
USE: strings
USE: words
USE: math
2004-11-09 12:25:13 -05:00
2004-12-07 23:21:32 -05:00
SYMBOL: irc-stream
2004-12-14 02:57:40 -05:00
SYMBOL: channels
2004-12-07 23:21:32 -05:00
SYMBOL: channel
2004-12-14 02:57:40 -05:00
SYMBOL: nickname
2004-11-09 12:25:13 -05:00
2004-12-14 02:57:40 -05:00
: irc-write ( s -- ) irc-stream get fwrite ;
: irc-print ( s -- ) irc-stream get fprint irc-stream get fflush ;
2004-11-09 12:25:13 -05:00
2004-12-14 02:57:40 -05:00
: nick ( nick -- )
dup nickname set "NICK " irc-write irc-print ;
2004-11-09 12:25:13 -05:00
2004-12-07 23:21:32 -05:00
: login ( nick -- )
2004-12-14 02:57:40 -05:00
dup nick
2004-12-07 23:21:32 -05:00
"USER " irc-write irc-write
" hostname servername :irc.factor" irc-print ;
2004-11-09 12:25:13 -05:00
2004-12-14 02:57:40 -05:00
: connect ( server -- ) 6667 <client> irc-stream set ;
: write-highlighted ( line -- )
dup nickname get index-of -1 =
f [ [ "ansi-fg" | "3" ] ] ? write-attr ;
: extract-nick ( line -- nick )
"!" split1 drop ;
: write-nick ( line -- )
"!" split1 drop [ [ "bold" | t ] ] write-attr ;
GENERIC: irc-display
PREDICATE: string privmsg "PRIVMSG" index-of -1 > ;
PREDICATE: string action "ACTION" index-of -1 > ;
M: string irc-display ( line -- )
print ;
M: privmsg irc-display ( line -- )
"PRIVMSG" split1 >r write-nick r>
write-highlighted terpri flush ;
! Doesn't look good
! M: action irc-display ( line -- )
! " * " write
! "ACTION" split1 >r write-nick r>
! write-highlighted terpri flush ;
2004-11-09 12:25:13 -05:00
2004-12-07 23:21:32 -05:00
: in-loop ( -- )
2004-12-14 02:57:40 -05:00
irc-stream get freadln [ irc-display in-loop ] when* ;
: input-thread ( -- ) [ in-loop ] in-thread ;
: disconnect ( -- ) irc-stream get fclose ;
2004-11-09 12:25:13 -05:00
2004-12-14 02:57:40 -05:00
: command ( line -- )
#! IRC /commands are just words.
" " split1 swap [
"irc" "listener" "parser" "scratchpad"
] search execute ;
2004-11-09 12:25:13 -05:00
2004-12-14 02:57:40 -05:00
: (msg) ( line nick -- )
"PRIVMSG " irc-write irc-write " :" irc-write irc-print ;
2004-11-09 12:25:13 -05:00
2004-12-14 02:57:40 -05:00
: say ( line -- )
channel get [ (msg) ] [ "No channel." print ] ifte* ;
2004-11-09 12:25:13 -05:00
2004-12-14 02:57:40 -05:00
: talk ( input -- ) "/" ?str-head [ command ] [ say ] ifte ;
: talk-loop ( -- ) read [ talk talk-loop ] when* ;
: irc ( nick server -- )
[
channels off
channel off
connect
login
input-thread
talk-loop
disconnect
] with-scope ;
! /commands
: join ( chan -- )
dup channels [ cons ] change
dup channel set
"JOIN " irc-write irc-print ;
2004-11-09 12:25:13 -05:00
2004-12-14 02:57:40 -05:00
: leave ( chan -- )
dup channels [ remove ] change
channels get dup [ car ] when channel set
"PART " irc-write irc-print ;
2004-11-09 12:25:13 -05:00
2004-12-14 02:57:40 -05:00
: msg ( line -- ) " " split1 swap (msg) ;
: me ( line -- ) "\u0001ACTION " swap "\u0001" cat3 say ;
: quit ( line -- ) drop disconnect ;