From 46497c5f135079cb26dd9b5ef62e4829f3c49f69 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Tue, 24 Jun 2008 17:27:30 +0200 Subject: [PATCH] message handling: improve IRC format handling for received messages 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 | 2 +- lib/rbot/message.rb | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb index a8b5543e..968f35f9 100644 --- a/lib/rbot/ircbot.rb +++ b/lib/rbot/ircbot.rb @@ -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 diff --git a/lib/rbot/message.rb b/lib/rbot/message.rb index abe53ce0..97e3673a 100644 --- a/lib/rbot/message.rb +++ b/lib/rbot/message.rb @@ -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 -- 2.39.2