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[@]} -lt 4 ]]; then
95 if [[ "${parts[1]}" == "PRIVMSG" ]]; then
101 if [[ ${parts[3]} =~ [:]*$my_irc_nick[:]*$ ]]; then
106 bot_command=${parts[4]}
107 options="${parts[@]:5}"
110 bot_command=${parts[3]}
111 bot_command=${bot_command#:}
112 channel=${parts[0]%\!*}
114 options="${parts[@]:4}"
118 if [[ $do_command -gt 0 ]]; then
121 send_help_message $channel
127 send_version $channel
130 add_reminder $channel "$options"
133 del_reminder $channel "$options"
136 list_reminders $channel
139 send_action $channel "is sorry, they don't know how to $bot_command."
155 send_line "PRIVMSG $channel :$content"
161 action_wrapper=$'\001'
163 content="${action_wrapper}ACTION ${content}${action_wrapper}"
164 send_privmsg "$channel" "$content"
167 send_help_message() {
170 send_privmsg $channel "Help:"
171 send_privmsg $channel " An IRC bot written in bash!"
172 send_privmsg $channel " help - display this message"
173 send_privmsg $channel " version - display version number"
174 send_privmsg $channel " quit - make the bot quit IRC"
175 send_privmsg $channel " add YYYY-mm-dd HH:MM reminder text"
176 send_privmsg $channel " del md5sum"
177 send_privmsg $channel " list - list reminders"
182 send_privmsg $channel "Version: $version"
188 read -a dataparts <<-EOF
191 # we now have a list of parts, they should be of the form
192 # YYYY-mm-dd HH:MM the text of the reminder
193 timestamp=$(date +"%s" --date="${dataparts[0]} ${dataparts[1]}")
194 if [[ $? -ne 0 ]]; then
195 send_privmsg $channel "Couldn't parse date/time ${dataparts[0]} ${dataparts[1]}"
197 text="${dataparts[@]:2}"
198 md5sum=$(echo -n "$timestamp $text" | md5sum | sed -e 's#[ ][ ]*-##;')
199 send_privmsg $channel "Added reminder for [$md5sum] ${dataparts[0]} ${dataparts[1]}: $text"
200 reminders[$timestamp-$md5sum]="$text"
205 current_timestamp="$(date +"%s")"
206 # loop through the reminders keys to see if it's past and alert if so
207 for tsmd5 in ${!reminders[@]}; do
209 if [[ $ts -le $current_timestamp ]]; then
210 send_privmsg $my_irc_channel "${reminders[$tsmd5]}"
211 # we need to remove this from the array now
212 unset "reminders[$tsmd5]"
220 for tsmd5 in ${!reminders[@]}; do
223 date_stamp="$(date --date="@$ts" +"%Y-%m-%d %H:%M %Z")"
224 send_privmsg $channel "[$md5sum] $date_stamp ${reminders[$tsmd5]}"
228 if [[ $count -eq 0 ]]; then
229 send_privmsg $channel "There are currently no reminders set, use add to add one."
237 for tsmd5 in ${!reminders[@]}; do
239 if [[ "$md5sum" == "$md5todelete" ]]; then
241 date_stamp="$(date --date="@$ts" +"%Y-%m-%d %H:%M %Z")"
242 send_privmsg $channel "Removed reminder [$md5sum] $date_stamp ${reminders[$tsmd5]}"
243 unset "reminders[$tsmd5]"
247 if [[ $found -eq 0 ]]; then
248 send_privmsg $channel "Couldn't find reminder $md5todelete"
253 trap cleanup EXIT SIGINT
256 # see if there's anything to read
257 read -t 0.2 -u 3 aline
263 # check that the irc_process is still running, restart it if not
264 if ( ! kill -0 $irc_process ); then