]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/bans.rb
Plugin header boilerplating.
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / bans.rb
index 03fe2f343af5c43d7da957a4d5316acee2085712..17281d4cc0b83162924303ee61c3a9c516ee36d6 100644 (file)
@@ -1,10 +1,19 @@
-# BansPlugin v3 for 0.9.11\r
+#-- vim:sw=2:et\r
+#++\r
 #\r
-# Managing kick and bans, automatically removing bans after timeouts, quiet bans, and kickban/quietban based on regexp\r
+# :title: Bans Plugin v3 for rbot 0.9.11 and later\r
 #\r
-# (c) 2006 Marco Gulino <marco@kmobiletools.org>\r
-# (c) 2007 kamu <mr.kamu@gmail.com>\r
-# (c) 2007 Giuseppe Bilotta <giuseppe.bilotta@gmail.com>\r
+# Author:: Marco Gulino <marco@kmobiletools.org>\r
+# Author:: kamu <mr.kamu@gmail.com>\r
+# Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>\r
+#\r
+# Copyright:: (C) 2006 Marco Gulino\r
+# Copyright:: (C) 2007 kamu, Giuseppe Bilotta\r
+#\r
+# License:: GPL V2.\r
+#\r
+# Managing kick and bans, automatically removing bans after timeouts, quiet\r
+# bans, and kickban/quietban based on regexp\r
 #\r
 # v1 -> v2 (kamu's version, never released)\r
 #   * reworked\r
 #\r
 # v2 -> v3 (GB)\r
 #   * remove the 'bans' prefix from most of the commands\r
-#   * (un)quiet has been renamed to (un)silence because 'quiet' was used to tell the bot to keep quiet\r
+#   * (un)quiet has been renamed to (un)silence because 'quiet' was used to\r
+#     tell the bot to keep quiet\r
 #   * both (un)quiet and (un)silence are accepted as actions\r
 #   * use the more descriptive 'onjoin' term for autoactions\r
-#   * convert v1's (0.9.10) :bans and :bansmasks to BadWordActions and WhitelistEntries\r
+#   * convert v1's (0.9.10) :bans and :bansmasks to BadWordActions and\r
+#     WhitelistEntries\r
 #   * enhanced list manipulation facilities\r
 #   * fixed regexp usage in requirements for plugin map\r
 #   * add proper auth management\r
-#\r
-# Licensed under GPL V2.\r
 \r
 OnJoinAction = Struct.new("OnJoinAction", :host, :action, :channel, :reason)\r
 BadWordAction = Struct.new("BadWordAction", :regexp, :action, :channel, :timer, :reason)\r
@@ -41,7 +50,7 @@ class BansPlugin < Plugin
   end\r
 \r
   def make_badword_rx(txt)\r
-    return /\b#{txt}\b/i\r
+    return /\b(?:#{txt})\b/i\r
   end\r
 \r
   def initialize\r
@@ -63,7 +72,7 @@ class BansPlugin < Plugin
           next\r
         end\r
         bans.delete(ar)\r
-        chan = ar[1]\r
+        chan = ar[1].downcase\r
         regexp = make_badword_rx(ar[2])\r
         badwords << BadWordAction.new(regexp, action, chan, "0s", "")\r
       }\r
@@ -203,6 +212,7 @@ class BansPlugin < Plugin
 \r
       m.okay\r
     rescue\r
+      error $!\r
       m.reply $!\r
     end\r
   end\r
@@ -218,7 +228,8 @@ class BansPlugin < Plugin
     autos = @registry[:onjoin]\r
     count = autos.length\r
 \r
-    idx = params[:idx].to_i\r
+    idx = nil\r
+    idx = params[:idx].to_i if params[:idx]\r
 \r
     if idx\r
       if idx > count\r
@@ -236,15 +247,20 @@ class BansPlugin < Plugin
           autos.delete rule if rule.host == host\r
         }\r
       rescue\r
+        error $!\r
         m.reply $!\r
       end\r
     end\r
     @registry[:onjoin] = autos\r
-    m.okay if count > autos.length\r
+    if count > autos.length\r
+      m.okay\r
+    else\r
+      m.reply "No matching onjoin rule for #{host} found"\r
+    end\r
   end\r
 \r
   def add_badword(m, params=nil)\r
-    regexp, channel = make_badword_rx(params[:regexp]), params[:channel].dup\r
+    regexp, channel = make_badword_rx(params[:regexp]), params[:channel].downcase.dup\r
     action, timer, reason = params[:action], params[:timer].dup, params[:reason].to_s\r
 \r
     badwords = @registry[:badwords]\r
@@ -266,7 +282,8 @@ class BansPlugin < Plugin
     badwords = @registry[:badwords]\r
     count = badwords.length\r
 \r
-    idx = params[:idx].to_i\r
+    idx = nil\r
+    idx = params[:idx].to_i if params[:idx]\r
 \r
     if idx\r
       if idx > count\r
@@ -282,12 +299,17 @@ class BansPlugin < Plugin
 \r
       badwords.each { |badword|\r
         next unless ['all', badword.channel].include?(channel)\r
+       debug "Removing #{badword.inspect}" if badword == regexp\r
         badwords.delete(badword) if badword == regexp\r
       }\r
     end\r
 \r
     @registry[:badwords] = badwords\r
