X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fquotes.rb;h=390b98f7077aa6753ef7a4e5013baeef2cf17fb0;hb=ad4bb5bf4fce69f1848f8b717abbab849fc37805;hp=10743dfa5aea4c3ece0405ffee5a475c4a03842e;hpb=ae8a977eb06d529038155f6679fefecee3ee7354;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/quotes.rb b/data/rbot/plugins/quotes.rb index 10743dfa..390b98f7 100644 --- a/data/rbot/plugins/quotes.rb +++ b/data/rbot/plugins/quotes.rb @@ -1,10 +1,13 @@ -Quote = Struct.new("Quote", "num", "date", "source", "quote") +# GB: Ok, we *really* need to switch to db for this plugin too + +Quote = Struct.new("Quote", :num, :date, :source, :quote) class QuotePlugin < Plugin def initialize super @lists = Hash.new Dir["#{@bot.botclass}/quotes/*"].each {|f| + next if File.directory?(f) channel = File.basename(f) @lists[channel] = Array.new if(!@lists.has_key?(channel)) IO.foreach(f) {|line| @@ -17,12 +20,23 @@ class QuotePlugin < Plugin end def save Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes")) + Dir.mkdir("#{@bot.botclass}/quotes/new") if(!FileTest.directory?("#{@bot.botclass}/quotes/new")) @lists.each {|channel, quotes| - File.open("#{@bot.botclass}/quotes/#{channel}", "w") {|file| - quotes.compact.each {|q| - file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}" + begin + debug "Writing new quotefile for channel #{channel} ..." + File.open("#{@bot.botclass}/quotes/new/#{channel}", "w") {|file| + quotes.compact.each {|q| + file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}" + } } - } + debug "Officializing quotefile for channel #{channel} ..." + File.rename("#{@bot.botclass}/quotes/new/#{channel}", + "#{@bot.botclass}/quotes/#{channel}") + rescue => e + error "failed to write quotefile for channel #{channel}!\n#{$!}" + error "#{e.class}: #{e}" + error e.backtrace.join("\n") + end } end def addquote(source, channel, quote) @@ -41,7 +55,7 @@ class QuotePlugin < Plugin else # random quote return @lists[channel].compact[rand(@lists[channel].nitems)], - @lists[channel].length - 1 + @lists[channel].length - 1 end end def delquote(channel, num) @@ -76,31 +90,31 @@ class QuotePlugin < Plugin return nil unless(@lists[channel].length > 0) matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i } if(matches.length > 0) - return matches[rand(matches.length)], @lists[channel].length - 1 + return matches[rand(matches.length)], @lists[channel].length - 1 else return nil end end def help(plugin, topic="") case topic - 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" - 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" - 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" - 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" - 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" - 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" - when "whoquote" - return "whoquote [] => show who added quote . You only need to supply if you are addressing #{@bot.nick} privately" - when "whenquote" - return "whenquote [] => show when quote was added. You only need to supply if you are addressing #{@bot.nick} privately" - else - return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote" + 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" + 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" + 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" + 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" + 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" + 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" + when "whoquote" + return "whoquote [] => show who added quote . You only need to supply if you are addressing #{@bot.nick} privately" + when "whenquote" + return "whenquote [] => show when quote was added. You only need to supply if you are addressing #{@bot.nick} privately" + else + return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote" end end def listen(m)