Mercurial > hg > egg-tcls
view spededb.tcl @ 0:1c4e2814cd41
Initial import.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 21 Sep 2010 13:12:49 +0300 |
parents | |
children |
line wrap: on
line source
########################################################################## # # SpedeDB v0.7 by ccr/TNSP <ccr@tnsp.org> # # Not for public use or distribution. If you happen to find this, # send your questions and/or problems to /dev/null, thank you. # ########################################################################## ### ### General options ### # Filename where the logged SPEDE data goes set spdb_file "data.spededb" # 1 = Verbose: Say messages PUBLIC when SPEDE is OK, bad, etc. # 0 = Quiet : Say privately set spdb_verbose 0 # 1 = Put some info to bot's Logfile when doing stuff... # 0 = Don't. set spdb_logmsg 1 # What IRC "command" should we use to send messages: # (Valid alternatives are "PRIVMSG" and "NOTICE") set spdb_preferredmsg "PRIVMSG" ### ### Search related settings ### # How many SPEDE's should the !spedefind command show (maximum limit) set spdb_showmax_pub 3 # For private-search, this is the default limit (user can change it) set spdb_showmax_priv 5 ########################################################################## # No need to look below this line ########################################################################## #------------------------------------------------------------------------- set spdb_name "SpedeDB" set spdb_version "0.7" #------------------------------------------------------------------------- ### Binding initializations bind pub - !spedefind spdb_pub_find bind pub - !spedeadd spdb_pub_add bind pub - !spede spdb_pub_spede bind msg - spedefind spdb_msg_find bind msg - spede spdb_msg_spede ### Initialization messages set spdb_message "$spdb_name v$spdb_version by ccr/TNSP" putlog "$spdb_message" #------------------------------------------------------------------------- ### Utility functions proc spdb_log { jarg } { global spdb_logmsg spdb_name if {$spdb_logmsg != 0} { putlog "$spdb_name: $jarg" } } proc spdb_ctime { utime } { if {$utime == "" || $utime == "*"} { set utime 0 } return [clock format $utime -format "%d.%m.%Y %H:%M"] } proc spdb_isnumber { uarg } { set ufoo 1 foreach i [split $uarg {}] { if ![string match \[0-9\] $i] {set ufoo 0} } return $ufoo } proc spdb_msg {apublic anick achan amsg} { global spdb_preferredmsg if {$apublic == 0} { putserv "$spdb_preferredmsg $anick :$amsg" } else { putserv "$spdb_preferredmsg $achan :$anick, $amsg" } } #------------------------------------------------------------------------- proc spdb_add {utext unick uhost uchan upublic} { global spdb_file if {"x$utext" == "x"} { spdb_msg $upublic $unick $uchan "pyh." return 0 } ### Create the database file if it does not exist yet set fd [open $spdb_file a+] close $fd ### OK. The SPEDE is valid, but let's check if we already know it. set fd [open $spdb_file r] set sindex 0 set smax -1 while {![eof $fd]} { gets $fd foo incr sindex set foo [split $foo "|"] set qindex [lindex $foo 0] if {$qindex > $smax} { set smax $qindex } } close $fd ### OK, the SPEDE was not already known and IS valid. Add it. incr smax set fd [open $spdb_file a+] puts $fd "$smax|$utext|[unixtime]|$unick|$uhost|$uchan" close $fd ### Log some data spdb_log "Added SPEDE #$smax ($unick @ $uchan): $utext" ### Let's report success to user spdb_msg $upublic $unick $uchan "tietokantaa sörkitty (#$smax / $sindex), kiitos." return 1 } #------------------------------------------------------------------------- proc spdb_find {ipatterns imax} { global spdb_file ### Search the database for pattern ### Clear the count, open the SPEDE logfile set iresults {} set nresults 0 set fd [open $spdb_file r] ### Start searching... while {![eof $fd]} { # Get one SPEDE for inspection gets $fd foo set irecord [split [string tolower $foo] "|"] set itext [lindex $irecord 1] set iname [lindex $irecord 3] # Match with all given patterns and rules set imatched 1 foreach ipattern $ipatterns { set foob [split [string tolower $ipattern] " "] set ftoken [lindex $foob 0] set fparam [lindex $foob 1] set fmatch [string match $fparam $itext] if {($ftoken == "+") && ($fmatch == 0)} { set imatched 0 } if {($ftoken == "-") && ($fmatch == 1)} { set imatched 0 } if {($ftoken == "%") && ([string match $fparam $iname] == 0)} { set imatched 0 } } # If the all patterns matched, add to the list... if {($imatched == 1) && ($foo != "")} { incr nresults lappend iresults $foo } } # Close file close $fd # Take only last imax results return [lrange $iresults [expr $nresults-$imax] $nresults] } #------------------------------------------------------------------------- proc spdb_get { unick uhand uindex } { global spdb_file set ifound 0 set iindex 0 set iresults {} set nresults 0 ### Create the database file if it does not exist yet set fd [open $spdb_file a+] close $fd ### OK. The SPEDE is valid, but let's check if we already know it. set fd [open $spdb_file r] if {$uindex == ""} { ### Log search spdb_log "$unick/$uhand get random SPEDE" ### Do search while {![eof $fd]} { gets $fd foo incr nresults lappend iresults $foo } set foo [split [lindex $iresults [rand $nresults]] "|"] set ifound 1 } else { ### Log search spdb_log "$unick/$uhand searched SPEDE #$uindex" ### Do search while {![eof $fd] && !$ifound} { gets $fd foo set foo [split $foo "|"] if {[lindex $foo 0] == $uindex} { set ifound 1 } } } ### Close file close $fd ### Return result if {$ifound} { return "#[lindex $foo 0]: [lindex $foo 1] ([lindex $foo 3])" } else { return "ei löydy." } } #------------------------------------------------------------------------- proc spdb_search {unick uhand uchan utext upublic} { global spdb_showmax_pub spmsg_nomatch ### Log search spdb_log "$unick/$uhand searched SPEDE: $utext" ### Parse the given command set footokens [split $utext " "] foreach ftoken $footokens { set foomark [string range $ftoken 0 0] set foopat [string range $ftoken 1 end] if {$foomark == "-" || $foomark == "+" || $foomark == "%"} { lappend ipatlist "$foomark *$foopat*" } else { lappend ipatlist "+ *$ftoken*" } } ### Get the matches set iresults [spdb_find $ipatlist $spdb_showmax_pub] ### Show the results if {$iresults != ""} { foreach i $iresults { set foo [split $i "|"] spdb_msg $upublic $unick $uchan "#[lindex $foo 0]: [lindex $foo 1] ([lindex $foo 3] @ [spdb_ctime [lindex $foo 2]])" } # If no SPEDEs were found } else { spdb_msg $upublic $unick $uchan "ei löydy." } } #------------------------------------------------------------------------- proc spdb_pub_spede {unick uhost uhand uchan utext} { spdb_msg 0 $uchan "" [spdb_get $unick $uhand $utext] } #------------------------------------------------------------------------- proc spdb_msg_spede {unick uhost uhand utext} { spdb_msg 0 $unick "" [spdb_get $unick $uhand $utext] } #------------------------------------------------------------------------- proc spdb_pub_add {unick uhost uhand uchan utext} { spdb_add $utext $unick $uhost $uchan 1 } #------------------------------------------------------------------------- proc spdb_pub_rm {unick uhost uhand uchan utext} { spdb_rm $utext $unick $uhost $uchan 1 } #------------------------------------------------------------------------- proc spdb_pub_find {unick uhost uhand uchan utext} { spdb_search $unick $uhand $uchan $utext 1 } #------------------------------------------------------------------------- proc spdb_msg_find {unick uhost uhand utext} { spdb_search $unick $uhand "" $utext 0 } #------------------------------------------------------------------------- # end of script