]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircbot.rb
Option to change the bot IRC name, thanks to jsn-
[user/henk/code/ruby/rbot.git] / lib / rbot / ircbot.rb
index a95301dd8aaf712b8c1ff042a2af9d6ea12ac9a2..7c0af845750676e148358213316dd8a881b1882f 100644 (file)
@@ -88,8 +88,9 @@ module Irc
 
 # Main bot class, which manages the various components, receives messages,
 # handles them or passes them to plugins, and contains core functionality.
-class IrcBot
-  # the bot's IrcAuth data
+class Bot
+  COPYRIGHT_NOTICE = "(c) Tom Gilbert and the rbot development team"
+  # the bot's Auth data
   attr_reader :auth
 
   # the bot's BotConfig data
@@ -134,7 +135,7 @@ class IrcBot
   # bot User in the client/server connection
   # TODO multiserver
   def myself
-    @client.client
+    @client.user
   end
 
   # bot User in the client/server connection
@@ -142,7 +143,7 @@ class IrcBot
     myself.nick
   end
 
-  # create a new IrcBot with botclass +botclass+
+  # create a new Bot with botclass +botclass+
   def initialize(botclass, params = {})
     # BotConfig for the core bot
     # TODO should we split socket stuff into ircsocket, etc?
@@ -183,6 +184,12 @@ class IrcBot
     BotConfig.register BotConfigStringValue.new('irc.nick', :default => "rbot",
       :desc => "IRC nickname the bot should attempt to use", :wizard => true,
       :on_change => Proc.new{|bot, v| bot.sendq "NICK #{v}" })
+    BotConfig.register BotConfigStringValue.new('irc.name',
+      :default => "Ruby bot", :requires_restart => true,
+      :desc => "IRC realname the bot should use")
+    BotConfig.register BotConfigBooleanValue.new('irc.name_copyright',
+      :default => true, :requires_restart => true,
+      :desc => "Append copyright notice to bot realname? (please don't disable unless it's really necessary)")
     BotConfig.register BotConfigStringValue.new('irc.user', :default => "rbot",
       :requires_restart => true,
       :desc => "local user the bot should appear to be", :wizard => true)
@@ -437,7 +444,7 @@ class IrcBot
 
 
     @socket = IrcSocket.new(@config['server.name'], @config['server.port'], @config['server.bindhost'], @config['server.sendq_delay'], @config['server.sendq_burst'], :ssl => @config['server.ssl'])
-    @client = IrcClient.new
+    @client = Client.new
 
     # Channels where we are quiet
     # Array of channels names where the bot should be quiet
@@ -534,6 +541,8 @@ class IrcBot
       @last_ping = nil
     }
     @client[:nick] = proc {|data|
+      # debug "Message source is #{data[:source].inspect}"
+      # debug "Bot is #{myself.inspect}"
       source = data[:source]
       old = data[:oldnick]
       new = data[:newnick]
@@ -695,8 +704,12 @@ class IrcBot
       raise e.class, "failed to connect to IRC server at #{@config['server.name']} #{@config['server.port']}: " + e
     end
     quit if $interrupted > 0
+
+    realname = @config['irc.name'].clone || 'Ruby bot'
+    realname << ' ' + COPYRIGHT_NOTICE if @config['irc.name_copyright'] 
+
     @socket.emergency_puts "PASS " + @config['server.password'] if @config['server.password']
-    @socket.emergency_puts "NICK #{@config['irc.nick']}\nUSER #{@config['irc.user']} 4 #{@config['server.name']} :Ruby bot. (c) Tom Gilbert"
+    @socket.emergency_puts "NICK #{@config['irc.nick']}\nUSER #{@config['irc.user']} 4 #{@config['server.name']} :#{realname}"
     quit if $interrupted > 0
     myself.nick = @config['irc.nick']
     myself.user = @config['irc.user']
@@ -710,6 +723,7 @@ class IrcBot
         connect
         @timer.start
 
+        quit_msg = nil
         while @socket.connected?
           quit if $interrupted > 0
 
@@ -735,6 +749,7 @@ class IrcBot
       rescue Errno::ETIMEDOUT, Errno::ECONNABORTED, TimeoutError, SocketError => e
         error "network exception: #{e.class}: #{e}"
         debug e.backtrace.join("\n")
+        quit_msg = e.to_s
       rescue BDB::Fatal => e
         fatal "fatal bdb error: #{e.class}: #{e}"
         fatal e.backtrace.join("\n")
@@ -746,6 +761,7 @@ class IrcBot
       rescue Exception => e
         error "non-net exception: #{e.class}: #{e}"
         error e.backtrace.join("\n")
+        quit_msg = e.to_s
       rescue => e
         fatal "unexpected exception: #{e.class}: #{e}"
         fatal e.backtrace.join("\n")
@@ -753,18 +769,13 @@ class IrcBot
         exit 2
       end
 
-      stop_server_pings
-      server.clear
-      if @socket.connected?
-        @socket.clearq
-        @socket.shutdown
-      end
+      disconnect(quit_msg)
 
-      log "disconnected"
+      log "\n\nDisconnected\n\n"
 
       quit if $interrupted > 0
 
-      log "waiting to reconnect"
+      log "\n\nWaiting to reconnect\n\n"
       sleep @config['server.reconnect_wait']
     end
   end
@@ -961,6 +972,26 @@ class IrcBot
     sendq "TOPIC #{where} :#{topic}", where, 2
   end
 
+  def disconnect(message = nil)
+    message = @lang.get("quit") if (message.nil? || message.empty?)
+    if @socket.connected?
+      debug "Clearing socket"
+      @socket.clearq
+      debug "Sending quit message"
+      @socket.emergency_puts "QUIT :#{message}"
+      debug "Flushing socket"
+      @socket.flush
+      debug "Shutting down socket"
+      @socket.shutdown
+    end
+    debug "Logging quits"
+    server.channels.each { |ch|
+      irclog "@ quit (#{message})", ch
+    }
+    stop_server_pings
+    @client.reset
+  end
+
   # disconnect from the server and cleanup all plugins and modules
   def shutdown(message = nil)
     @quit_mutex.synchronize do
@@ -973,21 +1004,7 @@ class IrcBot
       # rescue => e
       #   debug "failed to restore signals: #{e.inspect}\nProbably running on windows?"
       # end
-      message = @lang.get("quit") if (message.nil? || message.empty?)
-      if @socket.connected?
-        debug "Clearing socket"
-        @socket.clearq
-        debug "Sending quit message"
-        @socket.emergency_puts "QUIT :#{message}"
-        debug "Flushing socket"
-        @socket.flush
-        debug "Shutting down socket"
-        @socket.shutdown
-      end
-      debug "Logging quits"
-      server.channels.each { |ch|
-        irclog "@ quit (#{message})", ch
-      }
+      disconnect
       debug "Saving"
       save
       debug "Cleaning up"