Mercurial > hg > egg-tcls
view quotedb.tcl @ 25:65f94adedc98
Import refactored QuoteDB script.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 05 Sep 2011 18:39:19 +0300 |
parents | |
children | 3f999371de82 |
line wrap: on
line source
########################################################################## # # QuoteDB v1.0 by ccr/TNSP <ccr@tnsp.org> # (C) Copyright 2003-2011 Tecnic Software productions (TNSP) # # 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 ### # Path and filename of the SQLite database set qdb_file "quotedb.sqlite" # 1 = Verbose: Say messages PUBLIC when SPEDE is OK, bad, etc. # 0 = Quiet : Say privately set qdb_verbose 0 # 1 = Put some info to bot's Logfile when doing stuff... # 0 = Don't. set qdb_logmsg 1 # What IRC "command" should we use to send messages: # (Valid alternatives are "PRIVMSG" and "NOTICE") set qdb_preferredmsg "PRIVMSG" ### ### Search related settings ### # How many SPEDE's should the !spedefind command show (maximum limit) set qdb_showmax_pub 3 # For private-search, this is the default limit (user can change it) set qdb_showmax_priv 5 ########################################################################## # No need to look below this line ########################################################################## #------------------------------------------------------------------------- set qdb_name "QuoteDB" set qdb_version "1.0" #------------------------------------------------------------------------- ### Binding initializations bind pub - !spedefind spede_pub_find bind pub - !spedeadd spede_pub_add #bind pub - !spededel spede_pub_del bind pub - !spede spede_pub_get bind msg - spedefind spede_msg_find bind msg - spede spede_msg_get proc spede_pub_get {unick uhost uhand uchan utext} { qdb_msg 0 $uchan "" [qdb_get "spededb" $unick $uhand $utext] } proc spede_msg_get {unick uhost uhand utext} { qdb_msg 0 $unick "" [qdb_get "spededb" $unick $uhand $utext] } proc spede_pub_add {unick uhost uhand uchan utext} { qdb_add "spededb" $utext $unick $uhost $uchan 1 } proc spede_pub_find {unick uhost uhand uchan utext} { qdb_find "spededb" $unick $uhand $uchan $utext 1 } proc spede_msg_find {unick uhost uhand utext} { qdb_find "spededb" $unick $uhand "" $utext 0 } #bind pub - !mnfind mn_pub_find #bind pub - !mnadd mn_pub_add #bind pub - !mndel mn_pub_del #bind pub - !mn mn_pub_get #bind msg - mnfind mn_msg_find #bind msg - mn mn_msg_get #bind pub - !tuksufind tuksu_pub_find #bind pub - !tuksuadd tuksu_pub_add #bind pub - !tuksudel tuksu_pub_del #bind pub - !tuksu tuksu_pub_get #bind msg - tuksufind tuksu_msg_find #bind msg - tuksu tuksu_msg_get #------------------------------------------------------------------------- ### Initialization messages set qdb_message "$qdb_name v$qdb_version by ccr/TNSP" putlog "$qdb_message" ### Require packages package require sqlite3 ### SQLite database initialization if {[catch {sqlite3 quotedb $qdb_file} uerrmsg]} { putlog " Could not open SQLite3 database '$qdb_file': $uerrmsg" exit 2 } #------------------------------------------------------------------------- ### Utility functions proc qdb_log {jarg} { global qdb_logmsg qdb_name if {$qdb_logmsg != 0} { putlog "$qdb_name: $jarg" } } proc qdb_ctime {utime} { if {$utime == "" || $utime == "*"} { set utime 0 } return [clock format $utime -format "%d.%m.%Y %H:%M"] } proc qdb_msg {apublic anick achan amsg} { global qdb_preferredmsg if {$apublic == 0} { putserv "$qdb_preferredmsg $anick :$amsg" } else { putserv "$qdb_preferredmsg $achan :$anick, $amsg" } } proc qdb_escape {str} { return [string map {' ''} $str] } #------------------------------------------------------------------------- proc qdb_add {utable utext unick uhost uchan upublic} { global quotedb if {"x$utext" == "x"} { qdb_msg $upublic $unick $uchan "pyh." return 0 } set usql "INSERT INTO $utable (utime,utext,user,host,chan) VALUES ([unixtime], '[qdb_escape $utext]', '[qdb_escape $unick]', '[qdb_escape $uhost]', '[qdb_escape $uchan]')" if {[catch {quotedb eval $usql} uerrmsg]} { qdb_log "$uerrmsg on SQL:\n$sql" qdb_msg $upublic $unick $uchan "virhe sörkittäessä tietokantaa. uliskaa." return 0 } set quoteID [quotedb last_insert_rowid] set numRows [quotedb ### Log some data qdb_log "Added SPEDE #$quoteID ($unick@$uchan): $utext" ### Report success to user qdb_msg $upublic $unick $uchan "tietokantaa sörkitty (#$smax / $sindex), kiitos." return 1 } #------------------------------------------------------------------------- proc qdb_get {utable unick uhand uindex} { global quotedb set usql "SELECT id AS quoteID, utime AS utime, utext AS utext, user AS uuser, host AS uhost FROM $utable" if {[string match "^\[0-9\]+$" $uindex]} { set usql "$usql WHERE id=$uindex" } else { set usql "$usql ORDER BY RANDOM() LIMIT 1" } set uresults 0 quotedb eval $usql { incr uresults return "#${quoteID}: $utext ($uuser)" } return "ei löydy." } #------------------------------------------------------------------------- proc qdb_find {utable unick uhand uchan utext upublic} { global quotedb qdb_showmax_pub qdb_showmax_priv if {$upublic == 0} { set ulimit $qdb_showmax_priv } else { set ulimit $qdb_showmax_pub } ### Parse the given command qdb_log "$unick/$uhand/$uchan searched $utable: $utext" set ftokens [split $utext " "] foreach ftoken $ftokens { set fprefix [string range $ftoken 0 0] set fpattern [string range $ftoken 1 end] if {$fprefix == "-"} { lappend fpatlist "utext NOT LIKE '%[qdb_escape $fpattern]%'" } elseif {$fprefix == "%"} { lappend fpatlist "user='[qdb_escape $fpattern]'" } elseif {$fprefix == "@"} { # foo } else { lappend fpatlist "utext LIKE '%[qdb_escape $ftoken]%'" } } set uresults 0 set usql "SELECT id AS quoteID, utime AS utime, utext AS utext, user AS uuser, host AS uhost FROM $utable WHERE [join $fpatlist " AND "] ORDER BY utime DESC LIMIT $ulimit" quotedb eval $usql { incr uresults qdb_msg $upublic $unick $uchan "#${quoteID}: $utext ($uuser@[qdb_ctime $utime])" } if {$uresults == 0} { qdb_msg $upublic $unick $uchan "ei löydy." } return 0 } #------------------------------------------------------------------------- # end of script