4 # :title: Quotes plugin
8 define_structure :Quote, :num, :date, :source, :quote
10 class QuotePlugin < Plugin
15 Dir["#{@bot.botclass}/quotes/*"].each {|f|
16 next if File.directory?(f)
17 channel = File.basename(f)
18 @lists[channel] = Array.new if(!@lists.has_key?(channel))
20 if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/)
22 @lists[channel][num] = Quote.new(num, $2, $3, $4)
25 @changed[channel] = false
30 Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes"))
31 @lists.each {|channel, quotes|
34 debug "Writing new quotefile for channel #{channel} ..."
35 Utils.safe_save("#{@bot.botclass}/quotes/#{channel}") {|file|
36 quotes.compact.each {|q|
37 file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
40 @changed[channel] = false
42 debug "Not writing quotefile for channel #{channel} (unchanged)"
45 error "failed to write quotefile for channel #{channel}!\n#{$!}"
46 error "#{e.class}: #{e}"
47 error e.backtrace.join("\n")
58 def lastquote(channel)
59 @lists[channel].length-1
62 def addquote(source, channel, quote)
63 @lists[channel] = Array.new if(!@lists.has_key?(channel))
64 num = @lists[channel].length
65 @lists[channel][num] = Quote.new(num, Time.new, source.fullform, quote)
66 @changed[channel] = true
70 def getquote(source, channel, num=nil)
71 return nil unless(@lists.has_key?(channel))
72 return nil unless(@lists[channel].length > 0)
74 if(@lists[channel][num])
75 return @lists[channel][num], @lists[channel].length - 1
79 return @lists[channel].compact[rand(@lists[channel].nitems)],
80 @lists[channel].length - 1
84 def delquote(channel, num)
85 return false unless(@lists.has_key?(channel))
86 return false unless(@lists[channel].length > 0)
87 if(@lists[channel][num])
88 @lists[channel][num] = nil
89 @lists[channel].pop if num == @lists[channel].length - 1
90 @changed[channel] = true
96 def countquote(source, channel=nil, regexp=nil)
99 @lists.each_value {|l|
100 total += l.compact.length
104 return 0 unless(@lists.has_key?(channel))
105 return 0 unless(@lists[channel].length > 0)
107 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
109 matches = @lists[channel].compact
111 return matches.length
114 def searchquote(source, channel, regexp)
115 return nil unless(@lists.has_key?(channel))
116 return nil unless(@lists[channel].length > 0)
117 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
118 if(matches.length > 0)
119 return matches[rand(matches.length)], @lists[channel].length - 1
125 def help(plugin, topic="")
128 _("addquote [<channel>] <quote> => Add quote <quote> for channel <channel>. You only need to supply <channel> if you are addressing %{nick} privately.") % { :nick => @bot.nick }
130 _("delquote [<channel>] <num> => delete quote from <channel> with number <num>. You only need to supply <channel> if you are addressing %{nick} privately.") % { :nick => @bot.nick }
132 _("getquote [<channel>] [<num>] => get quote from <channel> with number <num>. You only need to supply <channel> if you are addressing %{nick} privately. Without <num>, a random quote will be returned.") % { :nick => @bot.nick }
134 _("searchquote [<channel>] <regexp> => search for quote from <channel> that matches <regexp>. You only need to supply <channel> if you are addressing %{nick} privately.") % { :nick => @bot.nick }
136 _("topicquote [<channel>] [<num>] => set topic to quote from <channel> with number <num>. You only need to supply <channel> if you are addressing %{nick} privately. Without <num>, a random quote will be set.") % { :nick => @bot.nick }
138 _("countquote [<channel>] <regexp> => count quotes from <channel> that match <regexp>. You only need to supply <channel> if you are addressing %{nick} privately.") % { :nick => @bot.nick }
140 _("whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing %{nick} privately") % { :nick => @bot.nick }
142 _("whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing %{nick} privately") % { :nick => @bot.nick }
144 _("lastquote [<channel>] => show the last quote in a given channel. You only need to supply <channel> if you are addressing %{nick} privately") % { :nick => @bot.nick }
146 _("Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote, lastquote") % { :nick => @bot.nick }
150 def cmd_addquote(m, p)
151 channel = p[:channel] || m.channel.to_s
152 quote = p[:quote].to_s
153 num = addquote(m.source, channel, quote)
154 m.reply _("added the quote (#%{num})") % { :num => num }
157 def cmd_delquote(m, p)
158 channel = p[:channel] || m.channel.to_s
160 if delquote(channel, num)
163 m.reply _("quote not found!")
167 def cmd_getquote(m, p)
168 channel = p[:channel] || m.channel.to_s
169 num = p[:num] ? p[:num].to_i : nil
170 quote, total = getquote(m.source, channel, num)
172 m.reply _("[%{num}] %{quote}") % {
174 :quote => quote.quote
177 m.reply _("quote not found!")
181 def cmd_whoquote(m, p)
182 channel = p[:channel] || m.channel.to_s
183 num = p[:num] ? p[:num].to_i : nil
184 quote, total = getquote(m.source, channel, num)
186 m.reply _("quote %{num} added by %{source}") % {
188 :source => quote.source
191 m.reply _("quote not found!")
195 def cmd_whenquote(m, p)
196 channel = p[:channel] || m.channel.to_s
197 num = p[:num] ? p[:num].to_i : nil
198 quote, total = getquote(m.source, channel, num)
200 m.reply _("quote %{num} added on %{date}") % {
205 m.reply _("quote not found!")
209 def cmd_searchquote(m, p)
210 channel = p[:channel] || m.channel.to_s
212 quote, total = searchquote(m.source, channel, reg)
214 m.reply _("[%{num}] %{quote}") % {
216 :quote => quote.quote
219 m.reply _("quote not found!")
223 def cmd_countquote(m, p)
224 channel = p[:channel] || m.channel.to_s
225 reg = p[:reg] ? p[:reg].to_s : nil
226 total = countquote(m.source, channel, reg)
228 m.reply _("%{total} quotes matching %{reg}") % {
233 m.reply _("%{total} quotes") % { :total => total }
237 def cmd_topicquote(m, p)
238 channel = p[:channel] || m.channel.to_s
239 num = p[:num] ? p[:num].to_i : nil
240 quote, total = getquote(m.source, channel, num)
242 @bot.topic channel, _("[%{num}] %{quote}") % {
244 :quote => quote.quote
247 m.reply _("quote not found!")
251 def cmd_lastquote(m, p)
252 channel = p[:channel] || m.channel.to_s
253 quote, total = getquote(m.source, channel, lastquote(channel))
255 m.reply _("[%{num}] %{quote}") % {
257 :quote => quote.quote
260 m.reply _("quote not found!")
265 plugin = QuotePlugin.new
266 plugin.register("quotes")
268 plugin.default_auth('other::edit', false) # Prevent random people from editing other channels quote lists by default
269 plugin.default_auth('other::view', true) # But allow them to view them
271 plugin.map "addquote :channel *quote", :action => :cmd_addquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::edit::add!'
272 plugin.map "delquote :channel :num", :action => :cmd_delquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::edit::del!'
273 plugin.map "getquote :channel [:num]", :action => :cmd_getquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::get!'
274 plugin.map "whoquote :channel :num", :action => :cmd_whoquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::who!'
275 plugin.map "whenquote :channel :num", :action => :cmd_whenquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::when!'
276 plugin.map "searchquote :channel *reg", :action => :cmd_searchquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::search!'
277 plugin.map "countquote :channel [*reg]", :action => :cmd_countquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::count!'
278 plugin.map "topicquote :channel [:num]", :action => :cmd_topicquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::topic!'
279 plugin.map "lastquote :channel", :action => :cmd_lastquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::last!'
281 plugin.default_auth('edit', false) # Prevent random people from removing quotes
282 plugin.default_auth('edit::add', true) # But allow them to add them
284 plugin.map "addquote *quote", :action => :cmd_addquote, :private => false, :auth_path => '!quote::edit::add!'
285 plugin.map "delquote :num", :action => :cmd_delquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::edit::del!'
286 plugin.map "getquote [:num]", :action => :cmd_getquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::get!'
287 plugin.map "whoquote :num", :action => :cmd_whoquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::who!'
288 plugin.map "whenquote :num", :action => :cmd_whenquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::when!'
289 plugin.map "searchquote *reg", :action => :cmd_searchquote, :private => false, :auth_path => '!quote::view::search!'
290 plugin.map "countquote [*reg]", :action => :cmd_countquote, :private => false, :auth_path => '!quote::view::count!'
291 plugin.map "topicquote [:num]", :action => :cmd_topicquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::topic!'
292 plugin.map "lastquote", :action => :cmd_lastquote, :private => false, :auth_path => '!quote::view::last!'