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 command = m.message.dup
147 if(m.address? && m.private?)
149 when (/^addquote\s+(#\S+)\s+(.*)/)
152 if(@bot.auth.allow?("quote::other::add", m.source, m.replyto))
154 num = addquote(m.source, channel, quote)
155 m.reply "added the quote (##{num})"
158 when (/^getquote\s+(#\S+)$/)
160 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
161 quote, total = getquote(m.source, channel)
163 m.reply "[#{quote.num}] #{quote.quote}"
165 m.reply "quote not found!"
168 when (/^getquote\s+(#\S+)\s+(\d+)$/)
171 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
172 quote, total = getquote(m.source, channel, num)
174 m.reply "[#{quote.num}] #{quote.quote}"
176 m.reply "quote not found!"
179 when (/^whoquote\s+(#\S+)\s+(\d+)$/)
182 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
183 quote, total = getquote(m.source, channel, num)
185 m.reply "quote #{quote.num} added by #{quote.source}"
187 m.reply "quote not found!"
190 when (/^whenquote\s+(#\S+)\s+(\d+)$/)
193 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
194 quote, total = getquote(m.source, channel, num)
196 m.reply "quote #{quote.num} added on #{quote.date}"
198 m.reply "quote not found!"
201 when (/^topicquote\s+(#\S+)$/)
203 if(@bot.auth.allow?("quote::other::topic", m.source, m.replyto))
204 quote, total = getquote(m.source, channel)
206 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
208 m.reply "quote not found!"
211 when (/^topicquote\s+(#\S+)\s+(\d+)$/)
214 if(@bot.auth.allow?("quote::other::topic", m.source, m.replyto))
215 quote, total = getquote(m.source, channel, num)
217 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
219 m.reply "quote not found!"
222 when (/^delquote\s+(#\S+)\s+(\d+)$/)
225 if(@bot.auth.allow?("quote::other::del", m.source, m.replyto))
226 if(delquote(channel, num))
229 m.reply "quote not found!"
232 when (/^searchquote\s+(#\S+)\s+(.*)$/)
235 if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
236 quote, total = searchquote(m.source, channel, reg)
238 m.reply "[#{quote.num}] #{quote.quote}"
240 m.reply "quote not found!"
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"
248 when (/^countquote\s+(#\S+)\s*(.*)$/)
251 if(@bot.auth.allow?("quote::other::get::count", m.source, m.replyto))
252 total = countquote(m.source, channel, reg)
254 m.reply "#{total} quotes match: #{reg}"
256 m.reply "#{total} quotes"
260 elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, "")))
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})"
268 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
269 quote, total = getquote(m.source, m.target.to_s)
271 m.reply "[#{quote.num}] #{quote.quote}"
273 m.reply "no quotes found!"
276 when (/^getquote\s+(\d+)$/)
278 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
279 quote, total = getquote(m.source, m.target.to_s, num)
281 m.reply "[#{quote.num}] #{quote.quote}"
283 m.reply "quote not found!"
286 when (/^whenquote\s+(\d+)$/)
288 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
289 quote, total = getquote(m.source, m.target.to_s, num)
291 m.reply "quote #{quote.num} added on #{quote.date}"
293 m.reply "quote not found!"
296 when (/^whoquote\s+(\d+)$/)
298 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
299 quote, total = getquote(m.source, m.target.to_s, num)
301 m.reply "quote #{quote.num} added by #{quote.source}"
303 m.reply "quote not found!"
306 when (/^topicquote$/)
307 if(@bot.auth.allow?("quote::topic", m.source, m.replyto))
308 quote, total = getquote(m.source, m.target.to_s)
310 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
312 m.reply "no quotes found!"
315 when (/^topicquote\s+(\d+)$/)
317 if(@bot.auth.allow?("quote::topic", m.source, m.replyto))
318 quote, total = getquote(m.source, m.target.to_s, num)
320 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
322 m.reply "quote not found!"
325 when (/^delquote\s+(\d+)$/)
327 if(@bot.auth.allow?("quote::del", m.source, m.replyto))
328 if(delquote(m.target.to_s, num))
331 m.reply "quote not found!"
334 when (/^searchquote\s+(.*)$/)
336 if(@bot.auth.allow?("quote::get", m.source, m.replyto))
337 quote, total = searchquote(m.source, m.target.to_s, reg)
339 m.reply "[#{quote.num}] #{quote.quote}"
341 m.reply "quote not found!"
344 when (/^countquote(?:\s+(.*))?$/)
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}"
351 m.reply "#{total} quotes"
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