forked from lucasad/zIRCd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.zsh
executable file
·270 lines (218 loc) · 5.08 KB
/
client.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/zsh
: <<EOF
Copyright 2013 Lucas A. Dohring
Licensed under the EUPL, Version 1.1 or – as soon they
will be approved by the European Commission - subsequent
versions of the EUPL (the "Licence");
You may not use this work except in compliance with the
Licence.
You may obtain a copy of the Licence at:
http://ec.europa.eu/idabc/eupl
Unless required by applicable law or agreed to in
writing, software distributed under the Licence is
distributed on an "AS IS" basis,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied.
See the Licence for the specific language governing
permissions and limitations under the Licence.
EOF
zmodload zsh/system zsh/net/socket zsh/mapfile zsh/sched
IFS=" "
SOCKFD=$1
readonly CDIR=clients/$$/
readonly VERSION=$VERSION
readonly CREATED=$CREATED
user=
nick=
realname=
hostname=
mode=
integer fd
integer uOP=1 uNOT=2 uWOPS=4 uINVI=8
. ./common.zsh
function parse() {
read command rest
IFS=: read params last <<< "$rest"
params=(${=params})
if [ -n "$last" ]; then
params+=$last
fi
command=$command:u
doCommand $command $params
}
function doCommand() {
case $command in
USER)
setUser $params
;;
NICK)
changeNick $params
;;
PRIVMSG)
sendMSG PRIVMSG $params
;;
NOTICE)
sendMSG NOTICE $params
;;
WHOIS)
doWhois $params
;;
MODE)
mode $params
;;
TOPIC|PART|KICK|JOIN|NAMES)
./channel.zsh $command $nick $params
;;
PONG)
resetWD
;;
PING)
echo :$HOST PONG :$params
;;
QUIT)
quit $params
;;
*)
echo :$HOST 421 $command :Unknown command $command
;;
esac
}
function doInit() {
mkdir "clients/$$"
mkfifo "clients/$$/sock"
exec {fd}<>"clients/$$/sock"
while read line; do echo $line >&$SOCKFD; done <&$fd &
}
function resetWD() {
sched -${#zsh_scheduled_events}
sched +300 echo :$HOST PING :$RANDOM
sched +600 quit "Ping timeout"
}
function doWhois() {
_nick=$1
if [ -z "$_nick" ]; then
echo :$HOST 431 :No nickname given
return 1
fi
if [ ! "$_nick" '=~' '^[[:alpha:]][[:alnum:]\-\[-\`_-}]{0,8}$' ]; then
echo :$HOST 432 $_nick :Erroneous nickname
return 1
fi
_realname=$mapfile["nicks/$_nick/realname"]
_user=$mapfile["nicks/$_nick/user"]
_hostname=$mapfile["nicks/$_nick/hostname"]
#realname=mapfile["nicks/$_nick/realname"]
echo :$HOST 311 $nick $_nick $_user $_hostname :$realname
echo :$HOST 318 $nick :End of WHOIS list
}
function setUser() {
_user=$1
_mode=$2
_hostname=$3
_realname=$4
if [[ -z "$_mode" || -z "$_realname" ]]; then
echo :$HOST 461 USER :Not enough parameters
fi
if [ -n "$user" ]; then
echo ":$HOST 462 :Unauthorized command (already registered)"
return 255
fi
user=$_user
mode=$_mode
hostname=$_hostname
realname=$_realname
mapfile["$CDIR/user"]=$user
mapfile["$CDIR/realname"]=$realname
mapfile["$CDIR/mode"]=$mode
mapfile["$CDIR/hostname"]=$hostname
if [ -n "$nick" ]; then
welcome
fi
}
function changeNick() {
_nick=$1
if [ -z "$_nick" ]; then
echo :$HOST 431 :No nickname given
return 1
fi
if [ ! "$_nick" '=~' '^[[:alpha:]][[:alnum:]\-\[-\`_-}]{0,8}$' ]; then
echo :$HOST 432 $_nick :Erroneous nickname
return 1
fi
if [ -d "nicks/$_nick" ]; then
echo :$HOST 433 $_nick :Nickname is already in use
return
fi
touch "lock/nicks/$_nick"
if zsystem flock -t 1 "lock/nicks/$_nick"; then
:
else
echo :$HOST 433 $nick :Nickname is already in use
fi
if [ -n "$nick" ]; then
rm "lock/nicks/$nick"
fi
ln -s "../clients/$$" "nicks/$_nick"
ln -s "../clients/$$/sock" "target/$_nick"
if [ -n "$nick" ]; then
# echo :$nick!$UPREFIX NICK $nick > nicks/$nick/channels/*
echo :$nick!$user NICK $_nick
rm target/$nick
rm nicks/$nick
rm lock/nicks/$nick
fi
nick=$_nick
if [ -n "$user" ]; then
welcome
fi
}
function doMode() {
if [[ "$1" =~ $CHANRE ]]; then
./channel.zsh MODE $nick $@
else
mode $@
fi
}
function mode() {
if [[ ! "$1" == "$nick" ]]; then
echo :$HOST 502 $nick :Cannot change mode for other users
return
fi
pmode=+
if [ -z $2 ]; then
if (( $mode&4 )); then
pmode=${pmode}w
fi
if (( $mode&8 )); then
pmode=${pmode}i
fi
echo :$nick MODE $nick $pmode
else
:
fi
}
function welcome() {
echo :$HOST 001 $nick :Welcome to the Internet Relay Network $nick!$UPREFIX
echo :$HOST 002 $nick :Your host is $HOST, running version $VERSION
echo :$HOST 003 $nick :This server was created $CREATED
echo :$HOST 004 $nick :$HOST $VERSION
echo :$HOST PING :$RANDOM
sched +300 quit "Ping timeout"
}
function sendALL() {
if channels=(/nicks/$nick/channels/*/sock); then
for channel in $channels; do
echo $@ > $channel
done
fi
}
function quit() {
sendALL :$nick!$UPREFIX QUIT :$1
echo :$nick!$UPREFIX QUIT :$1
exec {SOCKFD}>&-
exit
}
doInit
while read -u$1 line
do sed 's/^:\S* //' <<< "$line" | tr -d '\r' | parse >& $1; done
quit "Client Quit"