8 rm -f .bashbotfifo{in,out}
10 mkfifo .bashbotfifoout
12 # Setup some fds to talk to IRC
13 exec 3<> .bashbotfifoin
14 exec 4<> .bashbotfifoout
21 send_line "QUIT :Going back to hell"
24 if [[ $irc_process -gt 0 ]]; then
25 if (ps -p $irc_process > /dev/null); then
29 rm .bashbotfifo{in,out}
34 my_hostname="$(hostname)"
35 my_irc_channel="#alug"
38 my_irc_server="irc.oftc.net"
41 # now if there's a settings file, source that...
42 if [ -e "$(dirname $(readlink -f $0))/.botsettings.sh" ]; then
43 . "$(dirname $(readlink -f $0))/.botsettings.sh"
47 gnutls-cli --crlf $my_irc_server:$my_irc_port <&4 >&3 2>&3 &
50 while [ $connected == 0 ]; do
52 if [[ "$line" =~ NOTICE\ ]]; then
54 send_line PASS $my_irc_password
55 send_line NICK $my_irc_nick
56 send_line USER $my_irc_nick $my_hostname $my_irc_server $my_irc_nick
57 send_line JOIN $my_irc_channel
63 # Format of server messages
64 #:thingsendingmessage command params
66 #:iDunno!~brettp@mail.ipv6.sommitrealweird.co.uk PRIVMSG #bp :hello.
72 privmsg_regex="^[^ ]* PRIVMSG"
73 if [[ "${line}" =~ $ping_regex ]]; then
76 elif [[ "${line}" =~ $privmsg_regex ]]; then
83 echo "Processing $line"
88 # last character of the last element is going to be a \r so remove it
89 parts[-1]=${parts[-1]%$'\r'}
91 if [[ "${parts[1]}" == "PRIVMSG" ]]; then
97 if [[ ${parts[3]} =~ [:]*$my_irc_nick[:]*$ ]]; then
100 bot_command=${parts[4]}
101 options="${parts[@]:5}"
104 bot_command=${parts[3]}
105 bot_command=${bot_command#:}
106 channel=${parts[0]%\!*}
108 options="${parts[@]:4}"
112 if [[ $do_command -gt 0 ]]; then
115 send_help_message $channel
121 send_version $channel
124 add_reminder $channel "$options"
127 list_reminders $channel
130 send_action $channel "is sorry, they don't know how to $bot_command."
146 send_line "PRIVMSG $channel :$content"
152 action_wrapper=$'\001'
154 content="${action_wrapper}ACTION ${content}${action_wrapper}"
155 send_privmsg "$channel" "$content"
158 send_help_message() {
161 send_privmsg $channel "Help:"
162 send_privmsg $channel " An IRC bot written in bash!"
163 send_privmsg $channel " help - display this message"
164 send_privmsg $channel " version - display version number"
165 send_privmsg $channel " quit - make the bot quit IRC"
166 send_privmsg $channel " add YYYY-mm-dd HH:MM reminder text"
167 send_privmsg $channel " list - list reminders"
172 send_privmsg $channel "Version: $version"
178 read -a dataparts <<-EOF
181 # we now have a list of parts, they should be of the form
182 # YYYY-mm-dd HH:MM the text of the reminder
183 timestamp=$(date +"%s" --date="${dataparts[0]} ${dataparts[1]}")
184 if [[ $? -ne 0 ]]; then
185 send_privmsg $channel "Couldn't parse date/time ${dataparts[0]} ${dataparts[1]}"
187 send_privmsg $channel "Added reminder for ${dataparts[0]} ${dataparts[1]}: ${dataparts[@]:2}"
188 reminders[$timestamp]="${dataparts[@]:2}"
193 current_timestamp="$(date +"%s")"
194 # loop through the reminders keys to see if it's past and alert if so
195 for ts in ${!reminders[@]}; do
196 if [[ $ts -le $current_timestamp ]]; then
197 send_privmsg $my_irc_channel "${reminders[$ts]}"
198 # we need to remove this from the array now
199 unset "reminders[$ts]"
206 for ts in ${!reminders[@]}; do
207 date_stamp="$(date --date="@$ts" +"%Y-%m-%d %H:%M")"
208 send_privmsg $channel "$date_stamp ${reminders[$ts]}"
213 trap cleanup EXIT SIGINT
216 # see if there's anything to read
217 read -t 0.2 -u 3 aline
223 # check that the irc_process is still running, restart it if not
224 if ( ! kill -0 $irc_process ); then