]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/remind.rb
chucknorris: typo
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / remind.rb
index 5ad980aeac8d33012da673e0c0594796d8100f32..c10186f5861fd32c67a0604872f369678bde5f0f 100644 (file)
@@ -1,6 +1,17 @@
-require 'rbot/utils'
-
 class RemindPlugin < Plugin
+  # read a time in string format, turn it into "seconds from now".
+  # example formats handled are "5 minutes", "2 days", "five hours",
+  # "11:30", "15:45:11", "one day", etc.
+  #
+  # Throws:: RunTimeError "invalid time string" on parse failure
+  def timestr_offset(timestr)
+    Utils.parse_time_offset(timestr)
+  end
+
+  class UnparsedPeriodError < RuntimeError ; end
+  class NegativePeriodError < RuntimeError ; end
+  class ShortRepeatError < RuntimeError ; end
+
   def initialize
     super
     @reminders = Hash.new
@@ -12,25 +23,25 @@ class RemindPlugin < Plugin
       }
     }
     @reminders.clear
+    super
   end
   def help(plugin, topic="")
-    if(plugin =~ /^remind\+$/)
-      "see remind. remind+ can be used to remind someone else of something, using <nick> instead of 'me'. However this will generally require a higher auth level than remind."
-    else
-      "remind me [about] <message> in <time>, remind me [about] <message> every <time>, remind me [about] <message> at <time>, remind me no more [about] <message>, remind me no more"
-    end
+    "reminder plugin: remind <who> [about] <message> in <time>, remind <who> [about] <message> every <time>, remind <who> [about] <message> at <time>, remind <who> no more [about] <message>, remind <who> no more. Generally <who> should be 'me', but you can remind others (nick or channel) if you have remind_others auth"
   end
-  def add_reminder(who, subject, timestr, repeat=false)
+  def add_reminder(m, who, subject, timestr, repeat=false)
     begin
-      period = Irc::Utils.timestr_offset(timestr)
+      period = timestr_offset(timestr)
     rescue RuntimeError
-      return "couldn't parse that time string (#{timestr}) :("
+      raise UnparsedPeriodError
     end
+    raise NegativePeriodError if period <= 0
+    raise ShortRepeatError if period < 30 && repeat
+
     if(period <= 0)
       return "that time is in the past! (#{timestr})"
     end
     if(period < 30 && repeat)
-      return "repeats of less than 30 seconds are forbidden"
+      return
     end
     if(!@reminders.has_key?(who))
       @reminders[who] = Hash.new
@@ -40,24 +51,27 @@ class RemindPlugin < Plugin
 
     if(repeat)
       @reminders[who][subject] = @bot.timer.add(period) {
-        time = Time.now + period
-        tstr = time.strftime("%H:%M:%S")
+        tstr = (Time.now + period).strftime("%H:%M:%S")
         @bot.say who, "repeat reminder (next at #{tstr}): #{subject}"
       }
     else
       @reminders[who][subject] = @bot.timer.add_once(period) {
-        time = Time.now + period
-        tstr = time.strftime("%H:%M:%S")
+        tstr = Time.now.strftime("%H:%M:%S")
         @bot.say who, "reminder (#{tstr}): #{subject}"
       }
     end
-    return false
+
+    m.okay
   end
+
   def del_reminder(who, subject=nil)
     if(subject)
       if(@reminders.has_key?(who) && @reminders[who].has_key?(subject))
         @bot.timer.remove(@reminders[who][subject])
         @reminders[who].delete(subject)
+        return true
+      else
+        return false
       end
     else
       if(@reminders.has_key?(who))
@@ -65,90 +79,76 @@ class RemindPlugin < Plugin
           @bot.timer.remove(v)
         }
         @reminders.delete(who)
+        return true
+      else
+        return false
       end
     end
   end
-  def privmsg(m)
+  def remind(m, params)
+    who = params.has_key?(:who) ? params[:who] : m.sourcenick
+    string = params[:string].to_s
+    debug "in remind, string is: #{string}"
+    tried = []
 
