]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/quotes.rb
added 'alias rm' as alternative for 'alias remove'
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / quotes.rb
index 30e341bf81772a2fe459d6415830c4656cadd64e..2d9bedcd00b614cc872b877b54c2f5a4ee7ec798 100644 (file)
@@ -1,11 +1,18 @@
-# GB: Ok, we *really* need to switch to db for this plugin too
+#-- vim:sw=2:et
+#++
+#
+# :title: Quotes plugin
+#
+# TODO:: use message mapper instead of multiple ifs
+# TODO:: switch to db
 
-Quote = Struct.new("Quote", :num, :date, :source, :quote) unless defined?(Struct::Quote)
+define_structure :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)
@@ -16,22 +23,25 @@ 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"))
-    Dir.mkdir("#{@bot.botclass}/quotes/new") if(!FileTest.directory?("#{@bot.botclass}/quotes/new"))
     @lists.each {|channel, quotes|
       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}"
+        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}"
+            }
           }
-        }
-        debug "Officializing quotefile for channel #{channel} ..."
-        File.rename("#{@bot.botclass}/quotes/new/#{channel}",
-                    "#{@bot.botclass}/quotes/#{channel}")
+          @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}"
@@ -39,12 +49,20 @@ class QuotePlugin < Plugin
       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)
     return nil unless(@lists.has_key?(channel))
     return nil unless(@lists[channel].length > 0)
@@ -58,16 +76,19 @@ class QuotePlugin < Plugin
       @lists[channel].length - 1
     end
   end
+
   def delquote(channel, num)
     return false unless(@lists.has_key?(channel))
     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
   end
+
   def countquote(source, channel=nil, regexp=nil)
     unless(channel)
       total=0
@@ -85,6 +106,7 @@ class QuotePlugin < Plugin
     end
     return matches.length
   end
+
   def searchquote(source, channel, regexp)
     return nil unless(@lists.has_key?(channel))
     return nil unless(@lists[channel].length > 0)
@@ -95,6 +117,7 @@ class QuotePlugin < Plugin
       return nil
     end
   end
+
   def help(plugin, topic="")
     case topic
     when "addquote"
@@ -117,6 +140,7 @@ class QuotePlugin < Plugin
       return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
     end
   end
+
   def listen(m)
     return unless(m.kind_of? PrivMessage)
 
