#!/bin/sh # TODO: make sure only nagios can write to $inputf and $msgf, else do not run conf=/etc/ircbot.conf if ! test -r $conf; then echo "cannot read configuration file, exiting" exit 1 else # shellcheck source=ircbot.conf.default . $conf fi touch $msgf connect() { echo "NICK $nick" > $inputf echo "USER $nick 8 * : $nick" >> $inputf echo "PRIVMSG NickServ : identify $password" >> $inputf echo "JOIN $chan" >> $inputf echo "PRIVMSG $chan : $joinmsg" >> $inputf } reconnect() { if command -v systemctl >/dev/null; then # NOTE: systemd is configured to restart the service, so simply dying # with a non-zero exit code is enough to effectively reconnect. exit 2 else # FIXME: We need to make sure we are only running one instance of the # script at a time. This exec is therefore problematic. exec $0 fi } delay() { while read line do sleep 1.5 echo $line done } register() { echo "PRIVMSG NickServ : register $password $email" >> $inputf } loop() { tail -f $msgf | delay | while read line do echo "PRIVMSG #ops : $line" >> $inputf done } connect loop & tail -f $inputf | delay | nc $server $port | while read msg do echo "$msg" | tee -a $logf case "$msg" in *'PING'*) echo "$msg" | sed 's/PING/PONG/' >> $inputf ;; *'is not a registered nickname.'*) register ;; *'You have not registered'*) register ;; *'You have not joined'*) echo "JOIN $chan" >> $inputf ;; *'Cannot join'*) sleep 10; echo "JOIN $chan" >> $inputf ;; *'ERROR :Closing link:'*) reconnect ;; esac done