Improve reminders indexing
[bashbot.git] / bashbot.sh
old mode 100644 (file)
new mode 100755 (executable)
index a47be46..5023bdf
@@ -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"
 }
 
@@ -184,29 +194,59 @@ EOF
     if [[ $? -ne 0 ]]; then
         send_privmsg $channel "Couldn't parse date/time ${dataparts[0]} ${dataparts[1]}"
     else
-        send_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_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