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
10 Dir["#{@bot.botclass}/quotes/*"].each {|f|
11 next if File.directory?(f)
12 channel = File.basename(f)
13 @lists[channel] = Array.new if(!@lists.has_key?(channel))
15 if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/)
17 @lists[channel][num] = Quote.new(num, $2, $3, $4)
20 @changed[channel] = false
24 Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes"))
25 @lists.each {|channel, quotes|
28 debug "Writing new quotefile for channel #{channel} ..."
29 Utils.safe_save("#{@bot.botclass}/quotes/#{channel}") {|file|
30 quotes.compact.each {|q|
31 file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
34 @changed[channel] = false
36 debug "Not writing quotefile for channel #{channel} (unchanged)"
39 error "failed to write quotefile for channel #{channel}!\n#{$!}"
40 error "#{e.class}: #{e}"
41 error e.backtrace.join("\n")
49 def addquote(source, channel, quote)
50 @lists[channel] = Array.new if(!@lists.has_key?(channel))
51 num = @lists[channel].length
52 @lists[channel][num] = Quote.new(num, Time.new, source.fullform, quote)
53 @changed[channel] = true
56 def getquote(source, channel, num=nil)
57 return nil unless(@lists.has_key?(channel))
58 return nil unless(@lists[channel].length > 0)
60 if(@lists[channel][num])
61 return @lists[channel][num], @lists[channel].length - 1
65 return @lists[channel].compact[rand(@lists[channel].nitems)],
66 @lists[channel].length - 1
69 def delquote(channel, num)
70 return false unless(@lists.has_key?(channel))
71 return false unless(@lists[channel].length > 0)
72 if(@lists[channel][num])
73 @lists[channel][num] = nil
74 @lists[channel].pop if num == @lists[channel].length - 1
75 @changed[channel] = true
80 def countquote(source, channel=nil, regexp=nil)
83 @lists.each_value {|l|
84 total += l.compact.length
88 return 0 unless(@lists.has_key?(channel))
89 return 0 unless(@lists[channel].length > 0)
91 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
93 matches = @lists[channel].compact
97 def searchquote(source, channel, regexp)
98 return nil unless(@lists.has_key?(channel))
99 return nil unless(@lists[channel].length > 0)
100 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
101 if(matches.length > 0)
102 return matches[rand(matches.length)], @lists[channel].length - 1
107 def help(plugin, topic="")
110 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"
112 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"
114 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"
116 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"
118 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"
120 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"
122 return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
124 return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
126 return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
130 return unless(m.kind_of? PrivMessage)
132 command = m.message.dup
133 if(m.address? && m.private?)
135 when (/^addquote\s+(#\S+)\s+(.*)/)
138 if(@bot.auth.allow?("addquote", m.source, m.replyto))
140 num = addquote(m.source, channel, quote)
141 m.reply "added the quote (##{num})"
144 when (/^getquote\s+(#\S+)$/)
146 if(@bot.auth.allow?("getquote", m.source, m.replyto))
147 quote, total = getquote(m.source, channel)
149 m.reply "[#{quote.num}] #{quote.quote}"
151 m.reply "quote not found!"
154 when (/^getquote\s+(#\S+)\s+(\d+)$/)
157 if(@bot.auth.allow?("getquote", m.source, m.replyto))
158 quote, total = getquote(m.source, channel, num)
160 m.reply "[#{quote.num}] #{quote.quote}"
162 m.reply "quote not found!"
165 when (/^whoquote\s+(#\S+)\s+(\d+)$/)
168 if(@bot.auth.allow?("getquote", m.source, m.replyto))
169 quote, total = getquote(m.source, channel, num)
171 m.reply "quote #{quote.num} added by #{quote.source}"
173 m.reply "quote not found!"
176 when (/^whenquote\s+(#\S+)\s+(\d+)$/)
179 if(@bot.auth.allow?("getquote", m.source, m.replyto))
180 quote, total = getquote(m.source, channel, num)
182 m.reply "quote #{quote.num} added on #{quote.date}"
184 m.reply "quote not found!"
187 when (/^topicquote\s+(#\S+)$/)
189 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
190 quote, total = getquote(m.source, channel)
192 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
194 m.reply "quote not found!"
197 when (/^topicquote\s+(#\S+)\s+(\d+)$/)
200 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
201 quote, total = getquote(m.source, channel, num)
203 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
205 m.reply "quote not found!"
208 when (/^delquote\s+(#\S+)\s+(\d+)$/)
211 if(@bot.auth.allow?("delquote", m.source, m.replyto))
212 if(delquote(channel, num))
215 m.reply "quote not found!"
218 when (/^searchquote\s+(#\S+)\s+(.*)$/)
221 if(@bot.auth.allow?("getquote", m.source, m.replyto))
222 quote, total = searchquote(m.source, channel, reg)
224 m.reply "[#{quote.num}] #{quote.quote}"
226 m.reply "quote not found!"
229 when (/^countquote$/)
230 if(@bot.auth.allow?("getquote", m.source, m.replyto))
231 total = countquote(m.source)
232 m.reply "#{total} quotes"
234 when (/^countquote\s+(#\S+)\s*(.*)$/)
237 if(@bot.auth.allow?("getquote", m.source, m.replyto))
238 total = countquote(m.source, channel, reg)
240 m.reply "#{total} quotes match: #{reg}"
242 m.reply "#{total} quotes"
246 elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, "")))
248 when (/^addquote\s+(.+)/)
249 if(@bot.auth.allow?("addquote", m.source, m.replyto))
250 num = addquote(m.source, m.target.to_s, $1)
251 m.reply "added the quote (##{num})"
254 if(@bot.auth.allow?("getquote", m.source, m.replyto))
255 quote, total = getquote(m.source, m.target.to_s)
257 m.reply "[#{quote.num}] #{quote.quote}"
259 m.reply "no quotes found!"
262 when (/^getquote\s+(\d+)$/)
264 if(@bot.auth.allow?("getquote", m.source, m.replyto))
265 quote, total = getquote(m.source, m.target.to_s, num)
267 m.reply "[#{quote.num}] #{quote.quote}"
269 m.reply "quote not found!"
272 when (/^whenquote\s+(\d+)$/)
274 if(@bot.auth.allow?("getquote", m.source, m.replyto))
275 quote, total = getquote(m.source, m.target.to_s, num)
277 m.reply "quote #{quote.num} added on #{quote.date}"
279 m.reply "quote not found!"
282 when (/^whoquote\s+(\d+)$/)
284 if(@bot.auth.allow?("getquote", m.source, m.replyto))
285 quote, total = getquote(m.source, m.target.to_s, num)
287 m.reply "quote #{quote.num} added by #{quote.source}"
289 m.reply "quote not found!"
292 when (/^topicquote$/)
293 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
294 quote, total = getquote(m.source, m.target.to_s)
296 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
298 m.reply "no quotes found!"
301 when (/^topicquote\s+(\d+)$/)
303 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
304 quote, total = getquote(m.source, m.target.to_s, num)
306 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
308 m.reply "quote not found!"
311 when (/^delquote\s+(\d+)$/)
313 if(@bot.auth.allow?("delquote", m.source, m.replyto))
314 if(delquote(m.target.to_s, num))
317 m.reply "quote not found!"
320 when (/^searchquote\s+(.*)$/)
322 if(@bot.auth.allow?("getquote", m.source, m.replyto))
323 quote, total = searchquote(m.source, m.target.to_s, reg)
325 m.reply "[#{quote.num}] #{quote.quote}"
327 m.reply "quote not found!"
330 when (/^countquote(?:\s+(.*))?$/)
332 if(@bot.auth.allow?("getquote", m.source, m.replyto))
333 total = countquote(m.source, m.target.to_s, reg)
334 if(reg && reg.length > 0)
335 m.reply "#{total} quotes match: #{reg}"
337 m.reply "#{total} quotes"
344 plugin = QuotePlugin.new
345 plugin.register("quotes")