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)
9 when (/^(\S+)\s+(\S+)$/)
12 if(mult =~ /^([\d.]+)$/)
14 raise "invalid time string" unless num
46 raise "invalid time string"
50 when (/^(s|sec(ond)?s?)$/)
52 when (/^(m|min(ute)?s?)$/)
54 when (/^(h|h(ou)?rs?)$/)
57 return num * 60 * 60 * 24
59 raise "invalid time string"
61 when (/^(\d+):(\d+):(\d+)$/)
66 later = Time.mktime(now.year, now.month, now.day, hour, min, sec)
68 when (/^(\d+):(\d+)$/)
72 later = Time.mktime(now.year, now.month, now.day, hour, min, now.sec)
74 when (/^(\d+):(\d+)(am|pm)$/)
82 later = Time.mktime(now.year, now.month, now.day, hour, min, now.sec)
88 when (/^(s|sec(ond)?s?)$/)
90 when (/^(m|min(ute)?s?)$/)
92 when (/^(h|h(ou)?rs?)$/)
95 return num * 60 * 60 * 24
97 raise "invalid time string"
100 raise "invalid time string"
106 @reminders = Hash.new
109 @reminders.each_value {|v|
111 @bot.timer.remove(vv)
116 def help(plugin, topic="")
117 "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"
119 def add_reminder(who, subject, timestr, repeat=false)
121 period = timestr_offset(timestr)
123 return "couldn't parse that time string (#{timestr}) :("
126 return "that time is in the past! (#{timestr})"
128 if(period < 30 && repeat)
129 return "repeats of less than 30 seconds are forbidden"
131 if(!@reminders.has_key?(who))
132 @reminders[who] = Hash.new
133 elsif(@reminders[who].has_key?(subject))
134 del_reminder(who, subject)
138 @reminders[who][subject] = @bot.timer.add(period) {
139 tstr = (Time.now + period).strftime("%H:%M:%S")
140 @bot.say who, "repeat reminder (next at #{tstr}): #{subject}"
143 @reminders[who][subject] = @bot.timer.add_once(period) {
144 tstr = Time.now.strftime("%H:%M:%S")
145 @bot.say who, "reminder (#{tstr}): #{subject}"
150 def del_reminder(who, subject=nil)
152 if(@reminders.has_key?(who) && @reminders[who].has_key?(subject))
153 @bot.timer.remove(@reminders[who][subject])
154 @reminders[who].delete(subject)
160 if(@reminders.has_key?(who))
161 @reminders[who].each_value {|v|
164 @reminders.delete(who)
171 def remind(m, params)
172 who = params.has_key?(:who) ? params[:who] : m.sourcenick
173 string = params[:string].to_s
174 debug "in remind, string is: #{string}"
175 if(string =~ /^(.*)\s+in\s+(.*)$/)
178 if(err = add_reminder(who, subject, period))
179 m.reply "incorrect usage: " + err
182 elsif(string =~ /^(.*)\s+every\s+(.*)$/)
185 if(err = add_reminder(who, subject, period, true))
186 m.reply "incorrect usage: " + err
189 elsif(string =~ /^(.*)\s+at\s+(.*)$/)
192 if(err = add_reminder(who, subject, time))
193 m.reply "incorrect usage: " + err
202 def no_more(m, params)
203 who = params.has_key?(:who) ? params[:who] : m.sourcenick
204 deleted = params.has_key?(:string) ?
205 del_reminder(who, params[:string].to_s) : del_reminder(who)
209 m.reply "but I wasn't going to :/"
213 plugin = RemindPlugin.new
215 plugin.default_auth('other', false)
217 plugin.map 'remind me no more', :action => 'no_more'
218 plugin.map 'remind me no more [about] *string', :action => 'no_more'
219 plugin.map 'remind me [about] *string'
220 plugin.map 'remind :who no more', :auth_path => 'other', :action => 'no_more'
221 plugin.map 'remind :who no more [about] *string', :auth_path => 'other', :action => 'no_more'
222 plugin.map 'remind :who [about] *string', :auth_path => 'other'