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 channel = File.basename(f)
11 @lists[channel] = Array.new if(!@lists.has_key?(channel))
13 if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/)
15 @lists[channel][num] = Quote.new(num, $2, $3, $4)
21 Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes"))
22 Dir.mkdir("#{@bot.botclass}/quotes/new") if(!FileTest.directory?("#{@bot.botclass}/quotes/new"))
23 @lists.each {|channel, quotes|
25 debug "Writing new quotefile for channel #{channel} ..."
26 File.open("#{@bot.botclass}/quotes/new/#{channel}", "w") {|file|
27 quotes.compact.each {|q|
28 file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
31 debug "Officializing quotefile for channel #{channel} ..."
32 File.rename("#{@bot.botclass}/quotes/new/#{channel}",
33 "#{@bot.botclass}/quotes/#{channel}")
35 $stderr.puts "failed to write quotefile for channel #{channel}!\n#{$!}"
36 debug "#{e.class}: #{e}"
37 debug e.backtrace.join("\n")
41 def addquote(source, channel, quote)
42 @lists[channel] = Array.new if(!@lists.has_key?(channel))
43 num = @lists[channel].length
44 @lists[channel][num] = Quote.new(num, Time.new, source, quote)
47 def getquote(source, channel, num=nil)
48 return nil unless(@lists.has_key?(channel))
49 return nil unless(@lists[channel].length > 0)
51 if(@lists[channel][num])
52 return @lists[channel][num], @lists[channel].length - 1
56 return @lists[channel].compact[rand(@lists[channel].nitems)],
57 @lists[channel].length - 1
60 def delquote(channel, num)
61 return false unless(@lists.has_key?(channel))
62 return false unless(@lists[channel].length > 0)
63 if(@lists[channel][num])
64 @lists[channel][num] = nil
65 @lists[channel].pop if num == @lists[channel].length - 1
70 def countquote(source, channel=nil, regexp=nil)
73 @lists.each_value {|l|
74 total += l.compact.length
78 return 0 unless(@lists.has_key?(channel))
79 return 0 unless(@lists[channel].length > 0)
81 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
83 matches = @lists[channel].compact
87 def searchquote(source, channel, regexp)
88 return nil unless(@lists.has_key?(channel))
89 return nil unless(@lists[channel].length > 0)
90 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
91 if(matches.length > 0)
92 return matches[rand(matches.length)], @lists[channel].length - 1
97 def help(plugin, topic="")
100 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"
102 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"
104 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"
106 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"
108 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"
110 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"
112 return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
114 return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
116 return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
120 return unless(m.kind_of? PrivMessage)
122 command = m.message.dup
123 if(m.address? && m.private?)
125 when (/^addquote\s+(#\S+)\s+(.*)/)
128 if(@bot.auth.allow?("addquote", m.source, m.replyto))
130 num = addquote(m.source, channel, quote)
131 m.reply "added the quote (##{num})"
134 when (/^getquote\s+(#\S+)$/)
136 if(@bot.auth.allow?("getquote", m.source, m.replyto))
137 quote, total = getquote(m.source, channel)
139 m.reply "[#{quote.num}] #{quote.quote}"
141 m.reply "quote not found!"
144 when (/^getquote\s+(#\S+)\s+(\d+)$/)
147 if(@bot.auth.allow?("getquote", m.source, m.replyto))
148 quote, total = getquote(m.source, channel, num)
150 m.reply "[#{quote.num}] #{quote.quote}"
152 m.reply "quote not found!"
155 when (/^whoquote\s+(#\S+)\s+(\d+)$/)
158 if(@bot.auth.allow?("getquote", m.source, m.replyto))
159 quote, total = getquote(m.source, channel, num)
161 m.reply "quote #{quote.num} added by #{quote.source}"
163 m.reply "quote not found!"
166 when (/^whenquote\s+(#\S+)\s+(\d+)$/)
169 if(@bot.auth.allow?("getquote", m.source, m.replyto))
170 quote, total = getquote(m.source, channel, num)
172 m.reply "quote #{quote.num} added on #{quote.date}"
174 m.reply "quote not found!"
177 when (/^topicquote\s+(#\S+)$/)
179 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
180 quote, total = getquote(m.source, channel)
182 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
184 m.reply "quote not found!"
187 when (/^topicquote\s+(#\S+)\s+(\d+)$/)
190 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
191 quote, total = getquote(m.source, channel, num)
193 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
195 m.reply "quote not found!"
198 when (/^delquote\s+(#\S+)\s+(\d+)$/)
201 if(@bot.auth.allow?("delquote", m.source, m.replyto))
202 if(delquote(channel, num))
205 m.reply "quote not found!"
208 when (/^searchquote\s+(#\S+)\s+(.*)$/)
211 if(@bot.auth.allow?("getquote", m.source, m.replyto))
212 quote, total = searchquote(m.source, channel, reg)
214 m.reply "[#{quote.num}] #{quote.quote}"
216 m.reply "quote not found!"
219 when (/^countquote$/)
220 if(@bot.auth.allow?("getquote", m.source, m.replyto))
221 total = countquote(m.source)
222 m.reply "#{total} quotes"
224 when (/^countquote\s+(#\S+)\s*(.*)$/)
227 if(@bot.auth.allow?("getquote", m.source, m.replyto))
228 total = countquote(m.source, channel, reg)
230 m.reply "#{total} quotes match: #{reg}"
232 m.reply "#{total} quotes"
236 elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, "")))
238 when (/^addquote\s+(.+)/)
239 if(@bot.auth.allow?("addquote", m.source, m.replyto))
240 num = addquote(m.source, m.target, $1)
241 m.reply "added the quote (##{num})"
244 if(@bot.auth.allow?("getquote", m.source, m.replyto))
245 quote, total = getquote(m.source, m.target)
247 m.reply "[#{quote.num}] #{quote.quote}"
249 m.reply "no quotes found!"
252 when (/^getquote\s+(\d+)$/)
254 if(@bot.auth.allow?("getquote", m.source, m.replyto))
255 quote, total = getquote(m.source, m.target, num)
257 m.reply "[#{quote.num}] #{quote.quote}"
259 m.reply "quote not found!"
262 when (/^whenquote\s+(\d+)$/)
264 if(@bot.auth.allow?("getquote", m.source, m.replyto))
265 quote, total = getquote(m.source, m.target, num)
267 m.reply "quote #{quote.num} added on #{quote.date}"
269 m.reply "quote not found!"
272 when (/^whoquote\s+(\d+)$/)
274 if(@bot.auth.allow?("getquote", m.source, m.replyto))
275 quote, total = getquote(m.source, m.target, num)
277 m.reply "quote #{quote.num} added by #{quote.source}"
279 m.reply "quote not found!"
282 when (/^topicquote$/)
283 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
284 quote, total = getquote(m.source, m.target)
286 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
288 m.reply "no quotes found!"
291 when (/^topicquote\s+(\d+)$/)
293 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
294 quote, total = getquote(m.source, m.target, num)
296 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
298 m.reply "quote not found!"
301 when (/^delquote\s+(\d+)$/)
303 if(@bot.auth.allow?("delquote", m.source, m.replyto))
304 if(delquote(m.target, num))
307 m.reply "quote not found!"
310 when (/^searchquote\s+(.*)$/)
312 if(@bot.auth.allow?("getquote", m.source, m.replyto))
313 quote, total = searchquote(m.source, m.target, reg)
315 m.reply "[#{quote.num}] #{quote.quote}"
317 m.reply "quote not found!"
320 when (/^countquote(?:\s+(.*))?$/)
322 if(@bot.auth.allow?("getquote", m.source, m.replyto))
323 total = countquote(m.source, m.target, reg)
324 if(reg && reg.length > 0)
325 m.reply "#{total} quotes match: #{reg}"
327 m.reply "#{total} quotes"
334 plugin = QuotePlugin.new
335 plugin.register("quotes")