X-Git-Url: https://git.sommitrealweird.co.uk/bashbot.git/blobdiff_plain/d0ea976b674611afc494da40c2d5d866461df296..HEAD:/bashbot.sh diff --git a/bashbot.sh b/bashbot.sh old mode 100644 new mode 100755 index f692ddb..5023bdf --- a/bashbot.sh +++ b/bashbot.sh @@ -18,7 +18,7 @@ keeprunning=1 declare -A reminders cleanup() { - echo "QUIT :Going back to hell" >&4 + send_line "QUIT :Going back to hell" keeprunning=0 sleep 0.2 if [[ $irc_process -gt 0 ]]; then @@ -88,6 +88,10 @@ EOF # last character of the last element is going to be a \r so remove it parts[-1]=${parts[-1]%$'\r'} + if [[ ${#parts[@]} -lt 4 ]]; then + return + fi + if [[ "${parts[1]}" == "PRIVMSG" ]]; then sender=${parts[0]} channel=${parts[2]} @@ -96,6 +100,8 @@ EOF $my_irc_channel) if [[ ${parts[3]} =~ [:]*$my_irc_nick[:]*$ ]]; then do_command=1 + else + return fi bot_command=${parts[4]} options="${parts[@]:5}" @@ -123,11 +129,14 @@ EOF add) add_reminder $channel "$options" ;; + del) + del_reminder $channel "$options" + ;; list) list_reminders $channel ;; *) - send_line "PRIVMSG $channel :Sorry, don't know how to $bot_command." + send_action $channel "is sorry, they don't know how to $bot_command." ;; esac fi @@ -139,21 +148,38 @@ send_line() { sleep 0.1 } +send_privmsg() { + channel="$1" + content="$2" + + send_line "PRIVMSG $channel :$content" +} + +send_action() { + channel="$1" + content="$2" + action_wrapper=$'\001' + + content="${action_wrapper}ACTION ${content}${action_wrapper}" + send_privmsg "$channel" "$content" +} + send_help_message() { channel=$1 echo "Sending help!" - send_line "PRIVMSG $channel :Help:" - send_line "PRIVMSG $channel : An IRC bot written in bash!" - send_line "PRIVMSG $channel : help - display this message" - send_line "PRIVMSG $channel : version - display version number" - send_line "PRIVMSG $channel : quit - make the bot quit IRC" - send_line "PRIVMSG $channel : add YYYY-mm-dd HH:MM reminder text" - send_line "PRIVMSG $channel : list - list reminders" + send_privmsg $channel "Help:" + send_privmsg $channel " An IRC bot written in bash!" + send_privmsg $channel " help - display this message" + send_privmsg $channel " version - display version number" + send_privmsg $channel " quit - make the bot quit IRC" + send_privmsg $channel " add YYYY-mm-dd HH:MM reminder text" + send_privmsg $channel " del md5sum" + send_privmsg $channel " list - list reminders" } send_version() { channel=$1 - send_line "PRIVMSG $channel :Version: $version" + send_privmsg $channel "Version: $version" } add_reminder() { @@ -166,31 +192,61 @@ EOF # YYYY-mm-dd HH:MM the text of the reminder timestamp=$(date +"%s" --date="${dataparts[0]} ${dataparts[1]}") if [[ $? -ne 0 ]]; then - send_line "PRIVMSG $channel :Couldn't parse date/time ${dataparts[0]} ${dataparts[1]}" + send_privmsg $channel "Couldn't parse date/time ${dataparts[0]} ${dataparts[1]}" else - send_line "PRIVMSG $channel :Added reminder for ${dataparts[0]} ${dataparts[1]}: ${dataparts[@]:2}" - reminders[$timestamp]="${dataparts[@]:2}" + text="${dataparts[@]:2}" + md5sum=$(echo -n "$timestamp $text" | md5sum | sed -e 's#[ ][ ]*-##;') + send_privmsg $channel "Added reminder for [$md5sum] ${dataparts[0]} ${dataparts[1]}: $text" + reminders[$timestamp-$md5sum]="$text" fi } check_reminders() { current_timestamp="$(date +"%s")" # loop through the reminders keys to see if it's past and alert if so - for ts in ${!reminders[@]}; do + for tsmd5 in ${!reminders[@]}; do + ts=${tsmd5%-*} if [[ $ts -le $current_timestamp ]]; then - send_line "PRIVMSG $my_irc_channel :${reminders[$ts]}" + send_privmsg $my_irc_channel "${reminders[$tsmd5]}" # we need to remove this from the array now - unset "reminders[$ts]" + unset "reminders[$tsmd5]" fi done } list_reminders() { channel=$1 - for ts in ${!reminders[@]}; do - date_stamp="$(date --date="@$ts" +"%Y-%m-%d %H:%M")" - send_line "PRIVMSG $channel :$date_stamp ${reminders[$ts]}" + count=0 + for tsmd5 in ${!reminders[@]}; do + ts=${tsmd5%-*} + md5sum=${tsmd5#*-} + date_stamp="$(date --date="@$ts" +"%Y-%m-%d %H:%M %Z")" + send_privmsg $channel "[$md5sum] $date_stamp ${reminders[$tsmd5]}" + count=$((count+1)) done + + if [[ $count -eq 0 ]]; then + send_privmsg $channel "There are currently no reminders set, use add to add one." + fi +} + +del_reminder() { + channel="$1" + md5todelete="$2" + found=0 + for tsmd5 in ${!reminders[@]}; do + md5sum=${tsmd5#*-} + if [[ "$md5sum" == "$md5todelete" ]]; then + ts=${tsmd5%-*} + date_stamp="$(date --date="@$ts" +"%Y-%m-%d %H:%M %Z")" + send_privmsg $channel "Removed reminder [$md5sum] $date_stamp ${reminders[$tsmd5]}" + unset "reminders[$tsmd5]" + found=1 + fi + done + if [[ $found -eq 0 ]]; then + send_privmsg $channel "Couldn't find reminder $md5todelete" + fi } connect_to_server