-    if(m.params =~ /^(\S+)\s+(?:about\s+)?(.*)\s+in\s+(.*)$/)
-      who = $1
-      subject = $2
-      period = $3
-      if(who =~ /^me$/)
-        who = m.sourcenick
-      else
-        unless(m.plugin =~ /^remind\+$/)
-          m.reply "incorrect usage: use remind+ to remind persons other than yourself"
-          return
-        end
-      end
-      if(err = add_reminder(who, subject, period))
-        m.reply "incorrect usage: " + err
-        return
-      end
-    elsif(m.params =~ /^(\S+)\s+(?:about\s+)?(.*)\s+every\s+(.*)$/)
-      who = $1
-      subject = $2
-      period = $3
-      if(who =~ /^me$/)
-        who = m.sourcenick
-      else
-        unless(m.plugin =~ /^remind\+$/)
-          m.reply "incorrect usage: use remind+ to remind persons other than yourself"
-          return
-        end
-      end
-      if(err = add_reminder(who, subject, period, true))
-        m.reply "incorrect usage: " + err
-        return
-      end
-    elsif(m.params =~ /^(\S+)\s+(?:about\s+)?(.*)\s+at\s+(.*)$/)
-      who = $1
-      subject = $2
-      time = $3
-      if(who =~ /^me$/)
-        who = m.sourcenick
-      else
-        unless(m.plugin =~ /^remind\+$/)
-          m.reply "incorrect usage: use remind+ to remind persons other than yourself"
-          return
-        end
-      end
-      if(err = add_reminder(who, subject, time))
-        m.reply "incorrect usage: " + err
-        return
+    begin
+      if !tried.include?(:in) and string =~ /^(.*)\s+in\s+(.*)$/
+        subject = $1
+        period = $2
+        tried << :in
+        add_reminder(m, who, subject, period)
+        return true
       end
-    elsif(m.params =~ /^(\S+)\s+no\s+more\s+(?:about\s+)?(.*)$/)
-      who = $1
-      subject = $2
-      if(who =~ /^me$/)
-        who = m.sourcenick
-      else
-        unless(m.plugin =~ /^remind\+$/)
-          m.reply "incorrect usage: use remind+ to remind persons other than yourself"
-          return
-        end
+
+      if !tried.include?(:every) and string =~ /^(.*)\s+every\s+(.*)$/
+        subject = $1
+        period = $2
+        tried << :every
+        add_reminder(m, who, subject, period, true)
+        return true
       end
-      del_reminder(who, subject)
-    elsif(m.params =~ /^(\S+)\s+no\s+more$/)
-      who = $1
-      if(who =~ /^me$/)
-        who = m.sourcenick
-      else
-        unless(m.plugin =~ /^remind\+$/)
-          m.reply "incorrect usage: use remind+ to remind persons other than yourself"
-          return
-        end
+
+      if !tried.include?(:at) and string =~ /^(.*)\s+at\s+(.*)$/
+        subject = $1
+        time = $2
+        tried << :at
+        add_reminder(m, who, subject, time)
+        return true
       end
-      del_reminder(who)
+
+      usage(m)
+      return false
+    rescue NegativePeriodError
+      m.reply "that time is in the past! (#{timestr})"
+      return false
+    rescue ShortRepeatError
+      m.reply "repeats of less than 30 seconds are forbidden"
+      return false
+    rescue UnparsedPeriodError
+      retry
+    end
+
+  end
+
+  def no_more(m, params)
+    who = params.has_key?(:who) ? params[:who] : m.sourcenick
+    deleted = params.has_key?(:string) ?
+              del_reminder(who, params[:string].to_s) : del_reminder(who)
+    if deleted
+      m.okay
     else
-      m.reply "incorrect usage: " + help(m.plugin)
-      return
+      m.reply "but I wasn't going to :/"
     end
-    m.okay
   end
 end
 plugin = RemindPlugin.new
-plugin.register("remind")
-plugin.register("remind+")
+
+plugin.default_auth('other', false)
+
+plugin.map 'remind me no more', :action => 'no_more'
+plugin.map 'remind me no more [about] *string', :action => 'no_more'
+plugin.map 'remind me [about] *string'
+plugin.map 'remind :who no more', :auth_path => 'other', :action => 'no_more'
+plugin.map 'remind :who no more [about] *string', :auth_path => 'other', :action => 'no_more'
+plugin.map 'remind :who [about] *string', :auth_path => 'other'