]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/quotes.rb
ircbot: do not use debug in sendmsg() because it breaks script. script plugin: handle...
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / quotes.rb
index 674a9ed6c71bee9e825426f5641f9a7ae0167579..6851b65f2f2c0ff83b463482d5450a300ad2f728 100644 (file)
@@ -1,10 +1,14 @@
-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
+    @changed = 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|
@@ -13,22 +17,40 @@ class QuotePlugin < Plugin
           @lists[channel][num] = Quote.new(num, $2, $3, $4)
         end
       }
+      @changed[channel] = false
     }
   end
   def save
     Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes"))
     @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
+        if @changed[channel]
+          debug "Writing new quotefile for channel #{channel} ..."
+          Utils.safe_save("#{@bot.botclass}/quotes/#{channel}") {|file|
+            quotes.compact.each {|q| 
+              file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
+            }
+          }
+          @changed[channel] = false
+        else
+          debug "Not writing quotefile for channel #{channel} (unchanged)"
+        end
+      rescue => e
+        error "failed to write quotefile for channel #{channel}!\n#{$!}"
+        error "#{e.class}: #{e}"
+        error e.backtrace.join("\n")
+      end
     }
   end
+  def cleanup
+    @lists.clear
+    @changed.clear
+  end
   def addquote(source, channel, quote)
     @lists[channel] = Array.new if(!@lists.has_key?(channel))
     num = @lists[channel].length 
-    @lists[channel][num] = Quote.new(num, Time.new, source, quote)
+    @lists[channel][num] = Quote.new(num, Time.new, source.fullform, quote)
+    @changed[channel] = true
     return num
   end
   def getquote(source, channel, num=nil)
@@ -41,7 +63,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)
@@ -49,6 +71,8 @@ class QuotePlugin < Plugin
     return false unless(@lists[channel].length > 0)
     if(@lists[channel][num])
       @lists[channel][num] = nil
+      @lists[channel].pop if num == @lists[channel].length - 1
+      @changed[channel] = true
       return true
     end
     return false
@@ -75,31 +99,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 [<channel>] <quote> => Add quote <quote> for channel <channel>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !addquote without addressing if so configured"
-      when "delquote"
-        return "delquote [<channel>] <num> => delete quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !delquote without addressing if so configured"
-      when "getquote"
-        return "getquote [<channel>] [<num>] => get quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Without <num>, a random quote will be returned. Responds to !getquote without addressing if so configured"
-      when "searchquote"
-        return "searchquote [<channel>] <regexp> => search for quote from <channel> that matches <regexp>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !searchquote without addressing if so configured"
-      when "topicquote"
-        return "topicquote [<channel>] [<num>] => set topic to quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Without <num>, a random quote will be set. Responds to !topicquote without addressing if so configured"
-      when "countquote"
-        return "countquote [<channel>] <regexp> => count quotes from <channel> that match <regexp>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !countquote without addressing if so configured"
-      when "whoquote"
-        return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
-      when "whenquote"
-        return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
-      else
-        return "Quote module (Quote storage and retrieval) topics: addquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
+    when "addquote"
+      return "addquote [<channel>] <quote> => Add quote <quote> for channel <channel>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !addquote without addressing if so configured"
+    when "delquote"
+      return "delquote [<channel>] <num> => delete quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !delquote without addressing if so configured"
+    when "getquote"
+      return "getquote [<channel>] [<num>] => get quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Without <num>, a random quote will be returned. Responds to !getquote without addressing if so configured"
+    when "searchquote"
+      return "searchquote [<channel>] <regexp> => search for quote from <channel> that matches <regexp>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !searchquote without addressing if so configured"
+    when "topicquote"
+      return "topicquote [<channel>] [<num>] => set topic to quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Without <num>, a random quote will be set. Responds to !topicquote without addressing if so configured"
+    when "countquote"
+      return "countquote [<channel>] <regexp> => count quotes from <channel> that match <regexp>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !countquote without addressing if so configured"
+    when "whoquote"
+      return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
+    when "whenquote"
+      return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> 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)
@@ -223,12 +247,12 @@ class QuotePlugin < Plugin
       case command
         when (/^addquote\s+(.+)/)
           if(@bot.auth.allow?("addquote", m.source, m.replyto))
