comparison spededb.tcl @ 0:1c4e2814cd41

Initial import.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 21 Sep 2010 13:12:49 +0300
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1c4e2814cd41
1 ##########################################################################
2 #
3 # SpedeDB v0.7 by ccr/TNSP <ccr@tnsp.org>
4 #
5 # Not for public use or distribution. If you happen to find this,
6 # send your questions and/or problems to /dev/null, thank you.
7 #
8 ##########################################################################
9
10 ###
11 ### General options
12 ###
13 # Filename where the logged SPEDE data goes
14 set spdb_file "data.spededb"
15
16 # 1 = Verbose: Say messages PUBLIC when SPEDE is OK, bad, etc.
17 # 0 = Quiet : Say privately
18 set spdb_verbose 0
19
20 # 1 = Put some info to bot's Logfile when doing stuff...
21 # 0 = Don't.
22 set spdb_logmsg 1
23
24 # What IRC "command" should we use to send messages:
25 # (Valid alternatives are "PRIVMSG" and "NOTICE")
26 set spdb_preferredmsg "PRIVMSG"
27
28
29 ###
30 ### Search related settings
31 ###
32
33 # How many SPEDE's should the !spedefind command show (maximum limit)
34 set spdb_showmax_pub 3
35
36 # For private-search, this is the default limit (user can change it)
37 set spdb_showmax_priv 5
38
39
40 ##########################################################################
41 # No need to look below this line
42 ##########################################################################
43 #-------------------------------------------------------------------------
44 set spdb_name "SpedeDB"
45 set spdb_version "0.7"
46
47
48 #-------------------------------------------------------------------------
49 ### Binding initializations
50 bind pub - !spedefind spdb_pub_find
51 bind pub - !spedeadd spdb_pub_add
52 bind pub - !spede spdb_pub_spede
53 bind msg - spedefind spdb_msg_find
54 bind msg - spede spdb_msg_spede
55
56
57 ### Initialization messages
58 set spdb_message "$spdb_name v$spdb_version by ccr/TNSP"
59 putlog "$spdb_message"
60
61
62 #-------------------------------------------------------------------------
63 ### Utility functions
64 proc spdb_log { jarg } {
65 global spdb_logmsg spdb_name
66
67 if {$spdb_logmsg != 0} {
68 putlog "$spdb_name: $jarg"
69 }
70 }
71
72
73 proc spdb_ctime { utime } {
74
75 if {$utime == "" || $utime == "*"} {
76 set utime 0
77 }
78
79 return [clock format $utime -format "%d.%m.%Y %H:%M"]
80 }
81
82
83 proc spdb_isnumber { uarg } {
84 set ufoo 1
85
86 foreach i [split $uarg {}] {
87 if ![string match \[0-9\] $i] {set ufoo 0}
88 }
89
90 return $ufoo
91 }
92
93
94 proc spdb_msg {apublic anick achan amsg} {
95 global spdb_preferredmsg
96
97 if {$apublic == 0} {
98 putserv "$spdb_preferredmsg $anick :$amsg"
99 } else {
100 putserv "$spdb_preferredmsg $achan :$anick, $amsg"
101 }
102 }
103
104
105 #-------------------------------------------------------------------------
106 proc spdb_add {utext unick uhost uchan upublic} {
107 global spdb_file
108
109
110 if {"x$utext" == "x"} {
111 spdb_msg $upublic $unick $uchan "pyh."
112 return 0
113 }
114
115 ### Create the database file if it does not exist yet
116 set fd [open $spdb_file a+]
117 close $fd
118
119 ### OK. The SPEDE is valid, but let's check if we already know it.
120 set fd [open $spdb_file r]
121 set sindex 0
122 set smax -1
123
124 while {![eof $fd]} {
125 gets $fd foo
126 incr sindex
127
128 set foo [split $foo "|"]
129 set qindex [lindex $foo 0]
130 if {$qindex > $smax} { set smax $qindex }
131 }
132
133 close $fd
134
135 ### OK, the SPEDE was not already known and IS valid. Add it.
136
137 incr smax
138
139 set fd [open $spdb_file a+]
140 puts $fd "$smax|$utext|[unixtime]|$unick|$uhost|$uchan"
141 close $fd
142
143 ### Log some data
144 spdb_log "Added SPEDE #$smax ($unick @ $uchan): $utext"
145
146 ### Let's report success to user
147 spdb_msg $upublic $unick $uchan "tietokantaa sörkitty (#$smax / $sindex), kiitos."
148
149 return 1
150 }
151
152
153 #-------------------------------------------------------------------------
154 proc spdb_find {ipatterns imax} {
155 global spdb_file
156
157 ### Search the database for pattern
158 ### Clear the count, open the SPEDE logfile
159 set iresults {}
160 set nresults 0
161 set fd [open $spdb_file r]
162
163 ### Start searching...
164 while {![eof $fd]} {
165
166 # Get one SPEDE for inspection
167 gets $fd foo
168 set irecord [split [string tolower $foo] "|"]
169 set itext [lindex $irecord 1]
170 set iname [lindex $irecord 3]
171
172 # Match with all given patterns and rules
173 set imatched 1
174
175 foreach ipattern $ipatterns {
176
177 set foob [split [string tolower $ipattern] " "]
178 set ftoken [lindex $foob 0]
179 set fparam [lindex $foob 1]
180 set fmatch [string match $fparam $itext]
181
182 if {($ftoken == "+") && ($fmatch == 0)} { set imatched 0 }
183
184 if {($ftoken == "-") && ($fmatch == 1)} { set imatched 0 }
185
186 if {($ftoken == "%") && ([string match $fparam $iname] == 0)} { set imatched 0 }
187
188 }
189
190 # If the all patterns matched, add to the list...
191 if {($imatched == 1) && ($foo != "")} {
192 incr nresults
193 lappend iresults $foo
194 }
195
196 }
197
198 # Close file
199 close $fd
200
201 # Take only last imax results
202 return [lrange $iresults [expr $nresults-$imax] $nresults]
203 }
204
205
206
207 #-------------------------------------------------------------------------
208 proc spdb_get { unick uhand uindex } {
209 global spdb_file
210
211 set ifound 0
212 set iindex 0
213 set iresults {}
214 set nresults 0
215
216 ### Create the database file if it does not exist yet
217 set fd [open $spdb_file a+]
218 close $fd
219
220 ### OK. The SPEDE is valid, but let's check if we already know it.
221 set fd [open $spdb_file r]
222
223 if {$uindex == ""} {
224 ### Log search
225 spdb_log "$unick/$uhand get random SPEDE"
226
227 ### Do search
228 while {![eof $fd]} {
229 gets $fd foo
230 incr nresults
231 lappend iresults $foo
232 }
233
234 set foo [split [lindex $iresults [rand $nresults]] "|"]
235 set ifound 1
236
237 } else {
238 ### Log search
239 spdb_log "$unick/$uhand searched SPEDE #$uindex"
240
241 ### Do search
242 while {![eof $fd] && !$ifound} {
243 gets $fd foo
244 set foo [split $foo "|"]
245
246 if {[lindex $foo 0] == $uindex} {
247 set ifound 1
248 }
249 }
250 }
251
252 ### Close file
253 close $fd
254
255 ### Return result
256 if {$ifound} {
257 return "#[lindex $foo 0]: [lindex $foo 1] ([lindex $foo 3])"
258 } else {
259 return "ei löydy."
260 }
261 }
262
263
264 #-------------------------------------------------------------------------
265 proc spdb_search {unick uhand uchan utext upublic} {
266 global spdb_showmax_pub spmsg_nomatch
267
268 ### Log search
269 spdb_log "$unick/$uhand searched SPEDE: $utext"
270
271 ### Parse the given command
272 set footokens [split $utext " "]
273 foreach ftoken $footokens {
274 set foomark [string range $ftoken 0 0]
275 set foopat [string range $ftoken 1 end]
276
277 if {$foomark == "-" || $foomark == "+" || $foomark == "%"} {
278 lappend ipatlist "$foomark *$foopat*"
279 } else {
280 lappend ipatlist "+ *$ftoken*"
281 }
282 }
283
284
285 ### Get the matches
286
287 set iresults [spdb_find $ipatlist $spdb_showmax_pub]
288
289 ### Show the results
290 if {$iresults != ""} {
291 foreach i $iresults {
292 set foo [split $i "|"]
293 spdb_msg $upublic $unick $uchan "#[lindex $foo 0]: [lindex $foo 1] ([lindex $foo 3] @ [spdb_ctime [lindex $foo 2]])"
294 }
295
296 # If no SPEDEs were found
297 } else {
298 spdb_msg $upublic $unick $uchan "ei löydy."
299 }
300 }
301
302
303 #-------------------------------------------------------------------------
304 proc spdb_pub_spede {unick uhost uhand uchan utext} {
305
306 spdb_msg 0 $uchan "" [spdb_get $unick $uhand $utext]
307 }
308
309
310 #-------------------------------------------------------------------------
311 proc spdb_msg_spede {unick uhost uhand utext} {
312
313 spdb_msg 0 $unick "" [spdb_get $unick $uhand $utext]
314 }
315
316
317 #-------------------------------------------------------------------------
318 proc spdb_pub_add {unick uhost uhand uchan utext} {
319
320 spdb_add $utext $unick $uhost $uchan 1
321 }
322
323
324 #-------------------------------------------------------------------------
325 proc spdb_pub_rm {unick uhost uhand uchan utext} {
326
327 spdb_rm $utext $unick $uhost $uchan 1
328 }
329
330
331 #-------------------------------------------------------------------------
332 proc spdb_pub_find {unick uhost uhand uchan utext} {
333
334 spdb_search $unick $uhand $uchan $utext 1
335 }
336
337
338 #-------------------------------------------------------------------------
339 proc spdb_msg_find {unick uhost uhand utext} {
340
341 spdb_search $unick $uhand "" $utext 0
342 }
343
344
345 #-------------------------------------------------------------------------
346
347 # end of script