From cac56a450a41eadf23d7e5a1abf682d7d2f72194 Mon Sep 17 00:00:00 2001 From: "." <.@.> Date: Sun, 8 May 2016 13:18:38 +1200 Subject: [PATCH] strip color and formatting control codes from upstream messages --HG-- branch : nmdc-ircfrontend --- TODO.txt | 3 +-- server.go | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/TODO.txt b/TODO.txt index 4d4d241..1a44adf 100644 --- a/TODO.txt +++ b/TODO.txt @@ -4,8 +4,6 @@ PRE-RELEASE - client descriptions (CTCP USERINFO) -- remove color code escape sequences - WISHLIST ======== @@ -58,3 +56,4 @@ https://github.com/eXeC64/Rosella AGPLv3 license https://github.com/edmund-huber/ergonomadic MIT license +http://en.wikichip.org/wiki/irc/colors diff --git a/server.go b/server.go index efd66aa..656aadc 100644 --- a/server.go +++ b/server.go @@ -25,6 +25,7 @@ import ( "libnmdc" "log" "net" + "regexp" "strings" "time" ) @@ -172,6 +173,23 @@ func (s *Server) postGeneralMessageInRoom(msg string) { } +var rx_colorControlCode *regexp.Regexp = regexp.MustCompilePOSIX("\x03[0-9]+(,[0-9]+)?") +var rx_formattingControlCode *regexp.Regexp = regexp.MustCompile("(\x02|\x1D|\x1F|\x16|\x0F)") + +func reformatOutgoingMessageBody(body string) string { + + // Strip color codes + noColors := rx_colorControlCode.ReplaceAllString(body, "") + + // Strip formatting codes + return rx_formattingControlCode.ReplaceAllString(noColors, "") + +} + +func reformatIncomingMessageBody(body string) string { + return body +} + func (s *Server) upstreamWorker() { // Read loop @@ -210,14 +228,14 @@ func (s *Server) upstreamWorker() { s.sendChannelTopic(hubEvent.Nick) case libnmdc.EVENT_PRIVATE: - s.reply(rplMsg, hubEvent.Nick+"!"+hubEvent.Nick+"@"+hubEvent.Nick, s.upstreamLauncher.Self.Nick, hubEvent.Message) + s.reply(rplMsg, hubEvent.Nick+"!"+hubEvent.Nick+"@"+hubEvent.Nick, s.upstreamLauncher.Self.Nick, reformatIncomingMessageBody(hubEvent.Message)) case libnmdc.EVENT_PUBLIC: if hubEvent.Nick == s.upstreamLauncher.Self.Nick { // irc doesn't echo our own pubchat } else { // nick!username@userhost, but for us all three of those are always identical - s.reply(rplMsg, hubEvent.Nick+"!"+hubEvent.Nick+"@"+hubEvent.Nick, BLESSED_CHANNEL, hubEvent.Message) + s.reply(rplMsg, hubEvent.Nick+"!"+hubEvent.Nick+"@"+hubEvent.Nick, BLESSED_CHANNEL, reformatIncomingMessageBody(hubEvent.Message)) } case libnmdc.EVENT_SYSTEM_MESSAGE_FROM_CONN, libnmdc.EVENT_SYSTEM_MESSAGE_FROM_HUB: @@ -411,17 +429,17 @@ func (s *Server) handleJoinedCommand(command string, args []string) { if strings.HasPrefix(message, "\x01ACTION ") { message = "/me " + message[8:] - message = message[:len(message)-1] + message = message[:len(message)-1] // trailing \x01 } // IRC is case-insensitive case-preserving. We can respect that for the // channel name, but not really for user nicks if strings.ToLower(args[0]) == BLESSED_CHANNEL { - s.upstream.SayPublic(message) + s.upstream.SayPublic(reformatOutgoingMessageBody(message)) } else if s.upstream.UserExists(args[0]) { - s.upstream.SayPrivate(args[0], message) + s.upstream.SayPrivate(args[0], reformatOutgoingMessageBody(message)) } else { s.reply(errNoSuchNick, args[0])