changeset 60:ce327469582c

quotedb: Add "update" sub-command for modifying/updating recently added quotes.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 08 Sep 2011 22:16:54 +0300
parents 415c47fcc518
children dd4a5c0542c4
files quotedb.tcl
diffstat 1 files changed, 42 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/quotedb.tcl	Thu Sep 08 22:16:00 2011 +0300
+++ b/quotedb.tcl	Thu Sep 08 22:16:54 2011 +0300
@@ -29,6 +29,7 @@
 
 # Foo.
 set qdb_max_deltime 15
+set qdb_max_modtime 30
 
 ###
 ### Search related settings
@@ -227,6 +228,43 @@
 
 
 #-------------------------------------------------------------------------
+proc qdb_update {ucmd utable unick uhand uchan utext upublic} {
+  global quotedb qdb_max_modtime
+  set maxdiff [expr $qdb_max_modtime * 60]
+
+  if {$uhand == "" || $uhand == {}} {
+    qdb_msg $upublic $unick $uchan "pyh."
+    return 0
+  }
+
+  if {![regexp {^\s*([0-9]+)\s+(.+)$} $utext umatch unum uquote]} {
+    qdb_msg $upublic $unick $uchan "$ucmd update <id> <teksti>"
+    return 0
+  }
+
+  set usql "SELECT utime AS utime FROM $utable WHERE id=$unum AND user='[qdb_escape $uhand]'"
+  quotedb eval $usql {
+    set udiff [expr [unixtime] - $utime]
+    if {$udiff < $maxdiff} {
+      set usql "UPDATE $utable SET utext='[qdb_escape $uquote]', utime=[unixtime] WHERE id=$unum"
+      if {![qdb_sql_exec $upublic $unick $uchan $usql]} {
+        return 0
+      } else {
+        qdb_msg $upublic $unick $uchan "$utable #$unum päivitetty."
+        return 1
+      }
+    } else {
+      qdb_msg $upublic $unick $uchan "$utable quote #$unum vanhempi kuin $qdb_max_modtime minuuttia, ei voida päivittää."
+      return 0
+    }
+  }
+
+  qdb_msg $upublic $unick $uchan "#$unum ei löydy, tai se ei kuulu käyttäjälle '$uhand'."
+  return 0
+}
+
+
+#-------------------------------------------------------------------------
 proc qdb_toplist {ucmd utable unick uchan unum upublic} {
   global quotedb
 
@@ -401,6 +439,9 @@
   } elseif {[regexp {^del\s*([0-9]*)$} $utext umatch unum]} {
     # Delete quote
     qdb_delete $ucmd $utable $unick $uhand $uchan $unum $upublic
+  } elseif {[regexp {^update\s*(.*)$} $utext umatch unum]} {
+    # Modify/update quote
+    qdb_update $ucmd $utable $unick $uhand $uchan $unum $upublic
   } elseif {[regexp {^find\s*(.*)$} $utext umatch unum]} {
     # Find quote(s)
     qdb_find $ucmd $utable $unick $uhand $uchan $unum $upublic
@@ -408,7 +449,7 @@
     # Vote
     qdb_vote $ucmd $utable $unick $uhand $uchan $unum $upublic
   } else {
-    qdb_msg $upublic $unick $uchan "$ucmd add <teksti>|del <id>|find <parametrit>|vote|top3"
+    qdb_msg $upublic $unick $uchan "$ucmd add <teksti>|update <id> <teksti>|del <id>|find <parametrit>|vote|top3"
   }
 }