Commit | Line | Data |
---|---|---|
e58e51a8 | 1 | # Core bot code |
9bc33297 | 2 | |
e58e51a8 IJ |
3 | proc defset {varname val} { |
4 | upvar #0 $varname var | |
5 | if {![info exists var]} { set var $val } | |
6 | } | |
7 | ||
8 | # must set host | |
9 | defset port 6667 | |
10 | ||
11 | defset nick testbot | |
12 | defset ownfullname "testing bot" | |
13 | defset ownmailaddr test-irc-bot@example.com | |
e1ba63be | 14 | |
e58e51a8 IJ |
15 | defset musthaveping_ms 10000 |
16 | defset out_maxburst 6 | |
17 | defset out_interval 2100 | |
18 | defset out_lag_lag 5000 | |
19 | defset out_lag_very 25000 | |
7818b6af | 20 | |
20087363 IJ |
21 | proc manyset {list args} { |
22 | foreach val $list var $args { | |
23 | upvar 1 $var my | |
24 | set my $val | |
25 | } | |
26 | } | |
27 | ||
7a70431a IJ |
28 | proc try_except_finally {try except finally} { |
29 | global errorInfo errorCode | |
30 | set er [catch { uplevel 1 $try } emsg] | |
31 | if {$er} { | |
32 | set ei $errorInfo | |
33 | set ec $errorCode | |
34 | if {[catch { uplevel 1 $except } emsg3]} { | |
35 | append ei "\nALSO ERROR HANDLING ERROR:\n$emsg3" | |
36 | } | |
37 | } | |
38 | set er2 [catch { uplevel 1 $finally } emsg2] | |
39 | if {$er} { | |
40 | if {$er2} { | |
41 | append ei "\nALSO ERROR CLEANING UP:\n$emsg2" | |
42 | } | |
43 | return -code $er -errorinfo $ei -errorcode $ec $emsg | |
44 | } elseif {$er2} { | |
45 | return -code $er2 -errorinfo $errorInfo -errorcode $errorCode $emsg2 | |
46 | } else { | |
47 | return $emsg | |
48 | } | |
49 | } | |
50 | ||
7818b6af IJ |
51 | proc out__vars {} { |
52 | uplevel 1 { | |
53 | global out_queue out_creditms out_creditat out_interval out_maxburst | |
54 | global out_lag_lag out_lag_very | |
55 | #set pr [lindex [info level 0] 0] | |
56 | #puts $pr>[clock seconds]|$out_creditat|$out_creditms|[llength $out_queue]< | |
57 | } | |
58 | } | |
59 | ||
60 | proc out_lagged {} { | |
61 | out__vars | |
62 | if {[llength $out_queue]*$out_interval > $out_lag_very} { | |
63 | return 2 | |
64 | } elseif {[llength $out_queue]*$out_interval > $out_lag_lag} { | |
65 | return 1 | |
66 | } else { | |
67 | return 0 | |
68 | } | |
69 | } | |
70 | ||
71 | proc out_restart {} { | |
72 | out__vars | |
73 | ||
74 | set now [clock seconds] | |
75 | incr out_creditms [expr {($now - $out_creditat) * 1000}] | |
76 | set out_creditat $now | |
77 | if {$out_creditms > $out_maxburst*$out_interval} { | |
78 | set out_creditms [expr {$out_maxburst*$out_interval}] | |
79 | } | |
80 | out_runqueue $now | |
81 | } | |
82 | ||
83 | proc out_runqueue {now} { | |
cc2d31de | 84 | global sock |
7818b6af IJ |
85 | out__vars |
86 | ||
87 | while {[llength $out_queue] && $out_creditms >= $out_interval} { | |
88 | #puts rq>$now|$out_creditat|$out_creditms|[llength $out_queue]< | |
89 | manyset [lindex $out_queue 0] orgwhen msg | |
90 | set out_queue [lrange $out_queue 1 end] | |
91 | if {[llength $out_queue]} { | |
92 | append orgwhen "+[expr {$now - $orgwhen}]" | |
93 | append orgwhen ([llength $out_queue])" | |
94 | } | |
95 | puts "$orgwhen -> $msg" | |
96 | puts $sock $msg | |
97 | incr out_creditms -$out_interval | |
98 | } | |
99 | if {[llength $out_queue]} { | |
100 | after $out_interval out_nextmessage | |
101 | } | |
102 | } | |
103 | ||
104 | proc out_nextmessage {} { | |
105 | out__vars | |
106 | set now [clock seconds] | |
107 | incr out_creditms $out_interval | |
108 | set out_creditat $now | |
109 | out_runqueue $now | |
110 | } | |
111 | ||
112 | proc sendout_priority {priority command args} { | |
113 | global sock out_queue | |
9bc33297 IJ |
114 | if {[llength $args]} { |
115 | set la [lindex $args end] | |
116 | set args [lreplace $args end end] | |
117 | foreach i $args { | |
118 | if {[regexp {[: ]} $i]} { | |
cc2d31de | 119 | error "bad argument in output $i ($command $args)" |
9bc33297 IJ |
120 | } |
121 | } | |
cc2d31de | 122 | lappend args :$la |
9bc33297 IJ |
123 | } |
124 | set args [lreplace $args 0 -1 $command] | |
cc2d31de | 125 | set string [join $args { }] |
7818b6af IJ |
126 | set now [clock seconds] |
127 | set newe [list $now $string] | |
128 | if {$priority} { | |
129 | set out_queue [concat [list $newe] $out_queue] | |
130 | } else { | |
131 | lappend out_queue $newe | |
132 | } | |
133 | if {[llength $out_queue] == 1} { | |
134 | out_restart | |
135 | } | |
9bc33297 | 136 | } |
9bc33297 | 137 | |
7818b6af IJ |
138 | proc sendout {command args} { eval sendout_priority [list 0 $command] $args } |
139 | ||
9bc33297 IJ |
140 | proc log {data} { |
141 | puts $data | |
142 | } | |
143 | ||
144 | proc logerror {data} { | |
145 | log $data | |
cc2d31de IJ |
146 | } |
147 | ||
148 | proc saveeic {} { | |
149 | global saveei saveec errorInfo errorCode | |
150 | ||
151 | set saveei $errorInfo | |
152 | set saveec $errorCode | |
153 | ||
154 | puts ">$saveec|$saveei<" | |
155 | } | |
156 | ||
157 | proc bgerror {msg} { | |
158 | global save | |
159 | logerror $msg | |
160 | saveeic | |
161 | } | |
9bc33297 IJ |
162 | |
163 | proc onread {args} { | |
17bfe24c | 164 | global sock nick calling_nick errorInfo errorCode |
9bc33297 | 165 | |
28c8ab78 | 166 | if {[gets $sock line] == -1} { fail "EOF/error on input" } |
cc2d31de | 167 | regsub -all "\[^ -\176\240-\376\]" $line ? line |
9bc33297 | 168 | set org $line |
17bfe24c IJ |
169 | |
170 | set ei $errorInfo | |
171 | set ec $errorCode | |
a4a1f396 | 172 | catch { unset calling_nick } |
17bfe24c IJ |
173 | set errorInfo $ei |
174 | set errorCode $ec | |
175 | ||
9bc33297 IJ |
176 | if {[regexp -nocase {^:([^ ]+) (.*)} $line dummy prefix remain]} { |
177 | set line $remain | |
a4a1f396 IJ |
178 | if {[regexp {^([^!]+)!} $prefix dummy maybenick]} { |
179 | set calling_nick $maybenick | |
180 | if {"[irctolower $maybenick]" == "[irctolower $nick]"} return | |
181 | } | |
9bc33297 IJ |
182 | } else { |
183 | set prefix {} | |
184 | } | |
cc2d31de | 185 | if {![string length $line]} { return } |
9bc33297 IJ |
186 | if {![regexp -nocase {^([0-9a-z]+) *(.*)} $line dummy command line]} { |
187 | log "bad command: $org" | |
188 | return | |
189 | } | |
cc2d31de | 190 | set command [string toupper $command] |
9bc33297 | 191 | set params {} |
cc2d31de | 192 | while {[regexp {^([^ :]+) *(.*)} $line dummy thisword line]} { |
9bc33297 IJ |
193 | lappend params $thisword |
194 | } | |
195 | if {[regexp {^:(.*)} $line dummy thisword]} { | |
196 | lappend params $thisword | |
197 | } elseif {[string length $line]} { | |
198 | log "junk at end: $org" | |
199 | return | |
200 | } | |
cc2d31de IJ |
201 | if {"$command" == "PRIVMSG" && |
202 | [regexp {^[&#+!]} [lindex $params 0]] && | |
203 | ![regexp {^!} [lindex $params 1]]} { | |
204 | # on-channel message, ignore | |
422f52e4 | 205 | catch { |
83dd1224 | 206 | recordlastseen_p $prefix "talking on [lindex $params 0]" 1 |
422f52e4 | 207 | } |
cc2d31de IJ |
208 | return |
209 | } | |
83dd1224 | 210 | log "[clock seconds] <- $org" |
9bc33297 | 211 | set procname msg_$command |
cc2d31de | 212 | if {[catch { info body $procname }]} { return } |
9bc33297 IJ |
213 | if {[catch { |
214 | eval [list $procname $prefix $command] $params | |
215 | } emsg]} { | |
216 | logerror "error: $emsg ($prefix $command $params)" | |
cc2d31de | 217 | saveeic |
9bc33297 IJ |
218 | } |
219 | } | |
220 | ||
7ce72032 | 221 | proc sendprivmsg {dest l} { |
20087363 IJ |
222 | foreach v [split $l "\n"] { |
223 | sendout [expr {[ischan $dest] ? "PRIVMSG" : "NOTICE"}] $dest $v | |
224 | } | |
83dd1224 | 225 | } |
7818b6af IJ |
226 | proc sendaction_priority {priority dest what} { |
227 | sendout_priority $priority PRIVMSG $dest "\001ACTION $what\001" | |
228 | } | |
7ce72032 IJ |
229 | proc msendprivmsg {dest ll} { foreach l $ll { sendprivmsg $dest $l } } |
230 | proc msendprivmsg_delayed {delay dest ll} { after $delay [list msendprivmsg $dest $ll] } | |
83dd1224 | 231 | |
cc2d31de IJ |
232 | proc prefix_none {} { |
233 | upvar 1 p p | |
9bc33297 | 234 | if {[string length $p]} { error "prefix specified" } |
cc2d31de | 235 | } |
9bc33297 | 236 | |
cc2d31de | 237 | proc msg_PING {p c s1} { |
28c8ab78 | 238 | global musthaveping_after |
cc2d31de IJ |
239 | prefix_none |
240 | sendout PONG $s1 | |
534e26a9 | 241 | if {[info exists musthaveping_after]} connected |
9bc33297 IJ |
242 | } |
243 | ||
cc2d31de IJ |
244 | proc check_nick {n} { |
245 | if {[regexp -nocase {[^][\\`_^{|}a-z0-9-]} $n]} { error "bad char in nick" } | |
246 | if {[regexp {^[-0-9]} $n]} { error "bad nick start" } | |
247 | } | |
248 | ||
422f52e4 IJ |
249 | proc ischan {dest} { |
250 | return [regexp {^[&#+!]} $dest] | |
251 | } | |
252 | ||
253 | proc irctolower {v} { | |
254 | foreach {from to} [list "\\\[" "{" \ | |
255 | "\\\]" "}" \ | |
256 | "\\\\" "|" \ | |
257 | "~" "^"] { | |
258 | regsub -all $from $v $to v | |
259 | } | |
260 | return [string tolower $v] | |
261 | } | |
262 | ||
cc2d31de IJ |
263 | proc prefix_nick {} { |
264 | global nick | |
265 | upvar 1 p p | |
266 | upvar 1 n n | |
267 | if {![regexp {^([^!]+)!} $p dummy n]} { error "not from nick" } | |
268 | check_nick $n | |
a056c4bd IJ |
269 | if {"[irctolower $n]" == "[irctolower $nick]"} { |
270 | error "from myself" {} {} | |
271 | } | |
422f52e4 IJ |
272 | } |
273 | ||
7ce72032 | 274 | proc showintervalsecs {howlong} { |
d83fb8db IJ |
275 | return [showintervalsecs/[opt timeformat] $howlong] |
276 | } | |
277 | ||
278 | proc showintervalsecs/ks {howlong} { | |
7ce72032 IJ |
279 | if {$howlong < 1000} { |
280 | return "${howlong}s" | |
83dd1224 IJ |
281 | } else { |
282 | if {$howlong < 1000000} { | |
283 | set pfx k | |
284 | set scale 1000 | |
285 | } else { | |
286 | set pfx M | |
287 | set scale 1000000 | |
288 | } | |
289 | set value [expr "$howlong.0 / $scale"] | |
290 | foreach {min format} {100 %.0f 10 %.1f 1 %.2f} { | |
291 | if {$value < $min} continue | |
7ce72032 | 292 | return [format "$format${pfx}s" $value] |
83dd1224 IJ |
293 | } |
294 | } | |
295 | } | |
296 | ||
d83fb8db IJ |
297 | proc format_qty {qty unit} { |
298 | set o $qty | |
299 | append o " " | |
300 | append o $unit | |
301 | if {$qty != 1} { append o s } | |
302 | return $o | |
303 | } | |
304 | ||
305 | proc showintervalsecs/hms {qty} { | |
306 | set ul {second 60 minute 60 hour 24 day 7 week} | |
307 | set remainv 0 | |
308 | while {[llength $ul] > 1 && $qty >= [set uv [lindex $ul 1]]} { | |
309 | set remainu [lindex $ul 0] | |
310 | set remainv [expr {$qty % $uv}] | |
311 | set qty [expr {($qty-$remainv)/$uv}] | |
312 | set ul [lreplace $ul 0 1] | |
313 | } | |
314 | set o [format_qty $qty [lindex $ul 0]] | |
315 | if {$remainv} { | |
316 | append o " " | |
317 | append o [format_qty $remainv $remainu] | |
318 | } | |
319 | return $o | |
320 | } | |
321 | ||
7ce72032 IJ |
322 | proc showinterval {howlong} { |
323 | if {$howlong <= 0} { | |
324 | return {just now} | |
325 | } else { | |
326 | return "[showintervalsecs $howlong] ago" | |
327 | } | |
328 | } | |
329 | ||
83dd1224 IJ |
330 | proc showtime {when} { |
331 | return [showinterval [expr {[clock seconds] - $when}]] | |
332 | } | |
333 | ||
334 | proc def_msgproc {name argl body} { | |
335 | proc msg_$name "varbase $argl" "\ | |
336 | upvar #0 msg/\$varbase/dest d\n\ | |
337 | upvar #0 msg/\$varbase/str s\n\ | |
338 | upvar #0 msg/\$varbase/accum a\n\ | |
339 | $body" | |
340 | } | |
341 | ||
342 | def_msgproc begin {dest str} { | |
343 | set d $dest | |
344 | set s $str | |
345 | set a {} | |
346 | } | |
347 | ||
348 | def_msgproc append {str} { | |
349 | set ns "$s$str" | |
350 | if {[string length $s] && [string length $ns] > 65} { | |
351 | msg__sendout $varbase | |
352 | set s " [string trimleft $str]" | |
353 | } else { | |
354 | set s $ns | |
355 | } | |
356 | } | |
357 | ||
358 | def_msgproc finish {} { | |
359 | msg__sendout $varbase | |
360 | unset s | |
361 | unset d | |
362 | return $a | |
363 | } | |
364 | ||
365 | def_msgproc _sendout {} { | |
366 | lappend a [string trimright $s] | |
367 | set s {} | |
368 | } | |
369 | ||
370 | proc looking_whenwhere {when where} { | |
371 | set str [showtime [expr {$when-1}]] | |
372 | if {[string length $where]} { append str " on $where" } | |
373 | return $str | |
374 | } | |
375 | ||
376 | proc recordlastseen_n {n how here} { | |
377 | global lastseen lookedfor | |
422f52e4 | 378 | set lastseen([irctolower $n]) [list $n [clock seconds] $how] |
83dd1224 IJ |
379 | if {!$here} return |
380 | upvar #0 lookedfor([irctolower $n]) lf | |
381 | if {[info exists lf]} { | |
382 | switch -exact [llength $lf] { | |
383 | 0 { | |
384 | set ml {} | |
385 | } | |
386 | 1 { | |
387 | manyset [lindex $lf 0] when who where | |
388 | set ml [list \ | |
389 | "FYI, $who was looking for you [looking_whenwhere $when $where]."] | |
390 | } | |
391 | default { | |
392 | msg_begin tosend $n "FYI, people have been looking for you:" | |
393 | set i 0 | |
394 | set fin "" | |
395 | foreach e $lf { | |
396 | incr i | |
397 | if {$i == 1} { | |
398 | msg_append tosend " " | |
399 | } elseif {$i == [llength $lf]} { | |
400 | msg_append tosend " and " | |
401 | set fin . | |
402 | } else { | |
403 | msg_append tosend ", " | |
404 | } | |
405 | manyset $e when who where | |
406 | msg_append tosend \ | |
407 | "$who ([looking_whenwhere $when $where])$fin" | |
408 | } | |
409 | set ml [msg_finish tosend] | |
410 | } | |
411 | } | |
412 | unset lf | |
413 | msendprivmsg_delayed 1000 $n $ml | |
414 | } | |
422f52e4 | 415 | } |
281b5f05 IJ |
416 | |
417 | proc note_topic {showoff whoby topic} { | |
418 | if {[string length $whoby]} { | |
419 | set msg "FYI, $whoby has changed the topic on $showoff" | |
420 | } else { | |
421 | set msg "FYI, I have reset the topic on $showoff" | |
422 | } | |
423 | if {[string length $topic] < 160} { | |
424 | append msg " to $topic" | |
425 | } else { | |
426 | append msg " but it is too long to reproduce here !" | |
427 | } | |
428 | set showoff [irctolower $showoff] | |
429 | set tell [chandb_get $showoff topictell] | |
430 | if {[lsearch -exact $tell *] >= 0} { | |
431 | set tryspies [chandb_list] | |
432 | } else { | |
433 | set tryspies $tell | |
434 | } | |
281b5f05 IJ |
435 | foreach spy $tryspies { |
436 | set see [chandb_get $spy topicsee] | |
281b5f05 IJ |
437 | if {[lsearch -exact $see $showoff] >= 0 || \ |
438 | ([lsearch -exact $see *] >= 0 && \ | |
439 | [lsearch -exact $tell $spy] >= 0)} { | |
440 | sendprivmsg $spy $msg | |
441 | } | |
442 | } | |
443 | } | |
444 | ||
83dd1224 | 445 | proc recordlastseen_p {p how here} { |
422f52e4 | 446 | prefix_nick |
83dd1224 | 447 | recordlastseen_n $n $how $here |
422f52e4 IJ |
448 | } |
449 | ||
450 | proc chanmode_arg {} { | |
451 | upvar 2 args cm_args | |
452 | set rv [lindex $cm_args 0] | |
453 | set cm_args [lreplace cm_args 0 0] | |
454 | return $rv | |
455 | } | |
456 | ||
83dd1224 | 457 | proc chanmode_o1 {m g p chan} { |
20087363 | 458 | global nick chan_initialop |
83dd1224 IJ |
459 | prefix_nick |
460 | set who [chanmode_arg] | |
461 | recordlastseen_n $n "being nice to $who" 1 | |
462 | if {"[irctolower $who]" == "[irctolower $nick]"} { | |
534e26a9 IJ |
463 | set nlower [irctolower $n] |
464 | upvar #0 nick_unique($nlower) u | |
20087363 IJ |
465 | if {[chandb_exists $chan]} { |
466 | sendprivmsg $n Thanks. | |
467 | } elseif {![info exists u]} { | |
468 | sendprivmsg $n {Op me while not on the channel, why don't you ?} | |
469 | } else { | |
470 | set chan_initialop([irctolower $chan]) $u | |
471 | sendprivmsg $n \ | |
472 | "Thanks. You can use `channel manager ...' to register this channel." | |
473 | if {![nickdb_exists $n] || ![string length [nickdb_get $n username]]} { | |
474 | sendprivmsg $n \ | |
475 | "(But to do that you must register your nick securely first.)" | |
476 | } | |
477 | } | |
83dd1224 IJ |
478 | } |
479 | } | |
480 | ||
422f52e4 IJ |
481 | proc chanmode_o0 {m g p chan} { |
482 | global nick chandeop | |
483 | prefix_nick | |
484 | set who [chanmode_arg] | |
83dd1224 | 485 | recordlastseen_p $p "being mean to $who" 1 |
422f52e4 IJ |
486 | if {"[irctolower $who]" == "[irctolower $nick]"} { |
487 | set chandeop($chan) [list [clock seconds] $p] | |
488 | } | |
cc2d31de | 489 | } |
9bc33297 | 490 | |
422f52e4 IJ |
491 | proc msg_MODE {p c dest modelist args} { |
492 | if {![ischan $dest]} return | |
493 | if {[regexp {^\-(.+)$} $modelist dummy modelist]} { | |
494 | set give 0 | |
495 | } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} { | |
496 | set give 1 | |
497 | } else { | |
498 | error "invalid modelist" | |
499 | } | |
500 | foreach m [split $modelist] { | |
501 | set procname chanmode_$m$give | |
502 | if {[catch { info body $procname }]} { | |
83dd1224 | 503 | recordlastseen_p $p "fiddling with $dest" 1 |
422f52e4 IJ |
504 | } else { |
505 | $procname $m $give $p $dest | |
506 | } | |
507 | } | |
508 | } | |
509 | ||
534e26a9 IJ |
510 | proc leaving {lchan} { |
511 | foreach luser [array names nick_onchans] { | |
512 | upvar #0 nick_onchans($luser) oc | |
513 | set oc [grep tc {"$tc" != "$lchan"} $oc] | |
514 | } | |
515 | upvar #0 chan_nicks($lchan) nlist | |
516 | unset nlist | |
517 | } | |
518 | ||
519 | proc dojoin {lchan} { | |
520 | global chan_nicks | |
521 | sendout JOIN $lchan | |
522 | set chan_nicks($lchan) {} | |
523 | } | |
524 | ||
525 | proc check_justme {lchan} { | |
526 | global nick | |
527 | upvar #0 chan_nicks($lchan) nlist | |
528 | if {[llength $nlist] != 1} return | |
529 | if {"[lindex $nlist 0]" != "$nick"} return | |
530 | if {[chandb_exists $lchan]} { | |
531 | set mode [chandb_get $lchan mode] | |
532 | if {"$mode" != "*"} { | |
533 | sendout MODE $lchan $mode | |
534 | } | |
281b5f05 IJ |
535 | set topic [chandb_get $lchan topicset] |
536 | if {[string length $topic]} { | |
537 | sendout TOPIC $lchan $topic | |
538 | note_topic $lchan {} $topic | |
539 | } | |
534e26a9 IJ |
540 | } else { |
541 | sendout PART $lchan | |
542 | leaving $lchan | |
a4a1f396 IJ |
543 | } |
544 | } | |
545 | ||
a056c4bd | 546 | proc process_kickpart {chan user} { |
a4a1f396 | 547 | global nick |
a056c4bd | 548 | check_nick $user |
534e26a9 IJ |
549 | set luser [irctolower $user] |
550 | set lchan [irctolower $chan] | |
a056c4bd | 551 | if {![ischan $chan]} { error "not a channel" } |
534e26a9 IJ |
552 | if {"$luser" == "[irctolower $nick]"} { |
553 | leaving $lchan | |
554 | } else { | |
555 | upvar #0 nick_onchans($luser) oc | |
556 | upvar #0 chan_nicks($lchan) nlist | |
557 | set oc [grep tc {"$tc" != "$lchan"} $oc] | |
558 | set nlist [grep tn {"$tn" != "$luser"} $nlist] | |
559 | nick_case $user | |
560 | if {![llength $oc]} { | |
561 | nick_forget $luser | |
562 | } else { | |
563 | check_justme $lchan | |
564 | } | |
a4a1f396 | 565 | } |
534e26a9 | 566 | } |
a056c4bd | 567 | |
281b5f05 IJ |
568 | proc msg_TOPIC {p c dest topic} { |
569 | prefix_nick | |
570 | if {![ischan $dest]} return | |
571 | recordlastseen_n $n "changing the topic on $dest" 1 | |
572 | note_topic [irctolower $dest] $n $topic | |
573 | } | |
574 | ||
a056c4bd IJ |
575 | proc msg_KICK {p c chans users comment} { |
576 | set chans [split $chans ,] | |
577 | set users [split $users ,] | |
578 | if {[llength $chans] > 1} { | |
579 | foreach chan $chans user $users { process_kickpart $chan $user } | |
580 | } else { | |
581 | foreach user $users { process_kickpart [lindex $chans 0] $user } | |
582 | } | |
583 | } | |
584 | ||
585 | proc msg_KILL {p c user why} { | |
586 | nick_forget $user | |
587 | } | |
588 | ||
20087363 IJ |
589 | set nick_counter 0 |
590 | set nick_arys {onchans username unique} | |
534e26a9 IJ |
591 | # nick_onchans($luser) -> [list ... $lchan ...] |
592 | # nick_username($luser) -> <securely known local username> | |
593 | # nick_unique($luser) -> <counter> | |
594 | # nick_case($luser) -> $user (valid even if no longer visible) | |
595 | ||
596 | # chan_nicks($lchan) -> [list ... $luser ...] | |
a056c4bd | 597 | |
534e26a9 IJ |
598 | proc lnick_forget {luser} { |
599 | global nick_arys chan_nicks | |
a056c4bd | 600 | foreach ary $nick_arys { |
534e26a9 | 601 | upvar #0 nick_${ary}($luser) av |
a056c4bd IJ |
602 | catch { unset av } |
603 | } | |
534e26a9 IJ |
604 | foreach lch [array names chan_nicks] { |
605 | upvar #0 chan_nicks($lch) nlist | |
606 | set nlist [grep tn {"$tn" != "$luser"} $nlist] | |
607 | check_justme $lch | |
608 | } | |
609 | } | |
610 | ||
611 | proc nick_forget {user} { | |
612 | global nick_arys chan_nicks | |
613 | lnick_forget [irctolower $user] | |
614 | nick_case $user | |
a4a1f396 IJ |
615 | } |
616 | ||
534e26a9 | 617 | proc nick_case {user} { |
a4a1f396 | 618 | global nick_case |
534e26a9 | 619 | set nick_case([irctolower $user]) $user |
a056c4bd IJ |
620 | } |
621 | ||
83dd1224 | 622 | proc msg_NICK {p c newnick} { |
a4a1f396 | 623 | global nick_arys nick_case |
83dd1224 IJ |
624 | prefix_nick |
625 | recordlastseen_n $n "changing nicks to $newnick" 0 | |
626 | recordlastseen_n $newnick "changing nicks from $n" 1 | |
a056c4bd IJ |
627 | foreach ary $nick_arys { |
628 | upvar #0 nick_${ary}($n) old | |
629 | upvar #0 nick_${ary}($newnick) new | |
630 | if {[info exists new]} { error "nick collision ?! $ary $n $newnick" } | |
631 | if {[info exists old]} { set new $old; unset old } | |
632 | } | |
534e26a9 IJ |
633 | upvar #0 nick_onchans($new) |
634 | set luser [irctolower $n] | |
635 | set lusernew [irctolower $newnick] | |
636 | foreach ch $oc { | |
637 | upvar #0 chan_nicks($ch) nlist | |
638 | set nlist [grep tn {"$tn" != "$luser"} $nlist] | |
639 | lappend nlist $lusernew | |
640 | } | |
a4a1f396 | 641 | nick_case $newnick |
83dd1224 IJ |
642 | } |
643 | ||
20087363 IJ |
644 | proc nick_ishere {n} { |
645 | global nick_counter | |
534e26a9 | 646 | upvar #0 nick_unique([irctolower $n]) u |
20087363 IJ |
647 | if {![info exists u]} { set u [incr nick_counter].$n.[clock seconds] } |
648 | nick_case $n | |
649 | } | |
650 | ||
a056c4bd IJ |
651 | proc msg_JOIN {p c chan} { |
652 | prefix_nick | |
653 | recordlastseen_n $n "joining $chan" 1 | |
534e26a9 | 654 | upvar #0 nick_onchans([irctolower $n]) oc |
a056c4bd | 655 | lappend oc [irctolower $chan] |
20087363 | 656 | nick_ishere $n |
a056c4bd IJ |
657 | } |
658 | proc msg_PART {p c chan} { | |
659 | prefix_nick | |
660 | recordlastseen_n $n "leaving $chan" 1 | |
661 | process_kickpart $chan $n | |
662 | } | |
663 | proc msg_QUIT {p c why} { | |
664 | prefix_nick | |
665 | recordlastseen_n $n "leaving ($why)" 0 | |
666 | nick_forget $n | |
667 | } | |
422f52e4 | 668 | |
cc2d31de IJ |
669 | proc msg_PRIVMSG {p c dest text} { |
670 | prefix_nick | |
422f52e4 | 671 | if {[ischan $dest]} { |
83dd1224 | 672 | recordlastseen_n $n "invoking me in $dest" 1 |
422f52e4 | 673 | set output $dest |
cc2d31de | 674 | } else { |
83dd1224 | 675 | recordlastseen_n $n "talking to me" 1 |
422f52e4 IJ |
676 | set output $n |
677 | } | |
a4a1f396 | 678 | nick_case $n |
422f52e4 IJ |
679 | |
680 | if {[catch { | |
681 | regsub {^! *} $text {} text | |
682 | set ucmd [ta_word] | |
83dd1224 | 683 | set procname ucmd/[string tolower $ucmd] |
422f52e4 IJ |
684 | if {[catch { info body $procname }]} { |
685 | error "unknown command; try help for help" | |
686 | } | |
83dd1224 | 687 | $procname $p $dest |
422f52e4 | 688 | } rv]} { |
7ce72032 | 689 | sendprivmsg $n "error: $rv" |
422f52e4 | 690 | } else { |
7ce72032 | 691 | manyset $rv priv_msgs pub_msgs priv_acts pub_acts |
7a70431a | 692 | foreach {td val} [list $n $priv_acts $output $pub_acts] { |
7ce72032 | 693 | foreach l [split $val "\n"] { |
7818b6af | 694 | sendaction_priority 0 $td $l |
7ce72032 IJ |
695 | } |
696 | } | |
7a70431a | 697 | foreach {td val} [list $n $priv_msgs $output $pub_msgs] { |
422f52e4 | 698 | foreach l [split $val "\n"] { |
7a70431a | 699 | sendprivmsg $td $l |
422f52e4 IJ |
700 | } |
701 | } | |
702 | } | |
703 | } | |
704 | ||
a056c4bd | 705 | proc msg_INVITE {p c n chan} { |
534e26a9 | 706 | after 1000 [list dojoin [irctolower $chan]] |
a056c4bd IJ |
707 | } |
708 | ||
709 | proc grep {var predicate list} { | |
710 | set o {} | |
711 | upvar 1 $var v | |
712 | foreach v $list { | |
713 | if {[uplevel 1 [list expr $predicate]]} { lappend o $v } | |
714 | } | |
715 | return $o | |
716 | } | |
717 | ||
718 | proc msg_353 {p c dest type chan nicklist} { | |
719 | global names_chans nick_onchans | |
534e26a9 IJ |
720 | set lchan [irctolower $chan] |
721 | upvar #0 chan_nicks($lchan) nlist | |
722 | lappend names_chans $lchan | |
723 | if {![info exists nlist]} { | |
724 | # We don't think we're on this channel, so ignore it ! | |
725 | # Unfortunately, because we don't get a reply to PART, | |
726 | # we have to remember ourselves whether we're on a channel, | |
727 | # and ignore stuff if we're not, to avoid races. Feh. | |
728 | return | |
729 | } | |
730 | set nlist_new {} | |
731 | foreach user [split $nicklist { }] { | |
732 | regsub {^[@+]} $user {} user | |
733 | if {![string length $user]} continue | |
734 | check_nick $user | |
735 | set luser [irctolower $user] | |
736 | upvar #0 nick_onchans($luser) oc | |
737 | lappend oc $lchan | |
738 | lappend nlist_new $luser | |
739 | nick_ishere $user | |
a056c4bd | 740 | } |
534e26a9 | 741 | set nlist $nlist_new |
a056c4bd IJ |
742 | } |
743 | ||
744 | proc msg_366 {p c args} { | |
745 | global names_chans nick_onchans | |
534e26a9 IJ |
746 | set lchan [irctolower $c] |
747 | foreach luser [array names nick_onchans] { | |
748 | upvar #0 nick_onchans($luser) oc | |
749 | if {[llength names_chans] > 1} { | |
a056c4bd | 750 | set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc] |
a056c4bd | 751 | } |
534e26a9 | 752 | if {![llength $oc]} { lnick_forget $n } |
a056c4bd IJ |
753 | } |
754 | unset names_chans | |
755 | } | |
756 | ||
11d9bff9 IJ |
757 | proc ta_anymore {} { |
758 | upvar 1 text text | |
759 | return [expr {!![string length $text]}] | |
760 | } | |
761 | ||
422f52e4 IJ |
762 | proc ta_nomore {} { |
763 | upvar 1 text text | |
764 | if {[string length $text]} { error "too many parameters" } | |
765 | } | |
766 | ||
767 | proc ta_word {} { | |
768 | upvar 1 text text | |
769 | if {![regexp {^([^ ]+) *(.*)} $text dummy firstword text]} { | |
770 | error "too few parameters" | |
771 | } | |
772 | return $firstword | |
773 | } | |
774 | ||
775 | proc ta_nick {} { | |
776 | upvar 1 text text | |
777 | set v [ta_word] | |
778 | check_nick $v | |
779 | return $v | |
780 | } | |
781 | ||
83dd1224 IJ |
782 | proc def_ucmd {cmdname body} { |
783 | proc ucmd/$cmdname {p dest} " upvar 1 text text\n$body" | |
784 | } | |
785 | ||
7ce72032 IJ |
786 | proc ucmdr {priv pub args} { |
787 | return -code return [concat [list $priv $pub] $args] | |
422f52e4 | 788 | } |
e1ba63be | 789 | |
e6cc22dc | 790 | proc loadhelp {} { |
5e5be903 | 791 | global help_topics errorInfo |
e6cc22dc IJ |
792 | |
793 | catch { unset help_topics } | |
794 | set f [open helpinfos r] | |
d83fb8db IJ |
795 | try_except_finally { |
796 | set lno 0 | |
797 | while {[gets $f l] >= 0} { | |
798 | incr lno | |
799 | if {[regexp {^#.*} $l]} { | |
800 | } elseif {[regexp {^ *$} $l]} { | |
801 | if {[info exists topic]} { | |
802 | set help_topics($topic) [join $lines "\n"] | |
803 | unset topic | |
804 | unset lines | |
805 | } | |
5e5be903 IJ |
806 | } elseif {[regexp {^\:\:} $l]} { |
807 | } elseif {[regexp {^\:([-+._0-9a-z]*)$} $l dummy newtopic]} { | |
d83fb8db IJ |
808 | if {[info exists topic]} { |
809 | error "help $newtopic while in $topic" | |
810 | } | |
811 | set topic $newtopic | |
812 | set lines {} | |
5e5be903 | 813 | } elseif {[regexp {^[^:#]} $l]} { |
d83fb8db | 814 | set topic |
5e5be903 IJ |
815 | regsub -all {([^\\])\!\$?} _$l {\1} l |
816 | regsub -all {\\(.)} $l {\1} l | |
817 | regsub {^_} $l {} l | |
d83fb8db IJ |
818 | lappend lines [string trimright $l] |
819 | } else { | |
820 | error "eh ? $lno: $l" | |
e6cc22dc | 821 | } |
e6cc22dc | 822 | } |
d83fb8db | 823 | if {[info exists topic]} { error "unfinished topic $topic" } |
5e5be903 IJ |
824 | } { |
825 | set errorInfo "in helpinfos line $lno\n$errorInfo" | |
826 | } { | |
d83fb8db | 827 | close $f |
e6cc22dc | 828 | } |
422f52e4 IJ |
829 | } |
830 | ||
e6cc22dc | 831 | def_ucmd help { |
7818b6af IJ |
832 | if {[set lag [out_lagged]]} { |
833 | if {[ischan $dest]} { set replyto $dest } else { set replyto $n } | |
834 | if {$lag > 1} { | |
835 | sendaction_priority 1 $replyto \ | |
836 | "is very lagged. Please ask for help again later." | |
837 | ucmdr {} {} | |
838 | } else { | |
839 | sendaction_priority 1 $replyto \ | |
840 | "is lagged. Your help will arrive shortly ..." | |
841 | } | |
842 | } | |
843 | ||
e6cc22dc | 844 | upvar #0 help_topics([irctolower [string trim $text]]) info |
4fd2739c | 845 | if {![info exists info]} { ucmdr "No help on $text, sorry." {} } |
e6cc22dc IJ |
846 | ucmdr $info {} |
847 | } | |
e1ba63be | 848 | |
e6cc22dc IJ |
849 | def_ucmd ? { |
850 | global help_topics | |
851 | ucmdr $help_topics() {} | |
852 | } | |
e1ba63be | 853 | |
4fd2739c | 854 | proc check_username {target} { |
7ce72032 IJ |
855 | if { |
856 | [string length $target] > 8 || | |
857 | [regexp {[^-0-9a-z]} $target] || | |
858 | ![regexp {^[a-z]} $target] | |
859 | } { error "invalid username" } | |
4fd2739c IJ |
860 | } |
861 | ||
20087363 | 862 | proc somedb__head {} { |
7a70431a | 863 | uplevel 1 { |
20087363 IJ |
864 | set idl [irctolower $id] |
865 | upvar #0 ${nickchan}db($idl) ndbe | |
866 | binary scan $idl H* idh | |
867 | set idfn $fprefix$idh | |
868 | if {![info exists iddbe] && [file exists $idfn]} { | |
869 | set f [open $idfn r] | |
7a70431a IJ |
870 | try_except_finally { set newval [read $f] } {} { close $f } |
871 | if {[llength $newval] % 2} { error "invalid length" } | |
20087363 | 872 | set iddbe $newval |
7a70431a IJ |
873 | } |
874 | } | |
875 | } | |
876 | ||
20087363 IJ |
877 | proc def_somedb {name arglist body} { |
878 | foreach {nickchan fprefix} {nick users/n chan chans/c} { | |
879 | proc ${nickchan}db_$name $arglist \ | |
534e26a9 IJ |
880 | "set nickchan $nickchan; set fprefix $fprefix; $body" |
881 | } | |
882 | } | |
883 | ||
884 | def_somedb list {} { | |
885 | set list {} | |
886 | foreach path [glob -nocomplain -path $fprefix *] { | |
887 | binary scan $path "A[string length $fprefix]A*" afprefix thinghex | |
888 | if {"$afprefix" != "$fprefix"} { error "wrong prefix $path $afprefix" } | |
889 | lappend list [binary format H* $thinghex] | |
20087363 | 890 | } |
534e26a9 IJ |
891 | return $list |
892 | } | |
893 | ||
894 | proc def_somedb_id {name arglist body} { | |
895 | def_somedb $name [concat id $arglist] "somedb__head; $body" | |
7a70431a IJ |
896 | } |
897 | ||
534e26a9 | 898 | def_somedb_id exists {} { |
20087363 | 899 | return [info exists iddbe] |
7a70431a IJ |
900 | } |
901 | ||
534e26a9 | 902 | def_somedb_id delete {} { |
20087363 IJ |
903 | catch { unset iddbe } |
904 | file delete $idfn | |
7a70431a IJ |
905 | } |
906 | ||
20087363 | 907 | set default_settings_nick {timeformat ks} |
281b5f05 IJ |
908 | set default_settings_chan { |
909 | autojoin 1 | |
910 | mode * | |
911 | userinvite pub | |
912 | topicset {} | |
913 | topicsee {} | |
914 | topictell {} | |
915 | } | |
7a70431a | 916 | |
534e26a9 | 917 | def_somedb_id set {args} { |
20087363 IJ |
918 | upvar #0 default_settings_$nickchan def |
919 | if {![info exists iddbe]} { set iddbe $def } | |
920 | foreach {key value} [concat $iddbe $args] { set a($key) $value } | |
7a70431a IJ |
921 | set newval {} |
922 | foreach {key value} [array get a] { lappend newval $key $value } | |
20087363 | 923 | set f [open $idfn.new w] |
7a70431a IJ |
924 | try_except_finally { |
925 | puts $f $newval | |
926 | close $f | |
20087363 | 927 | file rename -force $idfn.new $idfn |
7a70431a | 928 | } { |
7a70431a | 929 | } { |
d83fb8db | 930 | catch { close $f } |
7a70431a | 931 | } |
20087363 | 932 | set iddbe $newval |
7a70431a IJ |
933 | } |
934 | ||
534e26a9 | 935 | def_somedb_id get {key} { |
20087363 IJ |
936 | upvar #0 default_settings_$nickchan def |
937 | if {[info exists iddbe]} { | |
534e26a9 | 938 | set l [concat $iddbe $def] |
7a70431a | 939 | } else { |
20087363 | 940 | set l $def |
7a70431a IJ |
941 | } |
942 | foreach {tkey value} $l { | |
943 | if {"$tkey" == "$key"} { return $value } | |
944 | } | |
945 | error "unset setting $key" | |
946 | } | |
947 | ||
20087363 IJ |
948 | proc opt {key} { |
949 | global calling_nick | |
950 | if {[info exists calling_nick]} { set n $calling_nick } { set n {} } | |
951 | return [nickdb_get $n $key] | |
952 | } | |
953 | ||
7a70431a IJ |
954 | proc check_notonchan {} { |
955 | upvar 1 dest dest | |
956 | if {[ischan $dest]} { error "that command must be sent privately" } | |
957 | } | |
958 | ||
959 | proc nick_securitycheck {strict} { | |
960 | upvar 1 n n | |
961 | if {![nickdb_exists $n]} { error "you are unknown to me, use `register'." } | |
20087363 | 962 | set wantu [nickdb_get $n username] |
7a70431a IJ |
963 | if {![string length $wantu]} { |
964 | if {$strict} { | |
965 | error "that feature is only available to secure users, sorry." | |
966 | } else { | |
967 | return | |
968 | } | |
969 | } | |
534e26a9 IJ |
970 | set luser [irctolower $n] |
971 | upvar #0 nick_username($luser) nu | |
7a70431a IJ |
972 | if {![info exists nu]} { |
973 | error "nick $n is secure, you must identify yourself first." | |
974 | } | |
975 | if {"$wantu" != "$nu"} { | |
976 | error "you are the wrong user - the nick $n belongs to $wantu, not $nu" | |
977 | } | |
978 | } | |
979 | ||
20087363 IJ |
980 | proc channel_securitycheck {channel n} { |
981 | # You must also call `nick_securitycheck 1' | |
c362e172 IJ |
982 | set mgrs [chandb_get $channel managers] |
983 | if {[lsearch -exact [irctolower $mgrs] [irctolower $n]] < 0} { | |
20087363 IJ |
984 | error "you are not a manager of $channel" |
985 | } | |
986 | } | |
987 | ||
988 | proc def_chancmd {name body} { | |
989 | proc channel/$name {} \ | |
990 | " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body" | |
991 | } | |
992 | ||
281b5f05 IJ |
993 | proc ta_listop {findnow procvalue} { |
994 | # findnow and procvalue are code fragments which will be executed | |
995 | # in the caller's level. findnow should set ta_listop_ev to | |
996 | # the current list, and procvalue should treat ta_listop_ev as | |
997 | # a proposed value in the list and check and possibly modify | |
998 | # (canonicalise?) it. After ta_listop, ta_listop_ev will | |
999 | # be the new value of the list. | |
1000 | upvar 1 ta_listop_ev exchg | |
1001 | upvar 1 text text | |
20087363 IJ |
1002 | set opcode [ta_word] |
1003 | switch -exact _$opcode { | |
281b5f05 | 1004 | _= { } |
20087363 | 1005 | _+ - _- { |
281b5f05 IJ |
1006 | uplevel 1 $findnow |
1007 | foreach item $exchg { set array($item) 1 } | |
20087363 IJ |
1008 | } |
1009 | default { | |
281b5f05 | 1010 | error "list change opcode must be one of + - =" |
20087363 IJ |
1011 | } |
1012 | } | |
281b5f05 IJ |
1013 | foreach exchg [split $text " "] { |
1014 | if {![string length $exchg]} continue | |
1015 | uplevel 1 $procvalue | |
20087363 | 1016 | if {"$opcode" != "-"} { |
281b5f05 | 1017 | set array($exchg) 1 |
20087363 | 1018 | } else { |
281b5f05 | 1019 | catch { unset array($exchg) } |
20087363 IJ |
1020 | } |
1021 | } | |
281b5f05 IJ |
1022 | set exchg [lsort [array names array]] |
1023 | } | |
1024 | ||
1025 | def_chancmd manager { | |
1026 | ta_listop { | |
1027 | if {[chandb_exists $chan]} { | |
1028 | set ta_listop_ev [chandb_get $chan managers] | |
1029 | } else { | |
1030 | set ta_listop_ev [list [irctolower $n]] | |
1031 | } | |
1032 | } { | |
1033 | check_nick $ta_listop_ev | |
1034 | set ta_listop_ev [irctolower $ta_listop_ev] | |
1035 | } | |
1036 | if {[llength $ta_listop_ev]} { | |
1037 | chandb_set $chan managers $ta_listop_ev | |
1038 | ucmdr "Managers of $chan: $ta_listop_ev" {} | |
20087363 IJ |
1039 | } else { |
1040 | chandb_delete $chan | |
1041 | ucmdr {} {} "forgets about managing $chan." {} | |
1042 | } | |
1043 | } | |
1044 | ||
1045 | def_chancmd autojoin { | |
1046 | set yesno [ta_word] | |
1047 | switch -exact [string tolower $yesno] { | |
1048 | no { set nv 0 } | |
1049 | yes { set nv 1 } | |
1050 | default { error "channel autojoin must be `yes' or `no' } | |
1051 | } | |
1052 | chandb_set $chan autojoin $nv | |
a49f2997 IJ |
1053 | ucmdr [expr {$nv ? "I will join $chan when I'm restarted " : \ |
1054 | "I won't join $chan when I'm restarted "}] {} | |
534e26a9 IJ |
1055 | } |
1056 | ||
5e5be903 IJ |
1057 | def_chancmd userinvite { |
1058 | set nv [string tolower [ta_word]] | |
1059 | switch -exact $nv { | |
1060 | pub { set txt "!invite will work for $chan, but it won't work by /msg" } | |
1061 | here { set txt "!invite and /msg invite will work, but only for users who are already on $chan." } | |
1062 | all { set txt "Any user will be able to invite themselves or anyone else to $chan." } | |
1063 | none { set txt "I will not invite anyone to $chan." } | |
1064 | default { | |
1065 | error "channel userinvite must be `pub', `here', `all' or `none' | |
1066 | } | |
1067 | } | |
1068 | chandb_set $chan userinvite $nv | |
1069 | ucmdr $txt {} | |
1070 | } | |
1071 | ||
281b5f05 IJ |
1072 | def_chancmd topic { |
1073 | set what [ta_word] | |
1074 | switch -exact $what { | |
1075 | leave { | |
1076 | ta_nomore | |
1077 | chandb_set $chan topicset {} | |
1078 | ucmdr "I won't ever change the topic of $chan." {} | |
1079 | } | |
1080 | set { | |
1081 | set t [string trim $text] | |
1082 | if {![string length $t]} { | |
1083 | error "you must specific the topic to set" | |
1084 | } | |
1085 | chandb_set $chan topicset $t | |
a49f2997 | 1086 | ucmdr "Whenever I'm alone on $chan, I'll set the topic to $t." {} |
281b5f05 IJ |
1087 | } |
1088 | see - tell { | |
1089 | ta_listop { | |
1090 | set ta_listop_ev [chandb_get $chan topic$what] | |
1091 | } { | |
1092 | if {"$ta_listop_ev" != "*"} { | |
1093 | if {![ischan $ta_listop_ev]} { | |
1094 | error "bad channel \`$ta_listop_ev' in topic $what" | |
1095 | } | |
1096 | set ta_listop_ev [irctolower $ta_listop_ev] | |
1097 | } | |
1098 | } | |
1099 | chandb_set $chan topic$what $ta_listop_ev | |
1100 | ucmdr "Topic $what list for $chan: $ta_listop_ev" {} | |
1101 | } | |
1102 | default { | |
1103 | error "unknown channel topic subcommand - see help channel" | |
1104 | } | |
1105 | } | |
1106 | } | |
1107 | ||
534e26a9 IJ |
1108 | def_chancmd mode { |
1109 | set mode [ta_word] | |
1110 | if {"$mode" != "*" && ![regexp {^(([-+][imnpst]+)+)$} $mode mode]} { | |
1111 | error {channel mode must be * or match ([-+][imnpst]+)+} | |
1112 | } | |
1113 | chandb_set $chan mode $mode | |
1114 | if {"$mode" == "*"} { | |
281b5f05 | 1115 | ucmdr "I won't ever change the mode of $chan." {} |
534e26a9 | 1116 | } else { |
281b5f05 | 1117 | ucmdr "Whenever I'm alone on $chan, I'll set the mode to $mode." {} |
534e26a9 | 1118 | } |
20087363 IJ |
1119 | } |
1120 | ||
1121 | def_chancmd show { | |
1122 | if {[chandb_exists $chan]} { | |
1123 | set l "Settings for $chan: autojoin " | |
1124 | append l [lindex {no yes} [chandb_get $chan autojoin]] | |
b60b5a0f IJ |
1125 | append l ", mode " [chandb_get $chan mode] |
1126 | append l ", userinvite " [chandb_get $chan userinvite] "." | |
20087363 | 1127 | append l "\nManagers: " |
8b6ee626 | 1128 | append l [join [chandb_get $chan managers] " "] |
281b5f05 IJ |
1129 | foreach {ts sep} {see "\n" tell " "} { |
1130 | set t [chandb_get $chan topic$ts] | |
1131 | append l $sep | |
1132 | if {[llength $t]} { | |
1133 | append l "Topic $ts list: $t." | |
1134 | } else { | |
1135 | append l "Topic $ts list is empty." | |
1136 | } | |
1137 | } | |
8b6ee626 IJ |
1138 | append l "\n" |
1139 | set t [chandb_get $chan topicset] | |
1140 | if {[string length $t]} { | |
1141 | append l "Topic to set: $t" | |
1142 | } else { | |
1143 | append l "I will not change the topic." | |
1144 | } | |
20087363 IJ |
1145 | ucmdr {} $l |
1146 | } else { | |
1147 | ucmdr {} "The channel $chan is not managed." | |
1148 | } | |
1149 | } | |
1150 | ||
1151 | def_ucmd op { | |
1152 | if {[ischan $dest]} { set target $dest } | |
1153 | if {[ta_anymore]} { set target [ta_word] } | |
1154 | ta_nomore | |
1155 | if {![info exists target]} { error "you must specify, or !... on, the channel" } | |
1156 | if {![ischan $target]} { error "not a valid channel" } | |
1157 | if {![chandb_exists $target]} { error "$target is not a managed channel." } | |
1158 | prefix_nick | |
1159 | nick_securitycheck 1 | |
1160 | channel_securitycheck $target $n | |
1161 | sendout MODE $target +o $n | |
1162 | } | |
1163 | ||
5e5be903 IJ |
1164 | def_ucmd invite { |
1165 | global chan_nicks | |
1166 | ||
1167 | if {[ischan $dest]} { | |
1168 | set target $dest | |
1169 | set onchan 1 | |
1170 | } else { | |
1171 | set target [ta_word] | |
1172 | set onchan 0 | |
1173 | } | |
1174 | set ltarget [irctolower $target] | |
1175 | if {![ischan $target]} { error "$target is not a channel." } | |
1176 | if {![info exists chan_nicks($ltarget)]} { error "I am not on $target." } | |
1177 | set ui [chandb_get $ltarget userinvite] | |
1178 | if {"$ui" == "pub" && !$onchan} { | |
1179 | error "Invitations to $target must be made with !invite." | |
1180 | } | |
1181 | if {"$ui" != "all"} { | |
1182 | prefix_nick | |
1183 | if {[lsearch -exact $chan_nicks($ltarget) [irctolower $n]] < 0} { | |
1184 | error "Invitations to $target may only be made by a user on the channel." | |
1185 | } | |
1186 | } | |
1187 | if {"$ui" == "none"} { | |
1188 | error "Sorry, I've not been authorised to invite people to $target." | |
1189 | } | |
1190 | if {![ta_anymore]} { | |
1191 | error "You have to say who to invite." | |
1192 | } | |
1193 | set invitees {} | |
1194 | while {[ta_anymore]} { | |
1195 | set invitee [ta_word] | |
1196 | check_nick $invitee | |
1197 | lappend invitees $invitee | |
1198 | } | |
1199 | foreach invitee $invitees { | |
1200 | sendout INVITE $invitee $ltarget | |
1201 | } | |
1202 | set who [lindex $invitees 0] | |
1203 | switch -exact llength $invitees { | |
1204 | 0 { error "zero invitees" } | |
1205 | 1 { } | |
1206 | 2 { append who " and [lindex $invitees 1]" } | |
1207 | * { | |
1208 | set who [join [lreplace $invitees end end] ", "] | |
1209 | append who " and [lindex $invitees [llength $invitees]]" | |
1210 | } | |
1211 | } | |
1212 | ucmdr {} "invites $who to $target." | |
1213 | } | |
1214 | ||
20087363 IJ |
1215 | def_ucmd channel { |
1216 | if {[ischan $dest]} { set target $dest } | |
1217 | if {![ta_anymore]} { | |
1218 | set subcmd show | |
1219 | } else { | |
1220 | set subcmd [ta_word] | |
1221 | } | |
1222 | if {[ischan $subcmd]} { | |
1223 | set target $subcmd | |
1224 | if {![ta_anymore]} { | |
1225 | set subcmd show | |
1226 | } else { | |
1227 | set subcmd [ta_word] | |
1228 | } | |
1229 | } | |
1230 | if {![info exists target]} { error "privately, you must specify a channel" } | |
1231 | set procname channel/$subcmd | |
1232 | if {"$subcmd" != "show"} { | |
1233 | if {[catch { info body $procname }]} { error "unknown channel setting $subcmd" } | |
1234 | prefix_nick | |
1235 | nick_securitycheck 1 | |
1236 | if {[chandb_exists $target]} { | |
1237 | channel_securitycheck $target $n | |
1238 | } else { | |
1239 | upvar #0 chan_initialop([irctolower $target]) io | |
534e26a9 | 1240 | upvar #0 nick_unique([irctolower $n]) u |
20087363 IJ |
1241 | if {![info exists io]} { error "$target is not a managed channel" } |
1242 | if {"$io" != "$u"} { error "you are not the interim manager of $target" } | |
1243 | if {"$subcmd" != "manager"} { error "use `channel manager' first" } | |
1244 | } | |
1245 | } | |
1246 | channel/$subcmd | |
1247 | } | |
1248 | ||
a4a1f396 IJ |
1249 | def_ucmd who { |
1250 | if {[ta_anymore]} { | |
1251 | set target [ta_word]; ta_nomore | |
1252 | set myself 1 | |
1253 | } else { | |
1254 | prefix_nick | |
1255 | set target $n | |
1256 | set myself [expr {"$target" != "$n"}] | |
1257 | } | |
534e26a9 IJ |
1258 | set ltarget [irctolower $target] |
1259 | upvar #0 nick_case($ltarget) ctarget | |
a4a1f396 | 1260 | set nshow $target |
534e26a9 IJ |
1261 | if {[info exists ctarget]} { |
1262 | upvar #0 nick_onchans($ltarget) oc | |
1263 | upvar #0 nick_username($ltarget) nu | |
1264 | if {[info exists oc]} { set nshow $ctarget } | |
a4a1f396 | 1265 | } |
534e26a9 | 1266 | if {![nickdb_exists $ltarget]} { |
a4a1f396 | 1267 | set ol "$nshow is not a registered nick." |
20087363 | 1268 | } elseif {[string length [set username [nickdb_get $target username]]]} { |
a4a1f396 IJ |
1269 | set ol "The nick $nshow belongs to the user $username." |
1270 | } else { | |
1271 | set ol "The nick $nshow is registered (but not to a username)." | |
1272 | } | |
534e26a9 | 1273 | if {![info exists ctarget] || ![info exists oc]} { |
a4a1f396 IJ |
1274 | if {$myself} { |
1275 | append ol "\nI can't see $nshow on anywhere." | |
1276 | } else { | |
1277 | append ol "\nYou aren't on any channels with me." | |
1278 | } | |
1279 | } elseif {![info exists nu]} { | |
1280 | append ol "\n$nshow has not identified themselves." | |
1281 | } elseif {![info exists username]} { | |
1282 | append ol "\n$nshow has identified themselves as the user $nu." | |
1283 | } elseif {"$nu" != "$username"} { | |
1284 | append ol "\nHowever, $nshow is being used by the user $nu." | |
1285 | } else { | |
1286 | append ol "\n$nshow has identified themselves to me." | |
1287 | } | |
1288 | ucmdr {} $ol | |
1289 | } | |
1290 | ||
7a70431a IJ |
1291 | def_ucmd register { |
1292 | prefix_nick | |
1293 | check_notonchan | |
1294 | set old [nickdb_exists $n] | |
1295 | if {$old} { nick_securitycheck 0 } | |
534e26a9 | 1296 | set luser [irctolower $n] |
7a70431a IJ |
1297 | switch -exact [string tolower [string trim $text]] { |
1298 | {} { | |
534e26a9 | 1299 | upvar #0 nick_username($luser) nu |
7a70431a IJ |
1300 | if {![info exists nu]} { |
1301 | ucmdr {} \ | |
a4a1f396 | 1302 | "You must identify yourself before using `register'. See `help identify', or use `register insecure'." |
7a70431a IJ |
1303 | } |
1304 | nickdb_set $n username $nu | |
1305 | ucmdr {} {} "makes a note of your username." {} | |
1306 | } | |
1307 | delete { | |
1308 | nickdb_delete $n | |
1309 | ucmdr {} {} "forgets your nickname." {} | |
1310 | } | |
1311 | insecure { | |
1312 | nickdb_set $n username {} | |
1313 | if {$old} { | |
1314 | ucmdr {} "Security is now disabled for your nickname !" | |
1315 | } else { | |
1316 | ucmdr {} "This is fine, but bear in mind that people will be able to mess with your settings. Channel management features need a secure registration." "makes an insecure registration for your nick." | |
1317 | } | |
1318 | } | |
1319 | } | |
11d9bff9 IJ |
1320 | } |
1321 | ||
1322 | proc timeformat_desc {tf} { | |
1323 | switch -exact $tf { | |
86892128 | 1324 | ks { return "Times will be displayed in seconds or kiloseconds." } |
11d9bff9 IJ |
1325 | hms { return "Times will be displayed in hours, minutes, etc." } |
1326 | default { error "invalid timeformat: $v" } | |
1327 | } | |
1328 | } | |
1329 | ||
1330 | proc def_setting {opt show_body set_body} { | |
1331 | proc set_show/$opt {} " | |
1332 | upvar 1 n n | |
1333 | set opt $opt | |
1334 | $show_body" | |
1335 | if {![string length $set_body]} return | |
1336 | proc set_set/$opt {} " | |
1337 | upvar 1 n n | |
1338 | upvar 1 text text | |
1339 | set opt $opt | |
1340 | $set_body" | |
1341 | } | |
1342 | ||
1343 | def_setting timeformat { | |
20087363 | 1344 | set tf [nickdb_get $n timeformat] |
11d9bff9 IJ |
1345 | return "$tf: [timeformat_desc $tf]" |
1346 | } { | |
1347 | set tf [string tolower [ta_word]] | |
1348 | ta_nomore | |
1349 | set desc [timeformat_desc $tf] | |
1350 | nickdb_set $n timeformat $tf | |
1351 | ucmdr {} $desc | |
1352 | } | |
1353 | ||
1354 | def_setting security { | |
20087363 | 1355 | set s [nickdb_get $n username] |
11d9bff9 IJ |
1356 | if {[string length $s]} { |
1357 | return "Your nick, $n, is controlled by the user $s." | |
1358 | } else { | |
1359 | return "Your nick, $n, is not secure." | |
1360 | } | |
1361 | } {} | |
1362 | ||
1363 | def_ucmd set { | |
1364 | prefix_nick | |
1365 | check_notonchan | |
1366 | if {![nickdb_exists $n]} { | |
d83fb8db | 1367 | ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)" |
11d9bff9 IJ |
1368 | } |
1369 | if {![ta_anymore]} { | |
1370 | set ol {} | |
1371 | foreach proc [lsort [info procs]] { | |
1372 | if {![regexp {^set_show/(.*)$} $proc dummy opt]} continue | |
1373 | lappend ol [format "%-10s %s" $opt [set_show/$opt]] | |
1374 | } | |
1375 | ucmdr {} [join $ol "\n"] | |
1376 | } else { | |
1377 | set opt [ta_word] | |
1378 | if {[catch { info body set_show/$opt }]} { | |
1379 | error "no setting $opt" | |
1380 | } | |
1381 | if {![ta_anymore]} { | |
1382 | ucmdr {} "$opt [set_show/$opt]" | |
1383 | } else { | |
86892128 | 1384 | nick_securitycheck 0 |
11d9bff9 IJ |
1385 | if {[catch { info body set_set/$opt }]} { |
1386 | error "setting $opt cannot be set with `set'" | |
1387 | } | |
1388 | set_set/$opt | |
1389 | } | |
1390 | } | |
1391 | } | |
7a70431a | 1392 | |
4fd2739c IJ |
1393 | def_ucmd identpass { |
1394 | set username [ta_word] | |
d83fb8db | 1395 | set passmd5 [md5sum "[ta_word]\n"] |
4fd2739c IJ |
1396 | ta_nomore |
1397 | prefix_nick | |
11d9bff9 | 1398 | check_notonchan |
534e26a9 IJ |
1399 | set luser [irctolower $n] |
1400 | upvar #0 nick_onchans($luser) onchans | |
4fd2739c | 1401 | if {![info exists onchans] || ![llength $onchans]} { |
7a70431a | 1402 | ucmdr "You must be on a channel with me to identify yourself." {} |
4fd2739c IJ |
1403 | } |
1404 | check_username $username | |
1405 | exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \ | |
1406 | irc-identpass $n | |
534e26a9 | 1407 | upvar #0 nick_username($luser) rec_username |
4fd2739c | 1408 | set rec_username $username |
7a70431a | 1409 | ucmdr "Pleased to see you, $username." {} |
4fd2739c IJ |
1410 | } |
1411 | ||
1412 | def_ucmd summon { | |
1413 | set target [ta_word] | |
1414 | ta_nomore | |
1415 | check_username $target | |
7ce72032 IJ |
1416 | prefix_nick |
1417 | ||
1418 | upvar #0 lastsummon($target) ls | |
1419 | set now [clock seconds] | |
1420 | if {[info exists ls]} { | |
1421 | set interval [expr {$now - $ls}] | |
1422 | if {$interval < 30} { | |
1423 | ucmdr {} \ | |
1424 | "Please be patient; $target was summoned only [showinterval $interval]." | |
1425 | } | |
1426 | } | |
1427 | regsub {^[^!]*!} $p {} path | |
1428 | if {[catch { | |
1429 | exec userv --timeout 3 $target irc-summon $n $path \ | |
1430 | [expr {[ischan $dest] ? "$dest" : ""}] \ | |
1431 | < /dev/null | |
1432 | } rv]} { | |
1433 | regsub -all "\n" $rv { / } rv | |
1434 | error $rv | |
1435 | } | |
1436 | if {[regexp {^problem (.*)} $rv dummy problem]} { | |
8a8d337d | 1437 | ucmdr {} "The user `$target' $problem." |
7ce72032 IJ |
1438 | } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} { |
1439 | set idletime [expr {$now - $idlesince}] | |
1440 | set ls $now | |
1441 | ucmdr {} {} {} "invites $target ($tty[expr { | |
1442 | $idletime > 10 ? ", idle for [showintervalsecs $idletime]" : "" | |
1443 | }]) to [expr { | |
1444 | [ischan $dest] ? "join us here" : "talk to you" | |
1445 | }]." | |
1446 | } else { | |
1447 | error "unexpected response from userv service: $rv" | |
1448 | } | |
1449 | } | |
1450 | ||
a69f7d2c IJ |
1451 | proc md5sum {value} { exec md5sum << $value } |
1452 | ||
83dd1224 | 1453 | def_ucmd seen { |
422f52e4 | 1454 | global lastseen nick |
83dd1224 IJ |
1455 | prefix_nick |
1456 | set ncase [ta_nick] | |
1457 | set nlower [irctolower $ncase] | |
422f52e4 | 1458 | ta_nomore |
83dd1224 IJ |
1459 | set now [clock seconds] |
1460 | if {"$nlower" == "[irctolower $nick]"} { | |
422f52e4 | 1461 | error "I am not self-aware." |
83dd1224 IJ |
1462 | } elseif {![info exists lastseen($nlower)]} { |
1463 | set rstr "I've never seen $ncase." | |
422f52e4 | 1464 | } else { |
83dd1224 IJ |
1465 | manyset $lastseen($nlower) realnick time what |
1466 | set howlong [expr {$now - $time}] | |
1467 | set string [showinterval $howlong] | |
1468 | set rstr "I last saw $realnick $string, $what." | |
1469 | } | |
1470 | if {[ischan $dest]} { | |
1471 | set where $dest | |
1472 | } else { | |
1473 | set where {} | |
1474 | } | |
1475 | upvar #0 lookedfor($nlower) lf | |
1476 | if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} } | |
1477 | set lf [list [list $now $n $where]] | |
1478 | foreach v $oldvalue { | |
1479 | if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue | |
1480 | lappend lf $v | |
cc2d31de | 1481 | } |
83dd1224 | 1482 | ucmdr {} $rstr |
cc2d31de IJ |
1483 | } |
1484 | ||
28c8ab78 IJ |
1485 | proc ensure_globalsecret {} { |
1486 | global globalsecret | |
1487 | ||
1488 | if {[info exists globalsecret]} return | |
1489 | set gsfile [open /dev/urandom r] | |
1490 | fconfigure $gsfile -translation binary | |
1491 | set globalsecret [read $gsfile 32] | |
1492 | binary scan $globalsecret H* globalsecret | |
1493 | close $gsfile | |
1494 | unset gsfile | |
1495 | } | |
1496 | ||
1497 | proc ensure_outqueue {} { | |
1498 | out__vars | |
1499 | if {[info exists out_queue]} return | |
8bf6ec5b | 1500 | set out_creditms 0 |
28c8ab78 IJ |
1501 | set out_creditat [clock seconds] |
1502 | set out_queue {} | |
1503 | set out_lag_reported 0 | |
1504 | set out_lag_reportwhen $out_creditat | |
1505 | } | |
1506 | ||
1507 | proc fail {msg} { | |
1508 | logerror "failing: $msg" | |
1509 | exit 1 | |
1510 | } | |
1511 | ||
1512 | proc ensure_connecting {} { | |
1513 | global sock ownfullname host port nick | |
1514 | global musthaveping_ms musthaveping_after | |
1515 | ||
1516 | if {[info exists sock]} return | |
cc2d31de IJ |
1517 | set sock [socket $host $port] |
1518 | fconfigure $sock -buffering line | |
cc2d31de IJ |
1519 | fconfigure $sock -translation crlf |
1520 | ||
b3d361ab | 1521 | sendout USER blight 0 * $ownfullname |
cc2d31de IJ |
1522 | sendout NICK $nick |
1523 | fileevent $sock readable onread | |
28c8ab78 IJ |
1524 | |
1525 | set musthaveping_after [after $musthaveping_ms \ | |
1526 | {fail "no ping within timeout"}] | |
cc2d31de IJ |
1527 | } |
1528 | ||
534e26a9 IJ |
1529 | proc connected {} { |
1530 | global musthaveping_after | |
1531 | ||
1532 | after cancel $musthaveping_after | |
1533 | unset musthaveping_after | |
1534 | ||
1535 | foreach chan [chandb_list] { | |
1536 | if {[chandb_get $chan autojoin]} { dojoin $chan } | |
1537 | } | |
1538 | } | |
1539 | ||
28c8ab78 IJ |
1540 | ensure_globalsecret |
1541 | ensure_outqueue | |
e6cc22dc | 1542 | loadhelp |
28c8ab78 | 1543 | ensure_connecting |