]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/remind.rb
ruby 1.9: get rid of Array#nitems
[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     Utils.parse_time_offset(timestr)
9   end
10
11   def initialize
12     super
13     @reminders = Hash.new
14   end
15   def cleanup
16     @reminders.each_value {|v|
17       v.each_value {|vv|
18         @bot.timer.remove(vv)
19       }
20     }
21     @reminders.clear
22     super
23   end
24   def help(plugin, topic="")
25     "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"
26   end
27   def add_reminder(who, subject, timestr, repeat=false)
28     begin
29       period = timestr_offset(timestr)
30     rescue RuntimeError
31       return "couldn't parse that time string (#{timestr}) :("
32     end
33     if(period <= 0)
34       return "that time is in the past! (#{timestr})"
35     end
36     if(period < 30 && repeat)
37       return "repeats of less than 30 seconds are forbidden"
38     end
39     if(!@reminders.has_key?(who))
40       @reminders[who] = Hash.new
41     elsif(@reminders[who].has_key?(subject))
42       del_reminder(who, subject)
43     end
44
45     if(repeat)
46       @reminders[who][subject] = @bot.timer.add(period) {
47         tstr = (Time.now + period).strftime("%H:%M:%S")
48         @bot.say who, "repeat reminder (next at #{tstr}): #{subject}"
49       }
50     else
51       @reminders[who][subject] = @bot.timer.add_once(period) {
52         tstr = Time.now.strftime("%H:%M:%S")
53         @bot.say who, "reminder (#{tstr}): #{subject}"
54       }
55     end
56     return false
57   end
58   def del_reminder(who, subject=nil)
59     if(subject)
60       if(@reminders.has_key?(who) && @reminders[who].has_key?(subject))
61         @bot.timer.remove(@reminders[who][subject])
62         @reminders[who].delete(subject)
63         return true
64       else
65         return false
66       end
67     else
68       if(@reminders.has_key?(who))
69         @reminders[who].each_value {|v|
70           @bot.timer.remove(v)
71         }
72         @reminders.delete(who)
73         return true
74       else
75         return false
76       end
77     end
78   end
79   def remind(m, params)
80     who = params.has_key?(:who) ? params[:who] : m.sourcenick
81     string = params[:string].to_s
82     debug "in remind, string is: #{string}"
83     if(string =~ /^(.*)\s+in\s+(.*)$/)
84       subject = $1
85       period = $2
86       if(err = add_reminder(who, subject, period))
87         m.reply "incorrect usage: " + err
88         return
89       end
90     elsif(string =~ /^(.*)\s+every\s+(.*)$/)
91       subject = $1
92       period = $2
93       if(err = add_reminder(who, subject, period, true))
94         m.reply "incorrect usage: " + err
95         return
96       end
97     elsif(string =~ /^(.*)\s+at\s+(.*)$/)
98       subject = $1
99       time = $2
100       if(err = add_reminder(who, subject, time))
101         m.reply "incorrect usage: " + err
102         return
103       end
104     else
105       usage(m)
106       return
107     end
108     m.okay
109   end
110   def no_more(m, params)
111     who = params.has_key?(:who) ? params[:who] : m.sourcenick
112     deleted = params.has_key?(:string) ?
113               del_reminder(who, params[:string].to_s) : del_reminder(who)
114     if deleted
115       m.okay
116     else
117       m.reply "but I wasn't going to :/"
118     end
119   end
120 end
121 plugin = RemindPlugin.new
122
123 plugin.default_auth('other', false)
124
125 plugin.map 'remind me no more', :action => 'no_more'
126 plugin.map 'remind me no more [about] *string', :action => 'no_more'
127 plugin.map 'remind me [about] *string'
128 plugin.map 'remind :who no more', :auth_path => 'other', :action => 'no_more'
129 plugin.map 'remind :who no more [about] *string', :auth_path => 'other', :action => 'no_more'
130 plugin.map 'remind :who [about] *string', :auth_path => 'other'
131