changeset 283:5b997bee1745

quotedb: Move various message texts to configuration file. Not everything yet.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 23 Jan 2015 13:20:17 +0200
parents 3893192d7aff
children 4ca3b5e39ca2
files config.quotedb.example quotedb.tcl
diffstat 2 files changed, 100 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/config.quotedb.example	Fri Jan 23 12:32:06 2015 +0200
+++ b/config.quotedb.example	Fri Jan 23 13:20:17 2015 +0200
@@ -28,7 +28,10 @@
 # (Valid alternatives are "PRIVMSG" and "NOTICE")
 set qdb_preferredmsg "PRIVMSG"
 
-# Foo.
+
+###
+### Search/command related settings
+###
 set qdb_max_deltime 15
 set qdb_max_modtime 30
 
@@ -41,6 +44,38 @@
 
 
 ###
+### Messages
+###
+
+set qdb_msg_invalid_user "Pyh."
+
+set qdb_msg_quote_does_not_exist "Quotea #%1 ei ole."
+
+set qdb_msg_no_matches "Ei löydy!"
+
+set qdb_msg_you_voted "Äänestit %1 -> %2"
+set qdb_msg_vote_updated "Ääni päivitetty #%1 -> %2"
+
+set qdb_msg_sql_error "Virhe sörkittäessä tietokantaa. uliskaa."
+set qdb_msg_add_success "Tietokantaa %1 sörkitty (#%2), kiitos."
+
+set qdb_msg_search_fmt "#%1: %2 (%3@%5)"
+
+set qdb_msg_toplist_limit "Top-listan määrän oltava %1-%2."
+set qdb_msg_toplist_fmt "%1. #%2: %3 (%4, %5)"
+
+set qdb_msg_update_override " (owner/time override)"
+set qdb_msg_update_ok "%1 #%2 päivitetty%3."
+
+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'."
+
+
+set qdb_msg_help "%1 add <teksti>|update <id> <teksti>|del <id>|find <parametrit>|vote|top3"
+
+
+###
 ### Bind commands for each database etc.
 ###
 
--- a/quotedb.tcl	Fri Jan 23 12:32:06 2015 +0200
+++ b/quotedb.tcl	Fri Jan 23 13:20:17 2015 +0200
@@ -29,7 +29,7 @@
 package require sqlite3
 
 ### SQLite database initialization
-if {[catch {sqlite3 quotedb $qdb_file} uerrmsg]} {
+if {[catch {sqlite3 qdb_handle $qdb_file} uerrmsg]} {
   putlog " Could not open SQLite3 database '$qdb_file': $uerrmsg"
   exit 2
 }
@@ -53,7 +53,7 @@
   return [clock format $utime -format "%d.%m.%Y %H:%M"]
 }
 
