changeset 538:f07c972e269a

weather: Refactor much of the messages system to match the quotedb one. Breaks compatibility with old config completely.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 07 Jul 2020 21:59:30 +0300
parents 210d22dac6d4
children c9dc79874939
files config.weather.example weather.tcl
diffstat 2 files changed, 96 insertions(+), 90 deletions(-) [+]
line wrap: on
line diff
--- a/config.weather.example	Tue Jul 07 14:43:49 2020 +0300
+++ b/config.weather.example	Tue Jul 07 21:59:30 2020 +0300
@@ -4,6 +4,23 @@
 #
 ##########################################################################
 
+###
+### Bind commands
+###
+bind pub - !sää weather_cmd_pub
+bind pub - !saa weather_cmd_pub
+bind msg - !sää weather_cmd_msg
+bind msg - !saa weather_cmd_msg
+
+bind pub - !sää weather_cmd_pub
+bind pub - !saa weather_cmd_pub
+bind msg - !sää weather_cmd_msg
+bind msg - !saa weather_cmd_msg
+
+
+###
+### Settings
+###
 # Channels where weather functionality is enabled
 # set weather_channels "#foobar;#baz"
 # You can use * to match substrings or everything
@@ -96,44 +113,38 @@
 lappend weather_msg_result "."
 
 
-set weather_msg_usage_prefix_1 "Käyttö: !sää "
-set weather_msg_usage_prefix_2 "        !sää "
+array set weather_messages {
+
+  "usage_prefix_1" "Käyttö: !sää "
+  "usage_prefix_2" "        !sää "
 
-set weather_msg_list_nearest $weather_msg_list_station
-set weather_msg_usage_nearest "lähin <lat>\[, \]<long> -- näyttää annettuja koordinaatteja lähimpänä olevat mittausasemat"
-set weather_msg_usage_nearest_invalid "lähin <lat>\[, \]<long> -- näyttää annettuja koordinaatteja lähimpänä olevat mittausasemat"
-set weather_msg_nearest_stations "Lähimmät mittausasemat (%1, %2): %3"
+  "list_nearest" $weather_msg_list_station
+  "usage_nearest" "lähin <lat>\[, \]<long> -- näyttää annettuja koordinaatteja lähimpänä olevat mittausasemat"
+  "usage_nearest_invalid" "lähin <lat>\[, \]<long> -- näyttää annettuja koordinaatteja lähimpänä olevat mittausasemat"
+  "nearest_stations" "Lähimmät mittausasemat (@1@, @2@): @3@"
 
 
-set weather_msg_usage_def_set "vakio <paikka>\[;<paikka2>\] -- asettaa vakiohavaintoaseman\nvakio -- näyttää nykyisen"
-set weather_msg_usage_alias "alias <alias> = <nimi> (Lisää alias-nimen, esim. alias perse = turku)"
-set weather_msg_usage_unalias "unalias <alias> (Poistaa aliaksen)"
+  "usage_def_set" "vakio <paikka>\[;<paikka2>\] -- asettaa vakiohavaintoaseman\nvakio -- näyttää nykyisen"
+  "usage_alias" "alias <alias> = <nimi> (Lisää alias-nimen, esim. alias perse = turku)"
+  "usage_unalias" "unalias <alias> (Poistaa aliaksen)"
 
-set weather_msg_usage_stations "asemat <patterni> -- listaa mittausasemat joiden nimi matsaa patternin kanssa"
-
-set weather_msg_usage_full "\[paikka\]\n$weather_msg_usage_def_set\n$weather_msg_usage_alias\n$weather_msg_usage_unalias\n$weather_msg_usage_stations"
+  "usage_stations" "asemat <patterni> -- listaa mittausasemat joiden nimi matsaa patternin kanssa"
 
-set weather_msg_def_set "Vakio-havaintoasemiksi asetettu: \002%1\002."
-set weather_msg_defloc "Käyttäjän \002%1\002 vakio-havaintoasemat ovat: \002%2\002."
-set weather_msg_def_not_set "Vakio-havaintoasemia ei asetettu käyttäjälle \002%1\002."
+  "usage_full" "\[paikka\]\n$weather_msg_usage_def_set\n$weather_msg_usage_alias\n$weather_msg_usage_unalias\n$weather_msg_usage_stations"
 
-set weather_msg_aliased "Aliasoitiin \002%1\002 = \002%2\002."
-set weather_msg_unaliased "Unaliasoitiin \002%1\002."
-set weather_msg_aliases "Aliakset: %1"
+  "def_set_to" "Vakio-havaintoasemiksi asetettu: \002@1@\002."
+  "def_value" "Käyttäjän \002@1@\002 vakio-havaintoasemat ovat: \002@2@\002."
+  "def_not_set" "Vakio-havaintoasemia ei asetettu käyttäjälle \002@1@\002."
 
-set weather_msg_user_not_known "Tuntematon käyttäjä."
-set weather_msg_no_access "Ei oikeuksia muuttaa asetuksia."
-
-set weather_msg_stations "Mittausasemat: %1"
-
-set weather_msg_no_results "\002%1\002: Ei mittaustietoja."
-set weather_msg_no_data_for_location "\002%1\002: Paikkakunnan tietoja ei saatu."
+  "aliased" "Aliasoitiin \002@1@\002 = \002@2@\002."
+  "unaliased" "Unaliasoitiin \002@1@\002."
+  "aliases" "Aliakset: @1@"
 
+  "user_not_known" "Tuntematon käyttäjä."
+  "no_access" "Ei oikeuksia muuttaa asetuksia."
 
-###
-### Bind commands
-###
-bind pub - !sää weather_cmd_pub
-bind pub - !saa weather_cmd_pub
-bind msg - !sää weather_cmd_msg
-bind msg - !saa weather_cmd_msg
+  "stations" "Mittausasemat: @1@"
+
+  "no_results" "\002@1@\002: Ei mittaustietoja."
+  "no_data_for_location" "\002@1@\002: Paikkakunnan tietoja ei saatu."
+}
--- a/weather.tcl	Tue Jul 07 14:43:49 2020 +0300
+++ b/weather.tcl	Tue Jul 07 21:59:30 2020 +0300
@@ -41,31 +41,29 @@
 }
 
 
