changeset 513:8abb0e8d54a2

quotedb: Refactor qdb_msg and message handling in general.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 02 May 2020 06:01:13 +0300
parents cc0efa677aa9
children a9cb1a248791
files config.quotedb.example quotedb.tcl
diffstat 2 files changed, 188 insertions(+), 123 deletions(-) [+]
line wrap: on
line diff
--- a/config.quotedb.example	Sat May 02 03:43:37 2020 +0300
+++ b/config.quotedb.example	Sat May 02 06:01:13 2020 +0300
@@ -58,33 +58,47 @@
 ### Messages
 ###
 
-set qdb_msg_invalid_user "Pyh."
+array set qdb_messages {
+
+  "name" "Quote"
 
-set qdb_msg_quote_does_not_exist "Quotea #%1 ei ole."
+  "invalid_user" "Pyh."
 
-set qdb_msg_no_matches "Ei löydy!"
+  "sql_error" "Virhe sörkittäessä @name@-tietokantaa. uliskaa."
+
+  "fmt_get"   "#@1@: @2@ (@3@@@4@, @5@)"
 
-set qdb_msg_you_voted "Äänestit #%1 -> %2, nyt yhteensä %3"
-set qdb_msg_vote_updated "Äänesi #%1 päivitetty -> %2, nyt yhteensä %3"
+  "fmt_search" "#@1@: @2@ (@3@@@4@)"
+  "no_matches" "@name@: #@1@ ei löydy."
 
-set qdb_msg_sql_error "Virhe sörkittäessä tietokantaa. uliskaa."
-set qdb_msg_add_success "Tietokantaa %1 sörkitty (#%2), kiitos."
+  "you_voted" "Äänestit @name@ #@1@ -> @2@, arvosana nyt @3@"
+  "vote_updated" "@name@-äänesi #@1@ päivitetty -> @2@, arvosana nyt @3@"
 
-set qdb_msg_search_fmt "#%1: %2 (%3@%5)"
+  "add_success" "@name@-tietokantaa sörkitty (#@1@), kiitos."
+
+  "toplist_limit" "Top-listan määrän oltava @1@ - @2@."
+  "fmt_toplist" "@1@. #@2@: @3@ (@4@, @5@)"
 
-set qdb_msg_toplist_limit "Top-listan määrän oltava %1-%2."
-set qdb_msg_toplist_fmt "%1. #%2: %3 (%4, %5)"
+  "update_override" " (owner/time override)"
+  "update_ok" "@name@ #@1@ päivitetty@2@."
+  "update_too_old" "@name@ quote #@1@ vanhempi kuin @2@ minuuttia, ei voida päivittää."
+  "update_override" " (owner/time override)"
 
-set qdb_msg_update_override " (owner/time override)"
-set qdb_msg_update_ok "%1 #%2 päivitetty%3."
+  "quote_not_found" "@name@ #@1@ ei löydy, tai se ei kuulu käyttäjälle '@2@'."
+
+  "stats" "@name@-tilastot: @1@ quotea, @2@ annettua ääntä. Top @3@ lisääjät: @4@"
 
-set qdb_msg_update_too_old "%1 quote #%2 vanhempi kuin %3 minuuttia, ei voida päivittää."
-
-set qdb_msg_not_found "%1 #%2 ei löydy, tai se ei kuulu käyttäjälle '%3'."
+  "help"           "@cmd@ <id> \002|\002 add <teksti> \002|\002 update <id> <teksti> \002|\002 del <id> \002|\002 find <parametrit> \002|\002 vote \[<id> \[1 | -1\]\] \002|\002 top <n> \002|\002 stats"
 
-set qdb_msg_stats "%1-tilastot: %2 quotea, %3 annettua ääntä. Top %4 lisääjät: %5"
+  "help_add"       "@cmd@ add <teksti>"
+  "help_update"    "@cmd@ update <id> <teksti>"
+  "help_delete"    "@cmd@ del <id>"
+  "help_vote"      "@cmd@ vote \[<id> \[1|-1\]\]"
 
-set qdb_msg_help "%1 <id>|add <teksti>|update <id> <teksti>|del <id>|find <parametrit>|vote \[<id> \[1|-1\]\]|top <n>|stats"
+  "quote_deleted"  "@name@ #@1@ poistettu@2@."
+  "quote_too_old"  "@name@ quote #@1@ vanhempi kuin @2@ minuuttia, ei poisteta."
+  "quote_does_not_exist" "Quotea #@1@ ei ole."
+}
 
 
 ###
