]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/quotes.rb
quotes plugin: cleanups
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / quotes.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Quotes plugin
5 #
6 # TODO:: use new auth system
7 # TODO:: use message mapper instead of multiple ifs
8 # TODO:: switch to db
9
10 define_structure :Quote, :num, :date, :source, :quote
11
12 class QuotePlugin < Plugin
13   def initialize
14     super
15     @lists = Hash.new
16     @changed = Hash.new
17     Dir["#{@bot.botclass}/quotes/*"].each {|f|
18       next if File.directory?(f)
19       channel = File.basename(f)
20       @lists[channel] = Array.new if(!@lists.has_key?(channel))
21       IO.foreach(f) {|line|
22         if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/)
23           num = $1.to_i
24           @lists[channel][num] = Quote.new(num, $2, $3, $4)
25         end
26       }
27       @changed[channel] = false
28     }
29   end
30
31   def save
32     Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes"))
33     @lists.each {|channel, quotes|
34       begin
35         if @changed[channel]
36           debug "Writing new quotefile for channel #{channel} ..."
37           Utils.safe_save("#{@bot.botclass}/quotes/#{channel}") {|file|
38             quotes.compact.each {|q| 
39               file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
40             }
41           }
42           @changed[channel] = false
43         else
44           debug "Not writing quotefile for channel #{channel} (unchanged)"
45         end
46       rescue => e
47         error "failed to write quotefile for channel #{channel}!\n#{$!}"
48         error "#{e.class}: #{e}"
49         error e.backtrace.join("\n")
50       end
51     }
52   end
53
54   def cleanup
55     @lists.clear
56     @changed.clear
57   end
58
59   def addquote(source, channel, quote)
60     @lists[channel] = Array.new if(!@lists.has_key?(channel))
61     num = @lists[channel].length 
62     @lists[channel][num] = Quote.new(num, Time.new, source.fullform, quote)
63     @changed[channel] = true
64     return num
65   end
66
67   def getquote(source, channel, num=nil)
68     return nil unless(@lists.has_key?(channel))
69     return nil unless(@lists[channel].length > 0)
70     if(num)
71       if(@lists[channel][num])
72         return @lists[channel][num], @lists[channel].length - 1
73       end
74     else
75       # random quote
76       return @lists[channel].compact[rand(@lists[channel].nitems)],
77       @lists[channel].length - 1
78     end
79   end
80
81   def delquote(channel, num)
82     return false unless(@lists.has_key?(channel))
83     return false unless(@lists[channel].length > 0)
84     if(@lists[channel][num])
85       @lists[channel][num] = nil
86       @lists[channel].pop if num == @lists[channel].length - 1
87       @changed[channel] = true
88       return true
89     end
90     return false
91   end
92
93   def countquote(source, channel=nil, regexp=nil)
94     unless(channel)
95       total=0
96       @lists.each_value {|l|
97         total += l.compact.length
98       }
99       return total
100     end
101     return 0 unless(@lists.has_key?(channel))
102     return 0 unless(@lists[channel].length > 0)
103     if(regexp)
104       matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
105     else
106       matches = @lists[channel].compact
107     end
108     return matches.length
109   end
110
111   def searchquote(source, channel, regexp)
112     return nil unless(@lists.has_key?(channel))
113     return nil unless(@lists[channel].length > 0)
114     matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
115     if(matches.length > 0)
116       return matches[rand(matches.length)], @lists[channel].length - 1
117     else
118       return nil
119     end
120   end
121
122   def help(plugin, topic="")
123     case topic
124     when "addquote"
125       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"
126     when "delquote"
127       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"
128     when "getquote"
129       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"
130     when "searchquote"
131       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"
132     when "topicquote"
133       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"
134     when "countquote"
135       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"
136     when "whoquote"
137       return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
138     when "whenquote"
139       return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
140     else
141       return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
142     end
143   end
144
145   def listen(m)
146     return unless(m.kind_of? PrivMessage)
147
148     command = m.message.dup
149     if(m.address? && m.private?)
150       case command
151         when (/^addquote\s+(#\S+)\s+(.*)/)
152           channel = $1
153           quote = $2
154           if(@bot.auth.allow?("addquote", m.source, m.replyto))
155             if(channel =~ /^#/)
156               num = addquote(m.source, channel, quote)
157               m.reply "added the quote (##{num})"
158             end
159           end
160         when (/^getquote\s+(#\S+)$/)
161           channel = $1
162           if(@bot.auth.allow?("getquote", m.source, m.replyto))
163             quote, total = getquote(m.source, channel)
164             if(quote)
165               m.reply "[#{quote.num}] #{quote.quote}"
166             else
167               m.reply "quote not found!"
168             end
169           end
170         when (/^getquote\s+(#\S+)\s+(\d+)$/)
171           channel = $1
172           num = $2.to_i
173           if(@bot.auth.allow?("getquote", m.source, m.replyto))
174             quote, total = getquote(m.source, channel, num)
175             if(quote)
176               m.reply "[#{quote.num}] #{quote.quote}"
177             else
178               m.reply "quote not found!"
179             end
180           end
181         when (/^whoquote\s+(#\S+)\s+(\d+)$/)
182           channel = $1
183           num = $2.to_i
184           if(@bot.auth.allow?("getquote", m.source, m.replyto))
185             quote, total = getquote(m.source, channel, num)
186             if(quote)
187               m.reply "quote #{quote.num} added by #{quote.source}"
188             else
189               m.reply "quote not found!"
190             end
191           end
192         when (/^whenquote\s+(#\S+)\s+(\d+)$/)
193           channel = $1
194           num = $2.to_i
195           if(@bot.auth.allow?("getquote", m.source, m.replyto))
196             quote, total = getquote(m.source, channel, num)
197             if(quote)
198               m.reply "quote #{quote.num} added on #{quote.date}"
199             else
200               m.reply "quote not found!"
201             end
202           end
203         when (/^topicquote\s+(#\S+)$/)
204           channel = $1
205           if(@bot.auth.allow?("topicquote", m.source, m.replyto))
206             quote, total = getquote(m.source, channel)
207             if(quote)
208               @bot.topic channel, "[#{quote.num}] #{quote.quote}"
209             else
210               m.reply "quote not found!"
211             end
212           end
213         when (/^topicquote\s+(#\S+)\s+(\d+)$/)
214           channel = $1
215           num = $2.to_i
216           if(@bot.auth.allow?("topicquote", m.source, m.replyto))
217             quote, total = getquote(m.source, channel, num)
218             if(quote)
219               @bot.topic channel, "[#{quote.num}] #{quote.quote}"
220             else
221               m.reply "quote not found!"
222             end
223           end
224         when (/^delquote\s+(#\S+)\s+(\d+)$/)
225           channel = $1
226           num = $2.to_i
227           if(@bot.auth.allow?("delquote", m.source, m.replyto))
228             if(delquote(channel, num))
229               m.okay
230             else
231               m.reply "quote not found!"
232             end
233           end
234         when (/^searchquote\s+(#\S+)\s+(.*)$/)
235           channel = $1
236           reg = $2
237           if(@bot.auth.allow?("getquote", m.source, m.replyto))
238             quote, total = searchquote(m.source, channel, reg)
239             if(quote)
240               m.reply "[#{quote.num}] #{quote.quote}"
241             else
242               m.reply "quote not found!"
243             end
244           end
245         when (/^countquote$/)
246           if(@bot.auth.allow?("getquote", m.source, m.replyto))
247             total = countquote(m.source)
248             m.reply "#{total} quotes"
249           end
250         when (/^countquote\s+(#\S+)\s*(.*)$/)
251           channel = $1
252           reg = $2
253           if(@bot.auth.allow?("getquote", m.source, m.replyto))
254             total = countquote(m.source, channel, reg)
255             if(reg.length > 0)
256               m.reply "#{total} quotes match: #{reg}"
257             else
258               m.reply "#{total} quotes"
259             end
260           end
261       end
262     elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, "")))
263       case command
264         when (/^addquote\s+(.+)/)
265           if(@bot.auth.allow?("addquote", m.source, m.replyto))
266             num = addquote(m.source, m.target.to_s, $1)
267             m.reply "added the quote (##{num})"
268           end
269         when (/^getquote$/)
270           if(@bot.auth.allow?("getquote", m.source, m.replyto))
271             quote, total = getquote(m.source, m.target.to_s)
272             if(quote)
273               m.reply "[#{quote.num}] #{quote.quote}"
274             else
275               m.reply "no quotes found!"
276             end
277           end
278         when (/^getquote\s+(\d+)$/)
279           num = $1.to_i
280           if(@bot.auth.allow?("getquote", m.source, m.replyto))
281             quote, total = getquote(m.source, m.target.to_s, num)
282             if(quote)
283               m.reply "[#{quote.num}] #{quote.quote}"
284             else
285               m.reply "quote not found!"
286             end
287           end
288         when (/^whenquote\s+(\d+)$/)
289           num = $1.to_i
290           if(@bot.auth.allow?("getquote", m.source, m.replyto))
291             quote, total = getquote(m.source, m.target.to_s, num)
292             if(quote)
293               m.reply "quote #{quote.num} added on #{quote.date}"
294             else
295               m.reply "quote not found!"
296             end
297           end
298         when (/^whoquote\s+(\d+)$/)
299           num = $1.to_i
300           if(@bot.auth.allow?("getquote", m.source, m.replyto))
301             quote, total = getquote(m.source, m.target.to_s, num)
302             if(quote)
303               m.reply "quote #{quote.num} added by #{quote.source}"
304             else
305               m.reply "quote not found!"
306             end
307           end
308         when (/^topicquote$/)
309           if(@bot.auth.allow?("topicquote", m.source, m.replyto))
310             quote, total = getquote(m.source, m.target.to_s)
311             if(quote)
312               @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
313             else
314               m.reply "no quotes found!"
315             end
316           end
317         when (/^topicquote\s+(\d+)$/)
318           num = $1.to_i
319           if(@bot.auth.allow?("topicquote", m.source, m.replyto))
320             quote, total = getquote(m.source, m.target.to_s, num)
321             if(quote)
322               @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
323             else
324               m.reply "quote not found!"
325             end
326           end
327         when (/^delquote\s+(\d+)$/)
328           num = $1.to_i
329           if(@bot.auth.allow?("delquote", m.source, m.replyto))
330             if(delquote(m.target.to_s, num))
331               m.okay
332             else
333               m.reply "quote not found!"
334             end
335           end
336         when (/^searchquote\s+(.*)$/)
337           reg = $1
338           if(@bot.auth.allow?("getquote", m.source, m.replyto))
339             quote, total = searchquote(m.source, m.target.to_s, reg)
340             if(quote)
341               m.reply "[#{quote.num}] #{quote.quote}"
342             else
343               m.reply "quote not found!"
344             end
345           end
346         when (/^countquote(?:\s+(.*))?$/)
347           reg = $1
348           if(@bot.auth.allow?("getquote", m.source, m.replyto))
349             total = countquote(m.source, m.target.to_s, reg)
350             if(reg && reg.length > 0)
351               m.reply "#{total} quotes match: #{reg}"
352             else
353               m.reply "#{total} quotes"
354             end
355           end
356       end
357     end
358   end
359 end
360 plugin = QuotePlugin.new
361 plugin.register("quotes")