1 Quote = Struct.new("Quote", "num", "date", "source", "quote")
3 class QuotePlugin < Plugin
7 Dir["#{@bot.botclass}/quotes/*"].each {|f|
8 channel = File.basename(f)
9 @lists[channel] = Array.new if(!@lists.has_key?(channel))
11 if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/)
13 @lists[channel][num] = Quote.new(num, $2, $3, $4)
19 Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes"))
20 @lists.each {|channel, quotes|
21 File.open("#{@bot.botclass}/quotes/#{channel}", "w") {|file|
22 quotes.compact.each {|q|
23 file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
28 def addquote(source, channel, quote)
29 @lists[channel] = Array.new if(!@lists.has_key?(channel))
30 num = @lists[channel].length
31 @lists[channel][num] = Quote.new(num, Time.new, source, quote)
34 def getquote(source, channel, num=nil)
35 return nil unless(@lists.has_key?(channel))
36 return nil unless(@lists[channel].length > 0)
38 if(@lists[channel][num])
39 return @lists[channel][num], @lists[channel].length - 1
43 return @lists[channel].compact[rand(@lists[channel].nitems)],
44 @lists[channel].length - 1
47 def delquote(channel, num)
48 return false unless(@lists.has_key?(channel))
49 return false unless(@lists[channel].length > 0)
50 if(@lists[channel][num])
51 @lists[channel][num] = nil
52 @lists[channel].pop if num == @lists[channel].length - 1
57 def countquote(source, channel=nil, regexp=nil)
60 @lists.each_value {|l|
61 total += l.compact.length
65 return 0 unless(@lists.has_key?(channel))
66 return 0 unless(@lists[channel].length > 0)
68 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
70 matches = @lists[channel].compact
74 def searchquote(source, channel, regexp)
75 return nil unless(@lists.has_key?(channel))
76 return nil unless(@lists[channel].length > 0)
77 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
78 if(matches.length > 0)
79 return matches[rand(matches.length)], @lists[channel].length - 1
84 def help(plugin, topic="")
87 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"
89 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"
91 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"
93 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"
95 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"
97 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"
99 return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
101 return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
103 return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
107 return unless(m.kind_of? PrivMessage)
109 command = m.message.dup
110 if(m.address? && m.private?)
112 when (/^addquote\s+(#\S+)\s+(.*)/)
115 if(@bot.auth.allow?("addquote", m.source, m.replyto))
117 num = addquote(m.source, channel, quote)
118 m.reply "added the quote (##{num})"
121 when (/^getquote\s+(#\S+)$/)
123 if(@bot.auth.allow?("getquote", m.source, m.replyto))
124 quote, total = getquote(m.source, channel)
126 m.reply "[#{quote.num}] #{quote.quote}"
128 m.reply "quote not found!"
131 when (/^getquote\s+(#\S+)\s+(\d+)$/)
134 if(@bot.auth.allow?("getquote", m.source, m.replyto))
135 quote, total = getquote(m.source, channel, num)
137 m.reply "[#{quote.num}] #{quote.quote}"
139 m.reply "quote not found!"
142 when (/^whoquote\s+(#\S+)\s+(\d+)$/)
145 if(@bot.auth.allow?("getquote", m.source, m.replyto))
146 quote, total = getquote(m.source, channel, num)
148 m.reply "quote #{quote.num} added by #{quote.source}"
150 m.reply "quote not found!"
153 when (/^whenquote\s+(#\S+)\s+(\d+)$/)
156 if(@bot.auth.allow?("getquote", m.source, m.replyto))
157 quote, total = getquote(m.source, channel, num)
159 m.reply "quote #{quote.num} added on #{quote.date}"
161 m.reply "quote not found!"
164 when (/^topicquote\s+(#\S+)$/)
166 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
167 quote, total = getquote(m.source, channel)
169 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
171 m.reply "quote not found!"
174 when (/^topicquote\s+(#\S+)\s+(\d+)$/)
177 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
178 quote, total = getquote(m.source, channel, num)
180 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
182 m.reply "quote not found!"
185 when (/^delquote\s+(#\S+)\s+(\d+)$/)
188 if(@bot.auth.allow?("delquote", m.source, m.replyto))
189 if(delquote(channel, num))
192 m.reply "quote not found!"
195 when (/^searchquote\s+(#\S+)\s+(.*)$/)
198 if(@bot.auth.allow?("getquote", m.source, m.replyto))
199 quote, total = searchquote(m.source, channel, reg)
201 m.reply "[#{quote.num}] #{quote.quote}"
203 m.reply "quote not found!"
206 when (/^countquote$/)
207 if(@bot.auth.allow?("getquote", m.source, m.replyto))
208 total = countquote(m.source)
209 m.reply "#{total} quotes"
211 when (/^countquote\s+(#\S+)\s*(.*)$/)
214 if(@bot.auth.allow?("getquote", m.source, m.replyto))
215 total = countquote(m.source, channel, reg)
217 m.reply "#{total} quotes match: #{reg}"
219 m.reply "#{total} quotes"
223 elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, "")))
225 when (/^addquote\s+(.+)/)
226 if(@bot.auth.allow?("addquote", m.source, m.replyto))
227 num = addquote(m.source, m.target, $1)
228 m.reply "added the quote (##{num})"
231 if(@bot.auth.allow?("getquote", m.source, m.replyto))
232 quote, total = getquote(m.source, m.target)
234 m.reply "[#{quote.num}] #{quote.quote}"
236 m.reply "no quotes found!"
239 when (/^getquote\s+(\d+)$/)
241 if(@bot.auth.allow?("getquote", m.source, m.replyto))
242 quote, total = getquote(m.source, m.target, num)
244 m.reply "[#{quote.num}] #{quote.quote}"
246 m.reply "quote not found!"
249 when (/^whenquote\s+(\d+)$/)
251 if(@bot.auth.allow?("getquote", m.source, m.replyto))
252 quote, total = getquote(m.source, m.target, num)
254 m.reply "quote #{quote.num} added on #{quote.date}"
256 m.reply "quote not found!"
259 when (/^whoquote\s+(\d+)$/)
261 if(@bot.auth.allow?("getquote", m.source, m.replyto))
262 quote, total = getquote(m.source, m.target, num)
264 m.reply "quote #{quote.num} added by #{quote.source}"
266 m.reply "quote not found!"
269 when (/^topicquote$/)
270 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
271 quote, total = getquote(m.source, m.target)
273 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
275 m.reply "no quotes found!"
278 when (/^topicquote\s+(\d+)$/)
280 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
281 quote, total = getquote(m.source, m.target, num)
283 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
285 m.reply "quote not found!"
288 when (/^delquote\s+(\d+)$/)
290 if(@bot.auth.allow?("delquote", m.source, m.replyto))
291 if(delquote(m.target, num))
294 m.reply "quote not found!"
297 when (/^searchquote\s+(.*)$/)
299 if(@bot.auth.allow?("getquote", m.source, m.replyto))
300 quote, total = searchquote(m.source, m.target, reg)
302 m.reply "[#{quote.num}] #{quote.quote}"
304 m.reply "quote not found!"
307 when (/^countquote(?:\s+(.*))?$/)
309 if(@bot.auth.allow?("getquote", m.source, m.replyto))
310 total = countquote(m.source, m.target, reg)
311 if(reg && reg.length > 0)
312 m.reply "#{total} quotes match: #{reg}"
314 m.reply "#{total} quotes"
321 plugin = QuotePlugin.new
322 plugin.register("quotes")