-
Notifications
You must be signed in to change notification settings - Fork 2
/
cite.tcl
251 lines (229 loc) · 6.39 KB
/
cite.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# eggdrop citation script
# (c)2012 cr0n
set vers "0.2"
set citedb "scripts/cite.db"
set dumpfile "scripts/cite.db.txt"
setudef flag cite
array set cites {}
set citecount 0
set citeschanged 0
bind pub o|F !list pub:cdblist
bind pub - !fact pub:cdbfact
bind msg - !fact msg:cdbfact
bind pub - !randfact pub:cdbrand
bind pub - !del pub:cdbdel
bind pub o|F !set pub:cdbset
bind pub o|F !save pub:cdbsave
bind pub o|R !reset pub:cdbreset
bind pub o|F !flush pub:cdbflush
bind pub - !grep pub:cdbsearch
bind msg - !grep msg:cdbsearch
bind pub - !add pub:cdbadd
bind pub - factbot: pub:cdbadd
bind time - "?0 *" auto:cdbsave
proc pub:cdbrand {n u h c a} {
if {![channel get $c cite]} { return }
global cites
set keys [lsort -integer [array names cites]]
if {[llength $keys] > 0} {
set id [lindex $keys [expr {int(rand()*[llength $keys])}]]
putquick "PRIVMSG $c :\[$id\] $cites($id)"
} else {
putquick "PRIVMSG $c :currently no facts exist"
}
}
proc pub:cdblist {n u h c a} {
if {![channel get $c cite]} { return }
global cites
set citequery [lsort -integer [array names cites]]
if {[llength $citequery] == 0} {
putquick "PRIVMSG $c :currently no facts exist"
} else {
foreach i $citequery {
putserv "PRIVMSG $c :\[$i\] $cites($i)"
}
}
}
proc msg:cdbfact {n u h a} {
global cites
set id [lindex $a 0]
if {[info exists cites($id)]} {
puthelp "PRIVMSG $n :\[$id\] $cites($id)"
} else {
puthelp "PRIVMSG $n :fact $id does not exist"
}
}
proc pub:cdbfact {n u h c a} {
if {![channel get $c cite]} { return }
global cites
set id [lindex $a 0]
if {[info exists cites($id)]} {
putquick "PRIVMSG $c :\[$id\] $cites($id)"
} else {
putquick "PRIVMSG $c :fact $id does not exist"
}
}
proc msg:cdbsearch {n u h a} {
global cites
set re [string trim $a]
if {[string length $re] < 1} {
puthelp "PRIVMSG $n ::P"
} else {
set re [regsub -all {\s+} $re {.*}]
set found 0
set out [list]
foreach i [lsort -integer [array names cites]] {
if {[regexp -nocase $re $cites($i)] == 1} {
incr found
if {$found > 10} break
lappend out "\[$i\] $cites($i)"
}
}
if {$found > 0} {
if {$found > 10} {
puthelp "PRIVMSG $n :found >10 results, please be more specific"
}
foreach line $out {
puthelp "PRIVMSG $n :$line"
}
} else {
puthelp "PRIVMSG $n :nothing found"
}
}
}
proc pub:cdbsearch {n u h c a} {
if {![channel get $c cite]} { return }
global cites
set re [string trim $a]
if {[string length $re] < 1} {
putquick "PRIVMSG $c ::P"
} else {
set re [regsub -all {\s+} $re {.*}]
set found 0
set out [list]
foreach i [lsort -integer [array names cites]] {
if {[regexp -nocase $re $cites($i)] == 1} {
incr found
if {$found > 5} break
lappend out "\[$i\] $cites($i)"
}
}
if {$found > 0} {
if {$found > 5} {
putserv "PRIVMSG $c :found >5 results, please be more specific"
}
foreach line $out {
putserv "PRIVMSG $c :$line"
}
} else {
putquick "PRIVMSG $c :nothing found"
}
}
}
proc pub:cdbdel {n u h c a} {
if {![channel get $c cite]} { return }
global cites citeschanged
set what [string trim $a]
set j 0
foreach i $what {
if {[info exists cites($i)]} {
unset cites($i)
incr j
set citeschanged 1
}
}
putquick "PRIVMSG $c :$j facts removed"
}
proc pub:cdbset {n u h c a} {
if {![channel get $c cite]} { return }
global cites citecount citeschanged
set id [lindex "$a" 0]
set txt [regsub {^\s*\d+\s+} [string trim $a] {}]
if {[string length $txt] > 0 && $id >= 0 && $id <= $citecount} {
putquick "PRIVMSG $c :\[$id\] $txt"
set cites($id) $txt
set citeschanged 1
} else {
putquick "PRIVMSG $c :new fact is empty or id is out of range"
}
}
proc pub:cdbadd {n u h c a} {
if {![channel get $c cite]} { return }
global cites citecount citeschanged
set txt [string trim $a]
if {[string length $txt] > 0 && $txt != "{}"} {
set keys [lsort -integer [array names cites]]
foreach i $keys {
if {$cites($i) == $txt} {
putquick "PRIVMSG $c :dupe, see fact $i"
return
}
}
incr citecount
putquick "PRIVMSG $c :\[$citecount\] $txt"
set cites($citecount) $txt
set citeschanged 1
} else {
putquick "PRIVMSG $c :no fact given"
}
}
proc int:cdbsave {} {
global citedb cites citecount citeschanged dumpfile
if {$citeschanged} {
set citeschanged 0
set fp [open $citedb w]
puts $fp [list array set cites [array get cites]]
puts $fp [list set citecount [set citecount]]
close $fp
set citequery [lsort -integer [array names cites]]
set fp [open $dumpfile w]
if {[llength $citequery] == 0} {
puts $fp "currently no facts exist"
} else {
foreach id $citequery {
puts $fp "\[$id\] $cites($id)"
}
}
close $fp
putlog "FactsDB saved"
} else {
putlog "FactsDB not saved (no change)"
}
}
proc auto:cdbsave {m h d w y} {
int:cdbsave
}
proc pub:cdbsave {n u h c a} {
if {![channel get $c cite]} { return }
int:cdbsave
}
proc pub:cdbreset {n u h c a} {
if {![channel get $c cite]} { return }
global cites citecount
set citecount 0
unset cites
array set cites {}
putquick "PRIVMSG $c :facts cleared"
}
proc int:cdbread {} {
global citedb cites citecount
uplevel {
if {[file exist $citedb]} {
unset cites
source $citedb
}
}
}
proc pub:cdbflush {n u h c a} {
if {![channel get $c cite]} { return }
global citedb cites citecount
uplevel {
if {[file exist $citedb]} {
unset cites
source $citedb
}
}
putquick "PRIVMSG $c :facts flushed"
}
int:cdbread
putlog "FactsDB v$vers successfully loaded."