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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
|
# pixseen.tcl --
#
# SQLite powered seen script. Keeps track of everyone, based on nickname.
#
# Copyright (c) 2010, Rickard Utgren <rutgren@gmail.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# RCS: $Id$
#
# v1.1 by Pixelz - unreleased
# - Fixed a problem with ValidTable always failing on some older SQLite versions
# - Fixed a problem with the public trigger never showing the syntax help
# - Minor fixes
# - Fixed a problem with glob & regex matching where only the oldest matches would ever be returned
# - Added a setting to change the maximum number of results returned from a query
# - Fixed a problem with Hand2Uhost never returning anything useful
#
# v1.0 by Pixelz - April 5, 2010
# - Initial release
# ToDo:
# - add some kind of botnet synching
# - auto-discover pixseen bots on link
# - auto-assign a primary bot for each channel that will answer public requests
# * Possible different routes for inter-bot information sharing:
# * "Synch everything" route:
# - synch databases on link
# - keep synching across the net whenever there's a change.
# - probably do this blindly, to account for IRCDs that doesn't show all joins/parts (ircu)
# - perhaps add some logic to this, so bots dont push info to other bots that are in the same channel (and would see it anyway), at least on non-ircu, non-chanmode-D channels
# * "Ask botnet" route:
# - Don't synch databases
# - Ask other bots for information on each seen request to the bot
# * Considerations:
# * For both of these methods, the clock probably has to fairly synched up, particularly for the "synch everything" route
# - A simple solution: check TS delta on link, and complain loudly if it's too high, or compensate for it somehow
# - Add IRCU support ("hidden" users in +D channels) -> names -d #chan (who #chan cd)
# - OR just tell quakenet users to use thommey's +D patch
# - add option to track every channel separately
# - track channel idle time, this would probably synergize well with the "track each channel separetly" option
# - find out if it's a good idea to [catch] each sql query (catch is slow, is there a better alternative?)
# - test the idx lookup stuff more, I suspect there's a bug in it somewhere
# - perhaps get rid of the daily unused-channels cleanup, and do it the same way as pixinfo.tcl.
# - Make sure this isn't hugely resource intensive first.
# - Add a setting to set the default matching type? OR Apply better logic to it? perhaps assume glob if it contains asterisk, Most Users fail at regex anyway...
package require Tcl 8.5
package require msgcat 1.4.2
package require eggdrop 1.6
package require sqlite3 3.3.0;# order by desc was added in this version
namespace eval ::pixseen {
# path to the database file
variable dbfile {scripts/pixseen.db}
# Output with NOTICE nick (0) or PRIVMSG #chan (1)
variable outnotc 1
# Language
variable defaultLang "en"
# Maximum number of results to display in public
variable pubResults 3
# Maximum number of results to display in private message
variable msgResults 5
# Maximum number of results to display in the partyline
variable dccResults 10
## end of settings ##
# list of locales, if you translate the script, add your translation to this list
variable locales [list "en"]
namespace import ::msgcat::*
# mcload fails to load _all_ .msg files, so we have to do it manually
foreach f [glob -nocomplain -directory [file join [file dirname [info script]] pixseen-msgs] -type {b c f l} *.msg] {
source -encoding {utf-8} $f
}
unset -nocomplain f
mclocale $defaultLang
setudef flag {seen}
setudef str {seenlang}
variable ::botnick
variable ::botnet-nick
variable ::nicklen
variable seenFlood
variable seenver {1.1}
variable dbVersion 1
}
## utility procs
proc ::pixseen::validlang {lang} {
variable locales
if {[lsearch -exact -nocase $locales $lang] == -1} {
return 0
} else {
return 1
}
}
# msgcat compatible duration proc
proc ::pixseen::pixduration {seconds} {
set map [list \
{years} [mc {years}] \
{year} [mc {year}] \
{months} [mc {months}] \
{month} [mc {month}] \
{weeks} [mc {weeks}] \
{week} [mc {week}] \
{days} [mc {days}] \
{day} [mc {day}] \
{hours} [mc {hours}] \
{hour} [mc {hour}] \
{minutes} [mc {minutes}] \
{minute} [mc {minute}] \
{seconds} [mc {seconds}] \
{second} [mc {second}] \
]
string map $map [duration $seconds]
}
# takes an idx and returns the user@host associated with it.
# ONLY to be called from finduhost. DON'T CALL THIS DIRECTLY!
proc ::pixseen::Idx2Uhost {idx} {
# for some mind-boggling reason, eggdrop doesn't provide the uhost for a
# lot of the partyline binds, so we extract it from dcclist
foreach item [dcclist chat] {
lassign $item i - u;
if {$idx eq $i} {
return $u
}
}
# this should never happen?
putlog [mc {%1$s error; %2$s was unable to extract uhost. PLEASE REPORT THIS BUG!} {pixseen.tcl} {::pixseen::Idx2Uhost}]
return
}
# takes a handle and returns the user@host associated with it.
# ONLY to be called from finduhost. DON'T CALL THIS DIRECTLY!
proc ::pixseen::Hand2Uhost {botname hand {chan {*}}} {
foreach item [whom $chan] {
lassign $item Hand Botname Uhost Flags Idle Away
if {[string equal -nocase $botname $Botname] && [string equal -nocase $hand $Hand]} {
return $Uhost
}
}
# this should never happen?
putlog [mc {%1$s error; %2$s was unable to extract uhost. PLEASE REPORT THIS BUG!} {pixseen.tcl} {::pixseen::Hand2Uhost}]
return
}
# figures out the uhost using different methods for different binds
# returns the uhost found, or "" if it can't find one.
proc ::pixseen::finduhost {bind args} {
# chjn isn't listed because that bind actually calls the proc with the uhost
# chon/chof is NOT triggered for remote users
# away/chpt (and chjn) IS triggered for remote users
switch -exact -- $bind {
{chon} - {chof} {
lassign $args idx
return [Idx2Uhost $idx]
}
{chpt} {
lassign $args idx botname hand chan
if {[string equal -nocase $botname ${::botnet-nick}]} {
# this is a local user, grab the uhost based on the idx
return [Idx2Uhost $idx]
} else {
# this is a remote user, make an educated guess
return [Hand2Uhost $botname $hand $chan]
}
}
{away} {
lassign $args idx botname
if {[string equal -nocase $botname ${::botnet-nick}]} {
# this is a local user, grab the uhost based on the idx
return [Idx2Uhost $idx]
} else {
# this is a remote user. it is NOT POSSIBLE to get their uhost
return
}
}
default {
return
}
}
}
# returns 1 if a module is loaded, 0 if not
proc ::pixseen::modloaded {module} {
if {[lsearch -exact -index 0 [modules] $module] == -1} {
return 0
} else {
return 1
}
}
# returns the name of a partyline channel, or whatever was passed to it
# if there's no name set
proc ::pixseen::partychanname {chan} {
if {(![modloaded assoc]) || ($chan == 0)} {
return $chan
} elseif {![catch {set name [assoc $chan]}]} {
if {$name ne {}} {
return $name
} else {
return $chan
}
} else {
return $chan
}
}
# returns 1 if the nick is valid on IRC, 0 if not
proc ::pixseen::validnick {nick} {
if {([info exists ::nicklen]) && ($::nicklen > 32)} {
set len $::nicklen
} else {
set len 32
}
if {[string length $nick] > $len} {
return 0
# FixMe: make sure these are all of the valid chars
} elseif {![regexp -- {^[a-zA-Z\|\[\]`^\{\}][a-zA-Z0-9\-_\|\[\]`^\{\}\\]*$} $nick]} {
return 0
} else {
return 1
}
}
# returns 1 if a channel is set +secret, 0 if not
proc ::pixseen::issecret {chan} {
if {[validchan $chan] && [channel get $chan secret]} {
return 1
} else {
return 0
}
}
proc ::pixseen::handseen {handle} {
if {![validuser $handle]} {
return
} else {
lassign [getuser $handle LASTON] timestamp location
# the location can be fun things like "linked" or "@botnick" so let's not even go there...
return [mc {%1$s was last seen %2$s ago.} $handle [pixduration [expr {[clock seconds] - $timestamp}]]]
}
}
proc ::pixseen::lastspoke {nick chan} {
if {[set idle [getchanidle $nick $chan]] == 0} {
return
} else {
# whose great idea was it to return idle time in minutes? o_O
return [pixduration [expr {$idle * 60}]]
}
}
# returns the channel if the user is logged in to the partyline. if not, returns ""
proc ::pixseen::onpartyline {handle} {
foreach item [whom *] {
lassign $item nick bot uhost flags idle away chan
if {[string equal -nocase $handle $nick]} { return [partychanname $chan] }
}
return
}
# checks the flood array and removes old timestamps.
# will eventually remove itself if we're not being flooded
proc ::pixseen::RemoveFlood {args} {
variable seenFlood
if {![array exists seenFlood]} { return }
set time [clock seconds]
foreach uhost [array names seenFlood] {
foreach timestamp $seenFlood($uhost) {
# don't append the timestamp if it's older than 60 seconds
if {[expr {$time - 60}] <= $timestamp} {
lappend stampList $timestamp
}
}
if {[info exists stampList]} {
set seenFlood($uhost) $stampList
} else {
array unset seenFlood $uhost
}
}
}
# returns 1 if we're flooded, 0 if not
proc ::pixseen::checkflood {uhost} {
variable seenFlood
RemoveFlood
# case 1: uhost doesn't exist in the array, we're not being flooded
if {![info exists seenFlood($uhost)]} {
set seenFlood($uhost) [clock seconds]
return 0
# case 2: the list for this uhost is full, so we're being flooded with 6 lines over 60 seconds
} elseif {[llength $seenFlood($uhost)] >= 6} {
set seenFlood($uhost) [concat "[lrange [lsort -integer $seenFlood($uhost)] 1 end] [clock seconds]"]
return 1
# case 3: the list for this uhost isn't full, we're not being flooded
} else {
lappend seenFlood($uhost) [clock seconds]
return 0
}
}
# Formats seen events for output
proc ::pixseen::formatevent {event nick uhost time chan reason othernick} {
set duration [pixduration [expr {[clock seconds] - $time}]]
set timestamp [clock format $time -format "%Y-%m-%dT%H:%M:%SZ" -gmt 1]
switch -exact -- $event {
{0} {;# part
if {$reason eq {}} {
if {[issecret $chan]} {
return [mc {%1$s (%2$s) was last seen parting a channel %3$s ago (%4$s)} $nick $uhost $duration $timestamp]
} else {
return [mc {%1$s (%2$s) was last seen parting %3$s %4$s ago (%4$s)} $nick $uhost $chan $duration $timestamp]
}
} else {
if {[issecret $chan]} {
return [mc {%1$s (%2$s) was last seen parting a channel %3$s ago (%4$s), stating "%5$s"} $nick $uhost $duration $timestamp $reason]
} else {
return [mc {%1$s (%2$s) was last seen parting %3$s %4$s ago (%4$s), stating "%5$s"} $nick $uhost $chan $duration $timestamp $reason]
}
}
}
{1} {;# join
if {[issecret $chan]} {
return [mc {%1$s (%2$s) was last seen joining a channel %3$s ago (%4$s)} $nick $uhost $duration $timestamp]
} else {
if {[onchan $nick $chan]} {
return [mc {%1$s (%2$s) was last seen joining %3$s %4$s ago (%5$s). %1$s is still on %3$s.} $nick $uhost $chan $duration $timestamp]
} else {
return [mc {%1$s (%2$s) was last seen joining %3$s %4$s ago (%5$s). I don't see %1$s on %3$s now, though.} $nick $uhost $chan $duration $timestamp]
}
}
}
{2} {;# nick (old)
if {[issecret $chan]} {
return [mc {%1$s (%2$s) was last seen changing nicks to %5$s on a channel %3$s ago (%4$s).} $nick $uhost $duration $timestamp $othernick]
} else {
return [mc {%1$s (%2$s) was last seen changing nicks to %6$s on %3$s %4$s ago (%5%s).} $nick $uhost $chan $duration $timestamp $othernick]
}
}
{3} {;# nick (new)
if {[issecret $chan]} {
return [mc {%1$s (%2$s) was last seen changing nicks from %5$s on a channel %3$s ago (%4$s)} $nick $uhost $duration $timestamp $othernick]
} else {
if {[onchan $nick $chan]} {
return [mc {%1$s (%2$s) was last seen changing nicks from %6$s on %3$s %4$s ago (%5$s). %1$s is still on %3$s.} $nick $uhost $chan $duration $timestamp $othernick]
} else {
return [mc {%1$s (%2$s) was last seen changing nicks from %6$s on %3$s %4$s ago (%5$s). I don't see %1$s on %3$s now, though.} $nick $uhost $chan $duration $timestamp $othernick]
}
}
}
{4} {;# sign (quit)
if {[issecret $chan]} {
return [mc {%1$s (%2$s) was last seen quitting from a channel %3$s ago (%4$s)} $nick $uhost $duration $timestamp]
} elseif {$reason eq {}} {
return [mc {%1$s (%2$s) was last seen quitting from %3$s %4$s ago (%5$s)} $nick $uhost $chan $duration $timestamp]
} else {
return [mc {%1$s (%2$s) was last seen quitting from %3$s %4$s ago (%5$s), stating "%6$s"} $nick $uhost $chan $duration $timestamp $reason]
}
}
{5} {;# splt (netsplit)
if {[issecret $chan]} {
return [mc {%1$s (%2$s) was last seen parting a channel due to a netsplit %3$s ago.} $nick $uhost $duration]
} else {
return [mc {%1$s (%2$s) was last seen parting %3$s due to a netsplit %4$s ago.} $nick $uhost $chan $duration]
}
}
{6} {;# rejn (netsplit rejoin)
if {[issecret $chan]} {
return [mc {%1$s (%2$s) was last seen rejoining a channel from a netsplit %3$s ago (%4$s)} $nick $uhost $duration $timestamp]
} else {
if {[onchan $nick $chan]} {
return [mc {%1$s (%2$s) was last seen rejoining %3$s from a netsplit %4$s ago (%5$s). %1$s is still on %3$s.} $nick $uhost $chan $duration $timestamp]
} else {
return [mc {%1$s (%2$s) was last seen rejoining %3$s from a netsplit %4$s ago (%5$s). I don't see %1$s on %3$s now, though.} $nick $uhost $chan $duration $timestamp]
}
}
}
{7} {;# kick
if {[issecret $chan]} {
return [mc {%1$s (%2$s) was last seen kicked from a channel %3$s ago (%4$s)} $nick $uhost $duration $timestamp]
} else {
return [mc {%1$s (%2$s) was last seen kicked from %3$s by %4$s %5$s ago (%6$s), with the reason "%7$s"} $nick $uhost $chan $othernick $duration $timestamp $reason]
}
}
{8} {;# chon (enter partyline)
if {[onpartyline $nick] ne {}} {
return [mc {%1$s (%2$s) was last seen entering the partyline %3$s ago (%4$s). %1$s is on the partyline right now.} $nick $uhost $duration $timestamp]
} else {
return [mc {%1$s (%2$s) was last seen entering the partyline %3$s ago (%4$s). I don't see %1$s on the partyline now, though.} $nick $uhost $duration $timestamp]
}
}
{9} {;# chof (leaves partyline)
if {[set pchan [onpartyline $nick]] ne {}} {
return [mc {%1$s (%2$s) was last seen leaving the partyline %3$s ago (%4$s). %1$s is on the partyline channel %4$s still.} $nick $uhost $duration $pchan $timestamp]
} else {
return [mc {%1$s (%2$s) was last seen leaving the partyline %3$s ago (%4$s)} $nick $uhost $duration $timestamp]
}
}
{10} {;# chjn (joins partyline channel)
if {[onpartyline $nick] ne {}} {
return [mc {%1$s (%2$s) was last seen entering the partyline on %3$s %4$s ago (%5$s). %1 is on the partyline right now.} $nick $uhost [partychanname $chan] $duration $timestamp]
} else {
return [mc {%1$s (%2$s) was last seen entering the partyline on %3$s %4$s ago (%5$s). I don't see %1$s on the partyline now, though.} $nick $uhost [partychanname $chan] $duration $timestamp]
}
}
{11} {;# chpt (parts partyline channel)
if {[set pchan [onpartyline $nick]] ne {}} {
return [mc {%1$s (%2$s) was last seen leaving the partyline from %3$s %4$s ago (%5$s). %1$s is on the partyline channel %6$s still.} $nick $uhost [partychanname $chan] $duration $timestamp $pchan]
} else {
return [mc {%1$s (%2$s) was last seen leaving the partyline from %3$s %4$s ago (%5$s)} $nick $uhost [partychanname $chan] $duration $timestamp]
}
}
{12} {;# away (partyline away)
if {[onpartyline $nick] ne {}} {
return [mc {%1$s was last seen marked as away (%2$s) on the partyline %3$s ago (%4$s). %1$s is on the partyline right now.} $nick $reason $duration $timestamp]
} else {
return [mc {%1$s was last seen marked as away (%2$s) on the partyline %3$s ago (%4$s). I don't see %1$s on the partyline now, though.} $nick $reason $duration $timestamp]
}
}
{13} {;# back (partyline back from away)
if {[onpartyline $nick] ne {}} {
return [mc {%1$s was last seen returning to the partyline %2$s ago (%3$s). %1$s is on the partyline right now.} $nick $duration $timestamp]
} else {
return [mc {%1$s was last seen returning to the partyline %2$s ago (%3$s). I don't see %1$s on the partyline now, though.} $nick $duration $timestamp]
}
}
{14} {;# chmsg (messaged channel)
if {[issecret $chan]} {
return [mc {%1$s (%2$s) was last seen talking on a channel %3$s ago (%4$s)} $nick $uhost $duration $timestamp]
} else {
if {[onchan $nick $chan]} {
return [mc {%1$s (%2$s) was last seen messaging on %3$s %4$s ago (%5$s). %1$s is still on %3$s.} $nick $uhost $chan $duration $timestamp]
} else {
return [mc {%1$s (%2$s) was last seen messaging on %3$s %4$s ago (%5$s). I don't see %1$s on %3$s now, though.} $nick $uhost $chan $duration $timestamp]
}
}
}
default {
putlog [mc {%1$s error; Unhandled event in %2$s: %3$s} {pixseen.tcl} {formatevent} $event]
return [mc {I don't remember seeing %s.} $nick]
}
}
}
## SQLite functions
# This is the SQLite collation function, if it's changed, the index has to be rebuilt with REINDEX or it'll cause Weird Behaviour
proc ::pixseen::rfccomp {a b} {
string compare [string map [list \{ \[ \} \] ~ ^ | \\] [string toupper $a]] [string map [list \{ \[ \} \] ~ ^ | \\] [string toupper $b]]
}
proc ::pixseen::chan2id {chan} {
if {[catch {seendb eval { INSERT OR IGNORE INTO chanTb VALUES(NULL, $chan); }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
if {[catch {set retval [seendb eval { SELECT chanid FROM chanTb WHERE chan=$chan LIMIT 1 }]} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
return -code error [mc {SQL error %1$s; %2$s} [seendb errorcode] $error]
} else {
return $retval
}
}
# SQLite regexp function, squelches regex errors and turn on nocase
proc ::pixseen::pixregexp {args} {
if {[catch {set result [regexp -nocase -- {*}$args]}]} {
return 0
} else {
return $result
}
}
##
proc ::pixseen::dbAdd {nick event timestamp uhost args} {
switch -exact -- $event {
{part} {;# 0
lassign $args chan reason
if {[catch {seendb eval { INSERT OR REPLACE INTO seenTb VALUES(0, $nick, $uhost, $timestamp, chan2id($chan), $reason, NULL) }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
{join} {;# 1
lassign $args chan
if {[catch {seendb eval { INSERT OR REPLACE INTO seenTb VALUES(1, $nick, $uhost, $timestamp, chan2id($chan), NULL, NULL) }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
{nick} {;# 2 & 3
lassign $args chan newnick
if {[catch {seendb eval {
-- old nick;
INSERT OR REPLACE INTO seenTb VALUES(2, $nick, $uhost, $timestamp, chan2id($chan), NULL, $newnick);
-- new nick;
INSERT OR REPLACE INTO seenTb VALUES(3, $newnick, $uhost, $timestamp, chan2id($chan), NULL, $nick);
}} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
{sign} {;# 4 (quit)
lassign $args chan reason
if {[catch {seendb eval { INSERT OR REPLACE INTO seenTb VALUES(4, $nick, $uhost, $timestamp, chan2id($chan), $reason, NULL) }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
{splt} {;# 5
lassign $args chan
if {[catch {seendb eval { INSERT OR REPLACE INTO seenTb VALUES(5, $nick, $uhost, $timestamp, chan2id($chan), NULL, NULL) }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
{rejn} {;# 6
lassign $args chan
if {[catch {seendb eval { INSERT OR REPLACE INTO seenTb VALUES(6, $nick, $uhost, $timestamp, chan2id($chan), NULL, NULL) }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
{kick} {;# 7
lassign $args chan reason aggressor
if {[catch {seendb eval { INSERT OR REPLACE INTO seenTb VALUES(7, $nick, $uhost, $timestamp, chan2id($chan), $reason, $aggressor) }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
{chon} {;# 8 (enters partyline)
if {[catch {seendb eval { INSERT OR REPLACE INTO seenTb VALUES(8, $nick, $uhost, $timestamp, NULL, NULL, NULL) }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
{chof} {;# 9 (leaves partyline)
if {[catch {seendb eval { INSERT OR REPLACE INTO seenTb VALUES(9, $nick, $uhost, $timestamp, NULL, NULL, NULL) }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
{chjn} {;# 10 (joins partyline channel)
lassign $args chan botname
if {[catch {seendb eval { INSERT OR REPLACE INTO seenTb VALUES(10, $nick, $uhost, $timestamp, chan2id($chan), NULL, $botname) }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
{chpt} {;# 11 (parts partyline channel)
lassign $args chan botname
if {[catch {seendb eval { INSERT OR REPLACE INTO seenTb VALUES(11, $nick, $uhost, $timestamp, chan2id($chan), NULL, $botname) }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
{away} {;# 12 (partyline away)
lassign $args botname reason
if {[catch {seendb eval { INSERT OR REPLACE INTO seenTb VALUES(12, $nick, $uhost, $timestamp, NULL, $reason, $botname) }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
{back} {;# 13 (partyline back from away)
lassign $args botname
if {[catch {seendb eval { INSERT OR REPLACE INTO seenTb VALUES(13, $nick, $uhost, $timestamp, NULL, NULL, $botname) }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
{chmsg} {;# 14
lassign $args chan
if {[catch {seendb eval { INSERT OR REPLACE INTO seenTb VALUES(14, $nick, $uhost, $timestamp, chan2id($chan), NULL, NULL) }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
default {
putlog [mc {%1$s error; Unhandled event in %2$s: %3$s} {pixseen.tcl} {dbAdd} $event]
return
}
}
return
}
## event binds
proc ::pixseen::PART {nick uhost hand chan msg} {
dbAdd $nick {part} [clock seconds] $uhost $chan $msg
return
}
proc ::pixseen::JOIN {nick uhost hand chan} {
dbAdd $nick {join} [clock seconds] $uhost $chan
return
}
proc ::pixseen::NICK {nick uhost hand chan newnick} {
dbAdd $nick {nick} [clock seconds] $uhost $chan $newnick
return
}
proc ::pixseen::SIGN {nick uhost hand chan reason} {
dbAdd $nick {sign} [clock seconds] $uhost $chan $reason
return
}
proc ::pixseen::SPLT {nick uhost hand chan} {
dbAdd $nick {splt} [clock seconds] $uhost $chan
return
}
proc ::pixseen::REJN {nick uhost hand chan} {
dbAdd $nick {rejn} [clock seconds] $uhost $chan
return
}
proc ::pixseen::KICK {nick uhost hand chan target reason} {
dbAdd $target {kick} [clock seconds] [getchanhost $target $chan] $chan $reason $nick
return
}
proc ::pixseen::CHON {hand idx} {
if {[set uhost [finduhost {chon} $idx]] ne {}} {
dbAdd $hand {chon} [clock seconds] $uhost
}
return
}
proc ::pixseen::CHOF {hand idx} {
if {[set uhost [finduhost {chof} $idx]] ne {}} {
dbAdd $hand {chon} [clock seconds] $uhost
}
return
}
proc ::pixseen::CHJN {botname hand chan flag idx uhost} {
dbAdd $hand {chon} [clock seconds] $uhost $chan $botname
return
}
proc ::pixseen::CHPT {botname hand idx chan} {
if {[set uhost [finduhost {chpt} $idx $botname $hand $chan]] ne {}} {
dbAdd $hand {chon} [clock seconds] $uhost $chan $botname
}
return
}
proc ::pixseen::AWAY {botname idx text} {
if {[string equal -nocase $botname ${::botnet-nick}]} {
# this is a local away
if {$text ne {}} {
# user is away
if {[set uhost [finduhost {away} $idx $botname]] ne {}} {
dbAdd [idx2hand $idx] {away} [clock seconds] $uhost $botname $text
}
} else {
# user has returned
if {[set uhost [idx2uhost {away} $idx $botname]] ne {}} {
dbAdd [idx2hand $idx] {back} [clock seconds] $uhost $botname
}
}
} else {
# this is a remote away. It's not possible to figure out the handle,
# let alone the uhost, so we bail out.
return
}
return
}
proc ::pixseen::CHMSG {nick uhost hand chan text} {
dbAdd $nick {chmsg} [clock seconds] $uhost $chan
return
}
##
# returns a list of: id event nick uhost time chan reason othernick
proc ::pixseen::dbGetNick {target} {
if {[catch {set result [seendb eval { SELECT event, nick, uhost, time, chanTb.chan, reason, othernick FROM seenTb, chanTb ON seenTb.chanid = chanTb.chanid WHERE nick=$target LIMIT 1 }]} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
return
}
return $result
}
# returns: a list of nicks matching the pattern
proc ::pixseen::dbSearchGlob {nick uhost chan} {
# transform GLOB syntax into LIKE syntax:
set nick [string map [list "\\" "\\\\" "%" "\%" "_" "\_" "*" "%" "?" "_"] $nick]
set uhost [string map [list "\\" "\\\\" "%" "\%" "_" "\_" "*" "%" "?" "_"] $uhost]
set chan [string map [list "\\" "\\\\" "%" "\%" "_" "\_" "*" "%" "?" "_"] $chan]
if {$nick eq {}} { set nick "*" }
if {$uhost eq {}} { set uhost "*" }
if {[catch { set result [seendb eval { SELECT nick FROM seenTb, chanTb ON seenTb.chanid = chanTb.chanid WHERE nick LIKE $nick ESCAPE '\' AND uhost LIKE $uhost ESCAPE '\' AND chanTb.chan LIKE $chan ESCAPE '\' ORDER BY seenTb.time DESC }] } error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
return
} else {
return $result
}
}
# returns: a list of nicks matching the pattern
proc ::pixseen::dbSearchRegex {nick uhost chan} {
if {[catch { set result [seendb eval { SELECT nick FROM seenTb, chanTb ON seenTb.chanid = chanTb.chanid WHERE nick REGEXP $nick AND uhost REGEXP $uhost AND chanTb.chan REGEXP $chan ORDER BY seenTb.time DESC }] } error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
return
} else {
return $result
}
}
# cleans out unused channels from the database
proc ::pixseen::dbCleanup {args} {
putlog [mc {%s: performing database maintenance...} {pixseen.tcl}]
if {[catch {set idList [seendb eval { SELECT chanid FROM chanTb WHERE chanid NOT IN (SELECT chanid FROM seenTb WHERE chanid = chanTb.chanid) }]} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
} elseif {$idList ne {}} {
foreach id $idList {
if {[catch {seendb eval { DELETE FROM chanTb WHERE chanid=$id }} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
}
}
return
}
# Parses command arguments.
# Returns: a list of "nick uhost chan mode".
# Mode 0 = exact matching (the default)
# Mode 1 = glob matching
# Mode 2 = regex matching
proc ::pixseen::ParseArgs {text} {
# !seen foobar
# !seen foobar.com
# !seen #foobar
# !seen foobar foobar.com
# !seen foobar #foobar
# !seen foobar.com #foobar
# !seen foobar.com foobar
# !seen #foobar foobar
# !seen #foobar foobar.com
# !seen foobar foobar.com #foobar
# default to exact mode
set mode 0
# grab the switches
set i 0
foreach item [set arg [split $text]] {
if {$item eq {--}} {
incr i
break
} elseif {[string index $item 0] eq {-}} {
switch -glob -nocase -- $item {
{-e*} { set mode 0 }
{-g*} { set mode 1 }
{-r*} { set mode 2 }
}
incr i
continue
} else {
break
}
}
if {([set arglen [llength [set arg [lrange $arg $i end]]]] < 1) || ($arglen > 3)} {
return
} elseif {$arglen == 3} {
lassign $arg nick uhost chan
} elseif {$mode == 2} {
lassign $arg nick uhost chan
if {$uhost eq {}} { set uhost {.*} }
if {$chan eq {}} { set chan {.*} }
} else {
set nick {*}
set uhost {*}
set chan {*}
set NickDone 0
set ChanDone 0
set UhostDone 0
foreach item $arg {
# nick
if {!$NickDone && [regexp -- {^[^#&!+.][^.]*$} $item]} {
set nick $item
set NickDone 1
continue
# channel
} elseif {!$ChanDone && [string match {[#&!+]*} $item]} {
set chan $item
set ChanDone 1
continue
# uhost
} elseif {!$UhostDone && [string match {*.*} $item]} {
set uhost $item
set UhostDone 1
continue
}
}
}
return [list $nick $uhost $chan $mode]
}
# output proc
proc ::pixseen::putseen {nick chan notcText {msgText {}}} {
variable outnotc
if {$outnotc == 0} {
puthelp "NOTICE $nick :\[seen\] $notcText"
} elseif {$msgText != {}} {
puthelp "NOTICE $chan :\[seen\] $msgText"
} else {
puthelp "NOTICE $chan :\[seen\] $notcText"
}
}
# Handle public !seen
# !seen [-exact/-glob/-regex] [--] <nick> [user@host] [channel]}
proc ::pixseen::pubm_seen {nick uhost hand chan text} {
variable defaultLang; variable pubResults
if {![info exists pubResults] || ![string is integer $pubResults]} { set pubResults {3} }
if {![channel get $chan {seen}]} {
return
} elseif {![matchattr $hand f|f $chan]} {
if {[checkflood $uhost] != 0} {
return
}
}
# Set the locale for this channel
if {[validlang [channel get $chan seenlang]]} { mclocale [channel get $chan seenlang] }
if {[set arg [ParseArgs [join [lrange [split $text] 1 end]]]] eq {}} {
putseen $nick $chan [mc {Usage: %s} {!seen [-exact/-glob/-regex] [--] <nick> [user@host] [channel]}] [mc {usage: %1$s} {!seen [-exact/-glob/-regex] <nick>}]
mclocale $defaultLang
return
} else {
lassign $arg Nick Uhost Chan Mode
}
if {[string equal -nocase $nick $Nick]} {
putseen $nick $chan [mc {Go look in a mirror.}] [mc {%s, go look in a mirror.} $nick]
mclocale $defaultLang
return
} elseif {[string equal -nocase ${::botnick} $Nick]} {
putseen $nick $chan [mc {You found me!}] [mc {You found me, %s!} $nick]
mclocale $defaultLang
return
# Tcldrop supports glob matching for onchan, so check if Nick is valid first
# } elseif {[validnick $Nick] && [onchan $Nick $chan]} {
# if {[set lastspoke [lastspoke $Nick $chan]] eq {}} {
# putseen $nick $chan [mc {%s is on the channel right now!} $Nick]
# } else {
# putseen $nick $chan [mc {%1$s is on the channel right now! %1$s last spoke %2$s ago.} $Nick $lastspoke]
# }
# mclocale $defaultLang
# return
# }
switch -exact -- $Mode {
{0} {;# exact
if {![validnick $Nick]} {
putseen $nick $chan [mc {That is not a valid nickname.}] [mc {%s, that is not a valid nickname.} $nick]
mclocale $defaultLang
return
} elseif {[set result [dbGetNick $Nick]] eq {}} {
if {[set handseen [handseen $Nick]] ne {}} {
putseen $nick $chan $handseen
mclocale $defaultLang
return 1
} else {
putseen $nick $chan [mc {I don't remember seeing %s.} $Nick]
mclocale $defaultLang
return
}
} else {
putseen $nick $chan [formatevent {*}$result]
mclocale $defaultLang
return 1
}
}
{1} {;# glob
set result [dbSearchGlob $Nick $Uhost $Chan]
}
{2} {;# regex
set result [dbSearchRegex $Nick $Uhost $Chan]
}
}
if {$result eq {}} {
if {[set handseen [handseen $Nick]] ne {}} {
putseen $nick $chan $handseen
mclocale $defaultLang
return 1
} else {
putseen $nick $chan [mc {There were no matches to your query.}]
mclocale $defaultLang
return
}
} else {
if {[set numMatches [llength $result]] > $pubResults} {
putseen $nick $chan [mc {Displaying %1$s of %2$s results:} $pubResults $numMatches]
} else {
putseen $nick $chan [mc {Displaying %1$s of %2$s results:} $numMatches $numMatches]
}
foreach match [lrange $result 0 [expr {$pubResults - 1}]] {
if {$match ne {}} {
putseen $nick $chan [formatevent {*}[dbGetNick $match]]
}
}
mclocale $defaultLang
return 1
}
mclocale $defaultLang
return
}
# Handle /msg botnick seen
proc ::pixseen::msgm_seen {nick uhost hand text} {
putlog "\[seen\] ${nick}!${uhost} (hand: ${hand}) requested seen in PM: $text"
variable msgResults
if {![info exists msgResults] || ![string is integer $msgResults]} { set msgResults {5} }
if {![matchattr $hand f]} {
if {[checkflood $uhost] != 0} {
return
}
}
if {[set arg [ParseArgs [join [lrange [split $text] 1 end]]]] eq {}} {
#puthelp "NOTICE $nick :[mc {Usage: %s} {seen [-exact/-glob/-regex] [--] <nick> [user@host] [channel]}]"
puthelp "NOTICE $nick :[mc {Usage: %s} {seen [-exact/-glob/-regex] [--] <nick>}]"
return
} else {
lassign $arg Nick Uhost Chan Mode
}
if {[string equal -nocase $nick $Nick]} {
puthelp "NOTICE $nick :[mc {Go look in a mirror.}]"
return
} elseif {[string equal -nocase ${::botnick} $Nick]} {
puthelp "NOTICE $nick :[mc {You found me!}]"
return
}
switch -exact -- $Mode {
{0} {;# exact
if {![validnick $Nick]} {
puthelp "NOTICE $nick :[mc {That is not a valid nickname.}]"
return
} elseif {[set result [dbGetNick $Nick]] eq {}} {
if {[set handseen [handseen $Nick]] ne {}} {
puthelp "NOTICE $nick :$handseen"
return 1
} else {
puthelp "NOTICE $nick :[mc {I don't remember seeing %s.} $Nick]"
return
}
} else {
puthelp "NOTICE $nick :[formatevent {*}$result]"
return 1
}
}
{1} {;# glob
set result [dbSearchGlob $Nick $Uhost $Chan]
}
{2} {;# regex
set result [dbSearchRegex $Nick $Uhost $Chan]
}
}
if {$result eq {}} {
if {[set handseen [handseen $Nick]] ne {}} {
puthelp "NOTICE $nick :$handseen"
return 1
} else {
puthelp "NOTICE $nick :[mc {There were no matches to your query.}]"
return
}
} else {
if {[set numMatches [llength $result]] > $msgResults} {
puthelp "NOTICE $nick :[mc {Displaying %1$s of %2$s results:} $msgResults $numMatches]"
} else {
puthelp "NOTICE $nick :[mc {Displaying %1$s of %2$s results:} $numMatches $numMatches]"
}
foreach match [lrange $result 0 [expr {$msgResults - 1}]] {
if {$match ne {}} {
puthelp "NOTICE $nick :[formatevent {*}[dbGetNick $match]]"
}
}
return 1
}
return
}
# Handle partyline .seen
proc ::pixseen::dcc_seen {hand idx text} {
variable dccResults
if {![info exists dccResults] || ![string is integer $dccResults]} { set dccResults {10} }
if {[set arg [ParseArgs $text]] eq {}} {
putdcc $idx [mc {Usage: %s} {.seen [-exact/-glob/-regex] [--] <nick> [user@host] [channel]}]
return
} else {
lassign $arg Nick Uhost Chan Mode
}
if {[string equal -nocase $hand $Nick]} {
putdcc $idx [mc {Go look in a mirror.}]
return
} elseif {[string equal -nocase ${::botnick} $Nick]} {
putdcc $idx [mc {You found me!}]
return
}
switch -exact -- $Mode {
{0} {;# exact
if {![validnick $Nick]} {
putdcc $idx [mc {That is not a valid nickname.}]
return
} elseif {[set result [dbGetNick $Nick]] eq {}} {
if {[set handseen [handseen $Nick]] ne {}} {
putdcc $idx $handseen
return 1
} else {
putdcc $idx [mc {I don't remember seeing %s.} $Nick]
return
}
} else {
putdcc $idx [formatevent {*}$result]
return 1
}
}
{1} {;# glob
set result [dbSearchGlob $Nick $Uhost $Chan]
}
{2} {;# regex
set result [dbSearchRegex $Nick $Uhost $Chan]
}
}
if {$result eq {}} {
if {[set handseen [handseen $Nick]] ne {}} {
putdcc $idx $handseen
return 1
} else {
putdcc $idx [mc {There were no matches to your query.}]
return
}
} else {
if {[set numMatches [llength $result]] > $dccResults} {
putdcc $idx [mc {Displaying %1$s of %2$s results:} $dccResults $numMatches]
} else {
putdcc $idx [mc {Displaying %1$s of %2$s results:} $numMatches $numMatches]
}
foreach match [lrange $result 0 [expr {$dccResults - 1}]] {
if {$match ne {}} {
putdcc $idx [formatevent {*}[dbGetNick $match]]
}
}
return 1
}
return
}
# verifies table information, return 1 if it's valid, 0 if not
proc ::pixseen::ValidTable {table data} {
switch -exact -- $table {
{pixseen} {
# 0 dbVersion INTEGER 1 {} 0
lassign $data id name type null default primaryKey
if {$id != 0 || $name ne {dbVersion} || $type ne {INTEGER} || $null < 1 || $default ne {} || $primaryKey > 0} {
return 0
}
}
{seenTb} {
foreach item $data {
lassign $data id name type null default primaryKey
switch -exact -- $id {
{0} {
# 0 event INTEGER 1 {} 0
if {$name ne {event} || $type ne {INTEGER} || $null < 1 || $default ne {} || $primaryKey > 0} {
return 0
}
}
{1} {
# 1 nick STRING 1 {} 1
if {$name ne {nick} || $type ne {STRING} || $null < 1 || $default ne {} || $primaryKey < 1} {
return 0
}
}
{2} {
# 2 uhost STRING 1 {} 0
if {$name ne {uhost} || $type ne {STRING} || $null < 1 || $default ne {} || $primaryKey > 0} {
return 0
}
}
{3} {
# 3 time INTEGER 1 {} 0
if {$name ne {time} || $type ne {INTEGER} || $null < 1 || $default ne {} || $primaryKey > 0} {
return 0
}
}
{4} {
# 4 chanid INTEGER 0 {} 0
if {$name ne {chanid} || $type ne {INTEGER} || $null > 0 || $default ne {} || $primaryKey > 0} {
return 0
}
}
{5} {
# 5 reason STRING 0 {} 0
if {$name ne {reason} || $type ne {STRING} || $null > 0 || $default ne {} || $primaryKey > 0} {
return 0
}
}
{6} {
# 6 othernick STRING 0 {} 0
if {$name ne {othernick} || $type ne {STRING} || $null > 0 || $default ne {} || $primaryKey > 0} {
return 0
}
}
default {
return 0
}
}
}
}
{chanTb} {
foreach item $data {
lassign $data id name type null default primaryKey
switch -exact -- $id {
{0} {
#0 chanid INTEGER 1 {} 1
if {$name ne {chanid} || $type ne {INTEGER} || $null < 1 || $default ne {} || $primaryKey < 1} {
return 0
}
}
{1} {
#1 chan STRING 1 {} 0
if {$name ne {chan} || $type ne {STRING} || $null < 1 || $default ne {} || $primaryKey > 0} {
return 0
}
}
default {
return 0
}
}
}
}
default {
return 0
}
}
return 1
}
# Prepare the database on load
proc ::pixseen::LOAD {args} {
variable dbfile; variable dbVersion
sqlite3 ::pixseen::seendb $dbfile
seendb collate IRCRFC ::pixseen::rfccomp
seendb function chan2id ::pixseen::chan2id
seendb function regexp ::pixseen::pixregexp
if {[catch {set result [seendb eval {SELECT tbl_name FROM sqlite_master}]} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
} elseif {$result eq {}} {
# there's no tables defined, so we define some
putlog [mc {%s: No existing database found, defining SQL schema.} {pixseen.tcl}]
if {[catch {seendb eval {
-- Create a table and populate it with a version integer in case we need to change the schema in the future.
CREATE TABLE pixseen (
dbVersion INTEGER UNIQUE NOT NULL
);
INSERT INTO pixseen VALUES(1);
-- Create the table where all our seen data goes
CREATE TABLE seenTb (
event INTEGER NOT NULL,
nick STRING PRIMARY KEY COLLATE IRCRFC UNIQUE NOT NULL,
uhost STRING COLLATE NOCASE NOT NULL,
time INTEGER NOT NULL,
chanid INTEGER,
reason STRING COLLATE NOCASE,
othernick STRING COLLATE IRCRFC
);
-- Create the table that holds channel IDs and their real names
CREATE TABLE chanTb (
chanid INTEGER PRIMARY KEY UNIQUE NOT NULL,
chan STRING UNIQUE NOT NULL COLLATE IRCRFC
);
}} error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
}
} else {
# There's already data in this database, so we verify the schema
# Verify the table names
if {[catch { set result [seendb eval { SELECT tbl_name FROM sqlite_master WHERE type='table' ORDER BY tbl_name }] } error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
die [mc {Fatal Error!}]
} elseif {[join $result] ne {chanTb pixseen seenTb}} {
putlog [mc {%1$s: FATAL ERROR; SQLite database corrupt, exiting.} {pixseen.tcl}]
die [mc {Fatal Error!}]
# Verify the pixseen table
} elseif {[catch { set result [seendb eval { PRAGMA table_info(pixseen) }] } error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
die [mc {Fatal Error!}]
} elseif {![ValidTable {pixseen} $result]} {
putlog [mc {%1$s: FATAL ERROR; SQLite database corrupt, exiting.} {pixseen.tcl}]
die [mc {Fatal Error!}]
# Verify the database version
} elseif {[catch { set result [seendb eval { SELECT dbVersion FROM pixseen LIMIT 1 }] } error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
die [mc {Fatal Error!}]
} elseif {$result != $dbVersion} {
putlog [mc {%1$s: FATAL ERROR; SQLite database corrupt, exiting.} {pixseen.tcl}]
die [mc {Fatal Error!}]
# Verify the seenTb table
} elseif {[catch { set result [seendb eval { PRAGMA table_info(seenTb) }] } error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
die [mc {Fatal Error!}]
} elseif {![ValidTable {seenTb} $result]} {
putlog [mc {%1$s: FATAL ERROR; SQLite database corrupt, exiting.} {pixseen.tcl}]
die [mc {Fatal Error!}]
# Verify the chanTb table
} elseif {[catch { set result [seendb eval { PRAGMA table_info(chanTb) }] } error]} {
putlog [mc {%1$s SQL error %2$s; %3$s} {pixseen.tcl} [seendb errorcode] $error]
die [mc {Fatal Error!}]
} elseif {![ValidTable {chanTb} $result]} {
putlog [mc {%1$s: FATAL ERROR; SQLite database corrupt, exiting.} {pixseen.tcl}]
die [mc {Fatal Error!}]
# Everything is OK!
} else {
# Do some database maintenance
dbCleanup
putlog [mc {%s: Loaded the seen database.} {pixseen.tcl}]
}
}
return
}
proc ::pixseen::UNLOAD {args} {
seendb close
putlog [mc {%s: Unloaded the seen database.} {pixseen.tcl}]
return
}
# We have to verify the password here to make sure that the die is successful
proc ::pixseen::msg_die {cmdString op} {
set hand [lindex $cmdString 3]
set pass [lindex $cmdString 4 0]
if {[passwdok $hand $pass]} {
UNLOAD
}
return
}
# chanset wrapper
# checks the language people set and complains if it's not supported.
proc ::pixseen::dcc_chanset {hand idx param} {
set chan [lindex [set arg [split $param]] 0]
if {![validchan $chan]} {
*DCC:CHANSET $hand $idx $param
return
}
set settings [lrange $arg 1 end]
set found 0
foreach setting $settings {
if {$found} {
set lang $setting
} elseif {[string equal -nocase $setting {seenlang}]} {
set found 1
}
}
if {[info exists lang] && ![validlang $lang]} {
putdcc $idx [mc {Error: Invalid seen language "%s".} $lang]
return
} else {
*DCC:CHANSET $hand $idx $param
return
}
}
# This proc will be renamed to ::*dcc:chanset on load. We call out real
# wrapper from here so that it can stay in the correct namespace
proc ::pixseen::*dcc:chanset {hand idx param} {
::pixseen::dcc_chanset $hand $idx $param
}
namespace eval ::pixseen {
# trace die so that we can unload the database properly before the bot exist
if {![info exists SetTraces]} {
trace add execution die enter ::pixseen::UNLOAD
# don't try to trace these on Tcldrop
if {![info exists ::tcldrop]} {
trace add execution *dcc:die enter ::pixseen::UNLOAD
trace add execution *msg:die enter ::pixseen::msg_die
# wrap chanset so we can validate the language people set
# FixMe: add Tcldrop equivalent
rename ::*dcc:chanset ::*DCC:CHANSET
rename ::pixseen::*dcc:chanset ::*dcc:chanset
}
variable SetTraces 1
}
# load the database if it's not already loaded
if {[info procs seendb] ne {seendb}} { ::pixseen::LOAD }
# unload the database on rehash & restart
bind evnt - {prerehash} ::pixseen::UNLOAD
bind evnt - {prerestart} ::pixseen::UNLOAD
# seen tracking events
bind part - "*" ::pixseen::PART
bind join - "*" ::pixseen::JOIN
bind nick - "*" ::pixseen::NICK
bind sign - "*" ::pixseen::SIGN
bind splt - "*" ::pixseen::SPLT
bind rejn - "*" ::pixseen::REJN
bind kick - "*" ::pixseen::KICK
bind chon - "*" ::pixseen::CHON
bind chof - "*" ::pixseen::CHOF
bind chjn - "*" ::pixseen::CHJN
bind chpt - "*" ::pixseen::CHPT
bind away - "*" ::pixseen::AWAY
bind pubm - "% *" ::pixseen::CHMSG
# triggers
bind pubm - {% !seen *} ::pixseen::pubm_seen
bind pubm - {% !seen} ::pixseen::pubm_seen
bind msgm - {seen *} ::pixseen::msgm_seen
bind msgm - {seen} ::pixseen::msgm_seen
bind dcc - {seen} ::pixseen::dcc_seen
# flood-array cleanup every 10 minutes
bind time - "?0 * * * *" ::pixseen::RemoveFlood
# do some database maintenance once daily
bind evnt - {logfile} ::pixseen::dbCleanup
putlog [mc {Loaded %1$s v%2$s by %3$s} {pixseen.tcl} $seenver {Pixelz}]
}
|