]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircbot.rb
ircbot: clean up ping timings on connect
[user/henk/code/ruby/rbot.git] / lib / rbot / ircbot.rb
index d5d3828be2cdb208beea16014d748d9ba219c491..6c8d6332b38e16177709f0cd45655741909ee588 100644 (file)
@@ -135,6 +135,12 @@ $interrupted = 0
 
 # these first
 require 'rbot/rbotconfig'
+begin
+  require 'rubygems'
+rescue LoadError
+  log "rubygems unavailable"
+end
+
 require 'rbot/load-gettext'
 require 'rbot/config'
 require 'rbot/config-compat'
@@ -205,11 +211,16 @@ class Bot
     @client.user
   end
 
-  # bot User in the client/server connection
+  # bot nick in the client/server connection
   def nick
     myself.nick
   end
 
+  # bot channels in the client/server connection
+  def channels
+    myself.channels
+  end
+
   # nick wanted by the bot. This defaults to the irc.nick config value,
   # but may be overridden by a manual !nick command
   def wanted_nick
@@ -437,41 +448,25 @@ class Bot
           botclass.gsub!("\\","/")
         end
       end
-      botclass += "/.rbot"
+      botclass = File.join(botclass, ".rbot")
     end
     botclass = File.expand_path(botclass)
     @botclass = botclass.gsub(/\/$/, "")
 
-    if FileTest.directory? botclass
-      # compare the templates dir with the current botclass, and fill it in with
-      # any missing file.
-      # Sadly, FileUtils.cp_r doesn't have an :update option, so we have to do it
-      # manually
-      template = File.join Config::datadir, 'templates'
-      # note that we use the */** pattern because we don't want to match
-      # keywords.rbot, which gets deleted on load and would therefore be missing always
-      missing = Dir.chdir(template) { Dir.glob('*/**') } - Dir.chdir(botclass) { Dir.glob('*/**') }
-      missing.map do |f|
-        dest = File.join(botclass, f)
-        FileUtils.mkdir_p File.dirname dest
-        FileUtils.cp File.join(template, f), dest
-      end
-    else
-      log "no #{botclass} directory found, creating from templates.."
-      if FileTest.exist? botclass
-        error "file #{botclass} exists but isn't a directory"
-        exit 2
-      end
-      FileUtils.cp_r Config::datadir+'/templates', botclass
-    end
-
-    Dir.mkdir("#{botclass}/registry") unless File.exist?("#{botclass}/registry")
-    Dir.mkdir("#{botclass}/safe_save") unless File.exist?("#{botclass}/safe_save")
+    repopulate_botclass_directory
 
-    # Time at which the last PING was sent
-    @last_ping = nil
-    # Time at which the last line was RECV'd from the server
-    @last_rec = nil
+    registry_dir = File.join(@botclass, 'registry')
+    Dir.mkdir(registry_dir) unless File.exist?(registry_dir)
+    unless FileTest.directory? registry_dir
+      error "registry storage location #{registry_dir} is not a directory"
+      exit 2
+    end
+    save_dir = File.join(@botclass, 'safe_save')
+    Dir.mkdir(save_dir) unless File.exist?(save_dir)
+    unless FileTest.directory? save_dir
+      error "safe save location #{save_dir} is not a directory"
+      exit 2
+    end
 
     @startup_time = Time.new
 
@@ -490,7 +485,9 @@ class Bot
 
     @logfile = @config['log.file']
     if @logfile.class!=String || @logfile.empty?
-      @logfile = "#{botclass}/#{File.basename(botclass).gsub(/^\.+/,'')}.log"
+      logfname =  File.basename(botclass).gsub(/^\.+/,'')
+      logfname << ".log"
+      @logfile = File.join(botclass, logfname)
       debug "Using `#{@logfile}' as debug log"
     end
 
@@ -549,7 +546,7 @@ class Bot
       end
     end
 
-    File.open($opts['pidfile'] || "#{@botclass}/rbot.pid", 'w') do |pf|
+    File.open($opts['pidfile'] || File.join(@botclass, 'rbot.pid'), 'w') do |pf|
       pf << "#{$$}\n"
     end
 
@@ -579,7 +576,6 @@ class Bot
     @auth.everyone.set_default_permission("*", true)
     @auth.botowner.password= @config['auth.password']
 
-    Dir.mkdir("#{botclass}/plugins") unless File.exist?("#{botclass}/plugins")
     @plugins = Plugins::manager
     @plugins.bot_associate(self)
     setup_plugins_path()
@@ -645,13 +641,15 @@ class Bot
       @config['irc.ignore_channels'].each { |channel|
         if m.target.downcase == channel.downcase
           m.ignored = true
+          break
         end
       }
       @config['irc.ignore_users'].each { |mask|
         if m.source.matches?(server.new_netmask(mask))
           m.ignored = true
+          break
         end
-      }
+      } unless m.ignored
 
       @plugins.irc_delegate('privmsg', m)
     }
@@ -774,15 +772,54 @@ class Bot
     trap_sigs
   end
 
