]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/remind.rb
remove whitespace
[user/henk/code/ruby/rbot.git] / 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
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
71           later += 60*60*24
72         end
73
74         return later - now
75       when (/^(\d+):(\d+)(am|pm)$/)
76         hour = $1.to_i
77         min = $2.to_i
78         ampm = $3
79         if ampm == "pm"
80           hour += 12
81         end
82         now = Time.now
83         later = Time.mktime(now.year, now.month, now.day, hour, min, now.sec)
84         return later - now
85       when (/^(\S+)$/)
86         num = 1
87         unit = $1
88         case unit
89           when (/^(s|sec(ond)?s?)$/)
90             return num
91           when (/^(m|min(ute)?s?)$/)
92             return num * 60
93           when (/^(h|h(ou)?rs?)$/)
94             return num * 60 * 60
95           when (/^(d|days?)$/)
96             return num * 60 * 60 * 24
97           else
98             raise "invalid time string"
99         end
100       else
101         raise "invalid time string"
102     end
103   end
104
105   def initialize
106     super
107     @reminders = Hash.new
108   end
109   def cleanup
110     @reminders.each_value {|v|
111       v.each_value {|vv|
112         @bot.timer.remove(vv)
113       }
114     }
115     @reminders.clear
116     super
117   end
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"
120   end
121   def add_reminder(who, subject, timestr, repeat=false)
122     begin
123       period = timestr_offset(timestr)
124     rescue RuntimeError
125       return "couldn't parse that time string (#{timestr}) :("
126     end
127     if(period <= 0)
128       return "that time is in the past! (#{timestr})"
129     end
130     if(period < 30 && repeat)
131       return "repeats of less than 30 seconds are forbidden"
132     end
133     if(!@reminders.has_key?(who))
134       @reminders[who] = Hash.new
135     elsif(@reminders[who].has_key?(subject))
136       del_reminder(who, subject)
137     end
138
139     if(repeat)
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}"
143       }
144     else
145       @reminders[who][subject] = @bot.timer.add_once(period) {
146         tstr = Time.now.strftime("%H:%M:%S")
147         @bot.say who, "reminder (#{tstr}): #{subject}"
148       }
149     end
150     return false
151   end
152   def del_reminder(who, subject=nil)
153     if(subject)
154       if(@reminders.has_key?(who) && @reminders[who].has_key?(subject))
155         @bot.timer.remove(@reminders[who][subject])
156         @reminders[who].delete(subject)
157         return true
158       else
159         return false
160       end
161     else
162       if(@reminders.has_key?(who))
163         @reminders[who].each_value {|v|
164           @bot.timer.remove(v)
165         }
166         @reminders.delete(who)
167         return true
168       else
169         return false
170       end
171     end
172   end
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+(.*)$/)
178       subject = $1
179       period = $2
180       if(err = add_reminder(who, subject, period))
181         m.reply "incorrect usage: " + err
182         return
183       end
184     elsif(string =~ /^(.*)\s+every\s+(.*)$/)
185       subject = $1
186       period = $2
187       if(err = add_reminder(who, subject, period, true))
188         m.reply "incorrect usage: " + err
189         return
190       end
191     elsif(string =~ /^(.*)\s+at\s+(.*)$/)
192       subject = $1
193       time = $2
194       if(err = add_reminder(who, subject, time))
195         m.reply "incorrect usage: " + err
196         return
197       end
198     else
199       usage(m)
200       return
201     end
202     m.okay
203   end
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)
208     if deleted
209       m.okay
210     else
211       m.reply "but I wasn't going to :/"
212     end
213   end
214 end
215 plugin = RemindPlugin.new
216
217 plugin.default_auth('other', false)
218
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'
225