diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2008-06-24 17:27:30 +0200 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2008-06-24 17:39:39 +0200 |
commit | 46497c5f135079cb26dd9b5ef62e4829f3c49f69 (patch) | |
tree | 785305cd88d90386583f1fa44ad434f582c2d839 /lib/rbot/message.rb | |
parent | a2cd248f43fafdd9f385e52403c145f4e5e9fc75 (diff) |
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.
Diffstat (limited to 'lib/rbot/message.rb')
-rw-r--r-- | lib/rbot/message.rb | 20 |
1 files changed, 18 insertions, 2 deletions
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 |