changeset 163:bee5cd89d41c

weather: Moar work on getting the frontend script code up and running.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 02 Jun 2014 19:29:33 +0300
parents 4f8a163b2bc1
children d5a0e4248f3e
files weather.tcl
diffstat 1 files changed, 171 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/weather.tcl	Mon Jun 02 15:56:01 2014 +0300
+++ b/weather.tcl	Mon Jun 02 19:29:33 2014 +0300
@@ -25,11 +25,28 @@
 ###
 ### Messages
 ###
-set weather_msg_usage_1 "Käyttö: !sää2 \[paikka\]"
-set weather_msg_usage_2 "Käyttö: !sää2 aseta <paikka>\[;<paikka2>\]"
+set weather_msg_usage_prefix_1 "Käyttö: !sää "
+set weather_msg_usage_prefix_2 "        !sää "
+
+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)"
+
+set weather_msg_usage_full "\[paikka\]\n$weather_msg_usage_def_set\n$weather_msg_usage_alias\n$weather_msg_usage_unalias"
+
+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."
+
+set weather_msg_aliased "Aliasoitiin \002%1\002 = \002%2\002."
+set weather_msg_unaliased "Unaliasoitiin \002%1\002."
+
 set weather_msg_user_not_known "Tuntematon käyttäjä."
-set weather_msg_no_results "Ei mittaustietoja."
-set weather_msg_no_data_for_location "Paikkakunnan tietoja ei saatu."
+set weather_msg_no_access "Ei oikeuksia muuttaa asetuksia."
+
+
+set weather_msg_no_results "\002%1\002: Ei mittaustietoja."
+set weather_msg_no_data_for_location "\002%1\002: Paikkakunnan tietoja ei saatu."
 
 
 ##########################################################################
@@ -39,10 +56,10 @@
 set weather_version "0.1"
 
 ### Binding initializations
-bind pub - !sää2 weather_cmd_pub
-bind pub - !saa2 weather_cmd_pub
-bind msg - !sää2 weather_cmd_msg
-bind msg - !saa2 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
 
 
 ### Initialization messages
@@ -60,7 +77,7 @@
 }
 
 
