1 class RemindPlugin < Plugin
2 # read a time in string format, turn it into "seconds from now".
3 # example formats handled are "5 minutes", "2 days", "five hours",
4 # "11:30", "15:45:11", "one day", etc.
6 # Throws:: RunTimeError "invalid time string" on parse failure
7 def timestr_offset(timestr)
8 Utils.parse_time_offset(timestr)
11 class UnparsedPeriodError < RuntimeError ; end
12 class NegativePeriodError < RuntimeError ; end
13 class ShortRepeatError < RuntimeError ; end
20 @reminders.each_value {|v|
28 def help(plugin, topic="")
29 "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"
31 def add_reminder(m, who, subject, timestr, repeat=false)
33 period = timestr_offset(timestr)
35 raise UnparsedPeriodError
37 raise NegativePeriodError if period <= 0
38 raise ShortRepeatError if period < 30 && repeat
41 return "that time is in the past! (#{timestr})"
43 if(period < 30 && repeat)
46 if(!@reminders.has_key?(who))
47 @reminders[who] = Hash.new
48 elsif(@reminders[who].has_key?(subject))
49 del_reminder(who, subject)
53 @reminders[who][subject] = @bot.timer.add(period) {
54 tstr = (Time.now + period).strftime("%H:%M:%S")
55 @bot.say who, "repeat reminder (next at #{tstr}): #{subject}"
58 @reminders[who][subject] = @bot.timer.add_once(period) {
59 tstr = Time.now.strftime("%H:%M:%S")
60 @bot.say who, "reminder (#{tstr}): #{subject}"
67 def del_reminder(who, subject=nil)
69 if(@reminders.has_key?(who) && @reminders[who].has_key?(subject))
70 @bot.timer.remove(@reminders[who][subject])
71 @reminders[who].delete(subject)
77 if(@reminders.has_key?(who))
78 @reminders[who].each_value {|v|
81 @reminders.delete(who)
89 who = params.has_key?(:who) ? params[:who] : m.sourcenick
90 string = params[:string].to_s
91 debug "in remind, string is: #{string}"
95 if !tried.include?(:in) and string =~ /^(.*)\s+in\s+(.*)$/
99 add_reminder(m, who, subject, period)
103 if !tried.include?(:every) and string =~ /^(.*)\s+every\s+(.*)$/
107 add_reminder(m, who, subject, period, true)
111 if !tried.include?(:at) and string =~ /^(.*)\s+at\s+(.*)$/
115 add_reminder(m, who, subject, time)
121 rescue NegativePeriodError
122 m.reply "that time is in the past! (#{timestr})"
124 rescue ShortRepeatError
125 m.reply "repeats of less than 30 seconds are forbidden"
127 rescue UnparsedPeriodError
133 def no_more(m, params)
134 who = params.has_key?(:who) ? params[:who] : m.sourcenick
135 deleted = params.has_key?(:string) ?
136 del_reminder(who, params[:string].to_s) : del_reminder(who)
140 m.reply "but I wasn't going to :/"
144 plugin = RemindPlugin.new
146 plugin.default_auth('other', false)
148 plugin.map 'remind me no more', :action => 'no_more'
149 plugin.map 'remind me no more [about] *string', :action => 'no_more'
150 plugin.map 'remind me [about] *string'
151 plugin.map 'remind :who no more', :auth_path => 'other', :action => 'no_more'
152 plugin.map 'remind :who no more [about] *string', :auth_path => 'other', :action => 'no_more'
153 plugin.map 'remind :who [about] *string', :auth_path => 'other'