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 @lists.each {|channel, quotes|
25 debug "Writing new quotefile for channel #{channel} ..."
26 Utils.safe_save("#{@bot.botclass}/quotes/#{channel}") {|file|
27 quotes.compact.each {|q|
28 file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
32 error "failed to write quotefile for channel #{channel}!\n#{$!}"
33 error "#{e.class}: #{e}"
34 error e.backtrace.join("\n")
38 def addquote(source, channel, quote)
39 @lists[channel] = Array.new if(!@lists.has_key?(channel))
40 num = @lists[channel].length
41 @lists[channel][num] = Quote.new(num, Time.new, source, quote)
44 def getquote(source, channel, num=nil)
45 return nil unless(@lists.has_key?(channel))
46 return nil unless(@lists[channel].length > 0)
48 if(@lists[channel][num])
49 return @lists[channel][num], @lists[channel].length - 1
53 return @lists[channel].compact[rand(@lists[channel].nitems)],
54 @lists[channel].length - 1
57 def delquote(channel, num)
58 return false unless(@lists.has_key?(channel))
59 return false unless(@lists[channel].length > 0)
60 if(@lists[channel][num])
61 @lists[channel][num] = nil
62 @lists[channel].pop if num == @lists[channel].length - 1
67 def countquote(source, channel=nil, regexp=nil)
70 @lists.each_value {|l|
71 total += l.compact.length
75 return 0 unless(@lists.has_key?(channel))
76 return 0 unless(@lists[channel].length > 0)
78 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
80 matches = @lists[channel].compact
84 def searchquote(source, channel, regexp)
85 return nil unless(@lists.has_key?(channel))
86 return nil unless(@lists[channel].length > 0)
87 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
88 if(matches.length > 0)
89 return matches[rand(matches.length)], @lists[channel].length - 1
94 def help(plugin, topic="")
97 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"
99 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"
101 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"
103 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"
105 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"
107 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"
109 return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
111 return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
113 return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
117 return unless(m.kind_of? PrivMessage)
119 command = m.message.dup
120 if(m.address? && m.private?)
122 when (/^addquote\s+(#\S+)\s+(.*)/)
125 if(@bot.auth.allow?("addquote", m.source, m.replyto))
127 num = addquote(m.source, channel, quote)
128 m.reply "added the quote (##{num})"
131 when (/^getquote\s+(#\S+)$/)
133 if(@bot.auth.allow?("getquote", m.source, m.replyto))
134 quote, total = getquote(m.source, channel)
136 m.reply "[#{quote.num}] #{quote.quote}"
138 m.reply "quote not found!"
141 when (/^getquote\s+(#\S+)\s+(\d+)$/)
144 if(@bot.auth.allow?("getquote", m.source, m.replyto))
145 quote, total = getquote(m.source, channel, num)
147 m.reply "[#{quote.num}] #{quote.quote}"
149 m.reply "quote not found!"
152 when (/^whoquote\s+(#\S+)\s+(\d+)$/)
155 if(@bot.auth.allow?("getquote", m.source, m.replyto))
156 quote, total = getquote(m.source, channel, num)
158 m.reply "quote #{quote.num} added by #{quote.source}"
160 m.reply "quote not found!"
163 when (/^whenquote\s+(#\S+)\s+(\d+)$/)
166 if(@bot.auth.allow?("getquote", m.source, m.replyto))
167 quote, total = getquote(m.source, channel, num)
169 m.reply "quote #{quote.num} added on #{quote.date}"
171 m.reply "quote not found!"
174 when (/^topicquote\s+(#\S+)$/)
176 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
177 quote, total = getquote(m.source, channel)
179 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
181 m.reply "quote not found!"
184 when (/^topicquote\s+(#\S+)\s+(\d+)$/)
187 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
188 quote, total = getquote(m.source, channel, num)
190 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
192 m.reply "quote not found!"
195 when (/^delquote\s+(#\S+)\s+(\d+)$/)
198 if(@bot.auth.allow?("delquote", m.source, m.replyto))
199 if(delquote(channel, num))
202 m.reply "quote not found!"
205 when (/^searchquote\s+(#\S+)\s+(.*)$/)
208 if(@bot.auth.allow?("getquote", m.source, m.replyto))
209 quote, total = searchquote(m.source, channel, reg)
211 m.reply "[#{quote.num}] #{quote.quote}"
213 m.reply "quote not found!"
216 when (/^countquote$/)
217 if(@bot.auth.allow?("getquote", m.source, m.replyto))
218 total = countquote(m.source)
219 m.reply "#{total} quotes"
221 when (/^countquote\s+(#\S+)\s*(.*)$/)
224 if(@bot.auth.allow?("getquote", m.source, m.replyto))
225 total = countquote(m.source, channel, reg)
227 m.reply "#{total} quotes match: #{reg}"
229 m.reply "#{total} quotes"
233 elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, "")))
235 when (/^addquote\s+(.+)/)
236 if(@bot.auth.allow?("addquote", m.source, m.replyto))
237 num = addquote(m.source, m.target.to_s, $1)
238 m.reply "added the quote (##{num})"
241 if(@bot.auth.allow?("getquote", m.source, m.replyto))
242 quote, total = getquote(m.source, m.target.to_s)
244 m.reply "[#{quote.num}] #{quote.quote}"
246 m.reply "no quotes found!"
249 when (/^getquote\s+(\d+)$/)
251 if(@bot.auth.allow?("getquote", m.source, m.replyto))
252 quote, total = getquote(m.source, m.target.to_s, num)
254 m.reply "[#{quote.num}] #{quote.quote}"
256 m.reply "quote not found!"
259 when (/^whenquote\s+(\d+)$/)
261 if(@bot.auth.allow?("getquote", m.source, m.replyto))
262 quote, total = getquote(m.source, m.target.to_s, num)
264 m.reply "quote #{quote.num} added on #{quote.date}"
266 m.reply "quote not found!"
269 when (/^whoquote\s+(\d+)$/)
271 if(@bot.auth.allow?("getquote", m.source, m.replyto))
272 quote, total = getquote(m.source, m.target.to_s, num)
274 m.reply "quote #{quote.num} added by #{quote.source}"
276 m.reply "quote not found!"
279 when (/^topicquote$/)
280 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
281 quote, total = getquote(m.source, m.target.to_s)
283 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
285 m.reply "no quotes found!"
288 when (/^topicquote\s+(\d+)$/)
290 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
291 quote, total = getquote(m.source, m.target.to_s, num)
293 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
295 m.reply "quote not found!"
298 when (/^delquote\s+(\d+)$/)
300 if(@bot.auth.allow?("delquote", m.source, m.replyto))
301 if(delquote(m.target.to_s, num))
304 m.reply "quote not found!"
307 when (/^searchquote\s+(.*)$/)
309 if(@bot.auth.allow?("getquote", m.source, m.replyto))
310 quote, total = searchquote(m.source, m.target.to_s, reg)
312 m.reply "[#{quote.num}] #{quote.quote}"
314 m.reply "quote not found!"
317 when (/^countquote(?:\s+(.*))?$/)
319 if(@bot.auth.allow?("getquote", m.source, m.replyto))
320 total = countquote(m.source, m.target.to_s, reg)
321 if(reg && reg.length > 0)
322 m.reply "#{total} quotes match: #{reg}"
324 m.reply "#{total} quotes"
331 plugin = QuotePlugin.new
332 plugin.register("quotes")