changeset 26:3f999371de82

Actually fix the quotedb script to work.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 05 Sep 2011 19:20:43 +0300
parents 65f94adedc98
children 6e381916b016
files quotedb.tcl
diffstat 1 files changed, 81 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/quotedb.tcl	Mon Sep 05 18:39:19 2011 +0300
+++ b/quotedb.tcl	Mon Sep 05 19:20:43 2011 +0300
@@ -27,6 +27,9 @@
 set qdb_preferredmsg "PRIVMSG"
 
 
+# Foo.
+set qdb_max_deltime 15
+
 ###
 ### Search related settings
 ###
@@ -50,31 +53,46 @@
 ### Binding initializations
 bind pub - !spedefind spede_pub_find
 bind pub - !spedeadd spede_pub_add
-#bind pub - !spededel spede_pub_del
+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_add  {unick uhost uhand uchan utext} { qdb_add "spededb" $utext $uhand $unick $uhost $uchan 1 }
+proc spede_pub_del  {unick uhost uhand uchan utext} { qdb_del "spededb" $utext $uhand $unick $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 - !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
+
+proc mn_pub_get  {unick uhost uhand uchan utext} { qdb_msg 0 $uchan "" [qdb_get "mndb" $unick $uhand $utext] }
+proc mn_msg_get  {unick uhost uhand utext}       { qdb_msg 0 $unick "" [qdb_get "mndb" $unick $uhand $utext] }
+proc mn_pub_add  {unick uhost uhand uchan utext} { qdb_add "mndb" $utext $uhand $unick $uhost $uchan 1 }
+proc mn_pub_del  {unick uhost uhand uchan utext} { qdb_del "mndb" $utext $uhand $unick $uchan 1 }
+proc mn_pub_find {unick uhost uhand uchan utext} { qdb_find "mndb" $unick $uhand $uchan $utext 1 }
+proc mn_msg_find {unick uhost uhand utext}       { qdb_find "mndb" $unick $uhand "" $utext 0 }
 
 
-#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
+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
+
+proc tuksu_pub_get  {unick uhost uhand uchan utext} { qdb_msg 0 $uchan "" [qdb_get "tuksudb" $unick $uhand $utext] }
+proc tuksu_msg_get  {unick uhost uhand utext}       { qdb_msg 0 $unick "" [qdb_get "tuksudb" $unick $uhand $utext] }
+proc tuksu_pub_add  {unick uhost uhand uchan utext} { qdb_add "tuksudb" $utext $uhand $unick $uhost $uchan 1 }
+proc tuksu_pub_del  {unick uhost uhand uchan utext} { qdb_del "tuksudb" $utext $uhand $unick $uchan 1 }
+proc tuksu_pub_find {unick uhost uhand uchan utext} { qdb_find "tuksudb" $unick $uhand $uchan $utext 1 }
+proc tuksu_msg_find {unick uhost uhand utext}       { qdb_find "tuksudb" $unick $uhand "" $utext 0 }
 
 
 #-------------------------------------------------------------------------
@@ -126,15 +144,15 @@
 
 
 #-------------------------------------------------------------------------
-proc qdb_add {utable utext unick uhost uchan upublic} {
+proc qdb_add {utable utext uhand unick uhost uchan upublic} {
   global quotedb
 
-  if {"x$utext" == "x"} {
+  if {"x$utext" == "x" || "$uhand" == "x" || $uhand == {}} {
     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]')"
+  set usql "INSERT INTO $utable (utime,utext,user,host,chan) VALUES ([unixtime], '[qdb_escape $utext]', '[qdb_escape $uhand]', '[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."
@@ -142,31 +160,70 @@
   }
 
   set quoteID [quotedb last_insert_rowid]
-  set numRows [quotedb
+  set numQuotes 0
+#  set numQuotes [quotedb ]
 
   ### Log some data
-  qdb_log "Added SPEDE #$quoteID ($unick@$uchan): $utext"
+  qdb_log "Added SPEDE #$quoteID ($unick/$uhand@$uchan): $utext"
 
   ### Report success to user
-  qdb_msg $upublic $unick $uchan "tietokantaa sörkitty (#$smax / $sindex), kiitos."
+  qdb_msg $upublic $unick $uchan "tietokantaa sörkitty (#$quoteID / $numQuotes), kiitos."
   return 1
 }
 
 
 #-------------------------------------------------------------------------
+proc qdb_del {utable utext uhand unick uchan upublic} {
+  global quotedb qdb_max_deltime
+
+  if {"x$utext" == "x" || "$uhand" == "x" || $uhand == {}} {
+    qdb_msg $upublic $unick $uchan "pyh."
+    return 0
+  }
+
+  set maxdiff [expr $qdb_max_deltime * 60]
+
+  if {[regexp "(\[0-9\]+)" $utext umatch unum]} {
+    set usql "SELECT id AS quoteID, utime AS utime, utext AS utext, user AS uuser, host AS uhost FROM $utable WHERE id=$unum AND user='[qdb_escape $uhand]'"
+  } else {
+    qdb_msg $upublic $unick $uchan "ID # annettava."
+  }
+  
+  quotedb eval $usql {
+    set udiff [expr [unixtime] - $utime]
+    if {$udiff < $maxdiff} {
+      set usql "DELETE FROM $utable WHERE id=$quoteID AND user='[qdb_escape $uhand]'"
+      if {[catch {quotedb eval $usql} uerrmsg]} {
+        qdb_log "$uerrmsg on SQL:\n$sql"
+        qdb_msg $upublic $unick $uchan "virhe sörkittäessä tietokantaa. uliskaa."
+      } else {
+        qdb_msg $upublic $unick $uchan "$utable ID #$quoteID poistettu."
+      }
+      return 0
+    } else {
+      qdb_msg $upublic $unick $uchan "$utable quote #$quoteID vanhempi kuin $qdb_max_deltime minuuttia, ei poisteta."
+      return 0
+    }
+  }
+
+  qdb_msg $upublic $unick $uchan "#$quoteID ei löydy, tai se ei kuulu käyttäjälle '$uhand'."
+}
+
+
+#-------------------------------------------------------------------------
 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"
+  if {[regexp "(\[0-9\]+)" $uindex umatch unum]} {
+    set usql "$usql WHERE id=$unum"
+    qdb_log "$unick/$uhand searched $utable #$unum"
   } else {
     set usql "$usql ORDER BY RANDOM() LIMIT 1"
+    qdb_log "$unick/$uhand get random $utable"
   }
 
-  set uresults 0
   quotedb eval $usql {
-    incr uresults
     return "#${quoteID}: $utext ($uuser)"
   }