]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
Sun Jul 31 02:20:08 BST 2005 Tom Gilbert <tom@linuxbrit.co.uk>
authorTom Gilbert <tom@linuxbrit.co.uk>
Sun, 31 Jul 2005 01:21:02 +0000 (01:21 +0000)
committerTom Gilbert <tom@linuxbrit.co.uk>
Sun, 31 Jul 2005 01:21:02 +0000 (01:21 +0000)
  * Updated docgen to generate rdoc again with the new repo structure
* added new restart command to the core bot, quits irc and reexecs the
bot, to pick up new code/libraries etc.

COPYING
ChangeLog
bin/rbot
docgen
lib/rbot/httputil.rb
lib/rbot/ircbot.rb
lib/rbot/language.rb
lib/rbot/messagemapper.rb
lib/rbot/plugins.rb

diff --git a/COPYING b/COPYING
index 7899637e127972f62d5bc0094fec2c4247eee947..bc09ecc2e6a51fe2f6e46b63aba71e8cee396244 100644 (file)
--- a/COPYING
+++ b/COPYING
@@ -1,4 +1,4 @@
-Copyright (C) 2002 Tom Gilbert.
+Copyright (C) 2002-2005 Tom Gilbert.
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to
index af545d9ad6102089f82641bead64acadb52c8b30..d470bae421cf4a1e25b370dfe0c02990c3ba58f1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Sun Jul 31 02:20:08 BST 2005  Tom Gilbert <tom@linuxbrit.co.uk>
+
+  * Updated docgen to generate rdoc again with the new repo structure
+       * added new restart command to the core bot, quits irc and reexecs the
+       bot, to pick up new code/libraries etc.
+
 Sat Jul 30 22:33:36 BST 2005  Tom Gilbert <tom@linuxbrit.co.uk>
 
   * Config items are now objects, various types are available.
index f65cb949875b0270663f59a11b6ef7ca2a3a26b0..38cf7e49ef9e58642fce3f905bed985017146dcb 100755 (executable)
--- a/bin/rbot
+++ b/bin/rbot
@@ -45,6 +45,8 @@ def debug(message=nil)
   #yield
 end
 
