figlet plugin: make it more friendly to external usage
[rbot] / data / rbot / plugins / remind.rb
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.
5   #
6   # Throws:: RunTimeError "invalid time string" on parse failure
7   def timestr_offset(timestr)
8     case timestr
9       when (/^(\S+)\s+(\S+)$/)
10         mult = $1
11         unit = $2
12         if(mult =~ /^([\d.]+)$/)
13           num = $1.to_f
14           raise "invalid time string" unless num
15         else
16           case mult
17             when(/^(one|an|a)$/)
18               num = 1
19             when(/^two$/)
20               num = 2
21             when(/^three$/)
22               num = 3
23             when(/^four$/)
24               num = 4
25             when(/^five$/)
26               num = 5
27             when(/^six$/)
28               num = 6
29             when(/^seven$/)
30               num = 7
31             when(/^eight$/)
32               num = 8
33             when(/^nine$/)
34               num = 9
35             when(/^ten$/)
36               num = 10
37             when(/^fifteen$/)
38               num = 15
39             when(/^twenty$/)
40               num = 20
41             when(/^thirty$/)
42               num = 30
43             when(/^sixty$/)
44               num = 60
45             else
46               raise "invalid time string"
47           end
48         end
49         case unit
50           when (/^(s|sec(ond)?s?)$/)
51             return num
52           when (/^(m|min(ute)?s?)$/)
53             return num * 60
54           when (/^(h|h(ou)?rs?)$/)
55             return num * 60 * 60
56           when (/^(d|days?)$/)
57             return num * 60 * 60 * 24
58           else
59             raise "invalid time string"
60         end
61       when (/^(\d+):(\d+):(\d+)$/)
62         hour = $1.to_i
63         min = $2.to_i
64         sec = $3.to_i
65         now = Time.now
66         later = Time.mktime(now.year, now.month, now.day, hour, min, sec)
67         return later - now
68       when (/^(\d+):(\d+)$/)
69         hour = $1.to_i
70         min = $2.to_i
71         now = Time.now
72         later = Time.mktime(now.year, now.month, now.day, hour, min, now.sec)
73         return later - now
74       when (/^(\d+):(\d+)(am|pm)$/)
75         hour = $1.to_i
76         min = $2.to_i
77         ampm = $3
78         if ampm == "pm"
79           hour += 12
80         end
81         now = Time.now
82         later = Time.mktime(now.year, now.month, now.day, hour, min, now.sec)
83         return later - now
84       when (/^(\S+)$/)
85         num = 1
86         unit = $1
87         case unit
88           when (/^(s|sec(ond)?s?)$/)
89             return num
90           when (/^(m|min(ute)?s?)$/)
91             return num * 60
92           when (/^(h|h(ou)?rs?)$/)
93             return num * 60 * 60
94           when (/^(d|days?)$/)
95             return num * 60 * 60 * 24
96           else
97             raise "invalid time string"
98         end
99       else
100         raise "invalid time string"
101     end
102   end
103
104   def initialize
105     super
106     @reminders = Hash.new
107   end
108   def cleanup
109     @reminders.each_value {|v|
110       v.each_value {|vv|
111         @bot.timer.remove(vv)
112       }
113     }
114     @reminders.clear
115     super
116   end
117   def help(plugin, topic="")
118     "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   end
120   def add_reminder(who, subject, timestr, repeat=false)
121     begin
122       period = timestr_offset(timestr)
123     rescue RuntimeError
124       return "couldn't parse that time string (#{timestr}) :("
125     end
126     if(period <= 0)
127       return "that time is in the past! (#{timestr})"
128     end
129     if(period < 30 && repeat)
130       return "repeats of less than 30 seconds are forbidden"
131     end
132     if(!@reminders.has_key?(who))
133       @reminders[who] = Hash.new
134     elsif(@reminders[who].has_key?(subject))
135       del_reminder(who, subject)
136     end
137
138     if(repeat)
139       @reminders[who][subject] = @bot.timer.add(period) {
140         tstr = (Time.now + period).strftime("%H:%M:%S")
141         @bot.say who, "repeat reminder (next at #{tstr}): #{subject}"
142       }
143     else
144       @reminders[who][subject] = @bot.timer.add_once(period) {
145         tstr = Time.now.strftime("%H:%M:%S")
146         @bot.say who, "reminder (#{tstr}): #{subject}"
147       }
148     end
149     return false
150   end
151   def del_reminder(who, subject=nil)
152     if(subject)
153       if(@reminders.has_key?(who) && @reminders[who].has_key?(subject))
154         @bot.timer.remove(@reminders[who][subject])
155         @reminders[who].delete(subject)
156         return true
157       else
158         return false
159       end
160     else
161       if(@reminders.has_key?(who))
162         @reminders[who].each_value {|v|
163           @bot.timer.remove(v)
164         }
165         @reminders.delete(who)
166         return true
167       else
168         return false
169       end
170     end
171   end
172   def remind(m, params)
173     who = params.has_key?(:who) ? params[:who] : m.sourcenick
174     string = params[:string].to_s
175     debug "in remind, string is: #{string}"
176     if(string =~ /^(.*)\s+in\s+(.*)$/)
177       subject = $1
178       period = $2
179       if(err = add_reminder(who, subject, period))
180         m.reply "incorrect usage: " + err
181         return
182       end
183     elsif(string =~ /^(.*)\s+every\s+(.*)$/)
184       subject = $1
185       period = $2
186       if(err = add_reminder(who, subject, period, true))
187         m.reply "incorrect usage: " + err
188         return
189       end
190     elsif(string =~ /^(.*)\s+at\s+(.*)$/)
191       subject = $1
192       time = $2
193       if(err = add_reminder(who, subject, time))
194         m.reply "incorrect usage: " + err
195         return
196       end
197     else
198       usage(m)
199       return
200     end
201     m.okay
202   end
203   def no_more(m, params)
204     who = params.has_key?(:who) ? params[:who] : m.sourcenick
205     deleted = params.has_key?(:string) ? 
206               del_reminder(who, params[:string].to_s) : del_reminder(who)
207     if deleted
208       m.okay
209     else 
210       m.reply "but I wasn't going to :/"
211     end
212   end
213 end
214 plugin = RemindPlugin.new
215
216 plugin.default_auth('other', false)
217
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_path => 'other', :action => 'no_more'
222 plugin.map 'remind :who no more [about] *string', :auth_path => 'other', :action => 'no_more'
223 plugin.map 'remind :who [about] *string', :auth_path => 'other'
224