Mercurial > hg > egg-tcls
comparison mndb.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 # MattiNykanenDB 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 quote data goes | |
14 set mndb_file "data.mndb" | |
15 | |
16 # 1 = Verbose: Say messages PUBLIC when quote is OK, bad, etc. | |
17 # 0 = Quiet : Say privately | |
18 set mndb_verbose 0 | |
19 | |
20 # 1 = Put some info to bot's Logfile when doing stuff... | |
21 # 0 = Don't. | |
22 set mndb_logmsg 1 | |
23 | |
24 # What IRC "command" should we use to send messages: | |
25 # (Valid alternatives are "PRIVMSG" and "NOTICE") | |
26 set mndb_preferredmsg "PRIVMSG" | |
27 | |
28 | |
29 ### | |
30 ### Search related settings | |
31 ### | |
32 | |
33 # How many quote's should the !mnfind command show (maximum limit) | |
34 set mndb_showmax_pub 3 | |
35 | |
36 # For private-search, this is the default limit (user can change it) | |
37 set mndb_showmax_priv 5 | |
38 | |
39 | |
40 ########################################################################## | |
41 # No need to look below this line | |
42 ########################################################################## | |
43 #------------------------------------------------------------------------- | |
44 set mndb_name "MattiNykanenDB" | |
45 set mndb_version "0.7" | |
46 | |
47 | |
48 #------------------------------------------------------------------------- | |
49 ### Binding initializations | |
50 bind pub - !mnfind mndb_pub_find | |
51 bind pub - !mnadd mndb_pub_add | |
52 bind pub - !mn mndb_pub_mn | |
53 bind msg - mnfind mndb_msg_find | |
54 bind msg - mn mndb_msg_mn | |
55 | |
56 | |
57 ### Initialization messages | |
58 set mndb_message "$mndb_name v$mndb_version by ccr/TNSP" | |
59 putlog "$mndb_message" | |
60 | |
61 | |
62 #------------------------------------------------------------------------- | |
63 ### Utility functions | |
64 proc mndb_log { jarg } { | |
65 global mndb_logmsg mndb_name | |
66 | |
67 if {$mndb_logmsg != 0} { | |
68 putlog "$mndb_name: $jarg" | |
69 } | |
70 } | |
71 | |
72 | |
73 proc mndb_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 mndb_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 mndb_msg {apublic anick achan amsg} { | |
95 global mndb_preferredmsg | |
96 | |
97 if {$apublic == 0} { | |
98 putserv "$mndb_preferredmsg $anick :$amsg" | |
99 } else { | |
100 putserv "$mndb_preferredmsg $achan :$anick, $amsg" | |
101 } | |
102 } | |
103 | |
104 | |
105 #------------------------------------------------------------------------- | |
106 proc mndb_add {utext unick uhost uchan upublic} { | |
107 global mndb_file | |
108 | |
109 if {[string length $utext] < 10} { | |
110 mndb_msg $upublic $unick $uchan "pyh." | |
111 return 0 | |
112 } | |
113 | |
114 ### Create the database file if it does not exist yet | |
115 set fd [open $mndb_file a+] | |
116 close $fd | |
117 | |
118 ### OK. The quote is valid, but let's check if we already know it. | |
119 set fd [open $mndb_file r] | |
120 set sindex 0 | |
121 set smax -1 | |
122 | |
123 while {![eof $fd]} { | |
124 gets $fd foo | |
125 incr sindex | |
126 | |
127 set foo [split $foo "|"] | |
128 set qindex [lindex $foo 0] | |
129 if {$qindex > $smax} { set smax $qindex } | |
130 } | |
131 | |
132 close $fd | |
133 | |
134 ### OK, the quote was not already known and IS valid. Add it. | |
135 | |
136 incr smax | |
137 | |
138 set fd [open $mndb_file a+] | |
139 puts $fd "$smax|$utext|[unixtime]|$unick|$uhost|$uchan" | |
140 close $fd | |
141 | |
142 ### Log some data | |
143 mndb_log "Added quote #$smax ($unick @ $uchan): $utext" | |
144 | |
145 ### Let's report success to user | |
146 mndb_msg $upublic $unick $uchan "tietokantaa sörkitty (#$smax / $sindex), kiitos." | |
147 | |
148 return 1 | |
149 } | |
150 | |
151 | |
152 #------------------------------------------------------------------------- | |
153 proc mndb_find {ipatterns imax} { | |
154 global mndb_file | |
155 | |
156 ### Search the database for pattern | |
157 ### Clear the count, open the quote logfile | |
158 set iresults {} | |
159 set nresults 0 | |
160 set fd [open $mndb_file r] | |
161 | |
162 ### Start searching... | |
163 while {![eof $fd]} { | |
164 | |
165 # Get one quote for inspection | |
166 gets $fd foo | |
167 set irecord [split [string tolower $foo] "|"] | |
168 set itext [lindex $irecord 1] | |
169 set iname [lindex $irecord 3] | |
170 | |
171 # Match with all given patterns and rules | |
172 set imatched 1 | |
173 | |
174 foreach ipattern $ipatterns { | |
175 | |
176 set foob [split [string tolower $ipattern] " "] | |
177 set ftoken [lindex $foob 0] | |
178 set fparam [lindex $foob 1] | |
179 set fmatch [string match $fparam $itext] | |
180 | |
181 if {($ftoken == "+") && ($fmatch == 0)} { set imatched 0 } | |
182 | |
183 if {($ftoken == "-") && ($fmatch == 1)} { set imatched 0 } | |
184 | |
185 if {($ftoken == "%") && ([string match $fparam $iname] == 0)} { set imatched 0 } | |
186 | |
187 } | |
188 | |
189 # If the all patterns matched, add to the list... | |
190 if {($imatched == 1) && ($foo != "")} { | |
191 incr nresults | |
192 lappend iresults $foo | |
193 } | |
194 | |
195 } | |
196 | |
197 # Close file | |
198 close $fd | |
199 | |
200 # Take only last imax results | |
201 return [lrange $iresults [expr $nresults-$imax] $nresults] | |
202 } | |
203 | |
204 | |
205 | |
206 #------------------------------------------------------------------------- | |
207 proc mndb_get { unick uhand uindex } { | |
208 global mndb_file | |
209 | |
210 set ifound 0 | |
211 set iindex 0 | |
212 set iresults {} | |
213 set nresults 0 | |
214 | |
215 ### Create the database file if it does not exist yet | |
216 set fd [open $mndb_file a+] | |
217 close $fd | |
218 | |
219 ### OK. The quote is valid, but let's check if we already know it. | |
220 set fd [open $mndb_file r] | |
221 | |
222 if {$uindex == ""} { | |
223 ### Log search | |
224 mndb_log "$unick/$uhand get random quote" | |
225 | |
226 ### Do search | |
227 while {![eof $fd]} { | |
228 gets $fd foo | |
229 incr nresults | |
230 lappend iresults $foo | |
231 } | |
232 | |
233 set foo [split [lindex $iresults [rand $nresults]] "|"] | |
234 set ifound 1 | |
235 | |
236 } else { | |
237 ### Log search | |
238 mndb_log "$unick/$uhand searched quote #$uindex" | |
239 | |
240 ### Do search | |
241 while {![eof $fd] && !$ifound} { | |
242 gets $fd foo | |
243 set foo [split $foo "|"] | |
244 | |
245 if {[lindex $foo 0] == $uindex} { | |
246 set ifound 1 | |
247 } | |
248 } | |
249 } | |
250 | |
251 ### Close file | |
252 close $fd | |
253 | |
254 ### Return result | |
255 if {$ifound} { | |
256 return "#[lindex $foo 0]: [lindex $foo 1]" | |
257 } else { | |
258 return "ei löydy." | |
259 } | |
260 } | |
261 | |
262 | |
263 #------------------------------------------------------------------------- | |
264 proc mndb_search {unick uhand uchan utext upublic} { | |
265 global mndb_showmax_pub spmsg_nomatch | |
266 | |
267 ### Log search | |
268 mndb_log "$unick/$uhand searched quote: $utext" | |
269 | |
270 ### Parse the given command | |
271 set footokens [split $utext " "] | |
272 foreach ftoken $footokens { | |
273 set foomark [string range $ftoken 0 0] | |
274 set foopat [string range $ftoken 1 end] | |
275 | |
276 if {$foomark == "-" || $foomark == "+" || $foomark == "%"} { | |
277 lappend ipatlist "$foomark *$foopat*" | |
278 } else { | |
279 lappend ipatlist "+ *$ftoken*" | |
280 } | |
281 } | |
282 | |
283 | |
284 ### Get the matches | |
285 | |
286 set iresults [mndb_find $ipatlist $mndb_showmax_pub] | |
287 | |
288 ### Show the results | |
289 if {$iresults != ""} { | |
290 foreach i $iresults { | |
291 set foo [split $i "|"] | |
292 mndb_msg $upublic $unick $uchan "#[lindex $foo 0]: [lindex $foo 1]" | |
293 } | |
294 | |
295 # If no quotes were found | |
296 } else { | |
297 mndb_msg $upublic $unick $uchan "ei löydy." | |
298 } | |
299 } | |
300 | |
301 | |
302 #------------------------------------------------------------------------- | |
303 proc mndb_pub_mn {unick uhost uhand uchan utext} { | |
304 | |
305 mndb_msg 0 $uchan "" [mndb_get $unick $uhand $utext] | |
306 } | |
307 | |
308 | |
309 #------------------------------------------------------------------------- | |
310 proc mndb_msg_mn {unick uhost uhand utext} { | |
311 | |
312 mndb_msg 0 $unick "" [mndb_get $unick $uhand $utext] | |
313 } | |
314 | |
315 | |
316 #------------------------------------------------------------------------- | |
317 proc mndb_pub_add {unick uhost uhand uchan utext} { | |
318 | |
319 mndb_add $utext $unick $uhost $uchan 1 | |
320 } | |
321 | |
322 | |
323 #------------------------------------------------------------------------- | |
324 proc mndb_pub_rm {unick uhost uhand uchan utext} { | |
325 | |
326 mndb_rm $utext $unick $uhost $uchan 1 | |
327 } | |
328 | |
329 | |
330 #------------------------------------------------------------------------- | |
331 proc mndb_pub_find {unick uhost uhand uchan utext} { | |
332 | |
333 mndb_search $unick $uhand $uchan $utext 1 | |
334 } | |
335 | |
336 | |
337 #------------------------------------------------------------------------- | |
338 proc mndb_msg_find {unick uhost uhand utext} { | |
339 | |
340 mndb_search $unick $uhand "" $utext 0 | |
341 } | |
342 | |
343 | |
344 #------------------------------------------------------------------------- | |
345 | |
346 # end of script |