summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/remind.rb
blob: e3f248ef6ef91529bd97e48f5e7a266b26faf50b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
class RemindPlugin < Plugin
  # read a time in string format, turn it into "seconds from now".
  # example formats handled are "5 minutes", "2 days", "five hours",
  # "11:30", "15:45:11", "one day", etc.
  #
  # Throws:: RunTimeError "invalid time string" on parse failure
  def timestr_offset(timestr)
    case timestr
      when (/^(\S+)\s+(\S+)$/)
        mult = $1
        unit = $2
        if(mult =~ /^([\d.]+)$/)
          num = $1.to_f
          raise "invalid time string" unless num
        else
          case mult
            when(/^(one|an|a)$/)
              num = 1
            when(/^two$/)
              num = 2
            when(/^three$/)
              num = 3
            when(/^four$/)
              num = 4
            when(/^five$/)
              num = 5
            when(/^six$/)
              num = 6
            when(/^seven$/)
              num = 7
            when(/^eight$/)
              num = 8
            when(/^nine$/)
              num = 9
            when(/^ten$/)
              num = 10
            when(/^fifteen$/)
              num = 15
            when(/^twenty$/)
              num = 20
            when(/^thirty$/)
              num = 30
            when(/^sixty$/)
              num = 60
            else
              raise "invalid time string"
          end
        end
        case unit
          when (/^(s|sec(ond)?s?)$/)
            return num
          when (/^(m|min(ute)?s?)$/)
            return num * 60
          when (/^(h|h(ou)?rs?)$/)
            return num * 60 * 60
          when (/^(d|days?)$/)
            return num * 60 * 60 * 24
          else
            raise "invalid time string"
        end
      when (/^(\d+):(\d+)(?:\:(\d+))?$/)
        hour = $1.to_i
        min = $2.to_i
        sec = $3.to_i
        now = Time.now
        later = Time.mktime(now.year, now.month, now.day, hour, min, sec)

        # if the given hour is earlier than current hour, given timestr
        # must have been meant to be in the future
        if hour < now.hour || hour <= now.hour && min < now.min
          later += 60*60*24
        end

        return later - now
      when (/^(\d+):(\d+)(am|pm)$/)
        hour = $1.to_i
        min = $2.to_i
        ampm = $3
        if ampm == "pm"
          hour += 12
        end
        now = Time.now
        later = Time.mktime(now.year, now.month, now.day, hour, min, now.sec)
        return later - now
      when (/^(\S+)$/)
        num = 1
        unit = $1
        case unit
          when (/^(s|sec(ond)?s?)$/)
            return num
          when (/^(m|min(ute)?s?)$/)
            return num * 60
          when (/^(h|h(ou)?rs?)$/)
            return num * 60 * 60
          when (/^(d|days?)$/)
            return num * 60 * 60 * 24
          else
            raise "invalid time string"
        end
      else
        raise "invalid time string"
    end
  end

  def initialize
    super
    @reminders = Hash.new
  end
  def cleanup
    @reminders.each_value {|v|
      v.each_value {|vv|
        @bot.timer.remove(vv)
      }
    }
    @reminders.clear
    super
  end
  def help(plugin, topic="")
    "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"
  end
  def add_reminder(who, subject, timestr, repeat=false)
    begin
      period = timestr_offset(timestr)
    rescue RuntimeError
      return "couldn't parse that time string (#{timestr}) :("
    end
    if(period <= 0)
      return "that time is in the past! (#{timestr})"
    end
    if(period < 30 && repeat)
      return "repeats of less than 30 seconds are forbidden"
    end
    if(!@reminders.has_key?(who))
      @reminders[who] = Hash.new
    elsif(@reminders[who].has_key?(subject))
      del_reminder(who, subject)
    end

    if(repeat)
      @reminders[who][subject] = @bot.timer.add(period) {
        tstr = (Time.now + period).strftime("%H:%M:%S")
        @bot.say who, "repeat reminder (next at #{tstr}): #{subject}"
      }
    else
      @reminders[who][subject] = @bot.timer.add_once(period) {
        tstr = Time.now.strftime("%H:%M:%S")
        @bot.say who, "reminder (#{tstr}): #{subject}"
      }
    end
    return false
  end
  def del_reminder(who, subject=nil)
    if(subject)
      if(@reminders.has_key?(who) && @reminders[who].has_key?(subject))
        @bot.timer.remove(@reminders[who][subject])
        @reminders[who].delete(subject)
        return true
      else
        return false
      end
    else
      if(@reminders.has_key?(who))
        @reminders[who].each_value {|v|
          @bot.timer.remove(v)
        }
        @reminders.delete(who)
        return true
      else
        return false
      end
    end
  end
  def remind(m, params)
    who = params.has_key?(:who) ? params[:who] : m.sourcenick
    string = params[:string].to_s
    debug "in remind, string is: #{string}"
    if(string =~ /^(.*)\s+in\s+(.*)$/)
      subject = $1
      period = $2
      if(err = add_reminder(who, subject, period))
        m.reply "incorrect usage: " + err
        return
      end
    elsif(string =~ /^(.*)\s+every\s+(.*)$/)
      subject = $1
      period = $2
      if(err = add_reminder(who, subject, period, true))
        m.reply "incorrect usage: " + err
        return
      end
    elsif(string =~ /^(.*)\s+at\s+(.*)$/)
      subject = $1
      time = $2
      if(err = add_reminder(who, subject, time))
        m.reply "incorrect usage: " + err
        return
      end
    else
      usage(m)
      return
    end
    m.okay
  end
  def no_more(m, params)
    who = params.has_key?(:who) ? params[:who] : m.sourcenick
    deleted = params.has_key?(:string) ? 
              del_reminder(who, params[:string].to_s) : del_reminder(who)
    if deleted
      m.okay
    else 
      m.reply "but I wasn't going to :/"
    end
  end
end
plugin = RemindPlugin.new

plugin.default_auth('other', false)

plugin.map 'remind me no more', :action => 'no_more'
plugin.map 'remind me no more [about] *string', :action => 'no_more'
plugin.map 'remind me [about] *string'
plugin.map 'remind :who no more', :auth_path => 'other', :action => 'no_more'
plugin.map 'remind :who no more [about] *string', :auth_path => 'other', :action => 'no_more'
plugin.map 'remind :who [about] *string', :auth_path => 'other'