X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fquotes.rb;h=c10666899b0024369f04f2610b87d6e358d6b90b;hb=41276911b95c934dd5f33ccef53b03bc9dd40975;hp=7ad6d570848b0b7d3b70c07f20d8645b26afdda3;hpb=e5dffc33c257bbe4edf41bba018f58104c0e6b57;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/quotes.rb b/data/rbot/plugins/quotes.rb index 7ad6d570..c1066689 100644 --- a/data/rbot/plugins/quotes.rb +++ b/data/rbot/plugins/quotes.rb @@ -3,17 +3,20 @@ # # :title: Quotes plugin # -# TODO:: use message mapper instead of multiple ifs # TODO:: switch to db define_structure :Quote, :num, :date, :source, :quote class QuotePlugin < Plugin + def dirname + 'quotes' + end + def initialize super @lists = Hash.new @changed = Hash.new - Dir["#{@bot.botclass}/quotes/*"].each {|f| + Dir[datafile('*')].each {|f| next if File.directory?(f) channel = File.basename(f) @lists[channel] = Array.new if(!@lists.has_key?(channel)) @@ -28,13 +31,13 @@ class QuotePlugin < Plugin end def save - Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes")) + Dir.mkdir(datafile) unless FileTest.directory? datafile @lists.each {|channel, quotes| begin if @changed[channel] debug "Writing new quotefile for channel #{channel} ..." - Utils.safe_save("#{@bot.botclass}/quotes/#{channel}") {|file| - quotes.compact.each {|q| + Utils.safe_save(datafile(channel)) {|file| + quotes.compact.each {|q| file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}" } } @@ -56,9 +59,13 @@ class QuotePlugin < Plugin super end + def lastquote(channel) + @lists[channel].length-1 + end + def addquote(source, channel, quote) @lists[channel] = Array.new if(!@lists.has_key?(channel)) - num = @lists[channel].length + num = @lists[channel].length @lists[channel][num] = Quote.new(num, Time.new, source.fullform, quote) @changed[channel] = true return num @@ -73,7 +80,7 @@ class QuotePlugin < Plugin end else # random quote - return @lists[channel].compact[rand(@lists[channel].nitems)], + return @lists[channel].compact.pick_one, @lists[channel].length - 1 end end @@ -119,26 +126,41 @@ class QuotePlugin < Plugin end end + def listquotes(source, channel, regexp) + return nil unless(@lists.has_key?(channel)) + return nil unless(@lists[channel].length > 0) + matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i } + if matches.length > 0 + return matches + else + return nil + end + end + def help(plugin, topic="") - case topic + case plugin when "addquote" - return "addquote [] => Add quote for channel . You only need to supply if you are addressing #{@bot.nick} privately. Responds to !addquote without addressing if so configured" + _("addquote [] => Add quote for channel . You only need to supply if you are addressing %{nick} privately.") % { :nick => @bot.nick } when "delquote" - return "delquote [] => delete quote from with number . You only need to supply if you are addressing #{@bot.nick} privately. Responds to !delquote without addressing if so configured" + _("delquote [] => delete quote from with number . You only need to supply if you are addressing %{nick} privately.") % { :nick => @bot.nick } when "getquote" - return "getquote [] [] => get quote from with number . You only need to supply if you are addressing #{@bot.nick} privately. Without , a random quote will be returned. Responds to !getquote without addressing if so configured" + _("getquote [] [] => get quote from with number . You only need to supply if you are addressing %{nick} privately. Without , a random quote will be returned.") % { :nick => @bot.nick } when "searchquote" - return "searchquote [] => search for quote from that matches . You only need to supply if you are addressing #{@bot.nick} privately. Responds to !searchquote without addressing if so configured" + _("searchquote [] => search for quote from that matches . You only need to supply if you are addressing %{nick} privately.") % { :nick => @bot.nick } + when "listquotes" + _("listquotes [] => list the quotes from that match . You only need to supply if you are addressing %{nick} privately.") % { :nick => @bot.nick } when "topicquote" - return "topicquote [] [] => set topic to quote from with number . You only need to supply if you are addressing #{@bot.nick} privately. Without , a random quote will be set. Responds to !topicquote without addressing if so configured" + _("topicquote [] [] => set topic to quote from with number . You only need to supply if you are addressing %{nick} privately. Without , a random quote will be set.") % { :nick => @bot.nick } when "countquote" - return "countquote [] => count quotes from that match . You only need to supply if you are addressing #{@bot.nick} privately. Responds to !countquote without addressing if so configured" + _("countquote [] => count quotes from that match . You only need to supply if you are addressing %{nick} privately.") % { :nick => @bot.nick } when "whoquote" - return "whoquote [] => show who added quote . You only need to supply if you are addressing #{@bot.nick} privately" + _("whoquote [] => show who added quote . You only need to supply if you are addressing %{nick} privately") % { :nick => @bot.nick } when "whenquote" - return "whenquote [] => show when quote was added. You only need to supply if you are addressing #{@bot.nick} privately" + _("whenquote [] => show when quote was added. You only need to supply if you are addressing %{nick} privately") % { :nick => @bot.nick } + when "lastquote" + _("lastquote [] => show the last quote in a given channel. You only need to supply if you are addressing %{nick} privately") % { :nick => @bot.nick } else - return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote" + _("Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, listquotes, topicquote, countquote, whoquote, whenquote, lastquote") % { :nick => @bot.nick } end end @@ -155,7 +177,7 @@ class QuotePlugin < Plugin if delquote(channel, num) m.okay else - m.reply "quote not found!" + m.reply _("quote not found!") end end @@ -215,6 +237,20 @@ class QuotePlugin < Plugin end end + def cmd_listquotes(m, p) + channel = p[:channel] || m.channel.to_s + reg = p[:reg].to_s + if quotes = listquotes(m.source, channel, reg) + m.reply _("%{total} quotes matching %{reg} found: %{list}") % { + :total => quotes.size, + :reg => reg, + :list => quotes.map {|q| q.num }.join(', ') + } + else + m.reply _("quote not found!") + end + end + def cmd_countquote(m, p) channel = p[:channel] || m.channel.to_s reg = p[:reg] ? p[:reg].to_s : nil @@ -242,11 +278,38 @@ class QuotePlugin < Plugin m.reply _("quote not found!") end end + + def cmd_lastquote(m, p) + channel = p[:channel] || m.channel.to_s + quote, total = getquote(m.source, channel, lastquote(channel)) + if quote + m.reply _("[%{num}] %{quote}") % { + :num => quote.num, + :quote => quote.quote + } + else + m.reply _("quote not found!") + end + end end plugin = QuotePlugin.new plugin.register("quotes") +plugin.default_auth('other::edit', false) # Prevent random people from editing other channels quote lists by default +plugin.default_auth('other::view', true) # But allow them to view them + +plugin.map "addquote :channel *quote", :action => :cmd_addquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::edit::add!' +plugin.map "delquote :channel :num", :action => :cmd_delquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::edit::del!' +plugin.map "getquote :channel [:num]", :action => :cmd_getquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::get!' +plugin.map "whoquote :channel :num", :action => :cmd_whoquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::who!' +plugin.map "whenquote :channel :num", :action => :cmd_whenquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::when!' +plugin.map "searchquote :channel *reg", :action => :cmd_searchquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::search!' +plugin.map "listquotes :channel *reg", :action => :cmd_listquotes, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::list!' +plugin.map "countquote :channel [*reg]", :action => :cmd_countquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::count!' +plugin.map "topicquote :channel [:num]", :action => :cmd_topicquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::topic!' +plugin.map "lastquote :channel", :action => :cmd_lastquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::last!' + plugin.default_auth('edit', false) # Prevent random people from removing quotes plugin.default_auth('edit::add', true) # But allow them to add them @@ -256,17 +319,8 @@ plugin.map "getquote [:num]", :action => :cmd_getquote, :private => false, :requ plugin.map "whoquote :num", :action => :cmd_whoquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::who!' plugin.map "whenquote :num", :action => :cmd_whenquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::view::when!' plugin.map "searchquote *reg", :action => :cmd_searchquote, :private => false, :auth_path => '!quote::view::search!' +plugin.map "listquotes *reg", :action => :cmd_listquotes, :private => false, :auth_path => '!quote::view::list!' plugin.map "countquote [*reg]", :action => :cmd_countquote, :private => false, :auth_path => '!quote::view::count!' plugin.map "topicquote [:num]", :action => :cmd_topicquote, :private => false, :requirements => { :num => /^\d+$/ }, :auth_path => '!quote::topic!' +plugin.map "lastquote", :action => :cmd_lastquote, :private => false, :auth_path => '!quote::view::last!' -plugin.default_auth('other::edit', false) # Prevent random people from editing other channels quote lists by default -plugin.default_auth('other::view', true) # But allow them to view them - -plugin.map "addquote :channel *quote", :action => :cmd_addquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::edit::add!' -plugin.map "delquote :channel :num", :action => :cmd_delquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::edit::del!' -plugin.map "getquote :channel [:num]", :action => :cmd_getquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::get!' -plugin.map "whoquote :channel :num", :action => :cmd_whoquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::who!' -plugin.map "whenquote :channel :num", :action => :cmd_whenquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::view::when!' -plugin.map "searchquote :channel *reg", :action => :cmd_searchquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::search!' -plugin.map "countquote [:channel] [*reg]", :action => :cmd_countquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN }, :auth_path => '!quote::other::view::count!' -plugin.map "topicquote :channel [:num]", :action => :cmd_topicquote, :requirements => { :channel => Regexp::Irc::GEN_CHAN, :num => /^\d+$/ }, :auth_path => '!quote::other::topic!'