summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/quotes.rb
blob: 2d9bedcd00b614cc872b877b54c2f5a4ee7ec798 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#-- vim:sw=2:et
#++
#
# :title: Quotes plugin
#
# TODO:: use message mapper instead of multiple ifs
# TODO:: switch to db

define_structure :Quote, :num, :date, :source, :quote

class QuotePlugin < Plugin
  def initialize
    super
    @lists = Hash.new
    @changed = Hash.new
    Dir["#{@bot.botclass}/quotes/*"].each {|f|
      next if File.directory?(f)
      channel = File.basename(f)
      @lists[channel] = Array.new if(!@lists.has_key?(channel))
      IO.foreach(f) {|line|
        if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/)
          num = $1.to_i
          @lists[channel][num] = Quote.new(num, $2, $3, $4)
        end
      }
      @changed[channel] = false
    }
  end

  def save
    Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes"))
    @lists.each {|channel, quotes|
      begin
        if @changed[channel]
          debug "Writing new quotefile for channel #{channel} ..."
          Utils.safe_save("#{@bot.botclass}/quotes/#{channel}") {|file|
            quotes.compact.each {|q| 
              file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
            }
          }
          @changed[channel] = false
        else
          debug "Not writing quotefile for channel #{channel} (unchanged)"
        end
      rescue => e
        error "failed to write quotefile for channel #{channel}!\n#{$!}"
        error "#{e.class}: #{e}"
        error e.backtrace.join("\n")
      end
    }
  end

  def cleanup
    @lists.clear
    @changed.clear
  end

  def addquote(source, channel, quote)
    @lists[channel] = Array.new if(!@lists.has_key?(channel))
    num = @lists[channel].length 
    @lists[channel][num] = Quote.new(num, Time.new, source.fullform, quote)
    @changed[channel] = true
    return num
  end

  def getquote(source, channel, num=nil)
    return nil unless(@lists.has_key?(channel))
    return nil unless(@lists[channel].length > 0)
    if(num)
      if(@lists[channel][num])
        return @lists[channel][num], @lists[channel].length - 1
      end
    else
      # random quote
      return @lists[channel].compact[rand(@lists[channel].nitems)],
      @lists[channel].length - 1
    end
  end

  def delquote(channel, num)
    return false unless(@lists.has_key?(channel))
    return false unless(@lists[channel].length > 0)
    if(@lists[channel][num])
      @lists[channel][num] = nil
      @lists[channel].pop if num == @lists[channel].length - 1
      @changed[channel] = true
      return true
    end
    return false
  end

  def countquote(source, channel=nil, regexp=nil)
    unless(channel)
      total=0
      @lists.each_value {|l|
        total += l.compact.length
      }
      return total
    end
    return 0 unless(@lists.has_key?(channel))
    return 0 unless(@lists[channel].length > 0)
    if(regexp)
      matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
    else
      matches = @lists[channel].compact
    end
    return matches.length
  end

  def searchquote(source, channel, regexp)
    return nil unless(@lists.has_key?(channel))
    return nil unless(@lists[channel].length > 0)
    matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
    if(matches.length > 0)
      return matches[rand(matches.length)], @lists[channel].length - 1
    else
      return nil
    end
  end

  def help(plugin, topic="")
    case topic
    when "addquote"
      return "addquote [<channel>] <quote> => Add quote <quote> for channel <channel>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !addquote without addressing if so configured"
    when "delquote"
      return "delquote [<channel>] <num> => delete quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !delquote without addressing if so configured"
    when "getquote"
      return "getquote [<channel>] [<num>] => get quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Without <num>, a random quote will be returned. Responds to !getquote without addressing if so configured"
    when "searchquote"
      return "searchquote [<channel>] <regexp> => search for quote from <channel> that matches <regexp>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !searchquote without addressing if so configured"
    when "topicquote"
      return "topicquote [<channel>] [<num>] => set topic to quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Without <num>, a random quote will be set. Responds to !topicquote without addressing if so configured"
    when "countquote"
      return "countquote [<channel>] <regexp> => count quotes from <channel> that match <regexp>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !countquote without addressing if so configured"
    when "whoquote"
      return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
    when "whenquote"
      return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
    else
      return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
    end
  end

  def listen(m)
    return unless(m.kind_of? PrivMessage)

    command = m.message.dup
    if(m.address? && m.private?)
      case command
        when (/^addquote\s+(#\S+)\s+(.*)/)
          channel = $1
          quote = $2
          if(@bot.auth.allow?("quote::other::add", m.source, m.replyto))
            if(channel =~ /^#/)
              num = addquote(m.source, channel, quote)
              m.reply "added the quote (##{num})"
            end
          end
        when (/^getquote\s+(#\S+)$/)
          channel = $1
          if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
            quote, total = getquote(m.source, channel)
            if(quote)
              m.reply "[#{quote.num}] #{quote.quote}"
            else
              m.reply "quote not found!"
            end
          end
        when (/^getquote\s+(#\S+)\s+(\d+)$/)
          channel = $1
          num = $2.to_i
          if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
            quote, total = getquote(m.source, channel, num)
            if(quote)
              m.reply "[#{quote.num}] #{quote.quote}"
            else
              m.reply "quote not found!"
            end
          end
        when (/^whoquote\s+(#\S+)\s+(\d+)$/)
          channel = $1
          num = $2.to_i
          if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
            quote, total = getquote(m.source, channel, num)
            if(quote)
              m.reply "quote #{quote.num} added by #{quote.source}"
            else
              m.reply "quote not found!"
            end
          end
        when (/^whenquote\s+(#\S+)\s+(\d+)$/)
          channel = $1
          num = $2.to_i
          if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
            quote, total = getquote(m.source, channel, num)
            if(quote)
              m.reply "quote #{quote.num} added on #{quote.date}"
            else
              m.reply "quote not found!"
            end
          end
        when (/^topicquote\s+(#\S+)$/)
          channel = $1
          if(@bot.auth.allow?("quote::other::topic", m.source, m.replyto))
            quote, total = getquote(m.source, channel)
            if(quote)
              @bot.topic channel, "[#{quote.num}] #{quote.quote}"
            else
              m.reply "quote not found!"
            end
          end
        when (/^topicquote\s+(#\S+)\s+(\d+)$/)
          channel = $1
          num = $2.to_i
          if(@bot.auth.allow?("quote::other::topic", m.source, m.replyto))
            quote, total = getquote(m.source, channel, num)
            if(quote)
              @bot.topic channel, "[#{quote.num}] #{quote.quote}"
            else
              m.reply "quote not found!"
            end
          end
        when (/^delquote\s+(#\S+)\s+(\d+)$/)
          channel = $1
          num = $2.to_i
          if(@bot.auth.allow?("quote::other::del", m.source, m.replyto))
            if(delquote(channel, num))
              m.okay
            else
              m.reply "quote not found!"
            end
          end
        when (/^searchquote\s+(#\S+)\s+(.*)$/)
          channel = $1
          reg = $2
          if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
            quote, total = searchquote(m.source, channel, reg)
            if(quote)
              m.reply "[#{quote.num}] #{quote.quote}"
            else
              m.reply "quote not found!"
            end
          end
        when (/^countquote$/)
          if(@bot.auth.allow?("quote::get::count", m.source, m.replyto))
            total = countquote(m.source)
            m.reply "#{total} quotes"
          end
        when (/^countquote\s+(#\S+)\s*(.*)$/)
          channel = $1
          reg = $2
          if(@bot.auth.allow?("quote::other::get::count", m.source, m.replyto))
            total = countquote(m.source, channel, reg)
            if(reg.length > 0)
              m.reply "#{total} quotes match: #{reg}"
            else
              m.reply "#{total} quotes"
            end
          end
      end
    elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, "")))
      case command
        when (/^addquote\s+(.+)/)
          if(@bot.auth.allow?("quote::add", m.source, m.replyto))
            num = addquote(m.source, m.target.to_s, $1)
            m.reply "added the quote (##{num})"
          end
        when (/^getquote$/)
          if(@bot.auth.allow?("quote::get", m.source, m.replyto))
            quote, total = getquote(m.source, m.target.to_s)
            if(quote)
              m.reply "[#{quote.num}] #{quote.quote}"
            else
              m.reply "no quotes found!"
            end
          end
        when (/^getquote\s+(\d+)$/)
          num = $1.to_i
          if(@bot.auth.allow?("quote::get", m.source, m.replyto))
            quote, total = getquote(m.source, m.target.to_s, num)
            if(quote)
              m.reply "[#{quote.num}] #{quote.quote}"
            else
              m.reply "quote not found!"
            end
          end
        when (/^whenquote\s+(\d+)$/)
          num = $1.to_i
          if(@bot.auth.allow?("quote::get", m.source, m.replyto))
            quote, total = getquote(m.source, m.target.to_s, num)
            if(quote)
              m.reply "quote #{quote.num} added on #{quote.date}"
            else
              m.reply "quote not found!"
            end
          end
        when (/^whoquote\s+(\d+)$/)
          num = $1.to_i
          if(@bot.auth.allow?("quote::get", m.source, m.replyto))
            quote, total = getquote(m.source, m.target.to_s, num)
            if(quote)
              m.reply "quote #{quote.num} added by #{quote.source}"
            else
              m.reply "quote not found!"
            end
          end
        when (/^topicquote$/)
          if(@bot.auth.allow?("quote::topic", m.source, m.replyto))
            quote, total = getquote(m.source, m.target.to_s)
            if(quote)
              @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
            else
              m.reply "no quotes found!"
            end
          end
        when (/^topicquote\s+(\d+)$/)
          num = $1.to_i
          if(@bot.auth.allow?("quote::topic", m.source, m.replyto))
            quote, total = getquote(m.source, m.target.to_s, num)
            if(quote)
              @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
            else
              m.reply "quote not found!"
            end
          end
        when (/^delquote\s+(\d+)$/)
          num = $1.to_i
          if(@bot.auth.allow?("quote::del", m.source, m.replyto))
            if(delquote(m.target.to_s, num))
              m.okay
            else
              m.reply "quote not found!"
            end
          end
        when (/^searchquote\s+(.*)$/)
          reg = $1
          if(@bot.auth.allow?("quote::get", m.source, m.replyto))
            quote, total = searchquote(m.source, m.target.to_s, reg)
            if(quote)
              m.reply "[#{quote.num}] #{quote.quote}"
            else
              m.reply "quote not found!"
            end
          end
        when (/^countquote(?:\s+(.*))?$/)
          reg = $1
          if(@bot.auth.allow?("quote::get::count", m.source, m.replyto))
            total = countquote(m.source, m.target.to_s, reg)
            if(reg && reg.length > 0)
              m.reply "#{total} quotes match: #{reg}"
            else
              m.reply "#{total} quotes"
            end
          end
      end
    end
  end
end

plugin = QuotePlugin.new
plugin.register("quotes")
plugin.default_auth('other', false) # Prevent random people from editing other channels quote lists by default
plugin.default_auth('other::get', true) # But allow them to view them
plugin.default_auth('del', false) # Prevent random people from removing quotes