-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc.zsh
executable file
·62 lines (52 loc) · 1.25 KB
/
irc.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/zsh
set -eu
module_reload(){
for m in modules/*.zsh; do
source "$m"
done
}
module_reload
zmodload -i zsh/net/tcp
zmodload -i zsh/stat
ztcp chat.freenode.net 6667
ircfd=$REPLY
NICK="${NICK:-myzshbot}"
print "NICK $NICK" >&$ircfd
print 'USER myzshbot "" "" :myzshbot' >&$ircfd
ztcp -vl 7000
listener=$REPLY
trap "{ztcp -c}" EXIT
clients=()
input="xxx"
while [ -n "$input" ]; do
if read -rt 0.1 input <&$ircfd; then
input=${input[0,$[ ${#input} - 1 ]]}
if [[ "$input" =~ ":([^ ]+) ([0-9]+) ([^ ]+) :(.*)" ]]; then
server_msg ${match[2]} "${match[4]}"
elif [[ "$input" =~ ":([^ ]+) PRIVMSG #([^ ]+) :(.*)" ]]; then
channel_msg ${match[1]} ${match[2]} ${match[3]}
elif [[ "$input" =~ "PING :(.*)" ]]; then
print "PONG :${match[1]}" >&$ircfd
else
print "No idea how to parse $input."
fi
elif ztcp -vta $listener; then
client=$REPLY
print "Got an rpc connection"
print "RPC Welcome" >&$client
clients+=($client)
fi
for ((i=${#clients};i>0;i--)); do
start=${(%):-%D\{%s\}}
if read -rt 2 clientin <&${clients[$i]}; then
do_rpc ${clients[$i]} $clientin
else
end=${(%):-%D\{%s\}}
if [[ $end = $start ]]; then
echo "Closing client socket"
ztcp -c ${clients[$i]} || true
clients[$i]=()
fi
fi
done
done