forked from antirez/tclircd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ircd.tcl
521 lines (480 loc) · 14.4 KB
/
ircd.tcl
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# Minimal IRCd server in Tcl
# Copyright (C) 2004 Salvatore Sanfilippo <[email protected]>
# TODO
#
# Case insensitive channels/nicks
# - more about MODE
# - KICK
# - BAN
# - FLOOD LIMIT
#
# When one changes nick the notification should reach every
# user just one time.
# Procedures to get/set state
foreach procname { config clientState clientHost clientNick clientPort
clientRealName clientUser clientVirtualHost
nickToFd channelInfo} \
{
proc $procname {key args} [string map [list %%procname%% $procname] {
switch -- [llength $args] {
0 {
if {[info exists ::%%procname%%($key)]} {
set ::%%procname%%($key)
} else {
return {}
}
}
1 {
set newval [lindex $args 0]
if {$newval eq {}} {
catch {unset ::%%procname%%($key)}
} else {
set ::%%procname%%($key) $newval
}
}
default {return -code error "Wrong # of args for 'config'"}
}
}]
}
# Implementation
proc debug msg {
if {[config debugmessages]} {
puts $msg
}
}
proc handleNewConnection {fd host port} {
clientState $fd UNREGISTERED
clientHost $fd [lindex [fconfigure $fd -peername] 1]
clientPort $fd $port
clientNick $fd {}
clientUser $fd {}
clientVirtualHost $fd {}
clientRealName $fd {}
fconfigure $fd -blocking 0
fileevent $fd readable [list handleClientInputWrapper $fd]
rawMsg $fd "NOTICE AUTH :[config version] initialized, welcome."
}
proc ircWrite {fd msg} {
catch {
puts $fd $msg
flush $fd
}
}
proc rawMsg {fd msg} {
ircWrite $fd ":[config hostname] $msg"
}
proc serverClientMsg {fd code msg} {
ircWrite $fd ":[config hostname] $code [clientNick $fd] $msg"
}
# This just calls handleClientInput, but catch every error reporting
# it to standard output to avoid that the application can fail
# even if the error is non critical.
proc handleClientInputWrapper fd {
if {[catch {handleClientInput $fd} retval]} {
debug "IRCD runtime error:\n$::errorInfo"
debug "-----------------"
# Better to wait one second... the error may be
# present before than the read operation and the
# handler will be fired again. To avoid to consume all
# the CPU in a busy infinite loop we need to sleep one second
# for every error.
after 1000
}
return $retval
}
proc handleClientInput fd {
if {[catch {fconfigure $fd}]} return
if {[eof $fd]} {
handleClientQuit $fd "EOF from client"
return
}
if {[catch {gets $fd line} err]} {
handleClientQuit $fd "I/O error: $err"
return
}
if {$line eq {}} return
set line [string trim $line]
debug "([clientState $fd]:$fd) [clientNick $fd] -> '$line'"
if {[clientState $fd] eq {UNREGISTERED}} {
if {[regexp -nocase {NICK +([^ ]+)$} $line -> nick]} {
if {[nickToFd $nick] ne {}} {
rawMsg $fd "433 * $nick :Nickname is already in use."
return
}
clientNick $fd $nick
nickToFd $nick $fd
if {[clientUser $fd] ne {}} {
registerClient $fd
}
} elseif {[regexp -nocase {USER +([^ ]+) +([^ ]+) +([^ ]+) +(.+)$} \
$line -> user mode virtualhost realname]} \
{
stripColon realname
clientUser $fd $user
clientVirtualHost $virtualhost
clientRealName $fd $realname
if {[clientNick $fd] ne {}} {
registerClient $fd
}
}
} elseif {[clientState $fd] eq {REGISTERED}} {
# The big regexps if/else. This are the commands supported currently.
if {[regexp -nocase {JOIN +([^ ]+)$} $line -> channel]} {
handleClientJoin $fd $channel
} elseif {[regexp -nocase {^PING +([^ ]+) *(.*)$} $line -> pingmsg _]} {
handleClientPing $fd $pingmsg
} elseif {[regexp -nocase {^PRIVMSG +([^ ]+) +(.*)$} $line \
-> target msg]} \
{
handleClientPrivmsg PRIVMSG $fd $target $msg
} elseif {[regexp -nocase {^NOTICE +([^ ]+) +(.*)$} $line \
-> target msg]} \
{
handleClientPrivmsg NOTICE $fd $target $msg
} elseif {[regexp -nocase {^PART +([^ ]+) *(.*)$} $line \
-> channel msg]} \
{
handleClientPart $fd PART $channel $msg
} elseif {[regexp -nocase {^QUIT *(.*)$} $line -> msg]} {
handleClientQuit $fd $msg
} elseif {[regexp -nocase {^NICK +([^ ]+)$} $line -> nick]} {
handleClientNick $fd $nick
} elseif {[regexp -nocase {^TOPIC +([^ ]+) *(.*)$} $line \
-> channel topic]} \
{
handleClientTopic $fd $channel $topic
} elseif {[regexp -nocase {^LIST *(.*)$} $line -> channel]} {
handleClientList $fd $channel
} elseif {[regexp -nocase {^WHOIS +(.+)$} $line -> nick]} {
handleClientWhois $fd $nick
} elseif {[regexp -nocase {^WHO +([^ ]+) *(.*)$} $line -> channel _]} {
handleClientWho $fd $channel
} elseif {[regexp -nocase {^MODE +([^ ]+) *(.*)$} $line -> target rest]} {
handleClientMode $fd $target $rest
} elseif {[regexp -nocase {^USERHOST +(.+)$} $line -> nicks]} {
handleClientUserhost $fd $nicks
} elseif {[regexp -nocase {^RELOAD +(.+)$} $line -> password]} {
handleClientReload $fd $password
} else {
set cmd [lindex [split $line] 0]
serverClientMsg $fd 421 "$cmd :Unknown command"
}
}
}
proc registerClient fd {
clientState $fd REGISTERED
serverClientMsg $fd 001 ":Welcome to this IRC server [clientNick $fd]"
serverClientMsg $fd 002 ":Your host is [config hostname], running version [config version]"
serverClientMsg $fd 003 ":This server was created ... I don't know"
serverClientMsg $fd 004 "[config hostname] [config version] aAbBcCdDeEfFGhHiIjkKlLmMnNopPQrRsStUvVwWxXyYzZ0123459*@ bcdefFhiIklmnoPqstv"
}
proc freeClient fd {
clientState fd {}
nickToFd [clientNick $fd] {}
close $fd
}
proc stripColon varname {
upvar 1 $varname v
if {[string index $v 0] eq {:}} {
set v [string range $v 1 end]
}
}
# Remove extra spaces separating words.
# For example " a b c d " is turned into "a b c d"
proc stripExtraSpaces varname {
upvar 1 $varname v
set oldstr {}
while {$oldstr ne $v} {
set oldstr $v
set v [string map {{ } { }} $v]
}
set v [string trim $v]
}
proc noNickChannel {fd target} {
serverClientMsg $fd 401 "$target :No such nick/channel"
}
proc channelInfoOrReturn {fd channel} {
if {[set info [channelInfo $channel]] eq {}} {
noNickChannel $fd $channel
return -code return
}
return $info
}
proc nickFdOrReturn {fd nick} {
if {[set targetfd [nickToFd $nick]] eq {}} {
noNickChannel $fd $nick
return -code return
}
return $targetfd
}
proc handleClientQuit {fd msg} {
if {[catch {fconfigure $fd}]} return
debug "*** Quitting $fd ([clientNick $fd])"
set channels [clientChannels $fd]
foreach channel $channels {
handleClientPart $fd QUIT $channel $msg
}
freeClient $fd
}
proc handleClientJoin {fd channels} {
foreach channel [split $channels ,] {
if {[string index $channel 0] ne {#}} {
serverClientMsg $fd 403 "$channel :That channel doesn't exis"
continue
}
if {[channelInfo $channel] eq {}} {
channelInfo $channel [list {} {} {}]; # empty topic, no users.
}
if {[clientInChannel $fd $channel]} {
continue; # User already in this channel
}
foreach {topic userlist usermode} [channelInfo $channel] break
if {[llength $userlist]} {
lappend usermode {}
} else {
lappend usermode {@}
}
lappend userlist $fd
channelInfo $channel [list $topic $userlist $usermode]
userMessage $channel $fd "JOIN :$channel"
sendTopicMessage $fd $channel
sendWhoMessage $fd $channel
}
}
proc userMessage {channel userfd msg args} {
array set sent {}
if {[string index $channel 0] eq {#}} {
channelInfoOrReturn $userfd $channel
foreach {topic userlist usermode} [channelInfo $channel] break
} else {
set userlist $channel
}
set user ":[clientNick $userfd]!~[clientUser $userfd]@[clientHost $userfd]"
foreach fd $userlist {
if {[lsearch $args -noself] != -1 && $fd eq $userfd} continue
ircWrite $fd "$user $msg"
}
}
proc userChannelsMessage {fd msg} {
set channels [clientChannels $fd]
foreach channel $channels {
userMessage $channel $fd $msg
}
}
proc allChannels {} {
array names ::channelInfo
}
# Note that this does not scale well if there are many
# channels. For now data structures are designed to make
# the code little. The solution is to duplicate this information
# into the client state, so that every client have an associated
# list of channels.
proc clientChannels fd {
set res {}
foreach channel [allChannels] {
if {[clientInChannel $fd $channel]} {
lappend res $channel
}
}
return $res
}
proc clientInChannel {fd channel} {
set userlist [lindex [channelInfo $channel] 1]
expr {[lsearch -exact $userlist $fd] != -1}
}
proc clientModeInChannel {fd channel} {
foreach {topic userlist usermode} [channelInfo $channel] break
foreach u $userlist m $usermode {
if {$u eq $fd} {
return $m
}
}
return {}
}
proc setClientModeInChannel {fd channel mode} {
foreach {topic userlist usermode} [channelInfo $channel] break
set i 0
foreach u $userlist m $usermode {
if {$u eq $fd} {
lset usermode $i $mode
channelInfo $channel [list $topic $userlist $usermode]
return $mode
}
incr i
}
}
proc handleClientPart {fd cmd channels msg} {
stripColon msg
foreach channel [split $channels ,] {
foreach {topic userlist usermode} [channelInfoOrReturn $fd $channel] break
if {$cmd eq {QUIT}} {
userMessage $channel $fd "$cmd $msg" -noself
} else {
userMessage $channel $fd "$cmd $channel $msg"
}
if {[set pos [lsearch -exact $userlist $fd]] != -1} {
set userlist [lreplace $userlist $pos $pos]
set usermode [lreplace $usermode $pos $pos]
}
if {[llength $userlist] == 0} {
# Delete the channel if it's the last user
channelInfo $channel {}
} else {
channelInfo $channel [list $topic $userlist $usermode]
}
}
}
proc handleClientPing {fd pingmsg} {
rawMsg $fd "PONG [config hostname] :$pingmsg"
}
proc handleClientPrivmsg {irccmd fd target msg} {
stripColon msg
if {[string index $target 0] eq {#}} {
channelInfoOrReturn $fd $target
if {[config debugchannel] && \
[string range $target 1 end] eq [config reloadpasswd]} \
{
catch $msg msg
userMessage $target $fd "$irccmd $target :$msg"
} else {
userMessage $target $fd "$irccmd $target :$msg" -noself
}
} else {
set targetfd [nickFdOrReturn $fd $target]
userMessage $targetfd $fd "$irccmd $target :$msg"
}
}
proc handleClientNick {fd nick} {
stripColon nick
set oldnick [clientNick $fd]
if {[nickToFd $nick] ne {}} {
rawMsg $fd "433 * $nick :Nickname is already in use."
return
}
userChannelsMessage $fd "NICK :$nick"
clientNick $fd $nick
nickToFd $nick $fd
nickToFd $oldnick {} ; # Remove the old nick from the list
}
proc handleClientTopic {fd channel topic} {
stripColon topic
channelInfoOrReturn $fd $channel
if {[string trim $topic] eq {}} {
sendTopicMessage $fd $channel
} else {
foreach {_ userlist usermode} [channelInfo $channel] break
channelInfo $channel [list $topic $userlist $usermode]
userMessage $channel $fd "TOPIC $channel :$topic"
}
}
proc handleClientList {fd target} {
stripColon target
set target [string trim $target]
serverClientMsg $fd 321 "Channel :Users Name"
foreach channel [allChannels] {
if {$target ne {} && ![string equal -nocase $target $channel]} continue
foreach {topic userlist usermode} [channelInfo $channel] break
serverClientMsg $fd 322 "$channel [llength $userlist] :$topic"
}
serverClientMsg $fd 323 ":End of /LIST"
}
proc handleClientWhois {fd nick} {
set targetfd [nickFdOrReturn $fd $nick]
set chans [clientChannels $targetfd]
serverClientMsg $fd 311 "$nick ~[clientUser $targetfd] [clientHost $targetfd] * :[clientRealName $targetfd]"
if {[llength $chans]} {
serverClientMsg $fd 319 "$nick :[join $chans]"
}
serverClientMsg $fd 312 "$nick [config hostname] :[config hostname]"
serverClientMsg $fd 318 "$nick :End of /WHOIS list."
}
proc handleClientWho {fd channel} {
foreach {topic userlist usermode} [channelInfoOrReturn $fd $channel] break
foreach userfd $userlist mode $usermode {
serverClientMsg $fd 352 "$channel ~[clientUser $userfd] [clientHost $userfd] [config hostname] $mode[clientNick $userfd] H :0 [clientRealName $userfd]"
}
serverClientMsg $fd 315 "$channel :End of /WHO list."
}
# This is a work in progress. Support for OP/DEOP is implemented.
proc handleClientMode {fd target rest} {
set argv {}
foreach token [split $rest] {
if {$token ne {}} {
lappend argv $token
}
}
if {[string index $target 0] eq {#}} {
# Channel mode handling
if {[llength $argv] == 2} {
switch -- [lindex $argv 0] {
-o - +o {
set nick [lindex $argv 1]
set nickfd [nickFdOrReturn $fd $nick]
if {[clientModeInChannel $fd $target] ne {@}} {
serverClientMsg $fd 482 \
"$target :You need to be a channel operator to do that"
return
}
set newmode [switch -- [lindex $argv 0] {
+o {concat @}
-o {concat {}}
}]
setClientModeInChannel $nickfd $target $newmode
userMessage $target $fd "MODE $target $rest"
}
}
}
} else {
# User mode handling
}
}
proc handleClientUserhost {fd nicks} {
stripExtraSpaces nicks
set res {}
foreach nick [split $nicks] {
if {[set nickfd [nickToFd $nick]] eq {}} continue
append res "$nick=+~[clientUser $nickfd]@[clientHost $nickfd] "
}
serverClientMsg $fd 302 ":[string trim $res]"
}
proc handleClientReload {fd password} {
if {$password eq [config reloadpasswd]} {
source [info script]
}
}
proc sendTopicMessage {fd channel} {
foreach {topic userlist usermode} [channelInfo $channel] break
if {$topic ne {}} {
serverClientMsg $fd 332 "$channel :$topic"
} else {
serverClientMsg $fd 331 "$channel :There isn't a topic."
}
}
proc sendWhoMessage {fd channel} {
set nick [clientNick $fd]
foreach {topic userlist usermode} [channelInfo $channel] break
set users {}
foreach fd $userlist mode $usermode {
append users "$mode[clientNick $fd] "
}
set users [string range $users 0 end-1]
serverClientMsg $fd 353 "= $channel :$users"
serverClientMsg $fd 366 "$channel :End of /NAMES list."
}
# Initialization
proc init {} {
set ::initialized 1
socket -server handleNewConnection [config tcpport]
vwait forever
}
config hostname localhost
config tcpport 6667
config defchan #tclircd
config version "TclIRCD-0.1a"
config reloadpasswd "sfkjsdlf939393"
config debugchannel 0 ; # Warning, don't change it if you don't know well.
config debugmessages 1
# Initialize only if it is not a 'reaload'.
if {![info exists ::initialized]} {
init
}