4 # :title: Quotes plugin
6 # TODO:: use message mapper instead of multiple ifs
9 define_structure :Quote, :num, :date, :source, :quote
11 class QuotePlugin < Plugin
16 Dir["#{@bot.botclass}/quotes/*"].each {|f|
17 next if File.directory?(f)
18 channel = File.basename(f)
19 @lists[channel] = Array.new if(!@lists.has_key?(channel))
21 if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/)
23 @lists[channel][num] = Quote.new(num, $2, $3, $4)
26 @changed[channel] = false
31 Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes"))
32 @lists.each {|channel, quotes|
35 debug "Writing new quotefile for channel #{channel} ..."
36 Utils.safe_save("#{@bot.botclass}/quotes/#{channel}") {|file|
37 quotes.compact.each {|q|
38 file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
41 @changed[channel] = false
43 debug "Not writing quotefile for channel #{channel} (unchanged)"
46 error "failed to write quotefile for channel #{channel}!\n#{$!}"
47 error "#{e.class}: #{e}"
48 error e.backtrace.join("\n")
59 def addquote(source, channel, quote)
60 @lists[channel] = Array.new if(!@lists.has_key?(channel))
61 num = @lists[channel].length
62 @lists[channel][num] = Quote.new(num, Time.new, source.fullform, quote)
63 @changed[channel] = true
67 def getquote(source, channel, num=nil)
68 return nil unless(@lists.has_key?(channel))
69 return nil unless(@lists[channel].length > 0)
71 if(@lists[channel][num])
72 return @lists[channel][num], @lists[channel].length - 1
76 return @lists[channel].compact[rand(@lists[channel].nitems)],
77 @lists[channel].length - 1
81 def delquote(channel, num)
82 return false unless(@lists.has_key?(channel))
83 return false unless(@lists[channel].length > 0)
84 if(@lists[channel][num])
85 @lists[channel][num] = nil
86 @lists[channel].pop if num == @lists[channel].length - 1
87 @changed[channel] = true
93 def countquote(source, channel=nil, regexp=nil)
96 @lists.each_value {|l|
97 total += l.compact.length
101 return 0 unless(@lists.has_key?(channel))
102 return 0 unless(@lists[channel].length > 0)
104 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
106 matches = @lists[channel].compact
108 return matches.length
111 def searchquote(source, channel, regexp)
112 return nil unless(@lists.has_key?(channel))
113 return nil unless(@lists[channel].length > 0)
114 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
115 if(matches.length > 0)
116 return matches[rand(matches.length)], @lists[channel].length - 1
122 def help(plugin, topic="")
125 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"
127 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"
129 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"
131 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"
133 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"
135 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"
137 return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
139 return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
141 return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
145 def cmd_addquote(m, p)
146 channel = p[:channel] || m.channel.to_s
147 quote = p[:quote].to_s
148 num = addquote(m.source, channel, quote)
149 m.reply _("added the quote (#%{num})") % { :num => num }
152 def cmd_delquote(m, p)
153 channel = p[:channel] || m.channel.to_s
155 if delquote(channel, num)
158 m.reply "quote not found!"
162 def cmd_getquote(m, p)
163 channel = p[:channel] || m.channel.to_s
164 num = p[:num] ? p[:num].to_i : nil
165 quote, total = getquote(m.source, channel, num)
167 m.reply _("[%{num}] %{quote}") % {
169 :quote => quote.quote
172 m.reply _("quote not found!")
176 def cmd_whoquote(m, p)
177 channel = p[:channel] || m.channel.to_s
178 num = p[:num] ? p[:num].to_i : nil
179 quote, total = getquote(m.source, channel, num)
181 m.reply _("quote %{num} added by %{source}") % {
183 :source => quote.source
186 m.reply _("quote not found!")
190 def cmd_whenquote(m, p)
191 channel = p[:channel] || m.channel.to_s
192 num = p[:num] ? p[:num].to_i : nil
193 quote, total = getquote(m.source, channel, num)
195 m.reply _("quote %{num} added on %{date}") % {
200 m.reply _("quote not found!")
204 def cmd_searchquote(m, p)
205 channel = p[:channel] || m.channel.to_s
207 quote, total = searchquote(m.source, channel, reg)
209 m.reply _("[%{num}] %{quote}") % {
211 :quote => quote.quote
214 m.reply _("quote not found!")
218 def cmd_countquote(m, p)
219 channel = p[:channel] || m.channel.to_s
220 reg = p[:reg] ? p[:reg].to_s : nil
221 total = countquote(m.source, channel, reg)
223 m.reply _("%{total} quotes matching %{reg}") % {
228 m.reply _("%{total} quotes") % { :total => total }
232 def cmd_topicquote(m, p)
233 channel = p[:channel] || m.channel.to_s
234 num = p[:num] ? p[:num].to_i : nil
235 quote, total = getquote(m.source, channel, num)
237 @bot.topic channel, _("[%{num}] %{quote}") % {
239 :quote => quote.quote
242 m.reply _("quote not found!")
247 plugin = QuotePlugin.new
248 plugin.register("quotes")
250 plugin.default_auth('edit', false) # Prevent random people from removing quotes
251 plugin.default_auth('edit::add', true) # But allow them to add them
253 plugin.map "addquote *quote", :action => :cmd_addquote, :private => false, :auth_path => '!quote::edit::add!'
254 plugin.map "delquote :num", :action => :cmd_delquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::edit::del!'
255 plugin.map "getquote [:num]", :action => :cmd_getquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::get!'
256 plugin.map "whoquote :num", :action => :cmd_whoquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::who!'
257 plugin.map "whenquote :num", :action => :cmd_whenquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::when!'
258 plugin.map "searchquote *reg", :action => :cmd_searchquote, :private => false, :auth_path => '!quote::view::search!'
259 plugin.map "countquote [*reg]", :action => :cmd_countquote, :private => false, :auth_path => '!quote::view::count!'
260 plugin.map "topicquote [:num]", :action => :cmd_topicquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::topic!'
262 plugin.default_auth('other::edit', false) # Prevent random people from editing other channels quote lists by default
263 plugin.default_auth('other::view', true) # But allow them to view them
265 plugin.map "addquote :channel *quote", :action => :cmd_addquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::edit::add!'
266 plugin.map "delquote :channel :num", :action => :cmd_delquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::edit::del!'
267 plugin.map "getquote :channel [:num]", :action => :cmd_getquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::get!'
268 plugin.map "whoquote :channel :num", :action => :cmd_whoquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::who!'
269 plugin.map "whenquote :channel :num", :action => :cmd_whenquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::when!'
270 plugin.map "searchquote :channel *reg", :action => :cmd_searchquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::search!'
271 plugin.map "countquote [:channel] [*reg]", :action => :cmd_countquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::count!'
272 plugin.map "topicquote :channel [:num]", :action => :cmd_topicquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::topic!'