+orig_opts = ARGV.dup
+
 opts = GetoptLong.new(
   ["--debug", "-d", GetoptLong::NO_ARGUMENT],
   ["--help",  "-h", GetoptLong::NO_ARGUMENT],
@@ -80,7 +82,7 @@ if ($opts["help"])
   exit 0
 end
 
-if(bot = Irc::IrcBot.new(ARGV.shift))
+if(bot = Irc::IrcBot.new(ARGV.shift, :argv => orig_opts))
   # just run the bot
   bot.mainloop
 end
diff --git a/docgen b/docgen
index bae6c807ee34a0badee602885cf17dcabdadbe95..c7943409ed1070063153af89c1bce9f0aab5d337 100755 (executable)
--- a/docgen
+++ b/docgen
@@ -1,3 +1,3 @@
 #!/bin/sh
-rdoc -a --exclude 'rbot/(db)?plugins/' --main rbot.rb -d
+rdoc -a -i 'lib' --exclude '(data/|setup.rb|post-config.rb|rbotconfig.rb|timertest.rb)' --main lib/rbot/ircbot.rb -o doc lib bin
 
index b49a42b1293fd46d4badf2e1b7e9f2fca24aae22..c6f51d8e5a39c9439d99ac289550e747bb8742d1 100644 (file)
@@ -1,4 +1,5 @@
 module Irc
+module Utils
 
 require 'resolv'
 require 'net/http'
@@ -135,5 +136,5 @@ class HttpUtil
     return nil
   end
 end
-
+end
 end
index d0010c2ae42ba4620d6a0fffdde664cb98597a9d..6bdf7bdf8eef03f59bf1a1ffa7efa88840183adb 100644 (file)
@@ -81,7 +81,7 @@ class IrcBot
   attr_reader :httputil
 
   # create a new IrcBot with botclass +botclass+
-  def initialize(botclass)
+  def initialize(botclass, params = {})
     # BotConfig for the core bot
     BotConfig.register BotConfigStringValue.new('server.name',
       :default => "localhost", :requires_restart => true,
@@ -124,6 +124,8 @@ class IrcBot
       :desc => "(flood prevention) max lines to burst to the server before throttling. Most ircd's allow bursts of up 5 lines, with non-burst limits of 512 bytes/2 seconds",
       :on_change => Proc.new {|bot, v| bot.socket.sendq_burst = v })
 
+    @argv = params[:argv]
+
     unless FileTest.directory? Config::DATADIR
       puts "data directory '#{Config::DATADIR}' not found, did you install.rb?"
       exit 2
@@ -144,22 +146,22 @@ class IrcBot
     Dir.mkdir("#{botclass}/logs") unless File.exist?("#{botclass}/logs")
 
     @startup_time = Time.new
-    @config = Irc::BotConfig.new(self)
+    @config = BotConfig.new(self)
     @timer = Timer::Timer.new(1.0) # only need per-second granularity
     @registry = BotRegistry.new self
     @timer.add(@config['core.save_every']) { save } if @config['core.save_every']
     @channels = Hash.new
     @logs = Hash.new
     
-    @httputil = Irc::HttpUtil.new(self)
-    @lang = Irc::Language.new(@config['core.language'])
-    @keywords = Irc::Keywords.new(self)
-    @auth = Irc::IrcAuth.new(self)
+    @httputil = Utils::HttpUtil.new(self)
+    @lang = Language::Language.new(@config['core.language'])
+    @keywords = Keywords.new(self)
+    @auth = IrcAuth.new(self)
 
     Dir.mkdir("#{botclass}/plugins") unless File.exist?("#{botclass}/plugins")
-    @plugins = Irc::Plugins.new(self, ["#{botclass}/plugins"])
+    @plugins = Plugins::Plugins.new(self, ["#{botclass}/plugins"])
 
-    @socket = Irc::IrcSocket.new(@config['server.name'], @config['server.port'], @config['server.bindhost'], @config['server.sendq_delay'], @config['server.sendq_burst'])
+    @socket = IrcSocket.new(@config['server.name'], @config['server.port'], @config['server.bindhost'], @config['server.sendq_delay'], @config['server.sendq_burst'])
     @nick = @config['irc.nick']
     if @config['core.address_prefix']
       @addressing_prefixes = @config['core.address_prefix'].split(" ")
@@ -167,7 +169,7 @@ class IrcBot
       @addressing_prefixes = Array.new
     end
     
-    @client = Irc::IrcClient.new
+    @client = IrcClient.new
     @client["PRIVMSG"] = proc { |data|
       message = PrivMessage.new(self, data["SOURCE"], data["TARGET"], data["MESSAGE"])
       onprivmsg(message)
@@ -339,9 +341,14 @@ class IrcBot
             @client.process reply
           end
         end
+      rescue TimeoutError, SocketError => e
+        puts "network exception: connection closed: #{e}"
+        puts e.backtrace.join("\n")
+        @socket.close # now we reconnect
       rescue => e # TODO be selective, only grab Network errors
-        puts "connection closed: #{e}"
+        puts "unexpected exception: connection closed: #{e}"
         puts e.backtrace.join("\n")
+        exit 2
       end
       
       puts "disconnected"
@@ -439,14 +446,12 @@ class IrcBot
   def topic(where, topic)
     sendq "TOPIC #{where} :#{topic}"
   end
-  
-  # message:: optional IRC quit message
-  # quit IRC, shutdown the bot
-  def quit(message=nil)
+
+  def shutdown(message = nil)
     trap("SIGTERM", "DEFAULT")
     trap("SIGHUP", "DEFAULT")
     trap("SIGINT", "DEFAULT")
-    message = @lang.get("quit") if (!message || message.length < 1)
+    message = @lang.get("quit") if (message.nil? || message.empty?)
     @socket.clearq
     save
     @plugins.cleanup
@@ -458,9 +463,23 @@ class IrcBot
     @socket.shutdown
     @registry.close
     puts "rbot quit (#{message})"
+  end
+  
+  # message:: optional IRC quit message
+  # quit IRC, shutdown the bot
+  def quit(message=nil)
+    shutdown(message)
     exit 0
   end
 
+  # totally shutdown and respawn the bot
+  def restart
+    shutdown("restarting, back in #{@config['server.reconnect_wait']}...")
+    sleep @config['server.reconnect_wait']
+    # now we re-exec
+    exec($0, *@argv)
+  end
+
   # call the save method for bot's config, keywords, auth and all plugins
   def save
     @registry.flush
@@ -552,6 +571,8 @@ class IrcBot
     case topic
       when "quit"
         return "quit [<message>] => quit IRC with message <message>"
+      when "restart"
+        return "restart => completely stop and restart the bot (including reconnect)"
       when "join"
         return "join <channel> [<key>] => join channel <channel> with secret key <key> if specified. #{@nick} also responds to invites if you have the required access level"
       when "part"
@@ -581,7 +602,7 @@ class IrcBot
       when "hello"
         return "hello|hi|hey|yo [#{@nick}] => greet the bot"
       else
-        return "Core help topics: quit, join, part, hide, save, rescan, nick, say, action, topic, quiet, talk, version, botsnack, hello"
+        return "Core help topics: quit, restart, config, join, part, hide, save, rescan, nick, say, action, topic, quiet, talk, version, botsnack, hello"
     end
   end
 
@@ -623,6 +644,8 @@ class IrcBot
           part $1 if(@auth.allow?("join", m.source, m.replyto))
         when (/^quit(?:\s+(.*))?$/i)
           quit $1 if(@auth.allow?("quit", m.source, m.replyto))
+        when (/^restart$/i)
+          restart if(@auth.allow?("quit", m.source, m.replyto))
         when (/^hide$/i)
           join 0 if(@auth.allow?("join", m.source, m.replyto))
         when (/^save$/i)
@@ -671,29 +694,6 @@ class IrcBot
             @channels[where].quiet = false if(@channels.has_key?(where))
             m.okay
           end
-        # TODO break this out into a config module
-        when (/^options get sendq_delay$/i)
-          if auth.allow?("config", m.source, m.replyto)
-            m.reply "options->sendq_delay = #{@socket.sendq_delay}"
-          end
-        when (/^options get sendq_burst$/i)
-          if auth.allow?("config", m.source, m.replyto)
-            m.reply "options->sendq_burst = #{@socket.sendq_burst}"
-          end
-        when (/^options set sendq_burst (.*)$/i)
-          num = $1.to_i
-          if auth.allow?("config", m.source, m.replyto)
-            @socket.sendq_burst = num
-            @config['irc.sendq_burst'] = num
-            m.okay
-          end
-        when (/^options set sendq_delay (.*)$/i)
-          freq = $1.to_f
-          if auth.allow?("config", m.source, m.replyto)
-            @socket.sendq_delay = freq
-            @config['irc.sendq_delay'] = freq
-            m.okay
-          end
         when (/^status\??$/i)
           m.reply status if auth.allow?("status", m.source, m.replyto)
         when (/^registry stats$/i)
index d48607b83bef5dd4636bffa3fd9244a4da316f77..c472c12e5fe282ea6546e824746d08124249e2ce 100644 (file)
@@ -1,4 +1,5 @@
 module Irc
+module Language
 
   class Language
     BotConfig.register BotConfigEnumValue.new('core.language', 
@@ -63,3 +64,4 @@ module Irc
   end
 
 end
+end
index f83fafb2902d5c7e36fd78c54f502cb1a4d24365..445b80f5101b4b17a73bebd42bcf2493573b947b 100644 (file)
@@ -9,7 +9,7 @@ module Irc
     end
     
     def map(*args)
-      @routes << Route.new(*args)
+      @routes << Template.new(*args)
     end
     
     def each
@@ -53,7 +53,7 @@ module Irc
 
   end
 
-  class Route
+  class Template
     attr_reader :defaults # The defaults hash
     attr_reader :options  # The options hash
     attr_reader :items
index d4e5be9fa614bbef4ee35f3b6542897dc701aa7d..ba01aa7d5c83a2c6e37f6c59e4a2b85c1f6c80e5 100644 (file)
@@ -1,4 +1,5 @@
 module Irc
+module Plugins
   require 'rbot/messagemapper'
 
   # base class for all rbot plugins
@@ -284,3 +285,4 @@ module Irc
   end
 
 end
+end