]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/quotes.rb
+ use the message() delegate instead of listen() when possible
[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 message mapper instead of multiple ifs
7 # TODO:: switch to db
8
9 define_structure :Quote, :num, :date, :source, :quote
10
11 class QuotePlugin < Plugin
12   def initialize
13     super
14     @lists = Hash.new
15     @changed = Hash.new
16     Dir["#{@bot.botclass}/quotes/*"].each {|f|
17       next if File.directory?(f)
18       channel = File.basename(f)
19       @lists[channel] = Array.new if(!@lists.has_key?(channel))
20       IO.foreach(f) {|line|
21         if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/)
22           num = $1.to_i
23           @lists[channel][num] = Quote.new(num, $2, $3, $4)
24         end
25       }
26       @changed[channel] = false
27     }
28   end
29
30   def save
31     Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes"))
32     @lists.each {|channel, quotes|
33       begin
34         if @changed[channel]
35           debug "Writing new quotefile for channel #{channel} ..."
36           Utils.safe_save("#{@bot.botclass}/quotes/#{channel}") {|file|
37             quotes.compact.each {|q| 
38               file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
39             }
40           }
41           @changed[channel] = false
42         else
43           debug "Not writing quotefile for channel #{channel} (unchanged)"
44         end
45       rescue => e
46         error "failed to write quotefile for channel #{channel}!\n#{$!}"
47         error "#{e.class}: #{e}"
48         error e.backtrace.join("\n")
49       end
50     }
51   end
52
53   def cleanup
54     @lists.clear
55     @changed.clear
56     super
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 message(m)
146     command = m.message.dup
147     if(m.address? && m.private?)
148       case command
149         when (/^addquote\s+(#\S+)\s+(.*)/)
150           channel = $1
151           quote = $2
152           if(@bot.auth.allow?("quote::other::add", m.source, m.replyto))
153             if(channel =~ /^#/)
154               num = addquote(m.source, channel, quote)
155               m.reply "added the quote (##{num})"
156             end
157           end
158         when (/^getquote\s+(#\S+)$/)
159           channel = $1
160           if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
161             quote, total = getquote(m.source, channel)
162             if(quote)
163               m.reply "[#{quote.num}] #{quote.quote}"
164             else
165               m.reply "quote not found!"
166             end
167           end
168         when (/^getquote\s+(#\S+)\s+(\d+)$/)
169           channel = $1
170           num = $2.to_i
171           if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
172             quote, total = getquote(m.source, channel, num)
173             if(quote)
174               m.reply "[#{quote.num}] #{quote.quote}"
175             else
176               m.reply "quote not found!"
177             end
178           end
179         when (/^whoquote\s+(#\S+)\s+(\d+)$/)
180           channel = $1
181           num = $2.to_i
182           if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
183             quote, total = getquote(m.source, channel, num)
184             if(quote)
185               m.reply "quote #{quote.num} added by #{quote.source}"
186             else
187               m.reply "quote not found!"
188             end
189           end
190         when (/^whenquote\s+(#\S+)\s+(\d+)$/)
191           channel = $1
192           num = $2.to_i
193           if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
194             quote, total = getquote(m.source, channel, num)
195             if(quote)
196               m.reply "quote #{quote.num} added on #{quote.date}"
197             else
198               m.reply "quote not found!"
199             end
200           end
201         when (/^topicquote\s+(#\S+)$/)
202           channel = $1
203           if(@bot.auth.allow?("quote::other::topic", m.source, m.replyto))
204             quote, total = getquote(m.source, channel)
205             if(quote)
206               @bot.topic channel, "[#{quote.num}] #{quote.quote}"
207             else
208               m.reply "quote not found!"
209             end
210           end
211         when (/^topicquote\s+(#\S+)\s+(\d+)$/)
212           channel = $1
213           num = $2.to_i
214           if(@bot.auth.allow?("quote::other::topic", m.source, m.replyto))
215             quote, total = getquote(m.source, channel, num)
216             if(quote)
217               @bot.topic channel, "[#{quote.num}] #{quote.quote}"
218             else
219               m.reply "quote not found!"
220             end
221           end
222         when (/^delquote\s+(#\S+)\s+(\d+)$/)
223           channel = $1
224           num = $2.to_i
225           if(@bot.auth.allow?("quote::other::del", m.source, m.replyto))
226             if(delquote(channel, num))
227               m.okay
228             else
229               m.reply "quote not found!"
230             end
231           end
232         when (/^searchquote\s+(#\S+)\s+(.*)$/)
233           channel = $1
234           reg = $2
235           if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
236             quote, total = searchquote(m.source, channel, reg)
237             if(quote)
238               m.reply "[#{quote.num}] #{quote.quote}"
239             else
240               m.reply "quote not found!"
241             end
242           end
243         when (/^countquote$/)
244           if(@bot.auth.allow?("quote::get::count", m.source, m.replyto))
245             total = countquote(m.source)
246             m.reply "#{total} quotes"
247           end
248         when (/^countquote\s+(#\S+)\s*(.*)$/)
249           channel = $1
250           reg = $2
251           if(@bot.auth.allow?("quote::other::get::count", m.source, m.replyto))
252             total = countquote(m.source, channel, reg)
253             if(reg.length > 0)
254               m.reply "#{total} quotes match: #{reg}"
255             else
256               m.reply "#{total} quotes"
257             end
258           end
259       end
260     elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, "")))
261       case command
262         when (/^addquote\s+(.+)/)
263           if(@bot.auth.allow?("quote::add", m.source, m.replyto))
264             num = addquote(m.source, m.target.to_s, $1)
265             m.reply "added the quote (##{num})"
266           end
267         when (/^getquote$/)
268           if(@bot.auth.allow?("quote::get", m.source, m.replyto))
269             quote, total = getquote(m.source, m.target.to_s)
270             if(quote)
271               m.reply "[#{quote.num}] #{quote.quote}"
272             else
273               m.reply "no quotes found!"
274             end
275           end
276         when (/^getquote\s+(\d+)$/)
277           num = $1.to_i
278           if(@bot.auth.allow?("quote::get", m.source, m.replyto))
279             quote, total = getquote(m.source, m.target.to_s, num)
280             if(quote)
281               m.reply "[#{quote.num}] #{quote.quote}"
282             else
283               m.reply "quote not found!"
284             end
285           end
286         when (/^whenquote\s+(\d+)$/)
287           num = $1.to_i
288           if(@bot.auth.allow?("quote::get", m.source, m.replyto))
289             quote, total = getquote(m.source, m.target.to_s, num)
290             if(quote)
291               m.reply "quote #{quote.num} added on #{quote.date}"
292             else
293               m.reply "quote not found!"
294             end
295           end
296         when (/^whoquote\s+(\d+)$/)
297           num = $1.to_i
298           if(@bot.auth.allow?("quote::get", m.source, m.replyto))
299             quote, total = getquote(m.source, m.target.to_s, num)
300             if(quote)
301               m.reply "quote #{quote.num} added by #{quote.source}"
302             else
303               m.reply "quote not found!"
304             end
305           end
306         when (/^topicquote$/)
307           if(@bot.auth.allow?("quote::topic", m.source, m.replyto))
308             quote, total = getquote(m.source, m.target.to_s)
309             if(quote)
310               @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
311             else
312               m.reply "no quotes found!"
313             end
314           end
315         when (/^topicquote\s+(\d+)$/)
316           num = $1.to_i
317           if(@bot.auth.allow?("quote::topic", m.source, m.replyto))
318             quote, total = getquote(m.source, m.target.to_s, num)
319             if(quote)
320               @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
321             else
322               m.reply "quote not found!"
323             end
324           end
325         when (/^delquote\s+(\d+)$/)
326           num = $1.to_i
327           if(@bot.auth.allow?("quote::del", m.source, m.replyto))
328             if(delquote(m.target.to_s, num))
329               m.okay
330             else
331               m.reply "quote not found!"
332             end
333           end
334         when (/^searchquote\s+(.*)$/)
335           reg = $1
336           if(@bot.auth.allow?("quote::get", m.source, m.replyto))
337             quote, total = searchquote(m.source, m.target.to_s, reg)
338             if(quote)
339               m.reply "[#{quote.num}] #{quote.quote}"
340             else
341               m.reply "quote not found!"
342             end
343           end
344         when (/^countquote(?:\s+(.*))?$/)
345           reg = $1
346           if(@bot.auth.allow?("quote::get::count", m.source, m.replyto))
347             total = countquote(m.source, m.target.to_s, reg)
348             if(reg && reg.length > 0)
349               m.reply "#{total} quotes match: #{reg}"
350             else
351               m.reply "#{total} quotes"
352             end
353           end
354       end
355     end
356   end
357 end
358
359 plugin = QuotePlugin.new
360 plugin.register("quotes")
361 plugin.default_auth('other', false) # Prevent random people from editing other channels quote lists by default
362 plugin.default_auth('other::get', true) # But allow them to view them
363 plugin.default_auth('del', false) # Prevent random people from removing quotes