@@ -94,28 +108,46 @@
 # Bind !quote -command for table "quotedb"
 bind pub - !vitsi vitsi_pub_cmd
 bind msg - !vitsi vitsi_msg_cmd
-proc vitsi_pub_cmd {unick uhost uhand uchan utext} { qdb_command "!vitsi" "quotedb" $unick $uhost $uhand $uchan $utext 1 "%2 (#%1)" ""}
-proc vitsi_msg_cmd {unick uhost uhand utext}       { qdb_command "!vitsi" "quotedb" $unick $uhost $uhand "" $utext 0 "%2 (#%1)" ""}
+proc vitsi_pub_cmd {unick uhost uhand uchan utext} { qdb_command "!vitsi" "quotedb" $unick $uhost $uhand $uchan $utext 1 }
+proc vitsi_msg_cmd {unick uhost uhand utext}       { qdb_command "!vitsi" "quotedb" $unick $uhost $uhand "" $utext 0 }
+set qdb_messages(quotedb_fmt_get) "@2@ (#@1@)"
+set qdb_messages(quotedb_urlprefix) ""
+set qdb_messages(quotedb_name) "Vitsi"
 
 
 # Bind !quote -command for table "kissa"
 bind pub - !kissa kissa_pub_cmd
 bind msg - !kissa kissa_msg_cmd
-proc kissa_pub_cmd {unick uhost uhand uchan utext} { qdb_command "!kissa" "kissadb" $unick $uhost $uhand $uchan $utext 1 "kissa %2 (#%1)" ""}
-proc kissa_msg_cmd {unick uhost uhand utext}       { qdb_command "!kissa" "kissadb" $unick $uhost $uhand "" $utext 0 "kissa %2 (#%1)" ""}
+proc kissa_pub_cmd {unick uhost uhand uchan utext} { qdb_command "!kissa" "kissadb" $unick $uhost $uhand $uchan $utext 1 }
+proc kissa_msg_cmd {unick uhost uhand utext}       { qdb_command "!kissa" "kissadb" $unick $uhost $uhand "" $utext 0 }
+set qdb_messages(kissadb_fmt_get) "kissa @2@ (#@1@)"
+set qdb_messages(kissadb_urlprefix) ""
+set qdb_messages(kissadb_name) "Kissa"
+set qdb_messages(kissadb_stats) "Kissa-tilastot: @1@ kissajuttua, @2@ äänestettyä kissamaisuutta. Top @3@ lisääjät: @4@"
+set qdb_messages(kissadb_no_matches) "Kissaa #@1@ ei ole olemassa."
 
 
 bind pub - !spede spede_pub_cmd
 bind msg - !spede spede_msg_cmd
-proc spede_pub_cmd {unick uhost uhand uchan utext} { qdb_command "!spede" "spededb" $unick $uhost $uhand $uchan $utext 1 "" "https://tnsp.org/spede/img/"}
-proc spede_msg_cmd {unick uhost uhand utext}       { qdb_command "!spede" "spededb" $unick $uhost $uhand "" $utext 0 "" "https://tnsp.org/spede/img/"}
+proc spede_pub_cmd {unick uhost uhand uchan utext} { qdb_command "!spede" "spededb" $unick $uhost $uhand $uchan $utext 1 }
+proc spede_msg_cmd {unick uhost uhand utext}       { qdb_command "!spede" "spededb" $unick $uhost $uhand "" $utext 0 }
+set qdb_messages(spededb_urlprefix) "https://tnsp.org/spede/img/"
+set qdb_messages(spededb_name) "Spede"
+set qdb_messages(spededb_stats) "Spede-tilastot: @1@ spedeä, @2@ äänestettyä spedeä. Top @3@ lisääjät: @4@"
+set qdb_messages(spededb_no_matches) "Spedeä #@1@ ei ole olemassa."
+
 
 bind pub - !mn mn_pub_cmd
 bind msg - !mn mn_msg_cmd