@@ -126,7 +150,7 @@ class QuotePlugin < Plugin
         when (/^addquote\s+(#\S+)\s+(.*)/)
           channel = $1
           quote = $2
-          if(@bot.auth.allow?("addquote", m.source, m.replyto))
+          if(@bot.auth.allow?("quote::other::add", m.source, m.replyto))
             if(channel =~ /^#/)
               num = addquote(m.source, channel, quote)
               m.reply "added the quote (##{num})"
@@ -134,7 +158,7 @@ class QuotePlugin < Plugin
           end
         when (/^getquote\s+(#\S+)$/)
           channel = $1
-          if(@bot.auth.allow?("getquote", m.source, m.replyto))
+          if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
             quote, total = getquote(m.source, channel)
             if(quote)
               m.reply "[#{quote.num}] #{quote.quote}"
@@ -145,7 +169,7 @@ class QuotePlugin < Plugin
         when (/^getquote\s+(#\S+)\s+(\d+)$/)
           channel = $1
           num = $2.to_i
-          if(@bot.auth.allow?("getquote", m.source, m.replyto))
+          if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
             quote, total = getquote(m.source, channel, num)
             if(quote)
               m.reply "[#{quote.num}] #{quote.quote}"
@@ -156,7 +180,7 @@ class QuotePlugin < Plugin
         when (/^whoquote\s+(#\S+)\s+(\d+)$/)
           channel = $1
           num = $2.to_i
-          if(@bot.auth.allow?("getquote", m.source, m.replyto))
+          if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
             quote, total = getquote(m.source, channel, num)
             if(quote)
               m.reply "quote #{quote.num} added by #{quote.source}"
@@ -167,7 +191,7 @@ class QuotePlugin < Plugin
         when (/^whenquote\s+(#\S+)\s+(\d+)$/)
           channel = $1
           num = $2.to_i
-          if(@bot.auth.allow?("getquote", m.source, m.replyto))
+          if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
             quote, total = getquote(m.source, channel, num)
             if(quote)
               m.reply "quote #{quote.num} added on #{quote.date}"
@@ -177,7 +201,7 @@ class QuotePlugin < Plugin
           end
         when (/^topicquote\s+(#\S+)$/)
           channel = $1
-          if(@bot.auth.allow?("topicquote", m.source, m.replyto))
+          if(@bot.auth.allow?("quote::other::topic", m.source, m.replyto))
             quote, total = getquote(m.source, channel)
             if(quote)
               @bot.topic channel, "[#{quote.num}] #{quote.quote}"
@@ -188,7 +212,7 @@ class QuotePlugin < Plugin
         when (/^topicquote\s+(#\S+)\s+(\d+)$/)
           channel = $1
           num = $2.to_i
-          if(@bot.auth.allow?("topicquote", m.source, m.replyto))
+          if(@bot.auth.allow?("quote::other::topic", m.source, m.replyto))
             quote, total = getquote(m.source, channel, num)
             if(quote)
               @bot.topic channel, "[#{quote.num}] #{quote.quote}"
@@ -199,7 +223,7 @@ class QuotePlugin < Plugin
         when (/^delquote\s+(#\S+)\s+(\d+)$/)
           channel = $1
           num = $2.to_i
-          if(@bot.auth.allow?("delquote", m.source, m.replyto))
+          if(@bot.auth.allow?("quote::other::del", m.source, m.replyto))
             if(delquote(channel, num))
               m.okay
             else
@@ -209,7 +233,7 @@ class QuotePlugin < Plugin
         when (/^searchquote\s+(#\S+)\s+(.*)$/)
           channel = $1
           reg = $2
-          if(@bot.auth.allow?("getquote", m.source, m.replyto))
+          if(@bot.auth.allow?("quote::other::get", m.source, m.replyto))
             quote, total = searchquote(m.source, channel, reg)
             if(quote)
               m.reply "[#{quote.num}] #{quote.quote}"
@@ -218,14 +242,14 @@ class QuotePlugin < Plugin
             end
           end
         when (/^countquote$/)
-          if(@bot.auth.allow?("getquote", m.source, m.replyto))
+          if(@bot.auth.allow?("quote::get::count", m.source, m.replyto))
             total = countquote(m.source)
             m.reply "#{total} quotes"
           end
         when (/^countquote\s+(#\S+)\s*(.*)$/)
           channel = $1
           reg = $2
-          if(@bot.auth.allow?("getquote", m.source, m.replyto))
+          if(@bot.auth.allow?("quote::other::get::count", m.source, m.replyto))
             total = countquote(m.source, channel, reg)
             if(reg.length > 0)
               m.reply "#{total} quotes match: #{reg}"
@@ -237,13 +261,13 @@ class QuotePlugin < Plugin
     elsif (m.address? || (@bot.config["QUOTE_LISTEN"] && command.gsub!(/^!/, "")))
       case command
         when (/^addquote\s+(.+)/)
-          if(@bot.auth.allow?("addquote", m.source, m.replyto))
-            num = addquote(m.source, m.target, $1)
+          if(@bot.auth.allow?("quote::add", m.source, m.replyto))
+            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)
+          if(@bot.auth.allow?("quote::get", m.source, m.replyto))
+            quote, total = getquote(m.source, m.target.to_s)
             if(quote)
               m.reply "[#{quote.num}] #{quote.quote}"
             else
@@ -252,8 +276,8 @@ class QuotePlugin < Plugin
           end
         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)
+          if(@bot.auth.allow?("quote::get", m.source, m.replyto))
+            quote, total = getquote(m.source, m.target.to_s, num)
             if(quote)
               m.reply "[#{quote.num}] #{quote.quote}"
             else
@@ -262,8 +286,8 @@ class QuotePlugin < Plugin
           end
         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)
+          if(@bot.auth.allow?("quote::get", m.source, m.replyto))
+            quote, total = getquote(m.source, m.target.to_s, num)
             if(quote)
               m.reply "quote #{quote.num} added on #{quote.date}"
             else
@@ -272,8 +296,8 @@ class QuotePlugin < Plugin
           end
         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)
+          if(@bot.auth.allow?("quote::get", m.source, m.replyto))
+            quote, total = getquote(m.source, m.target.to_s, num)
             if(quote)
               m.reply "quote #{quote.num} added by #{quote.source}"
             else
@@ -281,8 +305,8 @@ class QuotePlugin < Plugin
             end
           end
         when (/^topicquote$/)
-          if(@bot.auth.allow?("topicquote", m.source, m.replyto))
-            quote, total = getquote(m.source, m.target)
+          if(@bot.auth.allow?("quote::topic", m.source, m.replyto))
+            quote, total = getquote(m.source, m.target.to_s)
             if(quote)
               @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
             else
@@ -291,8 +315,8 @@ class QuotePlugin < Plugin
           end
         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)
+          if(@bot.auth.allow?("quote::topic", m.source, m.replyto))
+            quote, total = getquote(m.source, m.target.to_s, num)
             if(quote)
               @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
             else
@@ -301,8 +325,8 @@ class QuotePlugin < Plugin
           end
         when (/^delquote\s+(\d+)$/)
           num = $1.to_i
-          if(@bot.auth.allow?("delquote", m.source, m.replyto))
-            if(delquote(m.target, num))
+          if(@bot.auth.allow?("quote::del", m.source, m.replyto))
+            if(delquote(m.target.to_s, num))
               m.okay
             else
               m.reply "quote not found!"
@@ -310,8 +334,8 @@ class QuotePlugin < Plugin
           end
         when (/^searchquote\s+(.*)$/)
           reg = $1
-          if(@bot.auth.allow?("getquote", m.source, m.replyto))
-            quote, total = searchquote(m.source, m.target, reg)
+          if(@bot.auth.allow?("quote::get", m.source, m.replyto))
+            quote, total = searchquote(m.source, m.target.to_s, reg)
             if(quote)
               m.reply "[#{quote.num}] #{quote.quote}"
             else
@@ -320,8 +344,8 @@ class QuotePlugin < Plugin
           end
         when (/^countquote(?:\s+(.*))?$/)
           reg = $1
-          if(@bot.auth.allow?("getquote", m.source, m.replyto))
-            total = countquote(m.source, m.target, reg)
+          if(@bot.auth.allow?("quote::get::count", m.source, m.replyto))
+            total = countquote(m.source, m.target.to_s, reg)
             if(reg && reg.length > 0)
               m.reply "#{total} quotes match: #{reg}"
             else
@@ -332,5 +356,9 @@ class QuotePlugin < Plugin
     end
   end
 end
+
 plugin = QuotePlugin.new
 plugin.register("quotes")
+plugin.default_auth('other', false) # Prevent random people from editing other channels quote lists by default
+plugin.default_auth('other::get', true) # But allow them to view them
+plugin.default_auth('del', false) # Prevent random people from removing quotes