]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
message handling: improve IRC format handling for received messages
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Tue, 24 Jun 2008 15:27:30 +0000 (17:27 +0200)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Tue, 24 Jun 2008 15:39:39 +0000 (17:39 +0200)
Rather than stripping colors all around and keeping other format codes,
we only strip initial and final formatting before parsing the message.

We store the original, unstripped message in #logmessage() and a
fully stripped copy of the message in #plainmessage()

This means that most plugins will now have full formatting of arguments
preserved, while stupid IRC usage of formatting whole lines will not
interfere with bot usage. Plugins that need a fully stripped version of
the message can still access it.

lib/rbot/ircbot.rb
lib/rbot/message.rb

index a8b5543eb02006982eacb49e3cfc2cd2135e52a3..968f35f90fca1e02dcbd2944209e8315e7a35589 100644 (file)
@@ -929,7 +929,7 @@ class Bot
 
     # if target is a channel with +c or +C modes, strip colours
     if where.kind_of?(Channel) and where.mode.any?('c', 'C')
-      multi_line.replace(BasicUserMessage.stripcolour(multi_line).gsub(AttributeRx,''))
+      multi_line.replace BasicUserMessage.strip_formatting(multi_line)
     end
 
     messages = Array.new
index abe53ce013245ebf217225ca848ff482c03887c9..97e3673a9e5aa9320fb1ba33cc2aa42377aa68a8 100644 (file)
@@ -42,6 +42,8 @@ module Irc
   Color = "\003"
   ColorRx = /#{Color}\d?\d?(?:,\d\d?)?/
 
+  FormattingRx = /#{AttributeRx}|#{ColorRx}/
+
   # Standard color codes
   ColorCode = {
     :black      => 1,
@@ -120,12 +122,15 @@ module Irc
     # User/Channel message was sent to
     attr_reader :target
 
-    # contents of the message
+    # contents of the message (stripped of initial/final format codes)
     attr_accessor :message
 
     # contents of the message (for logging purposes)
     attr_accessor :logmessage
 
+    # contents of the message (stripped of all formatting)
+    attr_accessor :plainmessage
+
     # has the message been replied to/handled by a plugin?
     attr_accessor :replied
 
@@ -151,7 +156,7 @@ module Irc
       @source = source
       @address = false
       @target = target
-      @message = BasicUserMessage.stripcolour message
+      @message = message || ""
       @replied = false
       @server = server
       @ignored = false
@@ -167,6 +172,8 @@ module Irc
         end
       end
       @logmessage = @message.dup
+      @plainmessage = BasicUserMessage.strip_formatting(@message)
+      @message = BasicUserMessage.strip_initial_formatting(@message)
 
       if target && target == @bot.myself
         @address = true
@@ -219,6 +226,15 @@ module Irc
       ret
     end
 
+    def BasicUserMessage.strip_initial_formatting(string)
+      return "" unless string
+      ret = string.gsub(/^#{FormattingRx}|#{FormattingRx}$/,"")
+    end
+
+    def BasicUserMessage.strip_formatting(string)
+      string.gsub(FormattingRx,"")
+    end
+
   end
 
   # class for handling welcome messages from the server