]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/remind.rb
initial import of rbot
[user/henk/code/ruby/rbot.git] / rbot / plugins / remind.rb
1 require 'rbot/utils'
2
3 class RemindPlugin < Plugin
4   def initialize
5     super
6     @reminders = Hash.new
7   end
8   def cleanup
9     @reminders.each_value {|v|
10       v.each_value {|vv|
11         @bot.timer.remove(vv)
12       }
13     }
14     @reminders.clear
15   end
16   def help(plugin, topic="")
17     if(plugin =~ /^remind\+$/)
18       "see remind. remind+ can be used to remind someone else of something, using <nick> instead of 'me'. However this will generally require a higher auth level than remind."
19     else
20       "remind me [about] <message> in <time>, remind me [about] <message> every <time>, remind me [about] <message> at <time>, remind me no more [about] <message>, remind me no more"
21     end
22   end
23   def add_reminder(who, subject, timestr, repeat=false)
24     begin
25       period = Irc::Utils.timestr_offset(timestr)
26     rescue RuntimeError
27       return "couldn't parse that time string (#{timestr}) :("
28     end
29     if(period <= 0)
30       return "that time is in the past! (#{timestr})"
31     end
32     if(period < 30 && repeat)
33       return "repeats of less than 30 seconds are forbidden"
34     end
35     if(!@reminders.has_key?(who))
36       @reminders[who] = Hash.new
37     elsif(@reminders[who].has_key?(subject))
38       del_reminder(who, subject)
39     end
40
41     if(repeat)
42       @reminders[who][subject] = @bot.timer.add(period) {
43         time = Time.now + period
44         tstr = time.strftime("%H:%M:%S")
45         @bot.say who, "repeat reminder (next at #{tstr}): #{subject}"
46       }
47     else
48       @reminders[who][subject] = @bot.timer.add_once(period) {
49         time = Time.now + period
50         tstr = time.strftime("%H:%M:%S")
51         @bot.say who, "reminder (#{tstr}): #{subject}"
52       }
53     end
54     return false
55   end
56   def del_reminder(who, subject=nil)
57     if(subject)
58       if(@reminders.has_key?(who) && @reminders[who].has_key?(subject))
59         @bot.timer.remove(@reminders[who][subject])
60         @reminders[who].delete(subject)
61       end
62     else
63       if(@reminders.has_key?(who))
64         @reminders[who].each_value {|v|
65           @bot.timer.remove(v)
66         }
67         @reminders.delete(who)
68       end
69     end
70   end
71   def privmsg(m)
72
73     if(m.params =~ /^(\S+)\s+(?:about\s+)?(.*)\s+in\s+(.*)$/)
74       who = $1
75       subject = $2
76       period = $3
77       if(who =~ /^me$/)
78         who = m.sourcenick
79       else
80         unless(m.plugin =~ /^remind\+$/)
81           m.reply "incorrect usage: use remind+ to remind persons other than yourself"
82           return
83         end
84       end
85       if(err = add_reminder(who, subject, period))
86         m.reply "incorrect usage: " + err
87         return
88       end
89     elsif(m.params =~ /^(\S+)\s+(?:about\s+)?(.*)\s+every\s+(.*)$/)
90       who = $1
91       subject = $2
92       period = $3
93       if(who =~ /^me$/)
94         who = m.sourcenick
95       else
96         unless(m.plugin =~ /^remind\+$/)
97           m.reply "incorrect usage: use remind+ to remind persons other than yourself"
98           return
99         end
100       end
101       if(err = add_reminder(who, subject, period, true))
102         m.reply "incorrect usage: " + err
103         return
104       end
105     elsif(m.params =~ /^(\S+)\s+(?:about\s+)?(.*)\s+at\s+(.*)$/)
106       who = $1
107       subject = $2
108       time = $3
109       if(who =~ /^me$/)
110         who = m.sourcenick
111       else
112         unless(m.plugin =~ /^remind\+$/)
113           m.reply "incorrect usage: use remind+ to remind persons other than yourself"
114           return
115         end
116       end
117       if(err = add_reminder(who, subject, time))
118         m.reply "incorrect usage: " + err
119         return
120       end
121     elsif(m.params =~ /^(\S+)\s+no\s+more\s+(?:about\s+)?(.*)$/)
122       who = $1
123       subject = $2
124       if(who =~ /^me$/)
125         who = m.sourcenick
126       else
127         unless(m.plugin =~ /^remind\+$/)
128           m.reply "incorrect usage: use remind+ to remind persons other than yourself"
129           return
130         end
131       end
132       del_reminder(who, subject)
133     elsif(m.params =~ /^(\S+)\s+no\s+more$/)
134       who = $1
135       if(who =~ /^me$/)
136         who = m.sourcenick
137       else
138         unless(m.plugin =~ /^remind\+$/)
139           m.reply "incorrect usage: use remind+ to remind persons other than yourself"
140           return
141         end
142       end
143       del_reminder(who)
144     else
145       m.reply "incorrect usage: " + help(m.plugin)
146       return
147     end
148     @bot.okay m.replyto
149   end
150 end
151 plugin = RemindPlugin.new
152 plugin.register("remind")
153 plugin.register("remind+")
154