4 # :title: Quotes plugin
6 # TODO:: use message mapper instead of multiple ifs
9 define_structure :Quote, :num, :date, :source, :quote
11 class QuotePlugin < Plugin
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))
21 if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/)
23 @lists[channel][num] = Quote.new(num, $2, $3, $4)
26 @changed[channel] = false
31 Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes"))
32 @lists.each {|channel, quotes|
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}"
41 @changed[channel] = false
43 debug "Not writing quotefile for channel #{channel} (unchanged)"
46 error "failed to write quotefile for channel #{channel}!\n#{$!}"
47 error "#{e.class}: #{e}"
48 error e.backtrace.join("\n")
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
67 def getquote(source, channel, num=nil)
68 return nil unless(@lists.has_key?(channel))
69 return nil unless(@lists[channel].length > 0)
71 if(@lists[channel][num])
72 return @lists[channel][num], @lists[channel].length - 1
76 return @lists[channel].compact[rand(@lists[channel].nitems)],
77 @lists[channel].length - 1
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
93 def countquote(source, channel=nil, regexp=nil)
96 @lists.each_value {|l|
97 total += l.compact.length
101 return 0 unless(@lists.has_key?(channel))
102 return 0 unless(@lists[channel].length > 0)
104 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
106 matches = @lists[channel].compact
108 return matches.length
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
122 def help(plugin, topic="")
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"
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"
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"
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"
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"
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"
137 return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
139 return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
141 return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
146 return unless(m.kind_of? PrivMessage)
148 command = m.message.dup
149 if(m.address? && m.private?)
151 when (/^addquote\s+(#\S+)\s+(.*)/)
154 if(@bot.auth.allow?("quote::other::add", m.source, m.replyto))
156 num = addquote(m.source, channel, quote)
157 m.reply "added the quote (##{num})"
160 when (/^getquote\s+(#\S+)$/)
162 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
163 quote, total = getquote(m.source, channel)
165 m.reply "[#{quote.num}] #{quote.quote}"
167 m.reply "quote not found!"
170 when (/^getquote\s+(#\S+)\s+(\d+)$/)
173 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
174 quote, total = getquote(m.source, channel, num)
176 m.reply "[#{quote.num}] #{quote.quote}"
178 m.reply "quote not found!"
181 when (/^whoquote\s+(#\S+)\s+(\d+)$/)
184 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
185 quote, total = getquote(m.source, channel, num)
187 m.reply "quote #{quote.num} added by #{quote.source}"
189 m.reply "quote not found!"
192 when (/^whenquote\s+(#\S+)\s+(\d+)$/)
195 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
196 quote, total = getquote(m.source, channel, num)
198 m.reply "quote #{quote.num} added on #{quote.date}"
200 m.reply "quote not found!"
203 when (/^topicquote\s+(#\S+)$/)
205 if(@bot.auth.allow?("quote::other::topic", m.source, m.replyto))
206 quote, total = getquote(m.source, channel)
208 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
210 m.reply "quote not found!"
213 when (/^topicquote\s+(#\S+)\s+(\d+)$/)
216 if(@bot.auth.allow?("quote::other::topic", m.source, m.replyto))
217 quote, total = getquote(m.source, channel, num)
219 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
221 m.reply "quote not found!"
224 when (/^delquote\s+(#\S+)\s+(\d+)$/)
227 if(@bot.auth.allow?("quote::other::del", m.source, m.replyto))
228 if(delquote(channel, num))
231 m.reply "quote not found!"
234 when (/^searchquote\s+(#\S+)\s+(.*)$/)
237 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
238 quote, total = searchquote(m.source, channel, reg)
240 m.reply "[#{quote.num}] #{quote.quote}"
242 m.reply "quote not found!"
245 when (/^countquote$/)
246 if(@bot.auth.allow?("quote::get::count", m.source, m.replyto))
247 total = countquote(m.source)
248 m.reply "#{total} quotes"
250 when (/^countquote\s+(#\S+)\s*(.*)$/)
253 if(@bot.auth.allow?("quote::other::get::count", m.source, m.replyto))
254 total = countquote(m.source, channel, reg)
256 m.reply "#{total} quotes match: #{reg}"
258 m.reply "#{total} quotes"
262 elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, "")))
264 when (/^addquote\s+(.+)/)
265 if(@bot.auth.allow?("quote::add", m.source, m.replyto))
266 num = addquote(m.source, m.target.to_s, $1)
267 m.reply "added the quote (##{num})"
270 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
271 quote, total = getquote(m.source, m.target.to_s)
273 m.reply "[#{quote.num}] #{quote.quote}"
275 m.reply "no quotes found!"
278 when (/^getquote\s+(\d+)$/)
280 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
281 quote, total = getquote(m.source, m.target.to_s, num)
283 m.reply "[#{quote.num}] #{quote.quote}"
285 m.reply "quote not found!"
288 when (/^whenquote\s+(\d+)$/)
290 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
291 quote, total = getquote(m.source, m.target.to_s, num)
293 m.reply "quote #{quote.num} added on #{quote.date}"
295 m.reply "quote not found!"
298 when (/^whoquote\s+(\d+)$/)
300 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
301 quote, total = getquote(m.source, m.target.to_s, num)
303 m.reply "quote #{quote.num} added by #{quote.source}"
305 m.reply "quote not found!"
308 when (/^topicquote$/)
309 if(@bot.auth.allow?("quote::topic", m.source, m.replyto))
310 quote, total = getquote(m.source, m.target.to_s)
312 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
314 m.reply "no quotes found!"
317 when (/^topicquote\s+(\d+)$/)
319 if(@bot.auth.allow?("quote::topic", m.source, m.replyto))
320 quote, total = getquote(m.source, m.target.to_s, num)
322 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
324 m.reply "quote not found!"
327 when (/^delquote\s+(\d+)$/)
329 if(@bot.auth.allow?("quote::del", m.source, m.replyto))
330 if(delquote(m.target.to_s, num))
333 m.reply "quote not found!"
336 when (/^searchquote\s+(.*)$/)
338 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
339 quote, total = searchquote(m.source, m.target.to_s, reg)
341 m.reply "[#{quote.num}] #{quote.quote}"
343 m.reply "quote not found!"
346 when (/^countquote(?:\s+(.*))?$/)
348 if(@bot.auth.allow?("quote::get::count", 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}"
353 m.reply "#{total} quotes"
361 plugin = QuotePlugin.new
362 plugin.register("quotes")
363 plugin.default_auth('other', false) # Prevent random people from editing other channels quote lists by default
364 plugin.default_auth('other::get', true) # But allow them to view them
365 plugin.default_auth('del', false) # Prevent random people from removing quotes