From fb00cc975640ff180255a5d4fb832709a7aea9c9 Mon Sep 17 00:00:00 2001 From: Brett Parker Date: Wed, 18 Sep 2019 08:25:21 +0100 Subject: [PATCH 1/1] Improve reminders indexing - Add md5sum as a key - Add a delete function - Update help --- bashbot.sh | 55 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index f3c5676..5023bdf 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -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,6 +129,9 @@ EOF add) add_reminder $channel "$options" ;; + del) + del_reminder $channel "$options" + ;; list) list_reminders $channel ;; @@ -164,6 +173,7 @@ send_help_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" } @@ -185,29 +195,58 @@ EOF send_privmsg $channel "Couldn't parse date/time ${dataparts[0]} ${dataparts[1]}" else text="${dataparts[@]:2}" - send_privmsg $channel "Added reminder for ${dataparts[0]} ${dataparts[1]}: $text" - reminders[$timestamp]="$text" + 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_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_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 -- 2.39.5