summaryrefslogtreecommitdiff
path: root/lib/rbot/plugins/remind.rb
blob: 5ad980aeac8d33012da673e0c0594796d8100f32 (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
require 'rbot/utils'

class RemindPlugin < Plugin
  def initialize
    super
    @reminders = Hash.new
  end
  def cleanup
    @reminders.each_value {|v|
      v.each_value {|vv|
        @bot.timer.remove(vv)
      }
    }
    @reminders.clear
  end
  def help(plugin, topic="")
    if(plugin =~ /^remind\+$/)
      "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."
    else
      "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"
    end
  end
  def add_reminder(who, subject, timestr, repeat=false)
    begin
      period = Irc::Utils.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) {
        time = Time.now + period
        tstr = time.strftime("%H:%M:%S")
        @bot.say who, "repeat reminder (next at #{tstr}): #{subject}"
      }
    else
      @reminders[who][subject] = @bot.timer.add_once(period) {
        time = Time.now + period
        tstr = time.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)
      end
    else
      if(@reminders.has_key?(who))
        @reminders[who].each_value {|v|
          @bot.timer.remove(v)
        }
        @reminders.delete(who)
      end
    end
  end
  def privmsg(m)

    if(m.params =~ /^(\S+)\s+(?:about\s+)?(.*)\s+in\s+(.*)$/)
      who = $1
      subject = $2
      period = $3
      if(who =~ /^me$/)
        who = m.sourcenick
      else
        unless(m.plugin =~ /^remind\+$/)
          m.reply "incorrect usage: use remind+ to remind persons other than yourself"
          return
        end
      end
      if(err = add_reminder(who, subject, period))
        m.reply "incorrect usage: " + err
        return
      end
    elsif(m.params =~ /^(\S+)\s+(?:about\s+)?(.*)\s+every\s+(.*)$/)
      who = $1
      subject = $2
      period = $3
      if(who =~ /^me$/)
        who = m.sourcenick
      else
        unless(m.plugin =~ /^remind\+$/)
          m.reply "incorrect usage: use remind+ to remind persons other than yourself"
          return
        end
      end
      if(err = add_reminder(who, subject, period, true))
        m.reply "incorrect usage: " + err
        return
      end
    elsif(m.params =~ /^(\S+)\s+(?:about\s+)?(.*)\s+at\s+(.*)$/)
      who = $1
      subject = $2
      time = $3
      if(who =~ /^me$/)
        who = m.sourcenick
      else
        unless(m.plugin =~ /^remind\+$/)
          m.reply "incorrect usage: use remind+ to remind persons other than yourself"
          return
        end
      end
      if(err = add_reminder(who, subject, time))
        m.reply "incorrect usage: " + err
        return
      end
    elsif(m.params =~ /^(\S+)\s+no\s+more\s+(?:about\s+)?(.*)$/)
      who = $1
      subject = $2
      if(who =~ /^me$/)
        who = m.sourcenick
      else
        unless(m.plugin =~ /^remind\+$/)
          m.reply "incorrect usage: use remind+ to remind persons other than yourself"
          return
        end
      end
      del_reminder(who, subject)
    elsif(m.params =~ /^(\S+)\s+no\s+more$/)
      who = $1
      if(who =~ /^me$/)
        who = m.sourcenick
      else
        unless(m.plugin =~ /^remind\+$/)
          m.reply "incorrect usage: use remind+ to remind persons other than yourself"
          return
        end
      end
      del_reminder(who)
    else
      m.reply "incorrect usage: " + help(m.plugin)
      return
    end
    m.okay
  end
end
plugin = RemindPlugin.new
plugin.register("remind")
plugin.register("remind+")