+  def repopulate_botclass_directory
+    template_dir = File.join Config::datadir, 'templates'
+    if FileTest.directory? @botclass
+      # compare the templates dir with the current botclass dir, filling up the
+      # latter with any missing file. Sadly, FileUtils.cp_r doesn't have an
+      # :update option, so we have to do it manually.
+      # Note that we use the */** pattern because we don't want to match
+      # keywords.rbot, which gets deleted on load and would therefore be missing
+      # always
+      missing = Dir.chdir(template_dir) { Dir.glob('*/**') } - Dir.chdir(@botclass) { Dir.glob('*/**') }
+      missing.map do |f|
+        dest = File.join(@botclass, f)
+        FileUtils.mkdir_p(File.dirname(dest))
+        FileUtils.cp File.join(template_dir, f), dest
+      end
+    else
+      log "no #{@botclass} directory found, creating from templates..."
+      if FileTest.exist? @botclass
+        error "file #{@botclass} exists but isn't a directory"
+        exit 2
+      end
+      FileUtils.cp_r template_dir, @botclass
+    end
+  end
+
+  # Return a path under the current botclass by joining the mentioned
+  # components. The components are automatically converted to String
+  def path(*components)
+    File.join(@botclass, *(components.map {|c| c.to_s}))
+  end
+
   def setup_plugins_path
+    plugdir_default = File.join(Config::datadir, 'plugins')
+    plugdir_local = File.join(@botclass, 'plugins')
+    Dir.mkdir(plugdir_local) unless File.exist?(plugdir_local)
+
     @plugins.clear_botmodule_dirs
-    @plugins.add_botmodule_dir(Config::coredir + "/utils")
-    @plugins.add_botmodule_dir(Config::coredir)
-    @plugins.add_botmodule_dir("#{botclass}/plugins")
+    @plugins.add_core_module_dir(File.join(Config::coredir, 'utils'))
+    @plugins.add_core_module_dir(Config::coredir)
+    if FileTest.directory? plugdir_local
+      @plugins.add_plugin_dir(plugdir_local)
+    else
+      warning "local plugin location #{plugdir_local} is not a directory"
+    end
 
     @config['plugins.path'].each do |_|
-        path = _.sub(/^\(default\)/, Config::datadir + '/plugins')
-        @plugins.add_botmodule_dir(path)
+        path = _.sub(/^\(default\)/, plugdir_default)
+        @plugins.add_plugin_dir(path)
     end
   end
 
@@ -864,6 +901,9 @@ class Bot
 
   # connect the bot to IRC
   def connect
+    # make sure we don't have any spurious ping checks running
+    # (and initialize the vars if this is the first time we connect)
+    stop_server_pings
     begin
       quit if $interrupted > 0
       @socket.connect
@@ -941,7 +981,7 @@ class Bot
       rescue Errno::ETIMEDOUT, Errno::ECONNABORTED, TimeoutError, SocketError => e
         error "network exception: #{e.pretty_inspect}"
         quit_msg = e.to_s
-      rescue ServerError
+      rescue ServerError => e
         # received an ERROR from the server
         quit_msg = "server ERROR: " + e.message
         too_fast = e.message.index("reconnect too fast")
@@ -971,8 +1011,22 @@ class Bot
   # Type can be PRIVMSG, NOTICE, etc, but those you should really use the
   # relevant say() or notice() methods. This one should be used for IRCd
   # extensions you want to use in modules.
-  def sendmsg(type, where, original_message, options={})
-    opts = @default_send_options.merge(options)
+  def sendmsg(original_type, original_where, original_message, options={})
+
+    # filter message with sendmsg filters
+    ds = DataStream.new original_message.to_s.dup,
+      :type => original_type, :dest => original_where,
+      :options => @default_send_options.merge(options)
+    filters = filter_names(:sendmsg)
+    filters.each do |fname|
+      debug "filtering #{ds[:text]} with sendmsg filter #{fname}"
+      ds.merge! filter(self.global_filter_name(fname, :sendmsg), ds)
+    end
+
+    opts = ds[:options]
+    type = ds[:type]
+    where = ds[:dest]
+    filtered = ds[:text]
 
     # For starters, set up appropriate queue channels and rings
     mchan = opts[:queue_channel]
@@ -993,7 +1047,7 @@ class Bot
       end
     end
 
-    multi_line = original_message.to_s.gsub(/[\r\n]+/, "\n")
+    multi_line = filtered.gsub(/[\r\n]+/, "\n")
 
     # if target is a channel with nocolor modes, strip colours
     if where.kind_of?(Channel) and where.mode.any?(*config['server.nocolor_modes'])
@@ -1196,7 +1250,9 @@ class Bot
 
   # totally shutdown and respawn the bot
   def restart(message=nil)
-    message = "restarting, back in #{@config['server.reconnect_wait']}..." if (!message || message.empty?)
+    message = _("restarting, back in %{wait}...") % {
+      :wait => @config['server.reconnect_wait']
+    } if (!message || message.empty?)
     shutdown(message)
     sleep @config['server.reconnect_wait']
     begin