Mercurial > hg > egg-tcls
annotate ruoka.tcl @ 424:825cac46b1cb
Cosmetic / stray trailing whitespace cleanup.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 08 Jan 2017 03:55:55 +0200 |
parents | 44593f1fb916 |
children | 85fe3bc36307 |
rev | line source |
---|---|
0 | 1 ########################################################################## |
2 # | |
114
593874678e45
Clarify authorship by doing sed "s/ccr\/TNSP/Matti 'ccr' Hamalainen/g".
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
3 # RuokaLista v1.1 by Matti 'ccr' Hamalainen <ccr@tnsp.org> |
0 | 4 # (C) Copyright 2004-2010 Tecnic Software productions (TNSP) |
5 # | |
6 # Requires hae_ruoka.tcl to be run as a cronjob, for example | |
7 # 15 * * * * /absolute/path/to/hae_ruoka.tcl | |
8 # | |
9 # This script is freely distributable under GNU GPL (version 2) license. | |
10 # | |
11 ########################################################################## | |
12 | |
13 # Vakioravintola, jos muuta ei annettu/asetettu | |
14 set ruoka_restaurant "OAMK" | |
15 | |
16 # Datatiedosto, oltava sama kuin hae_ruoka.tcl:n vastaava asetus | |
17 set ruoka_datafile "/home/niinuska/bot/data.ruoka" | |
18 | |
19 # Kellonaika, jolloin default-päivä vaihtuu seuraavaksi vuorokaudeksi | |
20 # -1 = Ei vaihdeta | |
21 # esim. 15 = klo 15 jälkeen näytetään vakiona seuraavan päivän ruokalista | |
22 set ruoka_threshold 15 | |
23 | |
24 # Yleiset asetukset | |
25 set ruoka_logmsg 1 | |
26 set ruoka_preferredmsg "PRIVMSG" | |
27 | |
28 | |
29 ########################################################################## | |
30 # No need to look below this line | |
31 ########################################################################## | |
32 set ruoka_name "RuokaLista" | |
33 set ruoka_version "1.1" | |
34 | |
35 ### Binding initializations | |
36 bind pub - !ruoka ruoka_get_pub | |
216 | 37 bind msg - !ruoka ruoka_get_msg |
0 | 38 bind pub - !ruokaset ruoka_set_pub |
39 | |
40 ### Initialization messages | |
41 set ruoka_message "$ruoka_name v$ruoka_version by ccr/TNSP" | |
42 putlog "$ruoka_message" | |
43 | |
44 set ruoka_days {Sunnuntai Maanantai Tiistai Keskiviikko Torstai Perjantai Lauantai} | |
45 | |
46 #------------------------------------------------------------------------- | |
47 proc ruoka_log {arg} { | |
48 global ruoka_logmsg ruoka_name | |
49 if {$ruoka_logmsg != 0} { | |
50 putlog "$ruoka_name: $arg" | |
51 } | |
52 } | |
53 | |
54 | |
55 proc ruoka_msg {apublic anick achan amsg} { | |
56 global ruoka_preferredmsg | |
57 if {$apublic == 1} { | |
58 putserv "$ruoka_preferredmsg $achan :$amsg" | |
59 } else { | |
424
825cac46b1cb
Cosmetic / stray trailing whitespace cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
216
diff
changeset
|
60 putserv "$ruoka_preferredmsg $anick :$amsg" |
0 | 61 } |
62 } | |
63 | |
64 | |
65 proc ruoka_get_restaurants {urestaurants} { | |
66 global ruoka_datafile | |
67 upvar $urestaurants restaurants | |
68 | |
69 if {![catch {set ufile [open "$ruoka_datafile" r 0600]} uerrmsg]} { | |
70 while {![eof $ufile]} { | |
71 gets $ufile uline | |
72 set urec [split $uline "½"] | |
73 if {[llength $urec] == 4} { | |
74 set restaurants([lindex $urec 0]) 1 | |
75 } | |
76 } | |
77 } | |
78 } | |
79 | |
80 | |
81 proc ruoka_match_day {umatch} { | |
82 global ruoka_days | |
83 if {$umatch == "" || $umatch == "{}"} { return "" } | |
84 set udate "" | |
85 foreach i $ruoka_days { | |
86 if {[string match "$umatch*" [string tolower $i]]} { set udate $i } | |
87 } | |
88 return $udate | |
89 } | |
90 | |
91 | |
92 proc ruoka_match_restaurant {urestaurants umatch} { | |
93 upvar $urestaurants restaurants | |
94 if {$umatch == "" || $umatch == "{}"} { return "" } | |
95 foreach {nimi arvo} [array get restaurants] { | |
96 if {[string match "$umatch*" [string tolower $nimi]]} { | |
97 return $nimi | |
98 } | |
99 } | |
100 return "" | |
101 } | |
102 | |
103 | |
104 #------------------------------------------------------------------------- | |
105 proc ruoka_get {unick uhand uchan uargs upublic} { | |
106 global ruoka_restaurant ruoka_datafile ruoka_days ruoka_threshold | |
107 | |
108 # Get list of restaurants | |
109 ruoka_get_restaurants restaurants | |
424
825cac46b1cb
Cosmetic / stray trailing whitespace cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
216
diff
changeset
|
110 |
0 | 111 # Check and handle arguments |
112 set ulist [split [string tolower $uargs] " "] | |
113 set rarg1 [lindex $ulist 0] | |
114 set rarg2 [lindex $ulist 1] | |
115 | |
116 if {$rarg1 == "?" || $rarg1 == "help"} { | |
117 set tmp [join [array names restaurants] ", "] | |
118 ruoka_msg $upublic $unick $uchan "Käyttö: <!>ruoka \[ravintola\] \[päivä\]" | |
119 ruoka_msg $upublic $unick $uchan "Huom! Jos päivää ei anneta, tiedot haetaan tältä päivältä." | |
120 ruoka_msg $upublic $unick $uchan "Käyttö: <!>ruokaset <ravintola>" | |
121 ruoka_msg $upublic $unick $uchan "Jossa ravintola on yksi seuraavista: $tmp" | |
122 return 0 | |
123 } | |
124 | |
125 # Try to parse arguments, if any | |
126 set urestaurant "" | |
127 set udate "" | |
128 if {$rarg1 != ""} { | |
129 set udate [ruoka_match_day $rarg1] | |
130 if {$udate == ""} { | |
131 set udate [ruoka_match_day $rarg2] | |
132 set urestaurant [ruoka_match_restaurant restaurants $rarg1] | |
133 } else { | |
134 set urestaurant [ruoka_match_restaurant restaurants $rarg2] | |
135 } | |
136 } | |
137 | |
138 # Default date to today | |
139 if {$udate == ""} { | |
140 set sstamp [unixtime] | |
141 | |
142 # Check if threshold setting is active and act accordingly | |
143 if {$ruoka_threshold > 0 && [clock format $sstamp -format "%H"] > $ruoka_threshold} { | |
144 set sstamp [expr $sstamp + 60*60*(24 - $ruoka_threshold)+30] | |
145 } | |
146 | |
147 set udate [lindex $ruoka_days [clock format $sstamp -format "%w"]] | |
148 } | |
149 | |
150 # If no restaurant given, try to get one from user settings | |
151 if {$urestaurant == ""} { | |
152 set urestaurant [getuser $uhand XTRA "restaurant"] | |
153 # If still no known/set restaurant, use global default | |
154 if {$urestaurant == ""} { | |
155 set urestaurant $ruoka_restaurant | |
156 } | |
157 } | |
158 | |
159 # Read datafile | |
160 if {![catch {set ufile [open "$ruoka_datafile" r 0600]} uerrmsg]} { | |
161 set found 0 | |
162 while {![eof $ufile]} { | |
163 gets $ufile uline | |
164 set urec [split $uline "½"] | |
165 if {[llength $urec] == 4 && [lindex $urec 1] == $udate && [lindex $urec 0] == $urestaurant} { | |
166 ruoka_msg $upublic $unick $uchan "\002[lindex $urec 0]\002, [lindex $urec 1] \002[lindex $urec 2]\002: [lindex $urec 3]" | |
167 set found 1 | |
168 break | |
169 } | |
170 } | |
171 close $ufile | |
172 if {$found == 0} { | |
173 ruoka_msg $upublic $unick $uchan "Ei tietoja ($udate, $urestaurant)" | |
174 } | |
175 } else { | |
176 ruoka_log "Could not open datafile: $uerrmsg" | |
177 } | |
178 } | |
179 | |
180 | |
181 | |
182 #------------------------------------------------------------------------- | |
183 proc ruoka_get_pub {unick uhost uhand uchan uargs} { | |
184 ruoka_get $unick $uhand $uchan $uargs 1 | |
185 return 0 | |
186 } | |
187 | |
188 #------------------------------------------------------------------------- | |
189 proc ruoka_get_msg {unick uhost uhand uargs} { | |
190 ruoka_get $unick $uhand "PRIV" $uargs 0 | |
191 return 0 | |
192 } | |
193 | |
194 #------------------------------------------------------------------------- | |
195 proc ruoka_set_pub {unick uhost uhand uchan uargs} { | |
196 array unset restaurants | |
197 ruoka_get_restaurants restaurants | |
198 set rav "" | |
199 if {$uargs != "{}" && $uargs != ""} { | |
200 set rav [ruoka_match_restaurant restaurants $uargs] | |
201 } | |
202 if {$rav != ""} { | |
203 ruoka_msg 1 $unick $uchan "Vakioravintolaksi asetettu: $rav" | |
204 setuser $uhand XTRA "restaurant" "$rav" | |
205 } else { | |
206 set tmp [join [array names restaurants] ", "] | |
207 ruoka_msg 1 $unick $uchan "Ravintolan oltava yksi seuraavista: $tmp" | |
208 } | |
209 return 0 | |
210 } | |
211 | |
212 # end of script |