-proc mn_pub_cmd {unick uhost uhand uchan utext}    { qdb_command "!mn" "mndb" $unick $uhost $uhand $uchan $utext 1 "" "https://tnsp.org/mndb/img/" }
-proc mn_msg_cmd {unick uhost uhand utext}          { qdb_command "!mn" "mndb" $unick $uhost $uhand "" $utext 0 "" "https://tnsp.org/mndb/img/"}
+proc mn_pub_cmd {unick uhost uhand uchan utext}    { qdb_command "!mn" "mndb" $unick $uhost $uhand $uchan $utext 1 }
+proc mn_msg_cmd {unick uhost uhand utext}          { qdb_command "!mn" "mndb" $unick $uhost $uhand "" $utext 0 }
+set qdb_messages(mndb_urlprefix) "https://tnsp.org/mndb/img/"
+set qdb_messages(mndb_name) "MN"
+
 
 bind pub - !tuksu tuksu_pub_cmd
 bind msg - !tuksu tuksu_msg_cmd
-proc tuksu_pub_cmd {unick uhost uhand uchan utext} { qdb_command "!tuksu" "tuksudb" $unick $uhost $uhand $uchan $utext 1 "" "https://tnsp.org/tuksu/img/"}
-proc tuksu_msg_cmd {unick uhost uhand utext}       { qdb_command "!tuksu" "tuksudb" $unick $uhost $uhand "" $utext 0 "" "https://tnsp.org/tuksu/img/"}
+proc tuksu_pub_cmd {unick uhost uhand uchan utext} { qdb_command "!tuksu" "tuksudb" $unick $uhost $uhand $uchan $utext 1 }
+proc tuksu_msg_cmd {unick uhost uhand utext}       { qdb_command "!tuksu" "tuksudb" $unick $uhost $uhand "" $utext 0 }
+set qdb_messages(tuksudb_urlprefix) "https://tnsp.org/tuksu/img/"
+set qdb_messages(tuksudb_name) "Tuksu"
--- a/quotedb.tcl	Sat May 02 03:43:37 2020 +0300
+++ b/quotedb.tcl	Sat May 02 06:01:13 2020 +0300
@@ -50,80 +50,117 @@
   }
 }
 
