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 # if the given hour is earlier than current hour, given timestr
69 # must have been meant to be in the future
70 if hour < now.hour || hour <= now.hour && min < now.min
75 when (/^(\d+):(\d+)(am|pm)$/)
83 later = Time.mktime(now.year, now.month, now.day, hour, min, now.sec)
89 when (/^(s|sec(ond)?s?)$/)
91 when (/^(m|min(ute)?s?)$/)
93 when (/^(h|h(ou)?rs?)$/)
96 return num * 60 * 60 * 24
98 raise "invalid time string"
101 raise "invalid time string"
107 @reminders = Hash.new
110 @reminders.each_value {|v|
112 @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 tstr = (Time.now + period).strftime("%H:%M:%S")
142 @bot.say who, "repeat reminder (next at #{tstr}): #{subject}"
145 @reminders[who][subject] = @bot.timer.add_once(period) {
146 tstr = Time.now.strftime("%H:%M:%S")
147 @bot.say who, "reminder (#{tstr}): #{subject}"
152 def del_reminder(who, subject=nil)
154 if(@reminders.has_key?(who) && @reminders[who].has_key?(subject))
155 @bot.timer.remove(@reminders[who][subject])
156 @reminders[who].delete(subject)
162 if(@reminders.has_key?(who))
163 @reminders[who].each_value {|v|
166 @reminders.delete(who)
173 def remind(m, params)
174 who = params.has_key?(:who) ? params[:who] : m.sourcenick
175 string = params[:string].to_s
176 debug "in remind, string is: #{string}"
177 if(string =~ /^(.*)\s+in\s+(.*)$/)
180 if(err = add_reminder(who, subject, period))
181 m.reply "incorrect usage: " + err
184 elsif(string =~ /^(.*)\s+every\s+(.*)$/)
187 if(err = add_reminder(who, subject, period, true))
188 m.reply "incorrect usage: " + err
191 elsif(string =~ /^(.*)\s+at\s+(.*)$/)
194 if(err = add_reminder(who, subject, time))
195 m.reply "incorrect usage: " + err
204 def no_more(m, params)
205 who = params.has_key?(:who) ? params[:who] : m.sourcenick
206 deleted = params.has_key?(:string) ?
207 del_reminder(who, params[:string].to_s) : del_reminder(who)
211 m.reply "but I wasn't going to :/"
215 plugin = RemindPlugin.new
217 plugin.default_auth('other', false)
219 plugin.map 'remind me no more', :action => 'no_more'
220 plugin.map 'remind me no more [about] *string', :action => 'no_more'
221 plugin.map 'remind me [about] *string'
222 plugin.map 'remind :who no more', :auth_path => 'other', :action => 'no_more'
223 plugin.map 'remind :who no more [about] *string', :auth_path => 'other', :action => 'no_more'
224 plugin.map 'remind :who [about] *string', :auth_path => 'other'