-proc qdb_msg {apublic anick achan amsg} {
+proc qdb_msg_do {apublic anick achan amsg} {
   global qdb_preferredmsg
 
   if {$apublic == 0} {
@@ -63,15 +63,25 @@
   }
 }
 
+proc qdb_msg {apublic anick achan amsg {aargs {}}} {
+  set narg 1
+  foreach marg $aargs {
+    set amsg [string map [list "%$narg" $marg] $amsg]
+    incr narg
+  }
+  qdb_msg_do $apublic $anick $achan $amsg
+}
+
+
 proc qdb_escape {str} {
   return [string map {' ''} $str]
 }
 
 proc qdb_sql_exec { upublic unick uchan usql } {
-  global quotedb
-  if {[catch {quotedb eval $usql} uerrmsg]} {
+  global qdb_handle qdb_msg_sql_error
+  if {[catch {qdb_handle eval $usql} uerrmsg]} {
     qdb_log "$uerrmsg on SQL:\n$usql"
-    qdb_msg $upublic $unick $uchan "virhe sörkittäessä tietokantaa. uliskaa."
+    qdb_msg $upublic $unick $uchan $qdb_msg_sql_error
     return 0
   }
   return 1
@@ -103,13 +113,20 @@
   return $result
 }
 
+proc qdb_valid_user {upublic unick uchan uhand} {
+  global qdb_msg_invalid_user
+  if {$uhand == "" || $uhand == {}} {
+    qdb_msg $upublic $unick $uchan $qdb_msg_invalid_user
+    return 0
+  }
+  return 1
+}
 
 #-------------------------------------------------------------------------
 proc qdb_add {ucmd utable unick uhost uhand uchan utext upublic} {
-  global quotedb
+  global qdb_handle qdb_msg_add_success
 
-  if {$uhand == "" || $uhand == {}} {
-    qdb_msg $upublic $unick $uchan "pyh."
+  if {![qdb_valid_user $upublic $unick $uchan $uhand]} {
     return 0
   }
 
@@ -123,7 +140,7 @@
     return 0
   }
 
-  set quoteID [quotedb last_insert_rowid]
+  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 }
@@ -132,18 +149,18 @@
   qdb_log "Added $utable #$quoteID ($unick/$uhand@$uchan): $utext"
 
   ### Report success to user
-  qdb_msg $upublic $unick $uchan "tietokantaa $utable sörkitty (#$quoteID), kiitos."
+  qdb_msg $upublic $unick $uchan $qdb_msg_add_success [list $utable $quoteID]
   return 1
 }
 
 
 #-------------------------------------------------------------------------
 proc qdb_delete {ucmd utable unick uhand uchan utext upublic} {
-  global quotedb qdb_max_deltime
+  global qdb_handle qdb_max_deltime qdb_msg_not_found
+
   set maxdiff [expr $qdb_max_deltime * 60]
 
-  if {$uhand == "" || $uhand == {}} {
-    qdb_msg $upublic $unick $uchan "pyh."
+  if {![qdb_valid_user $upublic $unick $uchan $uhand]} {
     return 0
   }
 
@@ -161,7 +178,7 @@
     set qextra " (owner/time override)"
   }
   
-  quotedb eval $usql {
+  qdb_handle eval $usql {
     set udiff [expr [unixtime] - $utime]
     if {$udiff < $maxdiff || $qoverride} {
       set usql "DELETE FROM $utable WHERE id=$unum"
@@ -181,18 +198,19 @@
     }
   }
 
-  qdb_msg $upublic $unick $uchan "#$unum ei löydy, tai se ei kuulu käyttäjälle '$uhand'."
+  qdb_msg $upublic $unick $uchan $qdb_msg_not_found [list $utable $unum $uhand]
   return 0
 }
 
 
 #-------------------------------------------------------------------------
 proc qdb_update {ucmd utable unick uhand uchan utext upublic} {
-  global quotedb qdb_max_modtime
+  global qdb_handle qdb_max_modtime qdb_msg_update_override
+  global qdb_msg_update_ok qdb_msg_update_too_old qdb_msg_not_found
+
   set maxdiff [expr $qdb_max_modtime * 60]
 
-  if {$uhand == "" || $uhand == {}} {
-    qdb_msg $upublic $unick $uchan "pyh."
+  if {![qdb_valid_user $upublic $unick $uchan $uhand]} {
     return 0
   }
 
@@ -207,44 +225,44 @@
     append usql " AND user='[qdb_escape $uhand]'"
     set qextra ""
   } else {
-    set qextra " (owner/time override)"
+    set qextra $qdb_msg_update_override
   }
 
-  quotedb eval $usql {
+  qdb_handle eval $usql {
     set udiff [expr [unixtime] - $utime]
     if {$udiff < $maxdiff || $qoverride} {
       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$qextra."
+        qdb_msg $upublic $unick $uchan $qdb_msg_update_ok [list $utable $unum $qextra]
         return 1
       }
     } else {
-      qdb_msg $upublic $unick $uchan "$utable quote #$unum vanhempi kuin $qdb_max_modtime minuuttia, ei voida päivittää."
+      qdb_msg $upublic $unick $uchan $qdb_msg_update_too_old [list $utable $unum $qdb_max_modtime]
       return 0
     }
   }
 
-  qdb_msg $upublic $unick $uchan "#$unum ei löydy, tai se ei kuulu käyttäjälle '$uhand'."
+  qdb_msg $upublic $unick $uchan $qdb_msg_not_found [list $utable $unum $uhand]
   return 0
 }
 
 
 #-------------------------------------------------------------------------
 proc qdb_toplist {ucmd utable unick uchan unum upublic} {
-  global quotedb
+  global qdb_handle qdb_msg_toplist_limit qdb_msg_toplist_fmt
 
-  if {$unum < 2 || $unum > 4} {
-#    qdb_msg $upublic $unick $uchan "top-listan pituus oltava 2-4."
+  if {$unum < 2 || $unum > 5} {
+    qdb_msg $upublic $unick $uchan $qdb_msg_toplist_limit [list 2 5]
     return 0
   }
 
   set uresults 0
   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"
-  quotedb eval $usql {
+  qdb_handle eval $usql {
     incr uresults
-    qdb_msg $upublic $unick $uchan "${uresults}. #${quoteID}: $utext ($uuser, $rating)"
+    qdb_msg $upublic $unick $uchan $qdb_msg_toplist_fmt [list $uresults $quoteID $utext $uuser $rating]
   }
 
   return 0
@@ -253,10 +271,9 @@
 
 #-------------------------------------------------------------------------
 proc qdb_vote {ucmd utable unick uhand uchan utext upublic} {
-  global quotedb
+  global qdb_handle qdb_msg_you_voted qdb_msg_quote_does_not_exist qdb_msg_vote_updated
 
-  if {$uhand == "" || $uhand == {}} {
-    qdb_msg $upublic $unick $uchan "pyh."
+  if {![qdb_valid_user $upublic $unick $uchan $uhand]} {
     return 0
   }
 
@@ -264,7 +281,7 @@
   if {$utext == "" || [regexp {^\s*-1$} $utext umatch uvote]} {
     set usql "SELECT max(id) AS qid FROM ${utable}"
     set uid 0
-    quotedb eval $usql { set uid $qid }
+    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\]\]"
@@ -274,20 +291,20 @@
   # Check if given quote ID exists.
   set uresults 0
   set usql "SELECT id AS qid FROM ${utable} WHERE id=$uid"
-  quotedb eval $usql { incr uresults }
+  qdb_handle eval $usql { incr uresults }
   if {$uresults == 0} {
-    qdb_msg $upublic $unick $uchan "quotea #$uid ei ole."
+    qdb_msg $upublic $unick $uchan $qdb_msg_quote_does_not_exist [list $uid]
     return 0
   }
 
   # Check if user has previously voted this item
   set usql "SELECT id AS qid FROM ${utable}_votes WHERE user='[qdb_escape $uhand]' AND urlid=$uid"
-  quotedb eval $usql {
+  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]} {
       qdb_log "$uhand changed vote to $uvote on #$uid / $utable"
-      qdb_msg $upublic $unick $uchan "ääni päivitetty #$uid -> $uvote"
+      qdb_msg $upublic $unick $uchan $qdb_msg_vote_updated [list $uid $uvote]
       return 1
     } else {
       return 0
@@ -298,14 +315,14 @@
   set usql "INSERT INTO ${utable}_votes (user,urlid,vote) VALUES ('[qdb_escape $uhand]',$uid,$uvote)"
   if {[qdb_sql_exec $upublic $unick $uchan $usql]} {
     qdb_log "$uhand voted $uvote on #$uid / $utable"
-    qdb_msg $upublic $unick $uchan "äänestit #$uid -> $uvote"
+    qdb_msg $upublic $unick $uchan $qdb_msg_you_voted [list $uid $uvote]
   }
 }
 
 
 #-------------------------------------------------------------------------
 proc qdb_get {ucmd utable unick uchan uindex upublic} {
-  global quotedb
+  global qdb_handle qdb_msg_no_matches
 
   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"
   if {$uindex >= 0} {
@@ -316,7 +333,7 @@
     qdb_log "$unick get random $utable"
   }
 
-  quotedb eval $usql {
+  qdb_handle eval $usql {
     set qtmp [qdb_split "#${quoteID}: $utext ($uuser@[qdb_ctime $utime], $uvote)" 450]
     foreach qstr $qtmp {
       qdb_msg $upublic $unick $uchan $qstr
@@ -324,14 +341,14 @@
     return 1
   }
 
-  qdb_msg $upublic $unick $uchan "ei löydy."
+  qdb_msg $upublic $unick $uchan $qdb_msg_no_matches
   return 0
 }
 
 
 #-------------------------------------------------------------------------
 proc qdb_find {ucmd utable unick uhand uchan utext upublic} {
-  global quotedb qdb_showmax_pub qdb_showmax_priv
+  global qdb_handle qdb_showmax_pub qdb_showmax_priv qdb_msg_no_matches qdb_msg_search_fmt
 
   if {$upublic == 0} {
     set ulimit $qdb_showmax_priv
@@ -369,13 +386,13 @@
   ### Query the database and output results
   set uresults 0
   set usql "SELECT id AS quoteID, utime AS utime, utext AS utext, user AS uuser FROM $utable $fquery ORDER BY utime DESC LIMIT $ulimit"
-  quotedb eval $usql {
+  qdb_handle eval $usql {
     incr uresults
-    qdb_msg $upublic $unick $uchan "#${quoteID}: $utext ($uuser@[qdb_ctime $utime])"
+    qdb_msg $upublic $unick $uchan $qdb_msg_search_fmt [list $quoteID $utext $uuser $utime [qdb_ctime $utime]]
   }
   
   if {$uresults == 0} {
-    qdb_msg $upublic $unick $uchan "ei löydy."
+    qdb_msg $upublic $unick $uchan $qdb_msg_no_matches
     return 0
   }
   return 1
@@ -384,6 +401,8 @@
 
 #-------------------------------------------------------------------------
 proc qdb_command {ucmd utable unick uhost uhand uchan utext upublic} {
+  global qdb_msg_help
+
   set utext [string trim $utext]
 
   if {$utext == "" || $utext == {}} {
@@ -411,7 +430,7 @@
     # Vote
     qdb_vote $ucmd $utable $unick $uhand $uchan $unum $upublic
   } else {
-    qdb_msg $upublic $unick $uchan "$ucmd add <teksti>|update <id> <teksti>|del <id>|find <parametrit>|vote|top3"
+    qdb_msg $upublic $unick $uchan $qdb_msg_help [list $ucmd]
   }
 }