diff quotedb.tcl @ 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 794ecdfe6ac0
children a9cb1a248791
line wrap: on
line diff
--- 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"
   }
 }