comparison weather.tcl @ 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 3174701325c0
comparison
equal deleted inserted replaced
537:210d22dac6d4 538:f07c972e269a
39 putlog "$weather_name: $arg" 39 putlog "$weather_name: $arg"
40 } 40 }
41 } 41 }
42 42
43 43
44 proc weather_msg {apublic anick achan amsg {aargs {}}} { 44 proc weather_qm {uid} {
45 global weather_messages
46
47 if {[info exists weather_messages($uid)]} {
48 return $weather_messages($uid)
49 } else {
50 return $uid
51 }
52 }
53
54
55 proc weather_msg {apublic anick achan aid {aargs {}}} {
45 global weather_preferredmsg 56 global weather_preferredmsg
46 set narg 1 57 set amsg [weather_qm $aid]
47 foreach marg $aargs { 58 set amsg [string map [list "@cmd@" "!sää"] $amsg]
48 set amsg [string map [list "%$narg" $marg] $amsg] 59 utl_msg_args $weather_preferredmsg $apublic $anick $achan $amsg $aargs
49 incr narg 60 }
50 } 61
51 utl_msg_do $weather_preferredmsg $apublic $anick $achan $amsg 62
52 } 63 proc weather_smsg {apublic anick achan amsg {aargs {}}} {
53
54
55 proc weather_usage {apublic anick achan amsg} {
56 global weather_msg_usage_prefix_1 weather_msg_usage_prefix_2
57 global weather_preferredmsg 64 global weather_preferredmsg
58 set nline 0 65 set amsg [string map [list "@cmd@" "!sää"] $amsg]
59 foreach aline [split $amsg "\n"] { 66 utl_msg_args $weather_preferredmsg $apublic $anick $achan $amsg $aargs
60 if {[string range $aline 0 1] == ":"} {
61 utl_msg_do $weather_preferredmsg $apublic $anick $achan $aline
62 } elseif {$nline == 0} {
63 utl_msg_do $weather_preferredmsg $apublic $anick $achan "$weather_msg_usage_prefix_1$aline"
64 } else {
65 utl_msg_do $weather_preferredmsg $apublic $anick $achan "$weather_msg_usage_prefix_2$aline"
66 }
67 incr nline
68 }
69 } 67 }
70 68
71 69
72 #------------------------------------------------------------------------- 70 #-------------------------------------------------------------------------
73 proc weather_load_aliases {} { 71 proc weather_load_aliases {} {
279 return $astr 277 return $astr
280 } 278 }
281 279
282 280
283 # Get data by location key 281 # Get data by location key
284 proc weather_get_by_key {ukey} { 282 proc weather_get_str_by_key {ukey} {
285 global weather_data weather_msg_result 283 global weather_data weather_msg_result
286 return [weather_get_str $weather_data($ukey) $weather_msg_result] 284 return [weather_get_str $weather_data($ukey) $weather_msg_result]
287 } 285 }
288 286
289 287
318 316
319 317
320 #------------------------------------------------------------------------- 318 #-------------------------------------------------------------------------
321 proc weather_cmd {unick uhand uchan uargs upublic} { 319 proc weather_cmd {unick uhand uchan uargs upublic} {
322 global weather_default_locations weather_data weather_max_results weather_aliases 320 global weather_default_locations weather_data weather_max_results weather_aliases
323 global weather_msg_usage_full weather_msg_usage_def_set weather_msg_user_not_known 321 global weather_msg_list_station weather_msg_result weather_messages
324 global weather_msg_no_results weather_msg_no_data_for_location
325 global weather_msg_usage_alias weather_msg_usage_unalias weather_msg_defloc
326 global weather_msg_aliased weather_msg_unaliased weather_msg_no_access
327 global weather_msg_def_set weather_msg_def_not_set weather_msg_aliases
328 global weather_msg_usage_stations weather_msg_stations weather_msg_list_station
329 global weather_msg_list_nearest weather_msg_usage_nearest weather_msg_usage_nearest_invalid
330 global weather_msg_nearest_stations weather_msg_result
331 322
332 # Check and handle arguments 323 # Check and handle arguments
333 set rarglist [::textutil::split::splitx $uargs {\s+}] 324 set rarglist [::textutil::split::splitx $uargs {\s+}]
334 set rcmd [lindex $rarglist 0] 325 set rcmd [lindex $rarglist 0]
335 326
336 if {$rcmd == "?" || $rcmd == "help" || $rcmd == "apua"} { 327 if {$rcmd == "?" || $rcmd == "help" || $rcmd == "apua"} {
337 weather_usage $upublic $unick $uchan $weather_msg_usage_full 328 set help_full {}
329
330 # FIXME
331 foreach {ukey uvalue} [array get weather_messages] {
332 if {[string match "usage_*" $ukey]} {
333 lappend help_full $uvalue
334 }
335 }
336
337 weather_msg $upublic $unick $uchan "usage_full" $help_full
338 return 0 338 return 0
339 } 339 }
340 340
341 if {$rcmd == "asemat" || $rcmd == "stations"} { 341 if {$rcmd == "asemat" || $rcmd == "stations"} {
342 # List stations/locations matching the given pattern 342 # List stations/locations matching the given pattern
343 if {[llength $rarglist] < 2} { 343 if {[llength $rarglist] < 2} {
344 weather_usage $upublic $unick $uchan $weather_msg_usage_stations 344 weather_msg $upublic $unick $uchan "usage_stations"
345 return 0 345 return 0
346 } 346 }
347 347
348 set rmatch [join [lrange $rarglist 1 end] " "] 348 set rmatch [join [lrange $rarglist 1 end] " "]
349 set result {} 349 set result {}
353 lappend result [weather_get_str $uvalue $weather_msg_list_station] 353 lappend result [weather_get_str $uvalue $weather_msg_list_station]
354 } 354 }
355 } 355 }
356 356
357 set res [join $result " ; "] 357 set res [join $result " ; "]
358 weather_msg $upublic $unick $uchan $weather_msg_stations [list $res] 358 weather_msg $upublic $unick $uchan "stations_list" [list $res]
359 return 0 359 return 0
360 } elseif {$rcmd == "lahin" || $rcmd == "lähin" || $rcmd == "closest" || $rcmd == "nearest"} { 360 } elseif {$rcmd == "lahin" || $rcmd == "lähin" || $rcmd == "closest" || $rcmd == "nearest"} {
361 # List stations nearest to given coordinates 361 # List stations nearest to given coordinates
362 set qargs [join [lrange $rarglist 1 end] ""] 362 set qargs [join [lrange $rarglist 1 end] ""]
363 if {![regexp {@?(\d+|\d+\.\d+|\.\d+)\s*,\s*(\d+|\d+\.\d+|\.\d+)} $qargs -> d_lat d_lng]} { 363 if {![regexp {@?(\d+|\d+\.\d+|\.\d+)\s*,\s*(\d+|\d+\.\d+|\.\d+)} $qargs -> d_lat d_lng]} {
364 weather_usage $upublic $unick $uchan $weather_msg_usage_nearest 364 weather_msg $upublic $unick $uchan "usage_nearest"
365 return 0 365 return 0
366 } 366 }
367 367
368 # Check argument types 368 # Check argument types
369 if {![string is double -strict $d_lat] || ![string is double -strict $d_lng]} { 369 if {![string is double -strict $d_lat] || ![string is double -strict $d_lng]} {
370 weather_msg $upublic $unick $uchan $weather_msg_usage_nearest_invalid 370 weather_msg $upublic $unick $uchan "nearest_invalid"
371 return 0 371 return 0
372 } 372 }
373 373
374 # Calculate distances between given coordinates for each location 374 # Calculate distances between given coordinates for each location
375 set result {} 375 set result {}
391 lappend uresult [weather_get_str $weather_data([lindex $uval 0]) $weather_msg_list_nearest] 391 lappend uresult [weather_get_str $weather_data([lindex $uval 0]) $weather_msg_list_nearest]
392 } 392 }
393 393
394 # Print out the result 394 # Print out the result
395 set res [join $uresult " ; "] 395 set res [join $uresult " ; "]
396 weather_msg $upublic $unick $uchan $weather_msg_nearest_stations [list $d_lat $d_lng $res] 396 weather_msg $upublic $unick $uchan "nearest_stations" [list $d_lat $d_lng $res]
397 return 0 397 return 0
398 } elseif {$rcmd == "vakio" || $rcmd == "default" || $rcmd == "vakiot" || $rcmd == "defaults"} { 398 } elseif {$rcmd == "vakio" || $rcmd == "default" || $rcmd == "vakiot" || $rcmd == "defaults"} {
399 # List or set the default weather station name patterns for this user 399 # List or set the default weather station name patterns for this user
400 400
401 # Access check 401 # Access check
402 if {![utl_valid_user $uhand]} { 402 if {![utl_valid_user $uhand]} {
403 weather_msg $upublic $unick $uchan $weather_msg_user_not_known 403 weather_msg $upublic $unick $uchan "user_not_known"
404 return 0 404 return 0
405 } 405 }
406 406
407 if {[llength $rarglist] == 1} { 407 if {[llength $rarglist] == 1} {
408 set lista [getuser $uhand XTRA "weather_locations"] 408 set lista [getuser $uhand XTRA "weather_locations"]
409 if {$lista == "" || $lista == "{}"} { 409 if {$lista == "" || $lista == "{}"} {
410 weather_msg $upublic $unick $uchan $weather_msg_def_not_set [list $uhand] 410 weather_msg $upublic $unick $uchan "def_not_set" [list $uhand]
411 } else { 411 } else {
412 set lista [join [split $lista ";"] " ; "] 412 set lista [join [split $lista ";"] " ; "]
413 weather_msg $upublic $unick $uchan $weather_msg_defloc [list $uhand $lista] 413 weather_msg $upublic $unick $uchan "def_value" [list $uhand $lista]
414 } 414 }
415 } else { 415 } else {
416 # Split the list of desired locations 416 # Split the list of desired locations
417 set qlist [::textutil::split::splitx [join [lrange $rarglist 1 end] " "] {\s*\;\s*}] 417 set qlist [::textutil::split::splitx [join [lrange $rarglist 1 end] " "] {\s*\;\s*}]
418 set nlist [lsearch -all -inline -not -exact $qlist ""] 418 set nlist [lsearch -all -inline -not -exact $qlist ""]
419 419
420 if {[llength $nlist] > 0} { 420 if {[llength $nlist] > 0} {
421 weather_msg $upublic $unick $uchan $weather_msg_def_set [list [join $nlist " ; "]] 421 weather_msg $upublic $unick $uchan "def_set_to" [list [join $nlist " ; "]]
422 setuser $uhand XTRA "weather_locations" [join $nlist ";"] 422 setuser $uhand XTRA "weather_locations" [join $nlist ";"]
423 } else { 423 } else {
424 weather_usage $upublic $unick $uchan $weather_msg_usage_def_set 424 weather_msg $upublic $unick $uchan "usage_def_set"
425 } 425 }
426 } 426 }
427 return 0 427 return 0
428 } elseif {$rcmd == "alias"} { 428 } elseif {$rcmd == "alias"} {
429 # Alias a string to another, only certain users have access (+n flag) 429 # Alias a string to another, only certain users have access (+n flag)
430 if {![utl_valid_user $uhand] || ![matchattr $uhand n]} { 430 if {![utl_valid_user $uhand] || ![matchattr $uhand n]} {
431 weather_msg $upublic $unick $uchan $weather_msg_no_access 431 weather_msg $upublic $unick $uchan "no_access"
432 return 0 432 return 0
433 } 433 }
434 434
435 set qlist [::textutil::split::splitx [join [lrange $rarglist 1 end] " "] {\s*=\s*}] 435 set qlist [::textutil::split::splitx [join [lrange $rarglist 1 end] " "] {\s*=\s*}]
436 set nlist [lsearch -all -inline -not -exact $qlist ""] 436 set nlist [lsearch -all -inline -not -exact $qlist ""]
437 if {[llength $nlist] < 2} { 437 if {[llength $nlist] < 2} {
438 weather_usage $upublic $unick $uchan $weather_msg_usage_alias 438 weather_msg $upublic $unick $uchan "usage_alias"
439 return 0 439 return 0
440 } 440 }
441 441
442 set ualias [lindex $nlist 0] 442 set ualias [lindex $nlist 0]
443 set uname [lindex $nlist 1] 443 set uname [lindex $nlist 1]
444 set weather_aliases($ualias) $uname 444 set weather_aliases($ualias) $uname
445 weather_msg $upublic $unick $uchan $weather_msg_aliased [list $ualias $uname] 445 weather_msg $upublic $unick $uchan "alias_set" [list $ualias $uname]
446 446
447 weather_save_aliases 447 weather_save_aliases
448 return 0 448 return 0
449 } elseif {$rcmd == "unalias"} { 449 } elseif {$rcmd == "unalias"} {
450 # Remove one alias, only certain users have access (+n flag) 450 # Remove one alias, only certain users have access (+n flag)
451 if {![utl_valid_user $uhand] || ![matchattr $uhand n]} { 451 if {![utl_valid_user $uhand] || ![matchattr $uhand n]} {
452 weather_msg $upublic $unick $uchan $weather_msg_no_access 452 weather_msg $upublic $unick $uchan "no_access"
453 return 0 453 return 0
454 } 454 }
455 455
456 if {[llength $rarglist] < 2} { 456 if {[llength $rarglist] < 2} {
457 weather_usage $upublic $unick $uchan $weather_msg_usage_unalias 457 weather_msg $upublic $unick $uchan "usage_unalias"
458 return 0 458 return 0
459 } 459 }
460 460
461 set ualias [lindex $rarglist 1] 461 set ualias [lindex $rarglist 1]
462 weather_msg $upublic $unick $uchan "alias_unset" [list $ualias $weather_aliases($ualias)]
462 unset weather_aliases($ualias) 463 unset weather_aliases($ualias)
463 weather_msg $upublic $unick $uchan $weather_msg_unaliased [list $ualias]
464 464
465 weather_save_aliases 465 weather_save_aliases
466 return 0 466 return 0
467 } elseif {$rcmd == "list"} { 467 } elseif {$rcmd == "list"} {
468 # List all currently defined aliases 468 # List all currently defined aliases
469 set ulist {} 469 set ulist {}
470 foreach {ukey uvalue} [array get weather_aliases] { 470 foreach {ukey uvalue} [array get weather_aliases] {
471 lappend ulist "\002'$ukey' = '$uvalue'\002" 471 lappend ulist "\002'$ukey' = '$uvalue'\002"
472 } 472 }
473 weather_msg $upublic $unick $uchan $weather_msg_aliases [list [join $ulist ", "]] 473 weather_msg $upublic $unick $uchan "alias_list" [list [join $ulist ", "]]
474 return 0 474 return 0
475 } 475 }
476 476
477 # Get args or default location(s) 477 # Get args or default location(s)
478 set rargs [join $rarglist " "] 478 set rargs [join $rarglist " "]
487 set nresults 0 487 set nresults 0
488 set rarglist [::textutil::split::splitx $rargs "\s*\;\s*"] 488 set rarglist [::textutil::split::splitx $rargs "\s*\;\s*"]
489 foreach rarg $rarglist { 489 foreach rarg $rarglist {
490 if {$rarg == "min"} { 490 if {$rarg == "min"} {
491 # Min temp 491 # Min temp
492 set umin [weather_get_by_key "w_min"] 492 weather_msg $upublic $unick $uchan "temp_min" [list [weather_get_str_by_key "w_min"]]
493 weather_msg $upublic $unick $uchan "Min: $umin"
494 incr nresults 493 incr nresults
495 } elseif {$rarg == "max"} { 494 } elseif {$rarg == "max"} {
496 # Max temp 495 # Max temp
497 set umax [weather_get_by_key "w_max"] 496 weather_msg $upublic $unick $uchan "temp_max" [list [weather_get_str_by_key "w_max"]]
498 weather_msg $upublic $unick $uchan "Max: $umax"
499 incr nresults 497 incr nresults
500 } elseif {$rarg == "minmax" || $rarg == "min max" || $rarg == "maxmin" || $rarg == "max min"} { 498 } elseif {$rarg == "minmax" || $rarg == "min max" || $rarg == "maxmin" || $rarg == "max min"} {
501 # Min & Max temps 499 # Min & Max temps
502 set umin [weather_get_by_key "w_min"] 500 weather_msg $upublic $unick $uchan "temp_minmax" [list [weather_get_str_by_key "w_min"] [weather_get_str_by_key "w_max"]]
503 set umax [weather_get_by_key "w_max"]
504 weather_msg $upublic $unick $uchan "Min: $umin"
505 weather_msg $upublic $unick $uchan "Max: $umax"
506 incr nresults 501 incr nresults
507 } else { 502 } else {
508 # Location match 503 # Location match
509 set ufound 0 504 set ufound 0
510 set rarg [weather_get_alias $rarg] 505 set rarg [weather_get_alias $rarg]
511 foreach {ukey uvalue} [array get weather_data] { 506 foreach {ukey uvalue} [array get weather_data] {
512 if {![string match "w_*" $ukey] && [string match -nocase "*${rarg}*" $ukey]} { 507 if {![string match "w_*" $ukey] && [string match -nocase "*${rarg}*" $ukey]} {
513 if {[llength $uvalue] > 0} { 508 if {[llength $uvalue] > 0} {
514 weather_msg $upublic $unick $uchan [weather_get_str $uvalue $weather_msg_result] 509 weather_smsg $upublic $unick $uchan [weather_get_str $uvalue $weather_msg_result]
515 incr nresults 510 incr nresults
516 } else { 511 } else {
517 weather_msg $upublic $unick $uchan $weather_msg_no_results [list $ukey] 512 weather_msg $upublic $unick $uchan "no_results" [list $ukey]
518 } 513 }
519 incr ufound 514 incr ufound
520 } 515 }
521 516
522 # Check for results limit 517 # Check for results limit
524 return 0 519 return 0
525 } 520 }
526 } 521 }
527 522
528 if {$ufound == 0} { 523 if {$ufound == 0} {
529 weather_msg $upublic $unick $uchan $weather_msg_no_data_for_location [list $rarg] 524 weather_msg $upublic $unick $uchan "no_data_for_location" [list $rarg]
530 } 525 }
531 } 526 }
532 527
533 # Check for results limit 528 # Check for results limit
534 if {$nresults >= $weather_max_results} { 529 if {$nresults >= $weather_max_results} {