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

Contents of /eggdrop1.9/scripts/autobotchk

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


Revision 1.9 - (show annotations) (download)
Wed Dec 17 00:58:02 2003 UTC (15 years, 4 months ago) by wcc
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +0 -0 lines
* More cleanups.
* Updated alltools.
* botchk works now.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23