4 # :title: Quotes plugin
8 define_structure :Quote, :num, :date, :source, :quote
10 class QuotePlugin < Plugin
19 Dir[datafile '*'].each {|f|
20 next if File.directory?(f)
21 channel = File.basename(f)
22 @lists[channel] = Array.new if(!@lists.has_key?(channel))
24 if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/)
26 @lists[channel][num] = Quote.new(num, $2, $3, $4)
29 @changed[channel] = false
34 Dir.mkdir(datafile) unless FileTest.directory? datafile
35 @lists.each {|channel, quotes|
38 debug "Writing new quotefile for channel #{channel} ..."
39 Utils.safe_save(datafile channel) {|file|
40 quotes.compact.each {|q|
41 file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
44 @changed[channel] = false
46 debug "Not writing quotefile for channel #{channel} (unchanged)"
49 error "failed to write quotefile for channel #{channel}!\n#{$!}"
50 error "#{e.class}: #{e}"
51 error e.backtrace.join("\n")
62 def lastquote(channel)
63 @lists[channel].length-1
66 def addquote(source, channel, quote)
67 @lists[channel] = Array.new if(!@lists.has_key?(channel))
68 num = @lists[channel].length
69 @lists[channel][num] = Quote.new(num, Time.new, source.fullform, quote)
70 @changed[channel] = true
74 def getquote(source, channel, num=nil)
75 return nil unless(@lists.has_key?(channel))
76 return nil unless(@lists[channel].length > 0)
78 if(@lists[channel][num])
79 return @lists[channel][num], @lists[channel].length - 1
83 return @lists[channel].compact[rand(@lists[channel].nitems)],
84 @lists[channel].length - 1
88 def delquote(channel, num)
89 return false unless(@lists.has_key?(channel))
90 return false unless(@lists[channel].length > 0)
91 if(@lists[channel][num])
92 @lists[channel][num] = nil
93 @lists[channel].pop if num == @lists[channel].length - 1
94 @changed[channel] = true
100 def countquote(source, channel=nil, regexp=nil)
103 @lists.each_value {|l|
104 total += l.compact.length
108 return 0 unless(@lists.has_key?(channel))
109 return 0 unless(@lists[channel].length > 0)
111 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
113 matches = @lists[channel].compact
115 return matches.length
118 def searchquote(source, channel, regexp)
119 return nil unless(@lists.has_key?(channel))
120 return nil unless(@lists[channel].length > 0)
121 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
122 if(matches.length > 0)
123 return matches[rand(matches.length)], @lists[channel].length - 1
129 def help(plugin, topic="")
132 _("addquote [<channel>] <quote> => Add quote <quote> for channel <channel>. You only need to supply <channel> if you are addressing %{nick} privately.") % { :nick => @bot.nick }
134 _("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 }
136 _("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 }
138 _("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 }
140 _("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 }
142 _("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 }
144 _("whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing %{nick} privately") % { :nick => @bot.nick }
146 _("whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing %{nick} privately") % { :nick => @bot.nick }
148 _("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 }
150 _("Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote, lastquote") % { :nick => @bot.nick }
154 def cmd_addquote(m, p)
155 channel = p[:channel] || m.channel.to_s
156 quote = p[:quote].to_s
157 num = addquote(m.source, channel, quote)
158 m.reply _("added the quote (#%{num})") % { :num => num }
161 def cmd_delquote(m, p)
162 channel = p[:channel] || m.channel.to_s
164 if delquote(channel, num)
167 m.reply _("quote not found!")
171 def cmd_getquote(m, p)
172 channel = p[:channel] || m.channel.to_s
173 num = p[:num] ? p[:num].to_i : nil
174 quote, total = getquote(m.source, channel, num)
176 m.reply _("[%{num}] %{quote}") % {
178 :quote => quote.quote
181 m.reply _("quote not found!")
185 def cmd_whoquote(m, p)
186 channel = p[:channel] || m.channel.to_s
187 num = p[:num] ? p[:num].to_i : nil
188 quote, total = getquote(m.source, channel, num)
190 m.reply _("quote %{num} added by %{source}") % {
192 :source => quote.source
195 m.reply _("quote not found!")
199 def cmd_whenquote(m, p)
200 channel = p[:channel] || m.channel.to_s
201 num = p[:num] ? p[:num].to_i : nil
202 quote, total = getquote(m.source, channel, num)
204 m.reply _("quote %{num} added on %{date}") % {
209 m.reply _("quote not found!")
213 def cmd_searchquote(m, p)
214 channel = p[:channel] || m.channel.to_s
216 quote, total = searchquote(m.source, channel, reg)
218 m.reply _("[%{num}] %{quote}") % {
220 :quote => quote.quote
223 m.reply _("quote not found!")
227 def cmd_countquote(m, p)
228 channel = p[:channel] || m.channel.to_s
229 reg = p[:reg] ? p[:reg].to_s : nil
230 total = countquote(m.source, channel, reg)
232 m.reply _("%{total} quotes matching %{reg}") % {
237 m.reply _("%{total} quotes") % { :total => total }
241 def cmd_topicquote(m, p)
242 channel = p[:channel] || m.channel.to_s
243 num = p[:num] ? p[:num].to_i : nil
244 quote, total = getquote(m.source, channel, num)
246 @bot.topic channel, _("[%{num}] %{quote}") % {
248 :quote => quote.quote
251 m.reply _("quote not found!")
255 def cmd_lastquote(m, p)
256 channel = p[:channel] || m.channel.to_s
257 quote, total = getquote(m.source, channel, lastquote(channel))
259 m.reply _("[%{num}] %{quote}") % {
261 :quote => quote.quote
264 m.reply _("quote not found!")
269 plugin = QuotePlugin.new
270 plugin.register("quotes")
272 plugin.default_auth('other::edit', false) # Prevent random people from editing other channels quote lists by default
273 plugin.default_auth('other::view', true) # But allow them to view them
275 plugin.map "addquote :channel *quote", :action => :cmd_addquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::edit::add!'
276 plugin.map "delquote :channel :num", :action => :cmd_delquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::edit::del!'
277 plugin.map "getquote :channel [:num]", :action => :cmd_getquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::get!'
278 plugin.map "whoquote :channel :num", :action => :cmd_whoquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::who!'
279 plugin.map "whenquote :channel :num", :action => :cmd_whenquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::when!'
280 plugin.map "searchquote :channel *reg", :action => :cmd_searchquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::search!'
281 plugin.map "countquote :channel [*reg]", :action => :cmd_countquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::count!'
282 plugin.map "topicquote :channel [:num]", :action => :cmd_topicquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::topic!'
283 plugin.map "lastquote :channel", :action => :cmd_lastquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::last!'
285 plugin.default_auth('edit', false) # Prevent random people from removing quotes
286 plugin.default_auth('edit::add', true) # But allow them to add them
288 plugin.map "addquote *quote", :action => :cmd_addquote, :private => false, :auth_path => '!quote::edit::add!'
289 plugin.map "delquote :num", :action => :cmd_delquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::edit::del!'
290 plugin.map "getquote [:num]", :action => :cmd_getquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::get!'
291 plugin.map "whoquote :num", :action => :cmd_whoquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::who!'
292 plugin.map "whenquote :num", :action => :cmd_whenquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::when!'
293 plugin.map "searchquote *reg", :action => :cmd_searchquote, :private => false, :auth_path => '!quote::view::search!'
294 plugin.map "countquote [*reg]", :action => :cmd_countquote, :private => false, :auth_path => '!quote::view::count!'
295 plugin.map "topicquote [:num]", :action => :cmd_topicquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::topic!'
296 plugin.map "lastquote", :action => :cmd_lastquote, :private => false, :auth_path => '!quote::view::last!'