changeset 600:90da5691cc81

weather: Use new command matching.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 04 Feb 2021 12:16:32 +0200
parents 76d19fa28753
children acc2ba6be415
files config.weather.example weather.tcl
diffstat 2 files changed, 26 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/config.weather.example	Thu Feb 04 12:15:54 2021 +0200
+++ b/config.weather.example	Thu Feb 04 12:16:32 2021 +0200
@@ -22,6 +22,18 @@
 set weather_cmd_name "!saa"
 
 
+# Sub-command name regexp patterns
+array set weather_commands {
+  "help" {^(\?|help|apua)$}
+  "stations" {^(stations|asemat)$}
+  "nearest" {^(lahin|lähin|lähin|closest|nearest)$}
+  "default" {^(vakiot?|defaults?)$}
+  "alias" {^(alias)$}
+  "unalias" {^(unalias)$}
+  "list" {^(list|listaa?)$}
+}
+
+
 ###
 ### Settings
 ###
--- a/weather.tcl	Thu Feb 04 12:15:54 2021 +0200
+++ b/weather.tcl	Thu Feb 04 12:16:32 2021 +0200
@@ -291,6 +291,12 @@
 }
 
 
+proc weather_cmd_match { uid ustr } {
+  global weather_commands
+  return [utl_cmd_match weather_commands $uid $ustr]
+}
+
+
 #-------------------------------------------------------------------------
 proc weather_cmd {unick $uhost uhand uchan uargs upublic} {
   global weather_default_locations weather_data weather_max_results weather_aliases
@@ -300,14 +306,13 @@
   set rarglist [::textutil::split::splitx $uargs {\s+}]
   set rcmd [lindex $rarglist 0]
 
-  if {$rcmd == "?" || $rcmd == "help" || $rcmd == "apua"} {
+  if {[weather_cmd_match "help" $rcmd]} {
+    # Show help
     foreach ukey $weather_messages(help_full) {
       weather_msg $upublic $unick $uchan $ukey
     }
     return 1
-  }
-
-  if {$rcmd == "asemat" || $rcmd == "stations"} {
+  } elseif {[weather_cmd_match "stations" $rcmd]} {
     # List stations/locations matching the given pattern
     if {[llength $rarglist] < 2} {
       weather_msg $upublic $unick $uchan "help_stations"
@@ -335,7 +340,7 @@
 
     weather_msg $upublic $unick $uchan "stations_list" [list $res]
     return 1
-  } elseif {$rcmd == "lahin" || $rcmd == "lähin" || $rcmd == "closest" || $rcmd == "nearest"} {
+  } elseif {[weather_cmd_match "nearest" $rcmd]} {
     # 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]} {
@@ -373,7 +378,7 @@
     set res [join $uresult " ; "]
     weather_msg $upublic $unick $uchan "nearest_stations" [list $d_lat $d_lng $res]
     return 1
-  } elseif {$rcmd == "vakio" || $rcmd == "default" || $rcmd == "vakiot" || $rcmd == "defaults"} {
+  } elseif {[weather_cmd_match "default" $rcmd]} {
     # List or set the default weather station name patterns for this user
 
     # Access check
@@ -403,7 +408,7 @@
       }
     }
     return 1
-  } elseif {$rcmd == "alias"} {
+  } elseif {[weather_cmd_match "alias" $rcmd]} {
     # 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 "no_access"
@@ -430,7 +435,7 @@
 
     weather_save_aliases
     return 1
-  } elseif {$rcmd == "unalias"} {
+  } elseif {[weather_cmd_match "unalias" $rcmd]} {
     # Remove one alias, only certain users have access (+n flag)
     if {![utl_valid_user $uhand] || ![matchattr $uhand n]} {
       weather_msg $upublic $unick $uchan "no_access"
@@ -454,7 +459,7 @@
     weather_save_aliases
 
     return 1
-  } elseif {$rcmd == "list"} {
+  } elseif {[weather_cmd_match "list" $rcmd]} {
     # List all currently defined aliases
     set ulist {}
     set ulistitem [weather_qm "alias_item"]