3 class RemindPlugin < Plugin
4 # read a time in string format, turn it into "seconds from now".
5 # example formats handled are "5 minutes", "2 days", "five hours",
6 # "11:30", "15:45:11", "one day", etc.
8 # Throws:: RunTimeError "invalid time string" on parse failure
9 def timestr_offset(timestr)
11 when (/^(\S+)\s+(\S+)$/)
14 if(mult =~ /^([\d.]+)$/)
16 raise "invalid time string" unless num
48 raise "invalid time string"
52 when (/^(s|sec(ond)?s?)$/)
54 when (/^(m|min(ute)?s?)$/)
56 when (/^(h|h(ou)?rs?)$/)
59 return num * 60 * 60 * 24
61 raise "invalid time string"
63 when (/^(\d+):(\d+):(\d+)$/)
68 later = Time.mktime(now.year, now.month, now.day, hour, min, sec)
70 when (/^(\d+):(\d+)$/)
74 later = Time.mktime(now.year, now.month, now.day, hour, min, now.sec)
76 when (/^(\d+):(\d+)(am|pm)$/)
84 later = Time.mktime(now.year, now.month, now.day, hour, min, now.sec)
90 when (/^(s|sec(ond)?s?)$/)
92 when (/^(m|min(ute)?s?)$/)
94 when (/^(h|h(ou)?rs?)$/)
97 return num * 60 * 60 * 24
99 raise "invalid time string"
102 raise "invalid time string"
108 @reminders = Hash.new
111 @reminders.each_value {|v|
113 @bot.timer.remove(vv)
118 def help(plugin, topic="")
119 "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"
121 def add_reminder(who, subject, timestr, repeat=false)
123 period = timestr_offset(timestr)
125 return "couldn't parse that time string (#{timestr}) :("
128 return "that time is in the past! (#{timestr})"
130 if(period < 30 && repeat)
131 return "repeats of less than 30 seconds are forbidden"
133 if(!@reminders.has_key?(who))
134 @reminders[who] = Hash.new
135 elsif(@reminders[who].has_key?(subject))
136 del_reminder(who, subject)
140 @reminders[who][subject] = @bot.timer.add(period) {
141 time = Time.now + period
142 tstr = time.strftime("%H:%M:%S")
143 @bot.say who, "repeat reminder (next at #{tstr}): #{subject}"
146 @reminders[who][subject] = @bot.timer.add_once(period) {
147 time = Time.now + period
148 tstr = time.strftime("%H:%M:%S")
149 @bot.say who, "reminder (#{tstr}): #{subject}"
154 def del_reminder(who, subject=nil)
156 if(@reminders.has_key?(who) && @reminders[who].has_key?(subject))
157 @bot.timer.remove(@reminders[who][subject])
158 @reminders[who].delete(subject)
164 if(@reminders.has_key?(who))
165 @reminders[who].each_value {|v|
168 @reminders.delete(who)
175 def remind(m, params)
176 who = params.has_key?(:who) ? params[:who] : m.sourcenick
177 string = params[:string].to_s
178 debug "in remind, string is: #{string}"
179 if(string =~ /^(.*)\s+in\s+(.*)$/)
182 if(err = add_reminder(who, subject, period))
183 m.reply "incorrect usage: " + err
186 elsif(string =~ /^(.*)\s+every\s+(.*)$/)
189 if(err = add_reminder(who, subject, period, true))
190 m.reply "incorrect usage: " + err
193 elsif(string =~ /^(.*)\s+at\s+(.*)$/)
196 if(err = add_reminder(who, subject, time))
197 m.reply "incorrect usage: " + err
206 def no_more(m, params)
207 who = params.has_key?(:who) ? params[:who] : m.sourcenick
208 deleted = params.has_key?(:string) ?
209 del_reminder(who, params[:string].to_s) : del_reminder(who)
213 m.reply "but I wasn't going to :/"
217 plugin = RemindPlugin.new
218 plugin.map 'remind me no more', :action => 'no_more'
219 plugin.map 'remind me no more [about] *string', :action => 'no_more'
220 plugin.map 'remind me [about] *string'
221 plugin.map 'remind :who no more', :auth => 'remind_other', :action => 'no_more'
222 plugin.map 'remind :who no more [about] *string', :auth => 'remind_other', :action => 'no_more'
223 plugin.map 'remind :who [about] *string', :auth => 'remind_other'