-
Notifications
You must be signed in to change notification settings - Fork 1
/
global.tcl
348 lines (317 loc) · 11.9 KB
/
global.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
# Data arrangement:
## Servers(numeric): <server_name> <parent> <hops> <boottime> <linktime> <flags> <description>
## Users(numeric): <nickname> <server> <hops> <connectTime> <ident> <hostname> <modes/0> <authname/0> <opername/0> <fakehost/0> baseip <realname>
## Channels(lower(name)): <formatted name> <creation time> <modes> <key/0> <limit/-1>
## Chanlists(lower(name),<status>): <list of users>
## ChanTopic(lower(name)) "<set by (nickname!user@host)> <set time> <topic string>"
## -- where <status> can be: o, v, r or null
namespace eval Data {
variable Uplink "";
variable startBurst 0;
variable endBurst 0;
set array_list [list "servers" "users" "channels" "chanlists" "userchanlists" "authnames" "chanserv_channels" "chanserv_chanlevs" "nick2num" "pushmode" "chanserv_bans" "chanserv_log" "help_triggers" "help_friends" "away" "server2num" "staff_fakehosts"]
foreach array $array_list {
if {![array exists $array]} {
array set $array [list]
}
}
}
# Trap signals
trap sigintHandler SIGINT;
proc sigintHandler {} {
event::trigger SIGINT
after 1000 [list Connection::sendData "SQ [Core::getConfig core serverName] :Service shutting down"];
after 1200 [list exit];
}
proc rand {min max} {
return [expr {int(rand()*($max-$min+1)+$min)}]
}
# Gets the debug level.
proc debug {} {
if {[Core::getConfig core debugLevel] != ""} {
return [Core::getConfig core debugLevel];
} else {
return 5
}
}
proc getChildServers {numeric} {
set list [list];
foreach server [array names Data::servers] {
if {[getServerInfo $server parent] == $numeric} {
lappend list $server;
}
}
}
proc serverUserlist {numeric} {
set list [list];
foreach user [array names Data::users] {
if {[getUserInfo $user server] == $numeric} {
lappend list $user;
}
}
return $list;
}
proc userChanlist {numeric} {
if {[info exists Data::userchanlists($numeric)]} {
return [set Data::userchanlists($numeric)];
}
return "";
}
proc nick2num {nickname} {
set nickname [ircString lower $nickname];
if {[info exists Data::nick2num($nickname)]} { return [set Data::nick2num($nickname)] }
return "0";
}
proc server2num {server} {
set server [string tolower $server];
if {[info exists Data::server2num($server)]} { return [set Data::server2num($server)] }
return 0;
}
proc chanlist {channel} {
set channel [ircString lower $channel];
if {[info exists Data::chanlists($channel)]} {
return [set Data::chanlists($channel)];
}
return "";
}
proc strToSeconds {str} { foreach match [regexp -all -inline {[0-9]+[A-Za-z]+} $str] { regexp -all {([^a-zA-Z]+?)} $match -> number; regexp -all {([^0-9]+?)} $match -> letter; if {$letter == "w"} { incr time [expr {604800*$number}] } elseif {$letter == "d"} { incr time [expr {$number * 86400}] } elseif {$letter == "h"} { incr time [expr {$number*3600}] } elseif {$letter == "m"} { incr time [expr {$number*60}] } elseif {$time == "s"} { incr time $number } }; return $time; }
# Gets the clones of an IP.
proc getClones {source {return "0"}} {
set count "0"; set resultList [list];
foreach user [array names Data::users] {
if {[getUserInfo $user baseip] == [getUserInfo $source baseip] && ![string equal $user $source]} {
incr count;
if {$return == 1} { lappend resultList $user }
if {$return == 2} { lappend resultList [getUserInfo $user nickname] }
}
}
if {$return > 0} { return $resultList; }
return $count;
}
proc isAway {numeric} {
return [info exists Data::away($numeric)]
}
proc isop {numeric channel} {
set channel [ircString lower $channel];
if {[info exists Data::chanlists($channel,o)] && [lsearch -exact [set Data::chanlists($channel,o)] $numeric] > -1} {
return 1;
}
return 0;
}
proc isvoice {numeric channel} {
set channel [ircString lower $channel];
if {[info exists Data::chanlists($channel,v)] && [lsearch -exact [set Data::chanlists($channel,v)] $numeric] > -1} {
return 1;
}
return 0;
}
proc ison {numeric channel} {
set channel [ircString lower $channel];
if {[lsearch -exact [chanlist $channel] $numeric] > -1} {
return 1;
}
return 0;
}
proc isService {numeric} {
if {[string equal [string range $numeric 0 1] [Core::getConfig server serverNumeric]]} { return 1; }
if {[string first "s" [getServerInfo [getUserInfo $numeric server] flags]] > -1} { return 1; }
if {[string length [getUserInfo $numeric nickname]] == -1} { return 1; }
return 0;
}
proc isOper {numeric} {
if {[string first "o" [getUserInfo $numeric modes]] > -1} { return 1; }
return 0;
}
proc userHasServiceFlag {numeric} {
if {[string first "k" [getUserInfo $numeric modes]] > -1} { return 1; }
return 0;
}
proc userHasXtraOper {numeric} {
if {[string first "X" [getUserInfo $numeric modes]] > -1} { return 1; }
return 0;
}
proc getbanmask {numeric} {
if {[string first "h" [getUserInfo $numeric modes]] > -1 && [getUserInfo $numeric fakehost] != "0"} {
set banmask "*![string map [list "~" "*"] [getUserInfo $numeric fakehost]]";
} elseif {[set authname [getUserInfo $numeric authname]] != "0"} {
set banmask "*!*@${authname}.[Core::getConfig network hiddenhost]";
} else {
set banmask "*![string map [list "~" "*"] [getUserInfo $numeric ident]]@[getUserInfo $numeric hostname]";
}
return $banmask;
}
proc matchban {mask user} {
set hosts [list];
if {[getUserInfo $user fakehost] != "0"} { lappend hosts "[getUserInfo $user nickname]![getUserInfo $user fakehost]"; }
lappend hosts "[getUserInfo $user nickname]![getUserInfo $user ident]@[getUserInfo $user hostname]";
if {[getUserInfo $user authname] != "0"} { lappend hosts "[getUserInfo $user nickname]![getUserInfo $user ident]@[getUserInfo $user authname].[Core::getConfig network hiddenhost]" }
foreach host $hosts {
if {[string match -nocase $mask $host]} {
return 1;
}
}
return 0;
}
## Channels(lower(name)): <formatted name> <creation time> <modes> <key/0> <limit/-1>
proc getChanInfo {name returnValue} {
set name [ircString lower $name];
if {![info exists Data::channels($name)]} {
return "0"
}
set arrangement [list name creation modes key limit];
set returnValue [string tolower $returnValue];
if {[set loc [lsearch -exact $arrangement $returnValue]] > -1} {
return [lindex [set Data::channels($name)] $loc];
} else {
return "0";
}
}
proc getServerInfo {numeric returnValue} {
if {![info exists Data::servers($numeric)]} {
return "0"
}
set arrangement [list name parent hops boottime linktime flags description];
set returnValue [string tolower $returnValue];
if {[set loc [lsearch -exact $arrangement $returnValue]] > -1} {
if {$returnValue == "description"} {
return [join [lrange [set Data::servers($numeric)] $loc end]];
} else {
return [lindex [set Data::servers($numeric)] $loc];
}
} else {
return "0";
}
}
proc getUserInfo {numeric returnValue} {
if {![info exists Data::users($numeric)]} {
return "0"
}
set arrangement [list nickname server hops connecttime ident hostname modes authname opername fakehost baseip realname];
set returnValue [string tolower $returnValue];
if {[set loc [lsearch -exact $arrangement $returnValue]] > -1} {
if {$returnValue == "realname"} {
return [join [lrange [set Data::users($numeric)] $loc end]];
} else {
return [lindex [set Data::users($numeric)] $loc];
}
} else {
return "0";
}
}
proc getServerUsers {server} {
set returnlist [list];
foreach user [array names Data::users] {
if {[getUserInfo $user server] == $server} {
lappend returnlist $user;
}
}
return $returnlist;
}
# Because of IRC's scandanavian origin, the characters {}| are considered to be the lower case equivalents of the characters []\, respectively.
# This is a critical issue when determining the equivalence of two nicknames.
proc getFlagChanges {original flags} {
set type "+";
foreach flag [split $flags ""] {
if {$flag == "+" || $flag == "-"} { set type $flag; }
if {$type == "-" && [string first $flag $original] > -1} { set original [string map [list $flag ""] $original]; }
if {$type == "+" && [string first $flag $original] == -1} { append original $flag; }
}
return $original;
}
proc randString {{length "8"}} {
set characters "_+-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!"
set password ""
for {set x "0"} {$x <= $length} {incr x} {
append password "[lindex [split $characters ""] [rand 0 [llength [split $characters ""]]]]"
}
return $password
}
proc ircString {type str {compare ""}} {
if {$type == "lower"} {
set str [string map {"\[" "\{" "\]" "\}" "|" "\\"} $str];
return [string tolower $str];
} elseif {$type == "upper"} {
set str [string map {"\{" "\[" "\}" "\]" "\\" "|"} $str];
return [string toupper $str];
} elseif {$type === "equal"} {
return [string equal [ircString lower $str] [ircString lower $compare]]
}
}
# IP Stuff
# Converts a B64 IP to a long
proc b64dc {b64} {
# Base64 Converting stuff
set b64charset [split {ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]} ""]
# b64 to int
set num 0; set i [string length $b64]; incr i -1
foreach char [split $b64 ""] {
set num [expr {$num + (wide(1)<<(6*$i)) * [lsearch -exact $b64charset $char]}]
incr i -1
}
return $num
}
# Converts a long int to a B64 IP
proc b64ec {int {fill 0}} {
# Base64 Converting stuff
set b64charset [split {ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]} ""]
set result ""
while {$int > 0} {
set result [lindex $b64charset [expr {wide($int) & 0x3F}]]$result
set int [expr {wide($int) >> 6}]
}
while {[string length $result] < $fill} {
set result A$result
}
return $result
}
# Converts a long int to an IPv4 address
proc int2ip {num} {
binary scan [binary format I $num] cccc a b c d
set ip [format %d.%d.%d.%d [expr $a&255] [expr $b&255] [expr $c&255] [expr $d&255]]
}
# Converts an IPv4 IP address to a long
proc ip2int {ip} {
foreach {a b c d} [split $ip .] { break }
set num [expr {(((((wide($a)<<8)+$b)<<8)+$c)<<8)+$d}]
return $num
}
proc getIP {baseip} {
return [int2ip [b64dc $baseip]];
}
##########################
# Pushmode
##########################
proc pushmode {numeric channel mode {param ""} {queuespeed "medium"}} {
set channel [ircString lower $channel];
if {$queuespeed == "slow"} { set speed "400";
} elseif {$queuespeed == "medium"} { set speed "200";
} elseif {$queuespeed == "fast"} { set speed "75";
} elseif {$queuespeed == "instant"} { set speed "1"; }
if {![string equal -nocase $mode "--flush"]} {
if {[info exists Data::pushmode($numeric,$channel)]} {
set queue [set Data::pushmode($numeric,$channel)]
set modeCurrent [lindex [split $queue] 0];
set userCurrent [lrange [split $queue] 1 end];
append modeCurrent $mode;
if {$param != ""} { lappend userCurrent $param; }
set Data::pushmode($numeric,$channel) "$modeCurrent [join $userCurrent]";
puts "Queue now: $modeCurrent [join $userCurrent]";
} else {
if {$param != ""} {
set Data::pushmode($numeric,$channel) "$mode $param"
} else {
set Data::pushmode($numeric,$channel) $mode
}
}
if {[llength [lrange [split [set Data::pushmode($numeric,$channel)]] 1 end]] == 6} { pushmode $numeric $channel --flush }
after $speed [list pushmode $numeric $channel --flush]
} else {
if {[info exists Data::pushmode($numeric,$channel)]} {
set modes [lindex [split [set Data::pushmode($numeric,$channel)]] 0];
set params [join [lrange [split [set Data::pushmode($numeric,$channel)]] 1 end]];
puts "Executing Queue --> $modes $params";
Connection::sendRawData "$numeric M $channel $modes $params";
unset Data::pushmode($numeric,$channel);
}
}
}