4 # :title: Quotes plugin
8 define_structure :Quote, :num, :date, :source, :quote
10 class QuotePlugin < Plugin
17 @lists = @registry[:lists]
20 # migrate quotes from existing quote files in data directory
21 Dir[datafile('*')].each do |f|
22 next if File.directory?(f)
23 channel = File.basename(f)
24 @lists[channel] = Array.new if not @lists.has_key?(channel)
25 IO.foreach(f) do |line|
26 if(line =~ /^(\d+) \| ([^|]+) \| (\S+) \| (.*)$/)
28 @lists[channel][num] = Quote.new(num, $2, $3, $4)
36 @registry[:lists] = @lists
45 def lastquote(channel)
46 @lists[channel].length-1
49 def addquote(source, channel, quote)
50 @lists[channel] = Array.new if(!@lists.has_key?(channel))
51 num = @lists[channel].length
52 @lists[channel][num] = Quote.new(num, Time.new, source.fullform, quote)
56 def getquote(source, channel, num=nil)
57 return nil unless(@lists.has_key?(channel))
58 return nil unless(@lists[channel].length > 0)
60 if(@lists[channel][num])
61 return @lists[channel][num], @lists[channel].length - 1
65 return @lists[channel].compact.pick_one,
66 @lists[channel].length - 1
70 def delquote(channel, num)
71 return false unless(@lists.has_key?(channel))
72 return false unless(@lists[channel].length > 0)
73 if(@lists[channel][num])
74 @lists[channel][num] = nil
75 @lists[channel].pop if num == @lists[channel].length - 1
81 def countquote(source, channel=nil, regexp=nil)
84 @lists.each_value {|l|
85 total += l.compact.length
89 return 0 unless(@lists.has_key?(channel))
90 return 0 unless(@lists[channel].length > 0)
92 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
94 matches = @lists[channel].compact
99 def searchquote(source, channel, regexp)
100 return nil unless(@lists.has_key?(channel))
101 return nil unless(@lists[channel].length > 0)
102 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
103 if(matches.length > 0)
104 return matches[rand(matches.length)], @lists[channel].length - 1
110 def listquotes(source, channel, regexp)
111 return nil unless(@lists.has_key?(channel))
112 return nil unless(@lists[channel].length > 0)
113 matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
114 if matches.length > 0
121 def help(plugin, topic="")
124 _("addquote [<channel>] <quote> => Add quote <quote> for channel <channel>. You only need to supply <channel> if you are addressing %{nick} privately.") % { :nick => @bot.nick }
126 _("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 }
128 _("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 }
130 _("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 }
132 _("listquotes [<channel>] <regexp> => list the quotes from <channel> that match <regexp>. You only need to supply <channel> if you are addressing %{nick} privately.") % { :nick => @bot.nick }
134 _("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 }
136 _("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 }
138 _("whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing %{nick} privately") % { :nick => @bot.nick }
140 _("whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing %{nick} privately") % { :nick => @bot.nick }
142 _("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 }
144 _("Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, listquotes, topicquote, countquote, whoquote, whenquote, lastquote") % { :nick => @bot.nick }
148 def cmd_addquote(m, p)
149 channel = p[:channel] || m.channel.to_s
150 quote = p[:quote].to_s
151 num = addquote(m.source, channel, quote)
152 m.reply _("added the quote (#%{num})") % { :num => num }
155 def cmd_delquote(m, p)
156 channel = p[:channel] || m.channel.to_s
158 if delquote(channel, num)
161 m.reply _("quote not found!")
165 def cmd_getquote(m, p)
166 channel = p[:channel] || m.channel.to_s
167 num = p[:num] ? p[:num].to_i : nil
168 quote, total = getquote(m.source, channel, num)
170 m.reply _("[%{num}] %{quote}") % {
172 :quote => quote.quote
175 m.reply _("quote not found!")
179 def cmd_whoquote(m, p)
180 channel = p[:channel] || m.channel.to_s
181 num = p[:num] ? p[:num].to_i : nil
182 quote, total = getquote(m.source, channel, num)
184 m.reply _("quote %{num} added by %{source}") % {
186 :source => quote.source
189 m.reply _("quote not found!")
193 def cmd_whenquote(m, p)
194 channel = p[:channel] || m.channel.to_s
195 num = p[:num] ? p[:num].to_i : nil
196 quote, total = getquote(m.source, channel, num)
198 m.reply _("quote %{num} added on %{date}") % {
203 m.reply _("quote not found!")
207 def cmd_searchquote(m, p)
208 channel = p[:channel] || m.channel.to_s
210 quote, total = searchquote(m.source, channel, reg)
212 m.reply _("[%{num}] %{quote}") % {
214 :quote => quote.quote
217 m.reply _("quote not found!")
221 def cmd_listquotes(m, p)
222 channel = p[:channel] || m.channel.to_s
224 if quotes = listquotes(m.source, channel, reg)
225 m.reply _("%{total} quotes matching %{reg} found: %{list}") % {
226 :total => quotes.size,
228 :list => quotes.map {|q| q.num }.join(', ')
231 m.reply _("quote not found!")
235 def cmd_countquote(m, p)
236 channel = p[:channel] || m.channel.to_s
237 reg = p[:reg] ? p[:reg].to_s : nil
238 total = countquote(m.source, channel, reg)
240 m.reply _("%{total} quotes matching %{reg}") % {
245 m.reply _("%{total} quotes") % { :total => total }
249 def cmd_topicquote(m, p)
250 channel = p[:channel] || m.channel.to_s
251 num = p[:num] ? p[:num].to_i : nil
252 quote, total = getquote(m.source, channel, num)
254 @bot.topic channel, _("[%{num}] %{quote}") % {
256 :quote => quote.quote
259 m.reply _("quote not found!")
263 def cmd_lastquote(m, p)
264 channel = p[:channel] || m.channel.to_s
265 quote, total = getquote(m.source, channel, lastquote(channel))
267 m.reply _("[%{num}] %{quote}") % {
269 :quote => quote.quote
272 m.reply _("quote not found!")
277 plugin = QuotePlugin.new
278 plugin.register("quotes")
280 plugin.default_auth('other::edit', false) # Prevent random people from editing other channels quote lists by default
281 plugin.default_auth('other::view', true) # But allow them to view them
283 plugin.map "addquote :channel *quote", :action => :cmd_addquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::edit::add!'
284 plugin.map "delquote :channel :num", :action => :cmd_delquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::edit::del!'
285 plugin.map "getquote :channel [:num]", :action => :cmd_getquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::get!'
286 plugin.map "whoquote :channel :num", :action => :cmd_whoquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::who!'
287 plugin.map "whenquote :channel :num", :action => :cmd_whenquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::when!'
288 plugin.map "searchquote :channel *reg", :action => :cmd_searchquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::search!'
289 plugin.map "listquotes :channel *reg", :action => :cmd_listquotes, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::list!'
290 plugin.map "countquote :channel [*reg]", :action => :cmd_countquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::count!'
291 plugin.map "topicquote :channel [:num]", :action => :cmd_topicquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::topic!'
292 plugin.map "lastquote :channel", :action => :cmd_lastquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::last!'
294 plugin.default_auth('edit', false) # Prevent random people from removing quotes
295 plugin.default_auth('edit::add', true) # But allow them to add them
297 plugin.map "addquote *quote", :action => :cmd_addquote, :private => false, :auth_path => '!quote::edit::add!'
298 plugin.map "delquote :num", :action => :cmd_delquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::edit::del!'
299 plugin.map "getquote [:num]", :action => :cmd_getquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::get!'
300 plugin.map "whoquote :num", :action => :cmd_whoquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::who!'
301 plugin.map "whenquote :num", :action => :cmd_whenquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::when!'
302 plugin.map "searchquote *reg", :action => :cmd_searchquote, :private => false, :auth_path => '!quote::view::search!'
303 plugin.map "listquotes *reg", :action => :cmd_listquotes, :private => false, :auth_path => '!quote::view::list!'
304 plugin.map "countquote [*reg]", :action => :cmd_countquote, :private => false, :auth_path => '!quote::view::count!'
305 plugin.map "topicquote [:num]", :action => :cmd_topicquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::topic!'
306 plugin.map "lastquote", :action => :cmd_lastquote, :private => false, :auth_path => '!quote::view::last!'