/[cvs]/eggdrop1.6/scripts/autobotchk
ViewVC logotype

Contents of /eggdrop1.6/scripts/autobotchk

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.12 - (show annotations) (download)
Wed Jun 18 10:12:22 2008 UTC (11 years, 3 months ago) by tothwolf
Branch: MAIN
CVS Tags: HEAD
Changes since 1.11: +10 -3 lines
Updated exec magic tricks in the autobotchk and weed scripts.

Check for a working 'grep -E' before resorting to 'egrep'.

Use "$0" ${1+"$@"} in place of "$0" "$@" to avoid problems with broken sh implementations that expand "$@" into a single empty argument when there are no other arguments.

1 #! /bin/sh
2 # This trick is borrowed from Tothwolf's Wolfpack \
3 # Check for working 'grep -E' before using 'egrep' \
4 if echo a | (grep -E '(a|b)') >/dev/null 2>&1; \
5 then \
6 egrep="grep -E"; \
7 else \
8 egrep=egrep; \
9 fi; \
10 # Search for tclsh[0-9].[0-9] in each valid dir in PATH \
11 for dir in $(echo $PATH | sed 's/:/ /g'); \
12 do \
13 if test -d $dir; \
14 then \
15 files=$(/bin/ls $dir | $egrep '^tclsh[0-9]\.[0-9]$'); \
16 if test "$files" != ""; \
17 then \
18 versions="${versions:+$versions }$(echo $files | sed 's/tclsh//g')"; \
19 fi; \
20 fi; \
21 done; \
22 for ver in $versions; \
23 do \
24 tmpver=$(echo $ver | sed 's/\.//g'); \
25 if test "$lasttmpver" != ""; \
26 then \
27 if test "$tmpver" -gt "$lasttmpver"; \
28 then \
29 lastver=$ver; \
30 lasttmpver=$tmpver; \
31 fi; \
32 else \
33 lastver=$ver; \
34 lasttmpver=$tmpver; \
35 fi; \
36 done; \
37 exec tclsh$lastver "$0" ${1+"$@"}
38 #
39 # AutoBotchk - An eggdrop utility to autogenerate botchk/crontab entries
40 #
41 # Copyright (C) 1999, 2000, 2001, 2002 Jeff Fisher (guppy@eggheads.org)
42 #
43 # How to use
44 # ----------
45 #
46 # Most people begin to use AutoBotchk by moving it from the script
47 # directory to their Eggdrop directory -- this will save you from having to
48 # use the -dir option.
49 #
50 # If you run AutoBotchk without any arguments, it will present you with
51 # a list of valid ones. Most people run AutoBotchk by doing:
52 #
53 # ./autobotchk <config file>
54 #
55 # This will setup crontab to check every 10 minutes to see whether or not
56 # your bot needs to be restarted and it will e-mail if a restart was
57 # performed. A lot of people turn off crontab e-mail support; however, I do
58 # not recommend this since you will be unable to see any errors that might
59 # happen.
60 #
61 # Updates
62 # -------
63 # 27Sep2001: added new pidfile setting
64 # 14Nov2001: removed old autobotchk update entries and updated the help
65 # section a little bit. also made autobotchk move down one
66 # directory if being run from the scripts directory.
67 # 15Apr2003: cleaned up a few things, fixed a few bugs, and made a little
68 # love! j/k
69 #
70 # $Id: autobotchk,v 1.11 2003-04-15 17:41:57 guppy Exp $
71 #
72
73 if {$argc == 0} {
74 puts "\nusage: $argv0 <eggdrop config> \[options\]"
75 puts " -dir (directory to run autobotchk in)"
76 puts " -noemail (discard crontab e-mails)"
77 puts " -5 (5 minute checks)"
78 puts " -10 (10 minute checks)"
79 puts " -15 (15 minute checks)"
80 puts " -30 (30 minute checks)"
81 puts ""
82 exit
83 }
84
85 fconfigure stdout -buffering none
86
87 proc newsplit {text {split " "}} {
88 upvar $text ours
89 append ours $split
90 set index [string first $split $ours]
91 if {$index == -1} {
92 set ours ""
93 return ""
94 }
95 set tmp [string trim [string range $ours 0 $index]]
96 set ours [string trim [string range $ours [expr $index + [string length $split]] end]]
97 return $tmp
98 }
99
100 puts "\nautobotchk 1.10, (C) 2003 Jeff Fisher (guppy@eggheads.org)"
101 puts "------------------------------------------------------------\n"
102
103 set config [newsplit argv]
104 set dir [pwd]
105 set delay 10
106 set email 1
107
108 # If you renamed your eggdrop binary, you should change this variable
109 set binary "eggdrop"
110
111 while {[set opt [newsplit argv]] != ""} {
112 switch -- $opt {
113 "-time" -
114 "-1" { set delay 1 }
115 "-5" { set delay 5 }
116 "-10" { set delay 10 }
117 "-15" { set delay 15 }
118 "-20" { set delay 20 }
119 "-30" { set delay 30 }
120 "-nomail" -
121 "-noemail" {set email 0}
122 "-dir" {
123 set dir [newsplit argv]
124 if {[string match -* $dir]} {
125 puts "*** ERROR: you did not supply a directory name"
126 puts ""
127 exit
128 }
129 if {![file isdirectory $dir]} {
130 puts "*** ERROR: the directory you supplied is not a directory"
131 puts ""
132 exit
133 }
134 }
135 }
136 }
137
138 switch -- $delay {
139 "30" { set minutes "0,30" }
140 "20" { set minutes "0,20,40" }
141 "15" { set minutes "0,15,30,45" }
142 "5" { set minutes "0,5,10,15,20,25,30,35,40,45,50,55" }
143 "1" { set minutes "*" }
144 default { set minutes "0,10,20,30,40,50" }
145 }
146
147 if {[string match "*/scripts" $dir]} {
148 set dir [string range $dir 0 [expr [string length $dir] - 8]]
149 }
150
151 set dir [string trimright $dir /]
152
153 if {![file exists $dir/help] || ![file isdirectory $dir/help]} {
154 puts "*** ERROR: are you sure you are running from a bot directory?"
155 puts ""
156 exit
157 } elseif {![file exists $dir/$binary]} {
158 puts "*** ERROR: are you sure you are running from a bot directory?"
159 puts ""
160 exit
161 }
162
163 puts -nonewline "Opening '$config' for processing ... "
164
165 if {[catch {open $dir/$config r} fd]} {
166 puts "error:"
167 puts " $fd\n"
168 exit
169 } else {
170 puts "done"
171 }
172
173 set count 0
174 puts -nonewline "Scanning the config file "
175
176 while {![eof $fd]} {
177 incr count
178 if {$count == 100} {
179 puts -nonewline "."
180 set count 0
181 }
182 set line [gets $fd]
183 if {[set blarg [newsplit line]] != "set"} {
184 continue
185 }
186 switch -- [set opt [newsplit line]] {
187 "pidfile" -
188 "nick" -
189 "userfile" -
190 "botnet-nick" {
191 set $opt [string trim [newsplit line] " \""]
192 }
193 }
194 }
195 close $fd
196
197 if {$count != 0} {
198 puts -nonewline "."
199 }
200
201 puts " done"
202
203 if {![info exists {botnet-nick}] && [info exists nick]} {
204 puts " Defaulting \$botnet-nick to \"$nick\""
205 set botnet-nick $nick
206 }
207 if {![info exists pidfile]} {
208 puts " Defaulting \$pidfile to \"pid.${botnet-nick}\""
209 set pidfile "pid.${botnet-nick}"
210 }
211 if {![info exists {botnet-nick}] || ![info exists userfile]} {
212 puts " *** ERROR: could not find either \$userfile or \$botnet-nick"
213 puts ""
214 puts " Are you sure this is a valid eggdrop config file?"
215 puts ""
216 exit
217 }
218 if {[catch {open $dir/${botnet-nick}.botchk w} fd]} {
219 puts " *** ERROR: unable to open '${botnet-nick}.botchk' for writing"
220 puts ""
221 exit
222 }
223 puts $fd "#! /bin/sh
224 #
225 # ${botnet-nick}.botchk (generated on [clock format [clock seconds] -format "%B %d, %Y @ %I:%M%p"])
226 #
227 # Generated by AutoBotchk 1.10
228 # Copyright (C) 1999, 2000, 2001, 2002, 2003 Jeff Fisher <guppy@eggheads.org>
229 #
230
231 # change this to the directory you run your bot from:
232 botdir=\"$dir\"
233
234 # change this to the name of your bot's script in that directory:
235 botscript=\"$binary $config\"
236
237 # change this to the nickname of your bot (capitalization COUNTS)
238 botname=\"${botnet-nick}\"
239
240 # change this to the name of your bot's userfile (capitalization COUNTS)
241 userfile=\"$userfile\"
242
243 # change this to the name of your bot's pidfile (capitalization COUNTS)
244 pidfile=\"$pidfile\"
245
246 ########## you probably don't need to change anything below here ##########
247
248 cd \$botdir
249
250 # is there a pid file?
251 if test -r \$pidfile
252 then
253 # there is a pid file -- is it current?
254 botpid=`cat \$pidfile`
255 if `kill -CHLD \$botpid >/dev/null 2>&1`
256 then
257 # it's still going -- back out quietly
258 exit 0
259 fi
260 echo \"\"
261 echo \"Stale \$pidfile file, erasing...\"
262 rm -f \$pidfile
263 fi
264
265 if test -r CANTSTART.\$botname
266 then
267 if test -r \$userfile || test -r \$userfile~new || test -r \$userfile~bak
268 then
269 echo \"\"
270 echo \"Userfile found, removing check file 'CANTSTART.\$botname'...\"
271 rm -f CANTSTART.\$botname
272 fi
273 fi
274
275 # test if we have run botchk previously and didn't find a userfile
276 if test ! -f CANTSTART.\$botname
277 then
278 echo \"\"
279 echo \"Couldn't find bot '\$botname' running, reloading...\"
280 echo \"\"
281 # check for userfile and reload bot if found
282 if test -r \$userfile
283 then
284 # It's there, load the bot
285 ./\$botscript
286 exit 0
287 else
288 if test -r \$userfile~new
289 then
290 # Bot f*@!ed up while saving the userfile last time. Move it over.
291 echo \"Userfile missing. Using last saved userfile...\"
292 mv -f \$userfile~new \$userfile
293 ./\$botscript
294 exit 0
295 else
296 if test -r \$userfile~bak
297 then
298 # Userfile is missing, use backup userfile.
299 echo \"Userfile missing. Using backup userfile...\"
300 cp -f \$userfile~bak \$userfile
301 ./\$botscript
302 exit 0
303 else
304 # Well, nothing to work with...
305 echo \"No userfile. Could not reload the bot...\"
306 echo \"no userfile\" > CANTSTART.\$botname
307 exit 1
308 fi
309 fi
310 fi
311 fi
312
313 exit 0
314 "
315 close $fd
316 puts "Wrote '${botnet-nick}.botchk' successfully ([file size $dir/${botnet-nick}.botchk] bytes)"
317 if {[catch {exec chmod u+x $dir/${botnet-nick}.botchk} 0]} {
318 puts " *** ERROR: unable to 'chmod u+x' the output file"
319 puts ""
320 exit
321 }
322 puts -nonewline "Scanning crontab entries ... "
323
324 set tmp ".autobotchk[clock clicks].[pid]"
325 if {$email} {
326 set line "$minutes \* \* \* \* $dir/${botnet-nick}.botchk"
327 } {
328 set line "$minutes \* \* \* \* $dir/${botnet-nick}.botchk >\/dev\/null 2>&1"
329 }
330
331 if {[catch {exec crontab -l > $tmp} error ]} {
332 if {![string match "*no*cron*" [string tolower $error]] &&
333 ![string match "*can't open*" [string tolower $error]]} {
334 catch {file delete -force $tmp} 0
335 puts "error: unable to get crontab listing"
336 puts ""
337 puts $error
338 puts ""
339 exit
340 }
341 }
342
343 set fd [open $tmp r]
344 while {![eof $fd]} {
345 set z [gets $fd]
346 if {[string match "*$dir/${botnet-nick}.botchk*" $z] ||
347 [string match "*$dir//${botnet-nick}.botchk*" $z]} {
348 puts "found an existing entry, we're done now"
349 puts ""
350 exit
351 }
352 }
353
354 close $fd
355
356 puts "done"
357
358 puts -nonewline "Adding the new crontab entry ... "
359 set fd [open $tmp a]
360 puts $fd $line
361 close $fd
362
363 if {[catch {exec crontab $tmp} error]} {
364 puts "error: unable to do 'crontab $tmp'"
365 puts ""
366 puts $error
367 puts ""
368 exit
369 } else {
370 catch {file delete -force $tmp} 0
371 }
372
373 puts "done"
374 puts ""
375 puts "Use 'crontab -l' to view all your current crontab entries"
376 puts " 'crontab -r' to remove all your crontab entries"
377 puts ""

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23