]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircbot.rb
Plugin header boilerplating.
[user/henk/code/ruby/rbot.git] / lib / rbot / ircbot.rb
index 0fed549320813fe13fc254a18fd6e40b19819c50..bd19e6375e3b4021703c3c25a678714b9e083189 100644 (file)
@@ -373,7 +373,7 @@ class IrcBot
       end
       def STDOUT.write(str=nil)
         log str, 2
-        return str.to_s.length
+        return str.to_s.size
       end
       def STDERR.write(str=nil)
         if str.to_s.match(/:\d+: warning:/)
@@ -381,7 +381,7 @@ class IrcBot
         else
           error str, 2
         end
-        return str.to_s.length
+        return str.to_s.size
       end
     end
 
@@ -438,12 +438,12 @@ 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
-    myself.nick = @config['irc.nick']
 
     # Channels where we are quiet
-    # It's nil when we are not quiet, an empty list when we are quiet
-    # in all channels, a list of channels otherwise
-    @quiet = nil
+    # Array of channels names where the bot should be quiet
+    # '*' means all channels
+    #
+    @quiet = []
 
     @client[:welcome] = proc {|data|
       irclog "joined server #{@client.server} as #{myself}", "server"
@@ -497,6 +497,9 @@ class IrcBot
       unless ignored
         @plugins.delegate "listen", m
         @plugins.privmsg(m) if m.address?
+       if not m.replied
+          @plugins.delegate "unreplied", m
+        end
       end
     }
     @client[:notice] = proc { |data|
@@ -510,11 +513,19 @@ class IrcBot
       }
     }
     @client[:nicktaken] = proc { |data|
-      nickchg "#{data[:nick]}_"
+      new = "#{data[:nick]}_" 
+      nickchg new
+      # If we're setting our nick at connection because our choice was taken,
+      # we have to fix our nick manually, because there will be no NICK message
+      # yo inform us that our nick has been changed.
+      if data[:target] == '*'
+        debug "setting my connection nick to #{new}"
+        nick = new
+      end
       @plugins.delegate "nicktaken", data[:nick]
     }
     @client[:badnick] = proc {|data|
-      warning "bad nick (#{data[:nick]})"
+      arning "bad nick (#{data[:nick]})"
     }
     @client[:ping] = proc {|data|
       sendq "PONG #{data[:pingid]}"
@@ -632,24 +643,24 @@ class IrcBot
 
   # checks if we should be quiet on a channel
   def quiet_on?(channel)
-    return false unless @quiet
-    return true if @quiet.empty?
-    return @quiet.include?(channel.to_s)
+    return @quiet.include?('*') || @quiet.include?(channel.downcase)
   end
 
-  def set_quiet(channel=nil)
+  def set_quiet(channel)
     if channel
-      @quiet << channel.to_s unless @quiet.include?(channel.to_s)
+      ch = channel.downcase.dup
+      @quiet << ch unless @quiet.include?(ch)
     else
-      @quiet = []
+      @quiet.clear
+      @quiet << '*'
     end
   end
 
-  def reset_quiet(channel=nil)
+  def reset_quiet(channel)
     if channel
-      @quiet.delete_if { |x| x == channel.to_s }
+      @quiet.delete channel.downcase
     else
-      @quiet = nil
+      @quiet.clear
     end
   end
 
@@ -685,8 +696,10 @@ class IrcBot
     end
     quit if $interrupted > 0
     @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']} :Ruby bot. (c) Tom Gilbert and the rbot development team"
     quit if $interrupted > 0
+    myself.nick = @config['irc.nick']
+    myself.user = @config['irc.user']
   end
 
   # begin event handling loop
@@ -793,7 +806,7 @@ class IrcBot
       lines = Array.new
       message.each_line { |line|
         line.chomp!
-        next unless(line.length > 0)
+        next unless(line.size > 0)
         lines << line
       }
     else
@@ -808,7 +821,7 @@ class IrcBot
     # The maximum raw message length we can send is therefore 512 - 2 - 2
     # minus the length of our hostmask.
 
-    max_len = 508 - myself.fullform.length
+    max_len = 508 - myself.fullform.size
 
     # On servers that support IDENTIFY-MSG, we have to subtract 1, because messages
     # will have a + or - prepended
@@ -821,7 +834,7 @@ class IrcBot
     fixed = "#{type} #{where} :"
 
     # And this is what's left
-    left = max_len - fixed.length
+    left = max_len - fixed.size
 
     case opts[:overlong]
     when :split
@@ -829,8 +842,8 @@ class IrcBot
       split_at = opts[:split_at]
     when :truncate
       truncate = opts[:truncate_text]
-      truncate = @default_send_options[:truncate_text] if truncate.length > left
-      truncate = "" if truncate.length > left
+      truncate = @default_send_options[:truncate_text] if truncate.size > left
+      truncate = "" if truncate.size > left
     else
       raise "Unknown :overlong option #{opts[:overlong]} while sending #{original_message.inspect}"
     end
@@ -844,21 +857,21 @@ class IrcBot
       begin
         if max_lines > 0 and cmd_lines == max_lines - 1
           truncate = opts[:truncate_text]
-          truncate = @default_send_options[:truncate_text] if truncate.length > left
-          truncate = "" if truncate.length > left
+          truncate = @default_send_options[:truncate_text] if truncate.size > left
+          truncate = "" if truncate.size > left
           maxed = true
         end
-        if(left >= msg.length) and not maxed
+        if(left >= msg.size) and not maxed
           sendq "#{fixed}#{msg}", chan, ring
           log_sent(type, where, msg)
           cmd_lines += 1
           break
         end
         if truncate
-          line.replace msg.slice(0, left-truncate.length)
+          line.replace msg.slice(0, left-truncate.size)
           # line.sub!(/\s+\S*$/, truncate)
           line << truncate
-          raise "PROGRAMMER ERROR! #{line.inspect} of length #{line.length} > #{left}" if line.length > left
+          raise "PROGRAMMER ERROR! #{line.inspect} of size #{line.size} > #{left}" if line.size > left
           sendq "#{fixed}#{line}", chan, ring
           log_sent(type, where, line)
           return
@@ -866,13 +879,13 @@ class IrcBot
         line.replace msg.slice!(0, left)
         lastspace = line.rindex(opts[:split_at])
         if(lastspace)
-          msg.replace line.slice!(lastspace, line.length) + msg
+          msg.replace line.slice!(lastspace, line.size) + msg
           msg.gsub!(/^#{opts[:split_at]}/, "") if opts[:purge_split]
         end
         sendq "#{fixed}#{line}", chan, ring
         log_sent(type, where, line)
         cmd_lines += 1
-      end while(msg.length > 0)
+      end while(msg.size > 0)
     }
   end
 
@@ -884,20 +897,19 @@ class IrcBot
 
   # send a notice message to channel/nick +where+
   def notice(where, message, options={})
-    unless quiet_on?(where)
-      sendmsg "NOTICE", where, message, options
-    end
+    return if where.kind_of?(Channel) and quiet_on?(where)
+    sendmsg "NOTICE", where, message, options
   end
 
   # say something (PRIVMSG) to channel/nick +where+
   def say(where, message, options={})
-    unless quiet_on?(where)
-      sendmsg "PRIVMSG", where, message, options
-    end
+    return if where.kind_of?(Channel) and quiet_on?(where)
+    sendmsg "PRIVMSG", where, message, options
   end
 
   # perform a CTCP action with message +message+ to channel/nick +where+
   def action(where, message, options={})
+    return if where.kind_of?(Channel) and quiet_on?(where)
     mchan = options.fetch(:queue_channel, nil)
     mring = options.fetch(:queue_ring, nil)
     if mchan