1 # GB: Ok, we *really* need to switch to db for this plugin too
3 Quote = Struct.new("Quote", :num, :date, :source, :quote)
5 class QuotePlugin < Plugin
9 Dir["#{@bot.botclass}/quotes/*"].each {|f|
10 next if File.directory?(f)
11 channel = File.basename(f)
12 @lists[channel] = Array.new if(!@lists.has_key?(channel))
14 if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/)
16 @lists[channel][num] = Quote.new(num, $2, $3, $4)
22 Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes"))
23 Dir.mkdir("#{@bot.botclass}/quotes/new") if(!FileTest.directory?("#{@bot.botclass}/quotes/new"))
24 @lists.each {|channel, quotes|
26 debug "Writing new quotefile for channel #{channel} ..."
27 File.open("#{@bot.botclass}/quotes/new/#{channel}", "w") {|file|
28 quotes.compact.each {|q|
29 file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
32 debug "Officializing quotefile for channel #{channel} ..."
33 File.rename("#{@bot.botclass}/quotes/new/#{channel}",
34 "#{@bot.botclass}/quotes/#{channel}")
36 error "failed to write quotefile for channel #{channel}!\n#{$!}"
37 error "#{e.class}: #{e}"
38 error e.backtrace.join("\n")
42 def addquote(source, channel, quote)
43 @lists[channel] = Array.new if(!@lists.has_key?(channel))
44 num = @lists[channel].length
45 @lists[channel][num] = Quote.new(num, Time.new, source, quote)
48 def getquote(source, channel, num=nil)
49 return nil unless(@lists.has_key?(channel))
50 return nil unless(@lists[channel].length > 0)
52 if(@lists[channel][num])
53 return @lists[channel][num], @lists[channel].length - 1
57 return @lists[channel].compact[rand(@lists[channel].nitems)],
58 @lists[channel].length - 1
61 def delquote(channel, num)
62 return false unless(@lists.has_key?(channel))
63 return false unless(@lists[channel].length > 0)
64 if(@lists[channel][num])
65 @lists[channel][num] = nil
66 @lists[channel].pop if num == @lists[channel].length - 1
71 def countquote(source, channel=nil, regexp=nil)
74 @lists.each_value {|l|
75 total += l.compact.length
79 return 0 unless(@lists.has_key?(channel))
80 return 0 unless(@lists[channel].length > 0)
82 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
84 matches = @lists[channel].compact
88 def searchquote(source, channel, regexp)
89 return nil unless(@lists.has_key?(channel))
90 return nil unless(@lists[channel].length > 0)
91 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
92 if(matches.length > 0)
93 return matches[rand(matches.length)], @lists[channel].length - 1
98 def help(plugin, topic="")
101 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"
103 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"
105 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"
107 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"
109 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"
111 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"
113 return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
115 return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
117 return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
121 return unless(m.kind_of? PrivMessage)
123 command = m.message.dup
124 if(m.address? && m.private?)
126 when (/^addquote\s+(#\S+)\s+(.*)/)
129 if(@bot.auth.allow?("addquote", m.source, m.replyto))
131 num = addquote(m.source, channel, quote)
132 m.reply "added the quote (##{num})"
135 when (/^getquote\s+(#\S+)$/)
137 if(@bot.auth.allow?("getquote", m.source, m.replyto))
138 quote, total = getquote(m.source, channel)
140 m.reply "[#{quote.num}] #{quote.quote}"
142 m.reply "quote not found!"
145 when (/^getquote\s+(#\S+)\s+(\d+)$/)
148 if(@bot.auth.allow?("getquote", m.source, m.replyto))
149 quote, total = getquote(m.source, channel, num)
151 m.reply "[#{quote.num}] #{quote.quote}"
153 m.reply "quote not found!"
156 when (/^whoquote\s+(#\S+)\s+(\d+)$/)
159 if(@bot.auth.allow?("getquote", m.source, m.replyto))
160 quote, total = getquote(m.source, channel, num)
162 m.reply "quote #{quote.num} added by #{quote.source}"
164 m.reply "quote not found!"
167 when (/^whenquote\s+(#\S+)\s+(\d+)$/)
170 if(@bot.auth.allow?("getquote", m.source, m.replyto))
171 quote, total = getquote(m.source, channel, num)
173 m.reply "quote #{quote.num} added on #{quote.date}"
175 m.reply "quote not found!"
178 when (/^topicquote\s+(#\S+)$/)
180 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
181 quote, total = getquote(m.source, channel)
183 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
185 m.reply "quote not found!"
188 when (/^topicquote\s+(#\S+)\s+(\d+)$/)
191 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
192 quote, total = getquote(m.source, channel, num)
194 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
196 m.reply "quote not found!"
199 when (/^delquote\s+(#\S+)\s+(\d+)$/)
202 if(@bot.auth.allow?("delquote", m.source, m.replyto))
203 if(delquote(channel, num))
206 m.reply "quote not found!"
209 when (/^searchquote\s+(#\S+)\s+(.*)$/)
212 if(@bot.auth.allow?("getquote", m.source, m.replyto))
213 quote, total = searchquote(m.source, channel, reg)
215 m.reply "[#{quote.num}] #{quote.quote}"
217 m.reply "quote not found!"
220 when (/^countquote$/)
221 if(@bot.auth.allow?("getquote", m.source, m.replyto))
222 total = countquote(m.source)
223 m.reply "#{total} quotes"
225 when (/^countquote\s+(#\S+)\s*(.*)$/)
228 if(@bot.auth.allow?("getquote", m.source, m.replyto))
229 total = countquote(m.source, channel, reg)
231 m.reply "#{total} quotes match: #{reg}"
233 m.reply "#{total} quotes"
237 elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, "")))
239 when (/^addquote\s+(.+)/)
240 if(@bot.auth.allow?("addquote", m.source, m.replyto))
241 num = addquote(m.source, m.target, $1)
242 m.reply "added the quote (##{num})"
245 if(@bot.auth.allow?("getquote", m.source, m.replyto))
246 quote, total = getquote(m.source, m.target)
248 m.reply "[#{quote.num}] #{quote.quote}"
250 m.reply "no quotes found!"
253 when (/^getquote\s+(\d+)$/)
255 if(@bot.auth.allow?("getquote", m.source, m.replyto))
256 quote, total = getquote(m.source, m.target, num)
258 m.reply "[#{quote.num}] #{quote.quote}"
260 m.reply "quote not found!"
263 when (/^whenquote\s+(\d+)$/)
265 if(@bot.auth.allow?("getquote", m.source, m.replyto))
266 quote, total = getquote(m.source, m.target, num)
268 m.reply "quote #{quote.num} added on #{quote.date}"
270 m.reply "quote not found!"
273 when (/^whoquote\s+(\d+)$/)
275 if(@bot.auth.allow?("getquote", m.source, m.replyto))
276 quote, total = getquote(m.source, m.target, num)
278 m.reply "quote #{quote.num} added by #{quote.source}"
280 m.reply "quote not found!"
283 when (/^topicquote$/)
284 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
285 quote, total = getquote(m.source, m.target)
287 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
289 m.reply "no quotes found!"
292 when (/^topicquote\s+(\d+)$/)
294 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
295 quote, total = getquote(m.source, m.target, num)
297 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
299 m.reply "quote not found!"
302 when (/^delquote\s+(\d+)$/)
304 if(@bot.auth.allow?("delquote", m.source, m.replyto))
305 if(delquote(m.target, num))
308 m.reply "quote not found!"
311 when (/^searchquote\s+(.*)$/)
313 if(@bot.auth.allow?("getquote", m.source, m.replyto))
314 quote, total = searchquote(m.source, m.target, reg)
316 m.reply "[#{quote.num}] #{quote.quote}"
318 m.reply "quote not found!"
321 when (/^countquote(?:\s+(.*))?$/)
323 if(@bot.auth.allow?("getquote", m.source, m.replyto))
324 total = countquote(m.source, m.target, reg)
325 if(reg && reg.length > 0)
326 m.reply "#{total} quotes match: #{reg}"
328 m.reply "#{total} quotes"
335 plugin = QuotePlugin.new
336 plugin.register("quotes")