-proc qdb_msg {apublic anick achan amsg {aargs {}}} {
-  global qdb_preferredmsg
+
+proc qdb_qm {utable uid} {
+  global qdb_messages
+  set qid "${utable}_${uid}"
+
+  if {[info exists qdb_messages($qid)]} {
+    return $qdb_messages($qid)
+  } elseif {[info exists qdb_messages($uid)]} {
+    return $qdb_messages($uid)
+  } else {
+    return $uid
+  }
+}
+
+
+proc qdb_msg {acmd atable apublic anick achan aid {aargs {}}} {
+  global qdb_preferredmsg qdb_messages
+
+  # Get message
+  set amsg [qdb_qm $atable $aid]
+  set aname [qdb_qm $atable "name"]
+
+  # Map constant tokens
+  set amsg [string map [list "@cmd@" $acmd] $amsg]
+  set amsg [string map [list "@name@" $aname] $amsg]
+  set amsg [string map [list "@nick@" $anick] $amsg]
+  set amsg [string map [list "@chan@" $achan] $amsg]
+
+  # Map variable tokens
   set narg 1
   foreach marg $aargs {
-    set amsg [string map [list "%$narg" $marg] $amsg]
+    set amsg [string map [list "@$narg@" $marg] $amsg]
     incr narg
   }
+
   utl_msg_do $qdb_preferredmsg $apublic $anick $achan $amsg
 }
 
 
-proc qdb_sql_exec { upublic unick uchan usql } {
-  global qdb_handle qdb_msg_sql_error
+proc qdb_sql_exec {ucmd utable upublic unick uchan usql} {
+  global qdb_handle
   if {[catch {qdb_handle eval $usql} uerrmsg]} {
     qdb_log "$uerrmsg on SQL:\n$usql"
-    qdb_msg $upublic $unick $uchan $qdb_msg_sql_error
+    qdb_msg $ucmd $utable $upublic $unick $uchan "sql_error"
     return 0
   }
   return 1
 }
 
-proc qdb_valid_user {upublic unick uchan uhand} {
-  global qdb_msg_invalid_user
+
+proc qdb_valid_user {ucmd utable upublic unick uchan uhand} {
   if {$uhand == "" || $uhand == {}} {
-    qdb_msg $upublic $unick $uchan $qdb_msg_invalid_user
+    qdb_msg $ucmd $utable $upublic $unick $uchan "invalid_user"
     return 0
   }
   return 1
 }
 
+
+proc qdb_get_rating_for_id {utable uid} {
+  set usql "SELECT total(${utable}_votes.vote) AS qrating FROM ${utable}_votes WHERE urlid=${uid}"
+  set qrating 0
+  qdb_handle eval $usql { set urating $qrating }
+  return $qrating
+}
+
+
 #-------------------------------------------------------------------------
-proc qdb_add {ucmd utable unick uhost uhand uchan utext upublic} {
-  global qdb_handle qdb_msg_add_success
+proc qdb_add {ucmd utable upublic unick uhost uhand uchan utext} {
+  global qdb_handle
 
-  if {![qdb_valid_user $upublic $unick $uchan $uhand]} {
+  if {![qdb_valid_user $ucmd $utable $upublic $unick $uchan $uhand]} {
     return 0
   }
 
   if {$utext == "" || $utext == {}} {
-    qdb_msg $upublic $unick $uchan "$ucmd add <teksti>"
+    qdb_msg $ucmd $utable $upublic $unick $uchan "help_add"
     return 0
   }
 
   set usql "INSERT INTO $utable (utime,utext,user,host,chan) VALUES ([unixtime], '[utl_escape $utext]', '[utl_escape $uhand]', '[utl_escape $uhost]', '[utl_escape $uchan]')"
-  if {![qdb_sql_exec $upublic $unick $uchan $usql]} {
+  if {![qdb_sql_exec $ucmd $utable $upublic $unick $uchan $usql]} {
     return 0
   }
 
   set quoteID [qdb_handle last_insert_rowid]
 #  set numQuotes 0
 #  set usql "SELECT count(*) AS numQuotes FROM $utable"
-#  if {![qdb_sql_exec $upublic $unick $uchan $usql]} { return 0 }
+#  if {![qdb_sql_exec $ucmd $utable $upublic $unick $uchan $usql]} { return 0 }
 
   ### Log some data
   qdb_log "Added $utable #$quoteID ($unick/$uhand@$uchan): $utext"
 
   ### Report success to user
-  qdb_msg $upublic $unick $uchan $qdb_msg_add_success [list $utable $quoteID]
+  qdb_msg $ucmd $utable $upublic $unick $uchan "add_success" [list $quoteID]
   return 1
 }
 
 
 #-------------------------------------------------------------------------
-proc qdb_delete {ucmd utable unick uhand uchan utext upublic} {
-  global qdb_handle qdb_max_deltime qdb_msg_not_found
+proc qdb_delete {ucmd utable upublic unick uhost uhand uchan utext} {
+  global qdb_handle qdb_max_deltime
 
   set maxdiff [expr $qdb_max_deltime * 60]
 
-  if {![qdb_valid_user $upublic $unick $uchan $uhand]} {
+  if {![qdb_valid_user $ucmd $utable $upublic $unick $uchan $uhand]} {
     return 0
   }
 
   if {![regexp {^\s*([0-9]+)$} $utext umatch unum]} {
-    qdb_msg $upublic $unick $uchan "$ucmd del <id>"
+    qdb_msg $ucmd $utable $upublic $unick $uchan "help_delete"
     return 0
   }
 
@@ -133,47 +170,46 @@
     append usql " AND user='[utl_escape $uhand]'"
     set qextra ""
   } else {
-    set qextra " (owner/time override)"
+    set qextra [qdb_qm $utable "update_override"]
   }
 
   qdb_handle eval $usql {
     set udiff [expr [unixtime] - $utime]
     if {$udiff < $maxdiff || $qoverride} {
       set usql "DELETE FROM $utable WHERE id=$unum"
-      if {![qdb_sql_exec $upublic $unick $uchan $usql]} {
+      if {![qdb_sql_exec $ucmd $utable $upublic $unick $uchan $usql]} {
         return 0
       } else {
         set usql "DELETE FROM ${utable}_votes WHERE urlid=$unum"
-        if {![qdb_sql_exec $upublic $unick $uchan $usql]} {
+        if {![qdb_sql_exec $ucmd $utable $upublic $unick $uchan $usql]} {
           return 0
         }
-        qdb_msg $upublic $unick $uchan "$utable #$unum poistettu$qextra."
+        qdb_msg $ucmd $utable $upublic $unick $uchan "quote_deleted" [list $unum $qextra]
         return 1
       }
     } else {
-      qdb_msg $upublic $unick $uchan "$utable quote #$unum vanhempi kuin $qdb_max_deltime minuuttia, ei poisteta."
+      qdb_msg $ucmd $utable $upublic $unick $uchan "quote_too_old" [list $unum $qdb_max_deltime]
       return 0
     }
   }
 
-  qdb_msg $upublic $unick $uchan $qdb_msg_not_found [list $utable $unum $uhand]
+  qdb_msg $ucmd $utable $upublic $unick $uchan "quote_not_found" [list $unum $uhand]
   return 0
 }
 
 
 #-------------------------------------------------------------------------
-proc qdb_update {ucmd utable unick uhand uchan utext upublic} {
-  global qdb_handle qdb_max_modtime qdb_msg_update_override
-  global qdb_msg_update_ok qdb_msg_update_too_old qdb_msg_not_found
+proc qdb_update {ucmd utable upublic unick uhost uhand uchan utext} {
+  global qdb_handle qdb_max_modtime
 
   set maxdiff [expr $qdb_max_modtime * 60]
 
-  if {![qdb_valid_user $upublic $unick $uchan $uhand]} {
+  if {![qdb_valid_user $ucmd $utable $upublic $unick $uchan $uhand]} {
     return 0
   }
 
   if {![regexp {^\s*([0-9]+)\s+(.+)$} $utext umatch unum uquote]} {
-    qdb_msg $upublic $unick $uchan "$ucmd update <id> <teksti>"
+    qdb_msg $ucmd $utable $upublic $unick $uchan "help_update"
     return 0
   }
 
@@ -183,37 +219,41 @@
     append usql " AND user='[utl_escape $uhand]'"
     set qextra ""
   } else {
-    set qextra $qdb_msg_update_override
+    set qextra [qdb_qm $utable "update_override"]
   }
 
   qdb_handle eval $usql {
     set udiff [expr [unixtime] - $utime]
     if {$udiff < $maxdiff || $qoverride} {
       set usql "UPDATE $utable SET utext='[utl_escape $uquote]' WHERE id=$unum"
-      if {![qdb_sql_exec $upublic $unick $uchan $usql]} {
+      if {![qdb_sql_exec $ucmd $utable $upublic $unick $uchan $usql]} {
         return 0
       } else {
-        qdb_msg $upublic $unick $uchan $qdb_msg_update_ok [list $utable $unum $qextra]
+        qdb_msg $ucmd $utable $upublic $unick $uchan "update_ok" [list $unum $qextra]
         return 1
       }
     } else {
-      qdb_msg $upublic $unick $uchan $qdb_msg_update_too_old [list $utable $unum $qdb_max_modtime]
+      qdb_msg $ucmd $utable $upublic $unick $uchan "update_too_old" [list $unum $qdb_max_modtime]
       return 0
     }
   }
 
-  qdb_msg $upublic $unick $uchan $qdb_msg_not_found [list $utable $unum $uhand]
+  qdb_msg $ucmd $utable $upublic $unick $uchan "quote_not_found" [list $unum $uhand]
   return 0
 }
 
 
 #-------------------------------------------------------------------------
-proc qdb_toplist {utable unick uchan unum upublic} {
-  global qdb_handle qdb_msg_toplist_limit qdb_msg_toplist_fmt
+proc qdb_toplist {ucmd utable upublic unick uhost uhand uchan utext unum} {
+  global qdb_handle
   global qdb_toplist_min qdb_toplist_max
 
+  if {$unum == {} || $unum == ""} {
+    set unum $qdb_toplist_min
+  }
+
   if {$unum < $qdb_toplist_min || $unum > $qdb_toplist_max} {
-    qdb_msg $upublic $unick $uchan $qdb_msg_toplist_limit [list $qdb_toplist_min $qdb_toplist_max]
+    qdb_msg $ucmd $utable $upublic $unick $uchan "toplist_limit" [list $qdb_toplist_min $qdb_toplist_max]
     return 0
   }
 
@@ -221,7 +261,7 @@
   set usql "SELECT total(${utable}_votes.vote) AS rating, ${utable}.id AS quoteID, ${utable}.utext AS utext, ${utable}.utime AS utime,${utable}.user AS uuser, ${utable}.id AS uid FROM ${utable}_votes INNER JOIN $utable ON ${utable}_votes.urlid = ${utable}.id GROUP BY ${utable}.id ORDER BY rating DESC LIMIT $unum"
   qdb_handle eval $usql {
     incr uresults
-    qdb_msg $upublic $unick $uchan $qdb_msg_toplist_fmt [list $uresults $quoteID $utext $uuser $rating]
+    qdb_msg $ucmd $utable $upublic $unick $uchan "fmt_toplist" [list $uresults $quoteID $utext $uuser $rating]
   }
 
   return 0
@@ -229,19 +269,10 @@
 
 
 #-------------------------------------------------------------------------
-proc qdb_get_rating_for_id {utable uid} {
-  set usql "SELECT total(${utable}_votes.vote) AS qrating FROM ${utable}_votes WHERE urlid=${uid}"
-  set qrating 0
-  qdb_handle eval $usql { set urating $qrating }
-  return $qrating
-}
+proc qdb_vote {ucmd utable upublic unick uhost uhand uchan utext} {
+  global qdb_handle
 
-
-#-------------------------------------------------------------------------
-proc qdb_vote {ucmd utable unick uhand uchan utext upublic} {
-  global qdb_handle qdb_msg_you_voted qdb_msg_quote_does_not_exist qdb_msg_vote_updated
-
-  if {![qdb_valid_user $upublic $unick $uchan $uhand]} {
+  if {![qdb_valid_user $ucmd $utable $upublic $unick $uchan $uhand]} {
     return 0
   }
 
@@ -252,7 +283,7 @@
     qdb_handle eval $usql { set uid $qid }
   } elseif {[regexp {^\s*([0-9]+)$} $utext umatch uid]} {
   } elseif {![regexp {^\s*([0-9]+)\s+(-1|1)$} $utext umatch uid uvote]} {
-    qdb_msg $upublic $unick $uchan "$ucmd vote \[<id> \[1|-1\]\]"
+    qdb_msg $ucmd $utable $upublic $unick $uchan "help_vote"
     return 0
   }
 
@@ -261,7 +292,7 @@
   set usql "SELECT id AS qid FROM ${utable} WHERE id=$uid"
   qdb_handle eval $usql { incr uresults }
   if {$uresults == 0} {
-    qdb_msg $upublic $unick $uchan $qdb_msg_quote_does_not_exist [list $uid]
+    qdb_msg $ucmd $utable $upublic $unick $uchan "quote_does_not_exist" [list $uid]
     return 0
   }
 
@@ -270,10 +301,10 @@
   qdb_handle eval $usql {
     # Yes, update the previous vote
     set usql "UPDATE ${utable}_votes SET vote=$uvote WHERE id=$qid"
-    if {[qdb_sql_exec $upublic $unick $uchan $usql]} {
+    if {[qdb_sql_exec $ucmd $utable $upublic $unick $uchan $usql]} {
       set urating [qdb_get_rating_for_id $utable $uid]
       qdb_log "$uhand changed vote to $uvote on #$uid / $utable, total $urating"
-      qdb_msg $upublic $unick $uchan $qdb_msg_vote_updated [list $uid $uvote $urating]
+      qdb_msg $ucmd $utable $upublic $unick $uchan "vote_updated" [list $uid $uvote $urating]
       return 1
     } else {
       return 0
@@ -282,19 +313,20 @@
 
   # No previous votes, insert new
   set usql "INSERT INTO ${utable}_votes (user,urlid,vote) VALUES ('[utl_escape $uhand]',$uid,$uvote)"
-  if {[qdb_sql_exec $upublic $unick $uchan $usql]} {
+  if {[qdb_sql_exec $ucmd $utable $upublic $unick $uchan $usql]} {
     set urating [qdb_get_rating_for_id $utable $uid]
     qdb_log "$uhand voted $uvote on #$uid / $utable, total $urating"
-    qdb_msg $upublic $unick $uchan $qdb_msg_you_voted [list $uid $uvote $urating]
+    qdb_msg $ucmd $utable $upublic $unick $uchan "you_voted" [list $uid $uvote $urating]
   }
 }
 
 
 #-------------------------------------------------------------------------
-proc qdb_get {ufmt utable unick uchan uindex upublic urlprefix} {
-  global qdb_handle qdb_msg_no_matches
+proc qdb_get {ucmd utable upublic unick uhost uhand uchan utext uindex} {
+  global qdb_handle
 
-  set usql "SELECT total(${utable}_votes.vote) AS uvote, ${utable}.id AS quoteID, ${utable}.utext AS utext, ${utable}.utime AS utime, ${utable}.user AS uuser FROM ${utable} LEFT JOIN ${utable}_votes ON ${utable}_votes.urlid = ${utable}.id"
+  set usql "SELECT total(${utable}_votes.vote) AS uvote, ${utable}.id AS quoteID, ${utable}.utext AS uquote, ${utable}.utime AS utime, ${utable}.user AS uuser FROM ${utable} LEFT JOIN ${utable}_votes ON ${utable}_votes.urlid = ${utable}.id"
+
   if {$uindex >= 0} {
     append usql " WHERE ${utable}.id=$uindex GROUP BY ${utable}.id"
     qdb_log "$unick searched $utable #$uindex"
@@ -309,26 +341,24 @@
     return 0
   }
 
+  set urlprefix [qdb_qm $utable "urlprefix"]
+
   qdb_handle eval $usql {
-    if {[string range $utext 0 3] == "img:"} {
-      set utext "${urlprefix}[string range $utext 4 end]"
+    if {[string range $uquote 0 3] == "img:"} {
+      set uquote "$urlprefix[string range $uquote 4 end]"
     }
-    if {$ufmt != ""} {
-      qdb_msg $upublic $unick $uchan $ufmt [list $quoteID $utext $uuser [utl_ctime $utime] $uvote]
-    } else {
-      qdb_msg $upublic $unick $uchan "#${quoteID}: $utext ($uuser@[utl_ctime $utime], $uvote)"
-    }
+    qdb_msg $ucmd $utable $upublic $unick $uchan "fmt_get" [list $quoteID $uquote $uuser [utl_ctime $utime] $uvote]
     return 1
   }
 
-  qdb_msg $upublic $unick $uchan $qdb_msg_no_matches [list $unick]
+  qdb_msg $ucmd $utable $upublic $unick $uchan "no_matches" [list $uindex]
   return 0
 }
 
 
 #-------------------------------------------------------------------------
-proc qdb_stats {utable unick uchan upublic} {
-  global qdb_handle qdb_msg_stats
+proc qdb_stats {ucmd utable upublic unick uhost uhand uchan utext} {
+  global qdb_handle
 
   set usql "SELECT count(*) AS nvotes FROM ${utable}_votes"
   qdb_handle eval $usql {}
@@ -346,16 +376,17 @@
     lappend ntop "${user} (${uquotes})"
   }
 
-  qdb_msg $upublic $unick $uchan $qdb_msg_stats [list $utable $nquotes $nvotes $nindex [join $ntop ", "]]
+  qdb_msg $ucmd $utable $upublic $unick $uchan "stats" [list $nquotes $nvotes $nindex [join $ntop ", "]]
 
   return 0
 }
 
 
 #-------------------------------------------------------------------------
-proc qdb_find {utable unick uhand uchan utext upublic} {
-  global qdb_handle qdb_showmax_pub qdb_showmax_priv qdb_msg_no_matches qdb_msg_search_fmt
+proc qdb_find {ucmd utable upublic unick uhost uhand uchan utext} {
+  global qdb_handle qdb_showmax_pub qdb_showmax_priv
 
+  # Limit results based on public/private
   if {$upublic == 0} {
     set ulimit $qdb_showmax_priv
   } else {
@@ -394,11 +425,11 @@
   set usql "SELECT id AS quoteID, utime AS utime, utext AS utext, user AS uuser FROM $utable $fquery ORDER BY utime DESC LIMIT $ulimit"
   qdb_handle eval $usql {
     incr uresults
-    qdb_msg $upublic $unick $uchan $qdb_msg_search_fmt [list $quoteID $utext $uuser $utime [utl_ctime $utime]]
+    qdb_msg $ucmd $utable $upublic $unick $uchan "fmt_search" [list $quoteID $utext $uuser [utl_ctime $utime]]
   }
 
   if {$uresults == 0} {
-    qdb_msg $upublic $unick $uchan $qdb_msg_no_matches [list $unick]
+    qdb_msg $ucmd $utable $upublic $unick $uchan "no_matches"
     return 0
   }
   return 1
@@ -406,48 +437,50 @@
 
 
 #-------------------------------------------------------------------------
-proc qdb_command {ucmd utable unick uhost uhand uchan utext upublic ufmt_get urlprefix} {
-  global qdb_msg_help qdb_channels
+proc qdb_command {ucmd utable unick uhost uhand uchan utext upublic} {
+  global qdb_channels
 
+  # Check if command comes from allowed channel or is private
   if {$upublic && ![utl_match_delim_list $qdb_channels $uchan]} {
     return 0
   }
 
+  # Trim argument text
   set utext [string trim $utext]
 
   if {$utext == "" || $utext == {}} {
     # No arguments, assume random query
-    qdb_get $ufmt_get $utable $unick $uchan -1 $upublic $urlprefix
+    qdb_get $ucmd $utable $upublic $unick $uhost $uhand $uchan $utext -1
   } elseif {[regexp {^(stat|stats|tilasto|tilastot)$} $utext umatch]} {
     # Statistics
-    qdb_stats $utable $unick $uchan $upublic
+    qdb_stats $ucmd $utable $upublic $unick $uhost $uhand $uchan $utext
   } elseif {[regexp {^(last|latest|uusin|viimeisin)$} $utext umatch]} {
     # Latest added
-    qdb_get $ufmt_get $utable $unick $uchan -2 $upublic $urlprefix
+    qdb_get $ucmd $utable $upublic $unick $uhost $uhand $uchan $utext -2
   } elseif {[regexp {^([0-9]+)$} $utext umatch unum]} {
-    # Numeric argument, assume index query
-    qdb_get $ufmt_get $utable $unick $uchan $unum $upublic $urlprefix
-  } elseif {[regexp {^top\s*([0-9]+)$} $utext umatch unum]} {
+    # Numeric argument, assume id query
+    qdb_get $ucmd $utable $upublic $unick $uhost $uhand $uchan $utext $unum
+  } elseif {[regexp {^top\s*([0-9]+)?$} $utext umatch uargs]} {
     # Toplist of quotes
-    qdb_toplist $utable $unick $uchan $unum $upublic
-  } elseif {[regexp {^add\s*(.*)$} $utext umatch unum]} {
+    qdb_toplist $ucmd $utable $upublic $unick $uhost $uhand $uchan $utext $uargs
+  } elseif {[regexp {^add\s*(.*)$} $utext umatch uargs]} {
     # Add quote
-    qdb_add $ucmd $utable $unick $uhost $uhand $uchan $unum $upublic
-  } elseif {[regexp {^del\s*([0-9]*)$} $utext umatch unum]} {
+    qdb_add $ucmd $utable $upublic $unick $uhost $uhand $uchan $uargs
+  } elseif {[regexp {^del\s*([0-9]*)$} $utext umatch uargs]} {
     # Delete quote
-    qdb_delete $ucmd $utable $unick $uhand $uchan $unum $upublic
-  } elseif {[regexp {^update\s*(.*)$} $utext umatch unum]} {
+    qdb_delete $ucmd $utable $upublic $unick $uhost $uhand $uchan $uargs
+  } elseif {[regexp {^update\s*(.*)$} $utext umatch uargs]} {
     # Modify/update quote
-    qdb_update $ucmd $utable $unick $uhand $uchan $unum $upublic
-  } elseif {[regexp {^find\s*(.*)$} $utext umatch unum]} {
+    qdb_update $ucmd $utable $upublic $unick $uhost $uhand $uchan $uargs
+  } elseif {[regexp {^find\s*(.*)$} $utext umatch uargs]} {
     # Find quote(s)
-    qdb_find $utable $unick $uhand $uchan $unum $upublic
-  } elseif {[regexp {^vote\s*(.*)$} $utext umatch unum]} {
+    qdb_find $ucmd $utable $upublic $unick $uhost $uhand $uchan $uargs
+  } elseif {[regexp {^vote\s*(.*)$} $utext umatch uargs]} {
     # Vote
-    qdb_vote $ucmd $utable $unick $uhand $uchan $unum $upublic
+    qdb_vote $ucmd $utable $upublic $unick $uhost $uhand $uchan $uargs
   } else {
     # Help/usage
-    qdb_msg $upublic $unick $uchan $qdb_msg_help [list $ucmd]
+    qdb_msg $ucmd $utable $upublic $unick $uchan "help"
   }
 }