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")
58 def addquote(source, channel, quote)
59 @lists[channel] = Array.new if(!@lists.has_key?(channel))
60 num = @lists[channel].length
61 @lists[channel][num] = Quote.new(num, Time.new, source.fullform, quote)
62 @changed[channel] = true
66 def getquote(source, channel, num=nil)
67 return nil unless(@lists.has_key?(channel))
68 return nil unless(@lists[channel].length > 0)
70 if(@lists[channel][num])
71 return @lists[channel][num], @lists[channel].length - 1
75 return @lists[channel].compact[rand(@lists[channel].nitems)],
76 @lists[channel].length - 1
80 def delquote(channel, num)
81 return false unless(@lists.has_key?(channel))
82 return false unless(@lists[channel].length > 0)
83 if(@lists[channel][num])
84 @lists[channel][num] = nil
85 @lists[channel].pop if num == @lists[channel].length - 1
86 @changed[channel] = true
92 def countquote(source, channel=nil, regexp=nil)
95 @lists.each_value {|l|
96 total += l.compact.length
100 return 0 unless(@lists.has_key?(channel))
101 return 0 unless(@lists[channel].length > 0)
103 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
105 matches = @lists[channel].compact
107 return matches.length
110 def searchquote(source, channel, regexp)
111 return nil unless(@lists.has_key?(channel))
112 return nil unless(@lists[channel].length > 0)
113 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
114 if(matches.length > 0)
115 return matches[rand(matches.length)], @lists[channel].length - 1
121 def help(plugin, topic="")
124 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 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 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 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 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 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 return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
138 return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
140 return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
145 return unless(m.kind_of? PrivMessage)
147 command = m.message.dup
148 if(m.address? && m.private?)
150 when (/^addquote\s+(#\S+)\s+(.*)/)
153 if(@bot.auth.allow?("quote::other::add", m.source, m.replyto))
155 num = addquote(m.source, channel, quote)
156 m.reply "added the quote (##{num})"
159 when (/^getquote\s+(#\S+)$/)
161 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
162 quote, total = getquote(m.source, channel)
164 m.reply "[#{quote.num}] #{quote.quote}"
166 m.reply "quote not found!"
169 when (/^getquote\s+(#\S+)\s+(\d+)$/)
172 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
173 quote, total = getquote(m.source, channel, num)
175 m.reply "[#{quote.num}] #{quote.quote}"
177 m.reply "quote not found!"
180 when (/^whoquote\s+(#\S+)\s+(\d+)$/)
183 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
184 quote, total = getquote(m.source, channel, num)
186 m.reply "quote #{quote.num} added by #{quote.source}"
188 m.reply "quote not found!"
191 when (/^whenquote\s+(#\S+)\s+(\d+)$/)
194 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
195 quote, total = getquote(m.source, channel, num)
197 m.reply "quote #{quote.num} added on #{quote.date}"
199 m.reply "quote not found!"
202 when (/^topicquote\s+(#\S+)$/)
204 if(@bot.auth.allow?("quote::other::topic", m.source, m.replyto))
205 quote, total = getquote(m.source, channel)
207 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
209 m.reply "quote not found!"
212 when (/^topicquote\s+(#\S+)\s+(\d+)$/)
215 if(@bot.auth.allow?("quote::other::topic", m.source, m.replyto))
216 quote, total = getquote(m.source, channel, num)
218 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
220 m.reply "quote not found!"
223 when (/^delquote\s+(#\S+)\s+(\d+)$/)
226 if(@bot.auth.allow?("quote::other::del", m.source, m.replyto))
227 if(delquote(channel, num))
230 m.reply "quote not found!"
233 when (/^searchquote\s+(#\S+)\s+(.*)$/)
236 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
237 quote, total = searchquote(m.source, channel, reg)
239 m.reply "[#{quote.num}] #{quote.quote}"
241 m.reply "quote not found!"
244 when (/^countquote$/)
245 if(@bot.auth.allow?("quote::get::count", m.source, m.replyto))
246 total = countquote(m.source)
247 m.reply "#{total} quotes"
249 when (/^countquote\s+(#\S+)\s*(.*)$/)
252 if(@bot.auth.allow?("quote::other::get::count", m.source, m.replyto))
253 total = countquote(m.source, channel, reg)
255 m.reply "#{total} quotes match: #{reg}"
257 m.reply "#{total} quotes"
261 elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, "")))
263 when (/^addquote\s+(.+)/)
264 if(@bot.auth.allow?("quote::add", m.source, m.replyto))
265 num = addquote(m.source, m.target.to_s, $1)
266 m.reply "added the quote (##{num})"
269 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
270 quote, total = getquote(m.source, m.target.to_s)
272 m.reply "[#{quote.num}] #{quote.quote}"
274 m.reply "no quotes found!"
277 when (/^getquote\s+(\d+)$/)
279 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
280 quote, total = getquote(m.source, m.target.to_s, num)
282 m.reply "[#{quote.num}] #{quote.quote}"
284 m.reply "quote not found!"
287 when (/^whenquote\s+(\d+)$/)
289 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
290 quote, total = getquote(m.source, m.target.to_s, num)
292 m.reply "quote #{quote.num} added on #{quote.date}"
294 m.reply "quote not found!"
297 when (/^whoquote\s+(\d+)$/)
299 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
300 quote, total = getquote(m.source, m.target.to_s, num)
302 m.reply "quote #{quote.num} added by #{quote.source}"
304 m.reply "quote not found!"
307 when (/^topicquote$/)
308 if(@bot.auth.allow?("quote::topic", m.source, m.replyto))
309 quote, total = getquote(m.source, m.target.to_s)
311 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
313 m.reply "no quotes found!"
316 when (/^topicquote\s+(\d+)$/)
318 if(@bot.auth.allow?("quote::topic", m.source, m.replyto))
319 quote, total = getquote(m.source, m.target.to_s, num)
321 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
323 m.reply "quote not found!"
326 when (/^delquote\s+(\d+)$/)
328 if(@bot.auth.allow?("quote::del", m.source, m.replyto))
329 if(delquote(m.target.to_s, num))
332 m.reply "quote not found!"
335 when (/^searchquote\s+(.*)$/)
337 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
338 quote, total = searchquote(m.source, m.target.to_s, reg)
340 m.reply "[#{quote.num}] #{quote.quote}"
342 m.reply "quote not found!"
345 when (/^countquote(?:\s+(.*))?$/)
347 if(@bot.auth.allow?("quote::get::count", m.source, m.replyto))
348 total = countquote(m.source, m.target.to_s, reg)
349 if(reg && reg.length > 0)
350 m.reply "#{total} quotes match: #{reg}"
352 m.reply "#{total} quotes"
360 plugin = QuotePlugin.new
361 plugin.register("quotes")
362 plugin.default_auth('other', false) # Prevent random people from editing other channels quote lists by default
363 plugin.default_auth('other::get', true) # But allow them to view them
364 plugin.default_auth('del', false) # Prevent random people from removing quotes