-proc weather_msg {apublic anick achan amsg} {
+proc weather_msg_do {apublic anick achan amsg} {
   global weather_preferredmsg
   if {$apublic == 1} {
     putserv "$weather_preferredmsg $achan :$amsg"
@@ -70,6 +87,30 @@
 }
 
 
+proc weather_usage {apublic anick achan amsg} {
+  global weather_msg_usage_prefix_1 weather_msg_usage_prefix_2
+  set nline 0
+  foreach aline [split $amsg "\n"] {
+    if {$nline == 0} {
+      weather_msg_do $apublic $anick $achan "$weather_msg_usage_prefix_1$aline"
+    } else {
+      weather_msg_do $apublic $anick $achan "$weather_msg_usage_prefix_2$aline"
+    }
+    incr nline
+  }
+}
+
+
+proc weather_msg {apublic anick achan amsg {aargs {}}} {
+  set narg 1
+  foreach marg $aargs {
+    set amsg [string map [list "%$narg" $marg] $amsg]
+    incr narg
+  }
+  weather_msg_do $apublic $anick $achan $amsg
+}
+
+
 proc weather_ctime {utime} {
   return [clock format $utime -format "%H:%M"]
 }
@@ -83,12 +124,51 @@
 }
 
 
-proc weather_check_user {uhand} {
-  if {![weather_valid_user $uhand]} {
-    weather_msg $upublic $unick $uchan $weather_msg_user_not_known
-    return 0
+#-------------------------------------------------------------------------
+proc weather_load_aliases {} {
+  global weather_aliasfile weather_aliases
+
+  # Create dict
+  array unset weather_aliases
+  array set weather_aliases {}
+  
+  # Read datafile
+  if {![catch {set ufile [open $weather_aliasfile r 0600]} uerrmsg]} {
+    while {![eof $ufile]} {
+      gets $ufile uline
+      set udata [split $uline "|"]
+      if {[llength $udata] == 2} {
+        set weather_aliases([lindex $udata 0]) [lindex $udata 1]
+      }
+    }
+    close $ufile
+  } else {
+    weather_log "Could not open data file: $uerrmsg"
   }
-  return 1
+}
+
+
+proc weather_save_aliases {} {
+  global weather_aliasfile weather_aliases
+
+  if {![catch {set ufile [open $weather_aliasfile w 0600]} uerrmsg]} {
+    foreach {ukey uvalue} [array get weather_aliases] {
+      puts $ufile "$ukey|$uvalue"
+    }
+    close $ufile
+  } else {
+    weather_log "Could not open data file: $uerrmsg"
+  }
+}
+
+
+proc weather_get_alias {uname} {
+  global weather_aliases
+  set utmp [array get weather_aliases $uname]
+  if {[llength $utmp] > 0} {
+    return [lindex $utmp 1]
+  }
+  return $uname
 }
 
 
@@ -118,6 +198,7 @@
   # Create dict
   array unset weather_data
   array set weather_data {}
+
   set wtemp_min_val 500000
   set wtemp_max_val -500000
   set wtemp_min_key ""
@@ -175,12 +256,19 @@
   array set weather_data {}
 }
 
+if {![info exists weather_aliases]} {
+  array set weather_aliases {}
+}
+
 if {[info exists weather_running]} {
   set weather_last [expr [clock seconds] - $weather_running]
 } else {
   set weather_last -1
 }
 
+weather_log "Loading aliases."
+weather_load_aliases
+
 if {$weather_last < 0 || $weather_last > [expr $weather_check_period * 60]} {
   weather_log "Starting weather update."
   weather_exec
@@ -188,52 +276,91 @@
   weather_update
 }
 
+
 #-------------------------------------------------------------------------
 proc weather_cmd {unick uhand uchan uargs upublic} {
-  global weather_default_locations weather_data weather_max_results
-  global weather_msg_usage_1 weather_msg_usage_2 weather_msg_user_not_known
+  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
+
+  putlog "$unick : '$uhand' @ $uchan"
 
   # Check and handle arguments
   set rarglist [::textutil::split::splitx $uargs {\s+}]
   set rarg [lindex $rarglist 0]
 
   if {$rarg == "?" || $rarg == "help" || $rarg == "apua"} {
-    weather_msg $upublic $unick $uchan $weather_msg_usage_1
-    weather_msg $upublic $unick $uchan $weather_msg_usage_2
+    weather_usage $upublic $unick $uchan "$weather_msg_usage_full"
     return 0
   }
   
   # Setting the location
-  if {$rarg == "aseta" || $rarg == "set"} {
-    if {![weather_check_user $uhand]} {
+  if {$rarg == "vakio" || $rarg == "default" || $rarg == "vakiot"} {
+    if {![weather_valid_user $uhand]} {
+      weather_msg $upublic $unick $uchan $weather_msg_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]
+      } else {
+        set lista [join [split $lista ";"] " ; "]
+        weather_msg $upublic $unick $uchan $weather_msg_defloc [list $uhand $lista]
+      }
+    } else {
+      # Split the list of desired locations
+      set qlist [::textutil::split::splitx [join [lrange $rarglist 1 end] " "] {\s*\;\s*}]
+      set nlist [lsearch -all -inline -not -exact $qlist ""]
+
+      if {[llength $nlist] > 0} {
+        weather_msg $upublic $unick $uchan $weather_msg_def_set [list [join $nlist " ; "]]
+        setuser $uhand XTRA "weather_locations" [join $nlist ";"]
+      } else {
+        weather_usage $upublic $unick $uchan $weather_msg_usage_def_set
+      }
+    }
+    return 0
+  } elseif {$rarg == "alias"} {
+    if {![weather_valid_user $uhand] || ![matchattr $uhand n]} {
+      weather_msg $upublic $unick $uchan $weather_msg_no_access
       return 0
     }
 
-    # Split the list of desired locations
-    set qlist [::textutil::split::splitx [join [lrange $rarglist 1 end] " "] {\s*\;\s*}]
+    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
+      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]
 
-    if {[llength $nlist] > 0} {
-      set lista [join $nlist ";"]
-      weather_msg $upublic $unick $uchan "Vakiopaikoiksi asetettu: \002${lista}\002."
-      setuser $uhand XTRA "weather_locations" $lista
-    } else {
-      weather_msg $upublic $unick $uchan $weather_msg_usage_2
-    }
+    weather_save_aliases
     return 0
-  }
-
-  if {$rarg == "default" || $rarg == "vakio"} {
-    if {![weather_check_user $uhand]} {
+  } elseif {$rarg == "unalias"} {
+    if {![weather_valid_user $uhand] || ![matchattr $uhand n]} {
+      weather_msg $upublic $unick $uchan $weather_msg_no_access
       return 0
     }
 
-    set lista [getuser $uhand XTRA "weather_locations"]
-    if {$lista == "" || $lista == "{}"} {
-      set lista "Ei asetettu"
+    if {[llength $rarglist] < 2} {
+      weather_usage $upublic $unick $uchan $weather_msg_usage_unalias
+      return 0
     }
-    weather_msg $upublic $unick $uchan "Vakiopaikat ${uhand}: \002${lista}\002."
+
+    set ualias [lindex $rarglist 1]
+    unset weather_aliases($ualias)
+    weather_msg $upublic $unick $uchan $weather_msg_unaliased [list $ualias]
+
+    weather_save_aliases
     return 0
   }
 
@@ -269,26 +396,30 @@
     } else {
       # Location match
       set ufound 0
+      set rarg [weather_get_alias $rarg]
       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_data $ukey $uvalue]
             incr nresults
           } else {
-            weather_msg $upublic $unick $uchan "\002${ukey}\002: $weather_msg_no_results"
+            weather_msg $upublic $unick $uchan $weather_msg_no_results [list $ukey]
           }
           incr ufound
         }
-
+	
+	# Check for results limit
         if {$nresults >= $weather_max_results} {
           return 0
         }
       }
       
       if {$ufound == 0} {
-        weather_msg $upublic $unick $uchan "\002${rarg}\002: $weather_msg_no_data_for_location"
+        weather_msg $upublic $unick $uchan $weather_msg_no_data_for_location [list $rarg]
       }
     }
+    
+    # Check for results limit
     if {$nresults >= $weather_max_results} {
       return 0
     }