-            num = addquote(m.source, m.target, $1)
+            num = addquote(m.source, m.target.to_s, $1)
             m.reply "added the quote (##{num})"
           end
         when (/^getquote$/)
           if(@bot.auth.allow?("getquote", m.source, m.replyto))
-            quote, total = getquote(m.source, m.target)
+            quote, total = getquote(m.source, m.target.to_s)
             if(quote)
               m.reply "[#{quote.num}] #{quote.quote}"
             else
@@ -238,7 +262,7 @@ class QuotePlugin < Plugin
         when (/^getquote\s+(\d+)$/)
           num = $1.to_i
           if(@bot.auth.allow?("getquote", m.source, m.replyto))
-            quote, total = getquote(m.source, m.target, num)
+            quote, total = getquote(m.source, m.target.to_s, num)
             if(quote)
               m.reply "[#{quote.num}] #{quote.quote}"
             else
@@ -248,7 +272,7 @@ class QuotePlugin < Plugin
         when (/^whenquote\s+(\d+)$/)
           num = $1.to_i
           if(@bot.auth.allow?("getquote", m.source, m.replyto))
-            quote, total = getquote(m.source, m.target, num)
+            quote, total = getquote(m.source, m.target.to_s, num)
             if(quote)
               m.reply "quote #{quote.num} added on #{quote.date}"
             else
@@ -258,7 +282,7 @@ class QuotePlugin < Plugin
         when (/^whoquote\s+(\d+)$/)
           num = $1.to_i
           if(@bot.auth.allow?("getquote", m.source, m.replyto))
-            quote, total = getquote(m.source, m.target, num)
+            quote, total = getquote(m.source, m.target.to_s, num)
             if(quote)
               m.reply "quote #{quote.num} added by #{quote.source}"
             else
@@ -267,7 +291,7 @@ class QuotePlugin < Plugin
           end
         when (/^topicquote$/)
           if(@bot.auth.allow?("topicquote", m.source, m.replyto))
-            quote, total = getquote(m.source, m.target)
+            quote, total = getquote(m.source, m.target.to_s)
             if(quote)
               @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
             else
@@ -277,7 +301,7 @@ class QuotePlugin < Plugin
         when (/^topicquote\s+(\d+)$/)
           num = $1.to_i
           if(@bot.auth.allow?("topicquote", m.source, m.replyto))
-            quote, total = getquote(m.source, m.target, num)
+            quote, total = getquote(m.source, m.target.to_s, num)
             if(quote)
               @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
             else
@@ -287,7 +311,7 @@ class QuotePlugin < Plugin
         when (/^delquote\s+(\d+)$/)
           num = $1.to_i
           if(@bot.auth.allow?("delquote", m.source, m.replyto))
-            if(delquote(m.target, num))
+            if(delquote(m.target.to_s, num))
               m.okay
             else
               m.reply "quote not found!"
@@ -296,7 +320,7 @@ class QuotePlugin < Plugin
         when (/^searchquote\s+(.*)$/)
           reg = $1
           if(@bot.auth.allow?("getquote", m.source, m.replyto))
-            quote, total = searchquote(m.source, m.target, reg)
+            quote, total = searchquote(m.source, m.target.to_s, reg)
             if(quote)
               m.reply "[#{quote.num}] #{quote.quote}"
             else
@@ -306,7 +330,7 @@ class QuotePlugin < Plugin
         when (/^countquote(?:\s+(.*))?$/)
           reg = $1
           if(@bot.auth.allow?("getquote", m.source, m.replyto))
-            total = countquote(m.source, m.target, reg)
+            total = countquote(m.source, m.target.to_s, reg)
             if(reg && reg.length > 0)
               m.reply "#{total} quotes match: #{reg}"
             else