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
56 def countquote(source, channel=nil, regexp=nil)
59 @lists.each_value {|l|
60 total += l.compact.length
64 return 0 unless(@lists.has_key?(channel))
65 return 0 unless(@lists[channel].length > 0)
67 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/ }
69 matches = @lists[channel].compact
73 def searchquote(source, channel, regexp)
74 return nil unless(@lists.has_key?(channel))
75 return nil unless(@lists[channel].length > 0)
76 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/ }
77 if(matches.length > 0)
78 return matches[rand(matches.length)], @lists[channel].length - 1
83 def help(plugin, topic="")
86 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"
88 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"
90 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"
92 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"
94 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"
96 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"
98 return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
100 return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
102 return "Quote module (Quote storage and retrieval) topics: addquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
106 return unless(m.kind_of? PrivMessage)
108 command = m.message.dup
109 if(m.address? && m.private?)
111 when (/^addquote\s+(#\S+)\s+(.*)/)
114 if(@bot.auth.allow?("addquote", m.source, m.replyto))
116 num = addquote(m.source, channel, quote)
117 m.reply "added the quote (##{num})"
120 when (/^getquote\s+(#\S+)$/)
122 if(@bot.auth.allow?("getquote", m.source, m.replyto))
123 quote, total = getquote(m.source, channel)
125 m.reply "[#{quote.num}] #{quote.quote}"
127 m.reply "quote not found!"
130 when (/^getquote\s+(#\S+)\s+(\d+)$/)
133 if(@bot.auth.allow?("getquote", m.source, m.replyto))
134 quote, total = getquote(m.source, channel, num)
136 m.reply "[#{quote.num}] #{quote.quote}"
138 m.reply "quote not found!"
141 when (/^whoquote\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 #{quote.num} added by #{quote.source}"
149 m.reply "quote not found!"
152 when (/^whenquote\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 on #{quote.date}"
160 m.reply "quote not found!"
163 when (/^topicquote\s+(#\S+)$/)
165 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
166 quote, total = getquote(m.source, channel)
168 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
170 m.reply "quote not found!"
173 when (/^topicquote\s+(#\S+)\s+(\d+)$/)
176 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
177 quote, total = getquote(m.source, channel, num)
179 @bot.topic channel, "[#{quote.num}] #{quote.quote}"
181 m.reply "quote not found!"
184 when (/^delquote\s+(#\S+)\s+(\d+)$/)
187 if(@bot.auth.allow?("delquote", m.source, m.replyto))
188 if(delquote(channel, num))
191 m.reply "quote not found!"
194 when (/^searchquote\s+(#\S+)\s+(.*)$/)
197 if(@bot.auth.allow?("getquote", m.source, m.replyto))
198 quote, total = searchquote(m.source, channel, reg)
200 m.reply "[#{quote.num}] #{quote.quote}"
202 m.reply "quote not found!"
205 when (/^countquote$/)
206 if(@bot.auth.allow?("getquote", m.source, m.replyto))
207 total = countquote(m.source)
208 m.reply "#{total} quotes"
210 when (/^countquote\s+(#\S+)\s*(.*)$/)
213 if(@bot.auth.allow?("getquote", m.source, m.replyto))
214 total = countquote(m.source, channel, reg)
216 m.reply "#{total} quotes match: #{reg}"
218 m.reply "#{total} quotes"
222 elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, "")))
224 when (/^addquote\s+(.+)/)
225 if(@bot.auth.allow?("addquote", m.source, m.replyto))
226 num = addquote(m.source, m.target, $1)
227 m.reply "added the quote (##{num})"
230 if(@bot.auth.allow?("getquote", m.source, m.replyto))
231 quote, total = getquote(m.source, m.target)
233 m.reply "[#{quote.num}] #{quote.quote}"
235 m.reply "no quotes found!"
238 when (/^getquote\s+(\d+)$/)
240 if(@bot.auth.allow?("getquote", m.source, m.replyto))
241 quote, total = getquote(m.source, m.target, num)
243 m.reply "[#{quote.num}] #{quote.quote}"
245 m.reply "quote not found!"
248 when (/^whenquote\s+(\d+)$/)
250 if(@bot.auth.allow?("getquote", m.source, m.replyto))
251 quote, total = getquote(m.source, m.target, num)
253 m.reply "quote #{quote.num} added on #{quote.date}"
255 m.reply "quote not found!"
258 when (/^whoquote\s+(\d+)$/)
260 if(@bot.auth.allow?("getquote", m.source, m.replyto))
261 quote, total = getquote(m.source, m.target, num)
263 m.reply "quote #{quote.num} added by #{quote.source}"
265 m.reply "quote not found!"
268 when (/^topicquote$/)
269 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
270 quote, total = getquote(m.source, m.target)
272 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
274 m.reply "no quotes found!"
277 when (/^topicquote\s+(\d+)$/)
279 if(@bot.auth.allow?("topicquote", m.source, m.replyto))
280 quote, total = getquote(m.source, m.target, num)
282 @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
284 m.reply "quote not found!"
287 when (/^delquote\s+(\d+)$/)
289 if(@bot.auth.allow?("delquote", m.source, m.replyto))
290 if(delquote(m.target, num))
293 m.reply "quote not found!"
296 when (/^searchquote\s+(.*)$/)
298 if(@bot.auth.allow?("getquote", m.source, m.replyto))
299 quote, total = searchquote(m.source, m.target, reg)
301 m.reply "[#{quote.num}] #{quote.quote}"
303 m.reply "quote not found!"
306 when (/^countquote(?:\s+(.*))?$/)
308 if(@bot.auth.allow?("getquote", m.source, m.replyto))
309 total = countquote(m.source, m.target, reg)
310 if(reg && reg.length > 0)
311 m.reply "#{total} quotes match: #{reg}"
313 m.reply "#{total} quotes"
320 plugin = QuotePlugin.new
321 plugin.register("quotes")