-proc weather_msg {apublic anick achan amsg {aargs {}}} {
-  global weather_preferredmsg
-  set narg 1
-  foreach marg $aargs {
-    set amsg [string map [list "%$narg" $marg] $amsg]
-    incr narg
+proc weather_qm {uid} {
+  global weather_messages
+
+  if {[info exists weather_messages($uid)]} {
+    return $weather_messages($uid)
+  } else {
+    return $uid
   }
-  utl_msg_do $weather_preferredmsg $apublic $anick $achan $amsg
 }
 
 
-proc weather_usage {apublic anick achan amsg} {
-  global weather_msg_usage_prefix_1 weather_msg_usage_prefix_2
+proc weather_msg {apublic anick achan aid {aargs {}}} {
   global weather_preferredmsg
-  set nline 0
-  foreach aline [split $amsg "\n"] {
-    if {[string range $aline 0 1] == ":"} {
-      utl_msg_do $weather_preferredmsg $apublic $anick $achan $aline
-    } elseif {$nline == 0} {
-      utl_msg_do $weather_preferredmsg $apublic $anick $achan "$weather_msg_usage_prefix_1$aline"
-    } else {
-      utl_msg_do $weather_preferredmsg $apublic $anick $achan "$weather_msg_usage_prefix_2$aline"
-    }
-    incr nline
-  }
+  set amsg [weather_qm $aid]
+  set amsg [string map [list "@cmd@" "!sää"] $amsg]
+  utl_msg_args $weather_preferredmsg $apublic $anick $achan $amsg $aargs
+}
+
+
+proc weather_smsg {apublic anick achan amsg {aargs {}}} {
+  global weather_preferredmsg
+  set amsg [string map [list "@cmd@" "!sää"] $amsg]
+  utl_msg_args $weather_preferredmsg $apublic $anick $achan $amsg $aargs
 }
 
 
@@ -281,7 +279,7 @@
 
 
 # Get data by location key
-proc weather_get_by_key {ukey} {
+proc weather_get_str_by_key {ukey} {
   global weather_data weather_msg_result
   return [weather_get_str $weather_data($ukey) $weather_msg_result]
 }
@@ -320,28 +318,30 @@
 #-------------------------------------------------------------------------
 proc weather_cmd {unick uhand uchan uargs upublic} {
   global weather_default_locations weather_data weather_max_results weather_aliases
-  global weather_msg_usage_full weather_msg_usage_def_set weather_msg_user_not_known
-  global weather_msg_no_results weather_msg_no_data_for_location
-  global weather_msg_usage_alias weather_msg_usage_unalias weather_msg_defloc
-  global weather_msg_aliased weather_msg_unaliased weather_msg_no_access
-  global weather_msg_def_set weather_msg_def_not_set weather_msg_aliases
-  global weather_msg_usage_stations weather_msg_stations weather_msg_list_station
-  global weather_msg_list_nearest weather_msg_usage_nearest weather_msg_usage_nearest_invalid
-  global weather_msg_nearest_stations weather_msg_result
+  global weather_msg_list_station weather_msg_result weather_messages
 
   # Check and handle arguments
   set rarglist [::textutil::split::splitx $uargs {\s+}]
   set rcmd [lindex $rarglist 0]
 
   if {$rcmd == "?" || $rcmd == "help" || $rcmd == "apua"} {
-    weather_usage $upublic $unick $uchan $weather_msg_usage_full
+    set help_full {}
+
+    # FIXME
+    foreach {ukey uvalue} [array get weather_messages] {
+      if {[string match "usage_*" $ukey]} {
+        lappend help_full $uvalue
+      }
+    }
+
+    weather_msg $upublic $unick $uchan "usage_full" $help_full
     return 0
   }
 
   if {$rcmd == "asemat" || $rcmd == "stations"} {
     # List stations/locations matching the given pattern
     if {[llength $rarglist] < 2} {
-      weather_usage $upublic $unick $uchan $weather_msg_usage_stations
+      weather_msg $upublic $unick $uchan "usage_stations"
       return 0
     }
 
@@ -355,19 +355,19 @@
     }
 
     set res [join $result " ; "]
-    weather_msg $upublic $unick $uchan $weather_msg_stations [list $res]
+    weather_msg $upublic $unick $uchan "stations_list" [list $res]
     return 0
   } elseif {$rcmd == "lahin" || $rcmd == "lähin" || $rcmd == "closest" || $rcmd == "nearest"} {
     # List stations nearest to given coordinates
     set qargs [join [lrange $rarglist 1 end] ""]
     if {![regexp {@?(\d+|\d+\.\d+|\.\d+)\s*,\s*(\d+|\d+\.\d+|\.\d+)} $qargs -> d_lat d_lng]} {
-      weather_usage $upublic $unick $uchan $weather_msg_usage_nearest
+      weather_msg $upublic $unick $uchan "usage_nearest"
       return 0
     }
 
     # Check argument types
     if {![string is double -strict $d_lat] || ![string is double -strict $d_lng]} {
-      weather_msg $upublic $unick $uchan $weather_msg_usage_nearest_invalid
+      weather_msg $upublic $unick $uchan "nearest_invalid"
       return 0
     }
 
@@ -393,24 +393,24 @@
 
     # Print out the result
     set res [join $uresult " ; "]
-    weather_msg $upublic $unick $uchan $weather_msg_nearest_stations [list $d_lat $d_lng $res]
+    weather_msg $upublic $unick $uchan "nearest_stations" [list $d_lat $d_lng $res]
     return 0
   } elseif {$rcmd == "vakio" || $rcmd == "default" || $rcmd == "vakiot" || $rcmd == "defaults"} {
     # List or set the default weather station name patterns for this user
 
     # Access check
     if {![utl_valid_user $uhand]} {
-      weather_msg $upublic $unick $uchan $weather_msg_user_not_known
+      weather_msg $upublic $unick $uchan "user_not_known"
       return 0
     }
 
     if {[llength $rarglist] == 1} {
       set lista [getuser $uhand XTRA "weather_locations"]
       if {$lista == "" || $lista == "{}"} {
-        weather_msg $upublic $unick $uchan $weather_msg_def_not_set [list $uhand]
+        weather_msg $upublic $unick $uchan "def_not_set" [list $uhand]
       } else {
         set lista [join [split $lista ";"] " ; "]
-        weather_msg $upublic $unick $uchan $weather_msg_defloc [list $uhand $lista]
+        weather_msg $upublic $unick $uchan "def_value" [list $uhand $lista]
       }
     } else {
       # Split the list of desired locations
@@ -418,49 +418,49 @@
       set nlist [lsearch -all -inline -not -exact $qlist ""]
 
       if {[llength $nlist] > 0} {
-        weather_msg $upublic $unick $uchan $weather_msg_def_set [list [join $nlist " ; "]]
+        weather_msg $upublic $unick $uchan "def_set_to" [list [join $nlist " ; "]]
         setuser $uhand XTRA "weather_locations" [join $nlist ";"]
       } else {
-        weather_usage $upublic $unick $uchan $weather_msg_usage_def_set
+        weather_msg $upublic $unick $uchan "usage_def_set"
       }
     }
     return 0
   } elseif {$rcmd == "alias"} {
     # Alias a string to another, only certain users have access (+n flag)
     if {![utl_valid_user $uhand] || ![matchattr $uhand n]} {
-      weather_msg $upublic $unick $uchan $weather_msg_no_access
+      weather_msg $upublic $unick $uchan "no_access"
       return 0
     }
 
     set qlist [::textutil::split::splitx [join [lrange $rarglist 1 end] " "] {\s*=\s*}]
     set nlist [lsearch -all -inline -not -exact $qlist ""]
     if {[llength $nlist] < 2} {
-      weather_usage $upublic $unick $uchan $weather_msg_usage_alias
+      weather_msg $upublic $unick $uchan "usage_alias"
       return 0
     }
 
     set ualias [lindex $nlist 0]
     set uname [lindex $nlist 1]
     set weather_aliases($ualias) $uname
-    weather_msg $upublic $unick $uchan $weather_msg_aliased [list $ualias $uname]
+    weather_msg $upublic $unick $uchan "alias_set" [list $ualias $uname]
 
     weather_save_aliases
     return 0
   } elseif {$rcmd == "unalias"} {
     # Remove one alias, only certain users have access (+n flag)
     if {![utl_valid_user $uhand] || ![matchattr $uhand n]} {
-      weather_msg $upublic $unick $uchan $weather_msg_no_access
+      weather_msg $upublic $unick $uchan "no_access"
       return 0
     }
 
     if {[llength $rarglist] < 2} {
-      weather_usage $upublic $unick $uchan $weather_msg_usage_unalias
+      weather_msg $upublic $unick $uchan "usage_unalias"
       return 0
     }
 
     set ualias [lindex $rarglist 1]
+    weather_msg $upublic $unick $uchan "alias_unset" [list $ualias $weather_aliases($ualias)]
     unset weather_aliases($ualias)
-    weather_msg $upublic $unick $uchan $weather_msg_unaliased [list $ualias]
 
     weather_save_aliases
     return 0
@@ -470,7 +470,7 @@
     foreach {ukey uvalue} [array get weather_aliases] {
       lappend ulist "\002'$ukey' = '$uvalue'\002"
     }
-    weather_msg $upublic $unick $uchan $weather_msg_aliases [list [join $ulist ", "]]
+    weather_msg $upublic $unick $uchan "alias_list" [list [join $ulist ", "]]
     return 0
   }
 
@@ -489,20 +489,15 @@
   foreach rarg $rarglist {
     if {$rarg == "min"} {
       # Min temp
-      set umin [weather_get_by_key "w_min"]
-      weather_msg $upublic $unick $uchan "Min: $umin"
+      weather_msg $upublic $unick $uchan "temp_min" [list [weather_get_str_by_key "w_min"]]
       incr nresults
     } elseif {$rarg == "max"} {
       # Max temp
-      set umax [weather_get_by_key "w_max"]
-      weather_msg $upublic $unick $uchan "Max: $umax"
+      weather_msg $upublic $unick $uchan "temp_max" [list [weather_get_str_by_key "w_max"]]
       incr nresults
     } elseif {$rarg == "minmax" || $rarg == "min max" || $rarg == "maxmin" || $rarg == "max min"} {
       # Min & Max temps
-      set umin [weather_get_by_key "w_min"]
-      set umax [weather_get_by_key "w_max"]
-      weather_msg $upublic $unick $uchan "Min: $umin"
-      weather_msg $upublic $unick $uchan "Max: $umax"
+      weather_msg $upublic $unick $uchan "temp_minmax" [list [weather_get_str_by_key "w_min"] [weather_get_str_by_key "w_max"]]
       incr nresults
     } else {
       # Location match
@@ -511,10 +506,10 @@
       foreach {ukey uvalue} [array get weather_data] {
         if {![string match "w_*" $ukey] && [string match -nocase "*${rarg}*" $ukey]} {
           if {[llength $uvalue] > 0} {
-            weather_msg $upublic $unick $uchan [weather_get_str $uvalue $weather_msg_result]
+            weather_smsg $upublic $unick $uchan [weather_get_str $uvalue $weather_msg_result]
             incr nresults
           } else {
-            weather_msg $upublic $unick $uchan $weather_msg_no_results [list $ukey]
+            weather_msg $upublic $unick $uchan "no_results" [list $ukey]
           }
           incr ufound
         }
@@ -526,7 +521,7 @@
       }
 
       if {$ufound == 0} {
-        weather_msg $upublic $unick $uchan $weather_msg_no_data_for_location [list $rarg]
+        weather_msg $upublic $unick $uchan "no_data_for_location" [list $rarg]
       }
     }