comparison weather.tcl @ 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 2c9db9a29429
children ff64ef21c20d
comparison
equal deleted inserted replaced
162:4f8a163b2bc1 163:bee5cd89d41c
23 23
24 24
25 ### 25 ###
26 ### Messages 26 ### Messages
27 ### 27 ###
28 set weather_msg_usage_1 "Käyttö: !sää2 \[paikka\]" 28 set weather_msg_usage_prefix_1 "Käyttö: !sää "
29 set weather_msg_usage_2 "Käyttö: !sää2 aseta <paikka>\[;<paikka2>\]" 29 set weather_msg_usage_prefix_2 " !sää "
30
31 set weather_msg_usage_def_set "vakio <paikka>\[;<paikka2>\] -- asettaa vakiohavaintoaseman\nvakio -- näyttää nykyisen"
32 set weather_msg_usage_alias "alias <alias> = <nimi> (Lisää alias-nimen, esim. alias perse = turku)"
33 set weather_msg_usage_unalias "unalias <alias> (Poistaa aliaksen)"
34
35 set weather_msg_usage_full "\[paikka\]\n$weather_msg_usage_def_set\n$weather_msg_usage_alias\n$weather_msg_usage_unalias"
36
37 set weather_msg_def_set "Vakio-havaintoasemiksi asetettu: \002%1\002."
38 set weather_msg_defloc "Käyttäjän \002%1\002 vakio-havaintoasemat ovat: \002%2\002."
39 set weather_msg_def_not_set "Vakio-havaintoasemia ei asetettu käyttäjälle \002%1\002."
40
41 set weather_msg_aliased "Aliasoitiin \002%1\002 = \002%2\002."
42 set weather_msg_unaliased "Unaliasoitiin \002%1\002."
43
30 set weather_msg_user_not_known "Tuntematon käyttäjä." 44 set weather_msg_user_not_known "Tuntematon käyttäjä."
31 set weather_msg_no_results "Ei mittaustietoja." 45 set weather_msg_no_access "Ei oikeuksia muuttaa asetuksia."
32 set weather_msg_no_data_for_location "Paikkakunnan tietoja ei saatu." 46
47
48 set weather_msg_no_results "\002%1\002: Ei mittaustietoja."
49 set weather_msg_no_data_for_location "\002%1\002: Paikkakunnan tietoja ei saatu."
33 50
34 51
35 ########################################################################## 52 ##########################################################################
36 # No need to look below this line 53 # No need to look below this line
37 ########################################################################## 54 ##########################################################################
38 set weather_name "Weather" 55 set weather_name "Weather"
39 set weather_version "0.1" 56 set weather_version "0.1"
40 57
41 ### Binding initializations 58 ### Binding initializations
42 bind pub - !sää2 weather_cmd_pub 59 bind pub - !sää weather_cmd_pub
43 bind pub - !saa2 weather_cmd_pub 60 bind pub - !saa weather_cmd_pub
44 bind msg - !sää2 weather_cmd_msg 61 bind msg - !sää weather_cmd_msg
45 bind msg - !saa2 weather_cmd_msg 62 bind msg - !saa weather_cmd_msg
46 63
47 64
48 ### Initialization messages 65 ### Initialization messages
49 package require textutil::split 66 package require textutil::split
50 set weather_message "$weather_name v$weather_version by ccr/TNSP" 67 set weather_message "$weather_name v$weather_version by ccr/TNSP"
58 putlog "$weather_name: $arg" 75 putlog "$weather_name: $arg"
59 } 76 }
60 } 77 }
61 78
62 79
63 proc weather_msg {apublic anick achan amsg} { 80 proc weather_msg_do {apublic anick achan amsg} {
64 global weather_preferredmsg 81 global weather_preferredmsg
65 if {$apublic == 1} { 82 if {$apublic == 1} {
66 putserv "$weather_preferredmsg $achan :$amsg" 83 putserv "$weather_preferredmsg $achan :$amsg"
67 } else { 84 } else {
68 putserv "$weather_preferredmsg $anick :$amsg" 85 putserv "$weather_preferredmsg $anick :$amsg"
69 } 86 }
70 } 87 }
71 88
72 89
90 proc weather_usage {apublic anick achan amsg} {
91 global weather_msg_usage_prefix_1 weather_msg_usage_prefix_2
92 set nline 0
93 foreach aline [split $amsg "\n"] {
94 if {$nline == 0} {
95 weather_msg_do $apublic $anick $achan "$weather_msg_usage_prefix_1$aline"
96 } else {
97 weather_msg_do $apublic $anick $achan "$weather_msg_usage_prefix_2$aline"
98 }
99 incr nline
100 }
101 }
102
103
104 proc weather_msg {apublic anick achan amsg {aargs {}}} {
105 set narg 1
106 foreach marg $aargs {
107 set amsg [string map [list "%$narg" $marg] $amsg]
108 incr narg
109 }
110 weather_msg_do $apublic $anick $achan $amsg
111 }
112
113
73 proc weather_ctime {utime} { 114 proc weather_ctime {utime} {
74 return [clock format $utime -format "%H:%M"] 115 return [clock format $utime -format "%H:%M"]
75 } 116 }
76 117
77 118
81 } 122 }
82 return 0 123 return 0
83 } 124 }
84 125
85 126
86 proc weather_check_user {uhand} { 127 #-------------------------------------------------------------------------
87 if {![weather_valid_user $uhand]} { 128 proc weather_load_aliases {} {
88 weather_msg $upublic $unick $uchan $weather_msg_user_not_known 129 global weather_aliasfile weather_aliases
89 return 0 130
90 } 131 # Create dict
91 return 1 132 array unset weather_aliases
133 array set weather_aliases {}
134
135 # Read datafile
136 if {![catch {set ufile [open $weather_aliasfile r 0600]} uerrmsg]} {
137 while {![eof $ufile]} {
138 gets $ufile uline
139 set udata [split $uline "|"]
140 if {[llength $udata] == 2} {
141 set weather_aliases([lindex $udata 0]) [lindex $udata 1]
142 }
143 }
144 close $ufile
145 } else {
146 weather_log "Could not open data file: $uerrmsg"
147 }
148 }
149
150
151 proc weather_save_aliases {} {
152 global weather_aliasfile weather_aliases
153
154 if {![catch {set ufile [open $weather_aliasfile w 0600]} uerrmsg]} {
155 foreach {ukey uvalue} [array get weather_aliases] {
156 puts $ufile "$ukey|$uvalue"
157 }
158 close $ufile
159 } else {
160 weather_log "Could not open data file: $uerrmsg"
161 }
162 }
163
164
165 proc weather_get_alias {uname} {
166 global weather_aliases
167 set utmp [array get weather_aliases $uname]
168 if {[llength $utmp] > 0} {
169 return [lindex $utmp 1]
170 }
171 return $uname
92 } 172 }
93 173
94 174
95 #------------------------------------------------------------------------- 175 #-------------------------------------------------------------------------
96 proc weather_get_data {ukey udata} { 176 proc weather_get_data {ukey udata} {
116 global weather_datafile weather_data 196 global weather_datafile weather_data
117 197
118 # Create dict 198 # Create dict
119 array unset weather_data 199 array unset weather_data
120 array set weather_data {} 200 array set weather_data {}
201
121 set wtemp_min_val 500000 202 set wtemp_min_val 500000
122 set wtemp_max_val -500000 203 set wtemp_max_val -500000
123 set wtemp_min_key "" 204 set wtemp_min_key ""
124 set wtemp_max_key "" 205 set wtemp_max_key ""
125 206
173 #------------------------------------------------------------------------- 254 #-------------------------------------------------------------------------
174 if {![info exists weather_data]} { 255 if {![info exists weather_data]} {
175 array set weather_data {} 256 array set weather_data {}
176 } 257 }
177 258
259 if {![info exists weather_aliases]} {
260 array set weather_aliases {}
261 }
262
178 if {[info exists weather_running]} { 263 if {[info exists weather_running]} {
179 set weather_last [expr [clock seconds] - $weather_running] 264 set weather_last [expr [clock seconds] - $weather_running]
180 } else { 265 } else {
181 set weather_last -1 266 set weather_last -1
182 } 267 }
268
269 weather_log "Loading aliases."
270 weather_load_aliases
183 271
184 if {$weather_last < 0 || $weather_last > [expr $weather_check_period * 60]} { 272 if {$weather_last < 0 || $weather_last > [expr $weather_check_period * 60]} {
185 weather_log "Starting weather update." 273 weather_log "Starting weather update."
186 weather_exec 274 weather_exec
187 } else { 275 } else {
188 weather_update 276 weather_update
189 } 277 }
190 278
279
191 #------------------------------------------------------------------------- 280 #-------------------------------------------------------------------------
192 proc weather_cmd {unick uhand uchan uargs upublic} { 281 proc weather_cmd {unick uhand uchan uargs upublic} {
193 global weather_default_locations weather_data weather_max_results 282 global weather_default_locations weather_data weather_max_results weather_aliases
194 global weather_msg_usage_1 weather_msg_usage_2 weather_msg_user_not_known 283 global weather_msg_usage_full weather_msg_usage_def_set weather_msg_user_not_known
195 global weather_msg_no_results weather_msg_no_data_for_location 284 global weather_msg_no_results weather_msg_no_data_for_location
285 global weather_msg_usage_alias weather_msg_usage_unalias weather_msg_defloc
286 global weather_msg_aliased weather_msg_unaliased weather_msg_no_access
287 global weather_msg_def_set weather_msg_def_not_set
288
289 putlog "$unick : '$uhand' @ $uchan"
196 290
197 # Check and handle arguments 291 # Check and handle arguments
198 set rarglist [::textutil::split::splitx $uargs {\s+}] 292 set rarglist [::textutil::split::splitx $uargs {\s+}]
199 set rarg [lindex $rarglist 0] 293 set rarg [lindex $rarglist 0]
200 294
201 if {$rarg == "?" || $rarg == "help" || $rarg == "apua"} { 295 if {$rarg == "?" || $rarg == "help" || $rarg == "apua"} {
202 weather_msg $upublic $unick $uchan $weather_msg_usage_1 296 weather_usage $upublic $unick $uchan "$weather_msg_usage_full"
203 weather_msg $upublic $unick $uchan $weather_msg_usage_2
204 return 0 297 return 0
205 } 298 }
206 299
207 # Setting the location 300 # Setting the location
208 if {$rarg == "aseta" || $rarg == "set"} { 301 if {$rarg == "vakio" || $rarg == "default" || $rarg == "vakiot"} {
209 if {![weather_check_user $uhand]} { 302 if {![weather_valid_user $uhand]} {
210 return 0 303 weather_msg $upublic $unick $uchan $weather_msg_user_not_known
211 } 304 return 0
212 305 }
213 # Split the list of desired locations 306
214 set qlist [::textutil::split::splitx [join [lrange $rarglist 1 end] " "] {\s*\;\s*}] 307 if {[llength $rarglist] == 1} {
308 set lista [getuser $uhand XTRA "weather_locations"]
309 if {$lista == "" || $lista == "{}"} {
310 weather_msg $upublic $unick $uchan $weather_msg_def_not_set [list $uhand]
311 } else {
312 set lista [join [split $lista ";"] " ; "]
313 weather_msg $upublic $unick $uchan $weather_msg_defloc [list $uhand $lista]
314 }
315 } else {
316 # Split the list of desired locations
317 set qlist [::textutil::split::splitx [join [lrange $rarglist 1 end] " "] {\s*\;\s*}]
318 set nlist [lsearch -all -inline -not -exact $qlist ""]
319
320 if {[llength $nlist] > 0} {
321 weather_msg $upublic $unick $uchan $weather_msg_def_set [list [join $nlist " ; "]]
322 setuser $uhand XTRA "weather_locations" [join $nlist ";"]
323 } else {
324 weather_usage $upublic $unick $uchan $weather_msg_usage_def_set
325 }
326 }
327 return 0
328 } elseif {$rarg == "alias"} {
329 if {![weather_valid_user $uhand] || ![matchattr $uhand n]} {
330 weather_msg $upublic $unick $uchan $weather_msg_no_access
331 return 0
332 }
333
334 set qlist [::textutil::split::splitx [join [lrange $rarglist 1 end] " "] {\s*=\s*}]
215 set nlist [lsearch -all -inline -not -exact $qlist ""] 335 set nlist [lsearch -all -inline -not -exact $qlist ""]
216 336 if {[llength $nlist] < 2} {
217 if {[llength $nlist] > 0} { 337 weather_usage $upublic $unick $uchan $weather_msg_usage_alias
218 set lista [join $nlist ";"] 338 return 0
219 weather_msg $upublic $unick $uchan "Vakiopaikoiksi asetettu: \002${lista}\002." 339 }
220 setuser $uhand XTRA "weather_locations" $lista 340
221 } else { 341 set ualias [lindex $nlist 0]
222 weather_msg $upublic $unick $uchan $weather_msg_usage_2 342 set uname [lindex $nlist 1]
223 } 343 set weather_aliases($ualias) $uname
344 weather_msg $upublic $unick $uchan $weather_msg_aliased [list $ualias $uname]
345
346 weather_save_aliases
224 return 0 347 return 0
225 } 348 } elseif {$rarg == "unalias"} {
226 349 if {![weather_valid_user $uhand] || ![matchattr $uhand n]} {
227 if {$rarg == "default" || $rarg == "vakio"} { 350 weather_msg $upublic $unick $uchan $weather_msg_no_access
228 if {![weather_check_user $uhand]} { 351 return 0
229 return 0 352 }
230 } 353
231 354 if {[llength $rarglist] < 2} {
232 set lista [getuser $uhand XTRA "weather_locations"] 355 weather_usage $upublic $unick $uchan $weather_msg_usage_unalias
233 if {$lista == "" || $lista == "{}"} { 356 return 0
234 set lista "Ei asetettu" 357 }
235 } 358
236 weather_msg $upublic $unick $uchan "Vakiopaikat ${uhand}: \002${lista}\002." 359 set ualias [lindex $rarglist 1]
360 unset weather_aliases($ualias)
361 weather_msg $upublic $unick $uchan $weather_msg_unaliased [list $ualias]
362
363 weather_save_aliases
237 return 0 364 return 0
238 } 365 }
239 366
240 # Get args or default location(s) 367 # Get args or default location(s)
241 set rargs [join $rarglist " "] 368 set rargs [join $rarglist " "]
267 weather_msg $upublic $unick $uchan "Min: $umin | Max: $umax" 394 weather_msg $upublic $unick $uchan "Min: $umin | Max: $umax"
268 incr nresults 395 incr nresults
269 } else { 396 } else {
270 # Location match 397 # Location match
271 set ufound 0 398 set ufound 0
399 set rarg [weather_get_alias $rarg]
272 foreach {ukey uvalue} [array get weather_data] { 400 foreach {ukey uvalue} [array get weather_data] {
273 if {![string match "w_*" $ukey] && [string match -nocase "*${rarg}*" $ukey]} { 401 if {![string match "w_*" $ukey] && [string match -nocase "*${rarg}*" $ukey]} {
274 if {[llength $uvalue] > 0} { 402 if {[llength $uvalue] > 0} {
275 weather_msg $upublic $unick $uchan [weather_get_data $ukey $uvalue] 403 weather_msg $upublic $unick $uchan [weather_get_data $ukey $uvalue]
276 incr nresults 404 incr nresults
277 } else { 405 } else {
278 weather_msg $upublic $unick $uchan "\002${ukey}\002: $weather_msg_no_results" 406 weather_msg $upublic $unick $uchan $weather_msg_no_results [list $ukey]
279 } 407 }
280 incr ufound 408 incr ufound
281 } 409 }
282 410
411 # Check for results limit
283 if {$nresults >= $weather_max_results} { 412 if {$nresults >= $weather_max_results} {
284 return 0 413 return 0
285 } 414 }
286 } 415 }
287 416
288 if {$ufound == 0} { 417 if {$ufound == 0} {
289 weather_msg $upublic $unick $uchan "\002${rarg}\002: $weather_msg_no_data_for_location" 418 weather_msg $upublic $unick $uchan $weather_msg_no_data_for_location [list $rarg]
290 } 419 }
291 } 420 }
421
422 # Check for results limit
292 if {$nresults >= $weather_max_results} { 423 if {$nresults >= $weather_max_results} {
293 return 0 424 return 0
294 } 425 }
295 } 426 }
296 return 0 427 return 0