1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
|
#! /bin/sh
#
# Copyright (C) 1999-2003 Jeff Fisher (guppy@eggheads.org)
# Copyright (C) 2004-2025 Eggheads Development Team
#
# systemd formatting contributed by PeGaSuS
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# This trick is borrowed from Tothwolf's Wolfpack \
# Check for working 'grep -E' before using 'egrep' \
if echo a | (grep -E '(a|b)') >/dev/null 2>&1; \
then \
egrep="grep -E"; \
else \
egrep=egrep; \
fi; \
# Search for tclsh[0-9].[0-9] in each valid dir in PATH \
for dir in $(echo $PATH | sed 's/:/ /g'); \
do \
if test -d $dir; \
then \
files=$(/bin/ls $dir | $egrep '^tclsh[0-9]\.[0-9]$'); \
if test "$files" != ""; \
then \
versions="${versions:+$versions }$(echo $files | sed 's/tclsh//g')"; \
fi; \
fi; \
done; \
for ver in $versions; \
do \
tmpver=$(echo $ver | sed 's/\.//g'); \
if test "$lasttmpver" != ""; \
then \
if test "$tmpver" -gt "$lasttmpver"; \
then \
lastver=$ver; \
lasttmpver=$tmpver; \
fi; \
else \
lastver=$ver; \
lasttmpver=$tmpver; \
fi; \
done; \
exec tclsh$lastver "$0" ${1+"$@"}
#
# AutoBotchk - An eggdrop utility to autogenerate botchk/crontab entries
#
# Copyright (C) 1999-2003 Jeff Fisher (guppy@eggheads.org)
# Copyright (C) 2004-2025 Eggheads Development Team
#
# How to use
# ----------
#
# Most people begin to use AutoBotchk by moving it from the script
# directory to their Eggdrop directory -- this will save you from having to
# use the -dir option.
#
# If you run AutoBotchk without any arguments, it will present you with
# a list of valid ones. Most people run AutoBotchk by doing:
#
# ./autobotchk <config file>
#
# This will setup crontab to check every 10 minutes to see whether or not
# your bot needs to be restarted and it will e-mail if a restart was
# performed. A lot of people turn off crontab e-mail support; however, I do
# not recommend this since you will be unable to see any errors that might
# happen.
#
# Updates
# -------
# 27Sep2001: added new pidfile setting
# 14Nov2001: removed old autobotchk update entries and updated the help
# section a little bit. also made autobotchk move down one
# directory if being run from the scripts directory.
# 15Apr2003: cleaned up a few things, fixed a few bugs, and made a little
# love! j/k
# 13Dec2016: add possibility to use multiple confs; fixes for (botnet-)nick
# with len > handlen, for (botnet-)nicks with \[ \] and for
# $(botnet-)nick used in pidfile, userfile or (botnet-)nick
# 19Apr2017: Fix running this from a non-eggdrop dir.
# 28Nov2022: Added -systemd option, to (duh) create a systemd job
if {$argc == 0} {
puts "\nusage: $argv0 <eggdrop mainconfig> \[options\] \[additional configs\]"
puts "Options:"
puts "--------"
puts " systemd options:"
puts " -systemd (install systemd job instead of cron)"
puts ""
puts " crontab options:"
puts " -dir (directory to run autobotchk in)"
puts " -noemail (discard crontab e-mails)"
puts " -5 (5 minute checks)"
puts " -10 (10 minute checks)"
puts " -15 (15 minute checks)"
puts " -30 (30 minute checks)"
puts ""
puts "Additional Configs:"
puts "-------------------"
puts " If you source additional configs from your main config and they"
puts " contain either nick, botnet-nick, userfile or pidfile settings,"
puts " list them here so that autobotchk can check in them as well."
puts ""
exit
}
fconfigure stdout -buffering none
proc newsplit {text {split " "}} {
upvar $text ours
append ours $split
set index [string first $split $ours]
if {$index == -1} {
set ours ""
return ""
}
set tmp [string trim [string range $ours 0 $index]]
set ours [string trim [string range $ours [expr $index + [string length $split]] end]]
return $tmp
}
puts "\nautobotchk 1.11, (C) 1999-2003 Jeff Fisher (guppy@eggheads.org)"
puts " (C) 2004-2022 Eggheads Development Team"
puts "------------------------------------------------------------\n"
set mainconf [newsplit argv]
set confs [list $mainconf]
set dir [pwd]
set delay 10
set email 1
set systemd 0
catch {exec which kill} kill
# If you renamed your eggdrop binary, you should change this variable
set binary "eggdrop"
while {[set opt [newsplit argv]] != ""} {
switch -- $opt {
"-systemd" { set systemd 1 }
"-time" -
"-1" { set delay 1 }
"-5" { set delay 5 }
"-10" { set delay 10 }
"-15" { set delay 15 }
"-20" { set delay 20 }
"-30" { set delay 30 }
"-nomail" -
"-noemail" {set email 0}
"-dir" {
set dir [newsplit argv]
if {[string match -* $dir]} {
puts "*** ERROR: you did not supply a directory name"
puts ""
exit
}
if {![file isdirectory $dir]} {
puts "*** ERROR: the directory you supplied is not a directory"
puts ""
exit
}
}
default {
# Treat as extra config file
if {[file isfile $opt] && [file readable $opt]} { lappend confs $opt }
}
}
}
switch -- $delay {
"30" { set minutes "0,30" }
"20" { set minutes "0,20,40" }
"15" { set minutes "0,15,30,45" }
"5" { set minutes "0,5,10,15,20,25,30,35,40,45,50,55" }
"1" { set minutes "*" }
default { set minutes "0,10,20,30,40,50" }
}
if {[string match "*/scripts" $dir]} {
set dir [string range $dir 0 [expr [string length $dir] - 8]]
}
set dir [string trimright $dir /]
if {![file exists $dir/help] || ![file isdirectory $dir/help]} {
puts "*** ERROR: are you sure you are running from a bot directory?"
puts ""
exit
} elseif {![file exists $dir/$binary]} {
puts "*** ERROR: are you sure you are running from a bot directory?"
puts ""
exit
}
foreach config $confs {
puts -nonewline "Opening '$config' for processing ... "
if {[catch {open $dir/$config r} fd]} {
puts "error:"
puts " $fd\n"
exit
} else {
puts "done"
}
set count 0
puts -nonewline "Scanning the config file "
while {![eof $fd]} {
incr count
if {$count == 100} {
puts -nonewline "."
set count 0
}
set line [gets $fd]
if {[set blarg [newsplit line]] != "set"} {
continue
}
switch -- [set opt [newsplit line]] {
"pidfile" -
"nick" -
"userfile" -
"botnet-nick" {
set $opt [string trim [newsplit line] " \"{}"]
}
}
}
close $fd
if {$count != 0} {
puts -nonewline "."
}
puts " done"
}
if {![info exists {botnet-nick}] && [info exists nick]} {
puts " Defaulting \$botnet-nick to \"$nick\""
set botnet-nick $nick
}
if {![info exists {botnet-nick}] || ![info exists userfile]} {
puts " *** ERROR: could not find either \$userfile or \$botnet-nick"
puts ""
puts " Are you sure this is a valid eggdrop config file?"
puts ""
exit
}
catch {exec "$dir/$binary" -v} execres
if {![regexp {handlen=([0-9]+)} "$execres" -> handlen]} {
puts " Could not find handlen used, defaulting to 32."
# len 32 means up to index 31
set handlen 31
} else {
set handlen [expr $handlen - 1]
}
set nick [string range [subst -nocommands $nick] 0 $handlen]
set botnet-nick [string range [subst -nocommands ${botnet-nick}] 0 $handlen]
### systemd stuff
if {$systemd} {
puts "Enabling user lingering..."
if {[catch {exec loginctl enable-linger} res]} {
puts "Oops, something went wrong. Is systemd running on this system?"
exit
}
puts "Creating systemd directory..."
catch {file mkdir "$::env(HOME)/.config/systemd/user" } res
if {[catch {open "$::env(HOME)/.config/systemd/user/${botnet-nick}.service" w} fd]} {
puts " *** ERROR: unable to open '${botnet-nick}.service' for writing"
puts ""
exit
}
puts $fd "### Eggdrop systemd unit, generated by autosystemd"
puts $fd ""
puts $fd "\[Unit\]"
puts $fd "Description=${botnet-nick} (Eggdrop)"
puts $fd "After=default.target"
puts $fd ""
puts $fd "\[Service\]"
puts $fd "WorkingDirectory=${dir}"
puts $fd "ExecStart=${dir}/eggdrop ${config}"
puts $fd "ExecReload=${kill} -s HUP \$MAINPID"
puts $fd "Type=forking"
puts $fd "Restart=on-abnormal"
puts $fd ""
puts $fd "\[Install\]"
puts $fd "WantedBy=default.target"
close $fd
catch {exec systemctl --user enable ${botnet-nick}.service} res
puts $res
puts "systemd job successfully installed as '${botnet-nick}.service'."
puts "* Use 'systemctl --user <start|stop|restart|reload|enable|disable> ${botnet-nick}.service' to control your Eggdrop"
puts "Starting Eggdrop..."
if {[catch {exec systemctl --user start ${botnet-nick}.service} res]} {
puts "ERROR: Eggdrop did not start."
puts $res
} else {
puts "Success."
}
exit
}
if {![info exists pidfile]} {
puts " Defaulting \$pidfile to \"pid.${botnet-nick}\""
set pidfile "pid.${botnet-nick}"
}
set pidfile [subst -nocommands $pidfile]
if {[catch {open $dir/${botnet-nick}.botchk w} fd]} {
puts " *** ERROR: unable to open '${botnet-nick}.botchk' for writing"
puts ""
exit
}
puts $fd "#! /bin/sh
#
# ${botnet-nick}.botchk (generated on [clock format [clock seconds] -format "%B %d, %Y @ %I:%M%p"])
#
# Generated by AutoBotchk 1.11
# Copyright (C) 1999-2003 Jeff Fisher (guppy@eggheads.org)
# Copyright (C) 2004-2025 Eggheads Development Team
#
# change this to the directory you run your bot from:
botdir=\"$dir\"
# change this to the name of your bot's script in that directory:
botscript=\"$binary $mainconf\"
# change this to the nickname of your bot (capitalization COUNTS)
botname=\"${botnet-nick}\"
# change this to the name of your bot's userfile (capitalization COUNTS)
userfile=\"$userfile\"
# change this to the name of your bot's pidfile (capitalization COUNTS)
pidfile=\"$pidfile\"
########## you probably don't need to change anything below here ##########
cd \$botdir
# is there a pid file?
if test -r \$pidfile
then
# there is a pid file -- is it current?
botpid=`cat \$pidfile`
if `kill -CHLD \$botpid >/dev/null 2>&1`
then
# it's still going -- back out quietly
exit 0
fi
echo \"\"
echo \"Stale \$pidfile file, erasing...\"
rm -f \$pidfile
fi
if test -r CANTSTART.\$botname
then
if test -r \$userfile || test -r \$userfile~new || test -r \$userfile~bak
then
echo \"\"
echo \"Userfile found, removing check file 'CANTSTART.\$botname'...\"
rm -f CANTSTART.\$botname
fi
fi
# test if we have run botchk previously and didn't find a userfile
if test ! -f CANTSTART.\$botname
then
echo \"\"
echo \"Couldn't find bot '\$botname' running, reloading...\"
echo \"\"
# check for userfile and reload bot if found
if test -r \$userfile
then
# It's there, load the bot
./\$botscript
exit 0
else
if test -r \$userfile~new
then
# Bot f*@!ed up while saving the userfile last time. Move it over.
echo \"Userfile missing. Using last saved userfile...\"
mv -f \$userfile~new \$userfile
./\$botscript
exit 0
else
if test -r \$userfile~bak
then
# Userfile is missing, use backup userfile.
echo \"Userfile missing. Using backup userfile...\"
cp -f \$userfile~bak \$userfile
./\$botscript
exit 0
else
# Well, nothing to work with...
echo \"No userfile. Could not reload the bot...\"
echo \"no userfile\" > CANTSTART.\$botname
exit 1
fi
fi
fi
fi
exit 0
"
close $fd
puts "Wrote '${botnet-nick}.botchk' successfully ([file size $dir/${botnet-nick}.botchk] bytes)"
if {[catch {exec chmod u+x $dir/${botnet-nick}.botchk} 0]} {
puts " *** ERROR: unable to 'chmod u+x' the output file"
puts ""
exit
}
puts -nonewline "Scanning crontab entries ... "
set tmp ".autobotchk[clock clicks].[pid]"
set cronbotchk [string map {\\ \\\\ \[ \\\[ \] \\\]} "${botnet-nick}.botchk"]
if {$email} {
set line "$minutes \* \* \* \* $dir/${cronbotchk}"
} {
set line "$minutes \* \* \* \* $dir/${cronbotchk} >\/dev\/null 2>&1"
}
if {[catch {exec crontab -l > $tmp} error ]} {
if {![string match "*no*cron*" [string tolower $error]] &&
![string match "*can't open*" [string tolower $error]]} {
catch {file delete -force $tmp} 0
puts "error: unable to get crontab listing"
puts ""
puts $error
puts ""
exit
}
}
set cronbotchk [string map {\\ \\\\ \[ \\\[ \] \\\]} "${cronbotchk}"]
set fd [open $tmp r]
while {![eof $fd]} {
set z [gets $fd]
if {[string match "*$dir/${cronbotchk}*" $z] ||
[string match "*$dir//${cronbotchk}*" $z]} {
puts "found an existing entry, we're done now"
puts ""
exit
}
}
close $fd
puts "done"
puts -nonewline "Adding the new crontab entry ... "
set fd [open $tmp a]
puts $fd $line
close $fd
if {[catch {exec crontab $tmp} error]} {
puts "error: unable to do 'crontab $tmp'"
puts ""
puts $error
puts ""
exit
} else {
catch {file delete -force $tmp} 0
}
puts "done"
puts ""
puts "Use 'crontab -l' to view all your current crontab entries"
puts " 'crontab -r' to remove all your crontab entries"
puts ""
|