-    m.okay if count > badwords.length\r
+    if count > badwords.length\r
+      m.okay\r
+    else\r
+      m.reply "No matching badword #{regexp} found"\r
+    end\r
   end\r
 \r
   def add_whitelist(m, params=nil)\r
@@ -301,6 +323,7 @@ class BansPlugin < Plugin
 \r
       m.okay\r
     rescue\r
+      error $!\r
       m.reply $!\r
     end\r
   end\r
@@ -316,7 +339,8 @@ class BansPlugin < Plugin
     wl = @registry[:whitelist]\r
     count = wl.length\r
 \r
-    idx = params[:idx].to_i\r
+    idx = nil\r
+    idx = params[:idx].to_i if params[:idx]\r
 \r
     if idx\r
       if idx > count\r
@@ -334,34 +358,46 @@ class BansPlugin < Plugin
           wl.delete rule if rule.host == host\r
         }\r
       rescue\r
+        error $!\r
         m.reply $!\r
       end\r
     end\r
     @registry[:whitelist] = wl\r
-    m.okay if count > whitelist.length\r
+    if count > whitelist.length\r
+      m.okay\r
+    else\r
+      m.reply "No host matching #{host}"\r
+    end\r
   end\r
 \r
   private\r
   def check_channel(m, strchannel)\r
     begin\r
-      raise "must specify channel if using privmsg" if strchannel.empty? and m.private?\r
-      channel = strchannel.empty? ? m.target : m.server.channel(strchannel)\r
-      raise "I am not in that channel" unless channel.has_user?(@bot)\r
+      raise "must specify channel if using privmsg" if m.private? and not strchannel\r
+      channel = m.server.channel(strchannel) || m.target\r
+      raise "I am not in that channel" unless channel.has_user?(@bot.nick)\r
 \r
       return channel\r
     rescue\r
+      error $!\r
       m.reply $!\r
     end\r
   end\r
 \r
-  def do_cmd(action, nick, channel, timer="0s", reason="")\r
-    if timer.eql? "0s"\r
+  def do_cmd(action, nick, channel, timer_in=nil, reason=nil)\r
+    case timer_in\r
+    when nil\r
       timer = 0\r
+    when /^(\d+)s$/\r
+      timer = $1.to_i\r
+    when /^(\d+)m$/\r
+      timer = $1.to_i * 60\r
+    when /^(\d+)h$/\r
+      timer = $1.to_i * 60 * 60 \r
+    when /^(\d+)d$/\r
+      timer = $1.to_i * 60 * 60 * 24\r
     else\r
-      timer = $1.to_i if timer =~ /^(\d+)s$/\r
-      timer = $1.to_i * 60 if timer =~ /^(\d+)m$/\r
-      timer = $1.to_i * 60 * 60 if timer =~ /^(\d+)h$/\r
-      timer = $1.to_i * 60 * 60 * 24 if timer =~ /^(\d+)d$/\r
+      raise "Wrong time specifications"\r
     end\r
 \r
     case action\r
@@ -402,27 +438,27 @@ plugin.default_auth( 'list', true )
 \r
 plugin.map 'ban :nick :timer :channel', :action => 'ban_user',\r
   :requirements => {:timer => BansPlugin::TimerRe, :channel => BansPlugin::ChannelRe},\r
-  :defaults => {:timer => '0s', :channel => ''},\r
+  :defaults => {:timer => nil, :channel => nil},\r
   :auth_path => 'act'\r
 plugin.map 'unban :nick :channel', :action => 'unban_user',\r
   :requirements => {:channel => BansPlugin::ChannelRe},\r
-  :defaults => {:channel => ''},\r
+  :defaults => {:channel => nil},\r
   :auth_path => 'act'\r
 plugin.map 'kick :nick :channel *reason', :action => 'kick_user',\r
   :requirements => {:channel => BansPlugin::ChannelRe},\r
-  :defaults => {:channel => '', :reason => ''},\r
+  :defaults => {:channel => nil, :reason => 'requested'},\r
   :auth_path => 'act'\r
 plugin.map 'kickban :nick :timer :channel *reason', :action => 'kickban_user',\r
   :requirements => {:timer => BansPlugin::TimerRe, :channel => BansPlugin::ChannelRe},\r
-  :defaults => {:timer => '0s', :channel => '', :reason => ''},\r
+  :defaults => {:timer => nil, :channel => nil, :reason => 'requested'},\r
   :auth_path => 'act'\r
 plugin.map 'silence :nick :timer :channel', :action => 'silence_user',\r
   :requirements => {:timer => BansPlugin::TimerRe, :channel => BansPlugin::ChannelRe},\r
-  :defaults => {:timer => '0s', :channel => ''},\r
+  :defaults => {:timer => nil, :channel => nil},\r
   :auth_path => 'act'\r
 plugin.map 'unsilence :nick :channel', :action => 'unsilence_user',\r
   :requirements => {:channel => BansPlugin::ChannelRe},\r
-  :defaults => {:channel => ''},\r
+  :defaults => {:channel => nil},\r
   :auth_path => 'act'\r
 \r
 plugin.map 'bans add onjoin :host :action :channel *reason', :action => 'add_onjoin',\r