From 0e7e2b629c24a9a82b5bdafa9c479435cea6f033 Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 24 Mar 2018 14:37:40 +1300 Subject: [PATCH] move chatformatting, quirks to separate package --HG-- branch : nmdc-ircfrontend --- chatFormatting.go | 33 +++++++++++++++++++++++++++++ quirks.go | 26 +++++++++++++++++++++++ server.go | 54 ++--------------------------------------------- 3 files changed, 61 insertions(+), 52 deletions(-) create mode 100644 chatFormatting.go create mode 100644 quirks.go diff --git a/chatFormatting.go b/chatFormatting.go new file mode 100644 index 0000000..772effd --- /dev/null +++ b/chatFormatting.go @@ -0,0 +1,33 @@ +package main + +import ( + "regexp" + "strings" +) + +var ( + rx_colorControlCode *regexp.Regexp = regexp.MustCompilePOSIX("\x03[0-9]+(,[0-9]+)?") + 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 { + + // "Greentext" filter + // TODO markdown filters + if len(body) > 0 && body[0] == '>' { + return "\x033" + strings.Replace(body, "implying", "\x02implying\x02", -1) + + } else { + return body + } +} diff --git a/quirks.go b/quirks.go new file mode 100644 index 0000000..7f60f49 --- /dev/null +++ b/quirks.go @@ -0,0 +1,26 @@ +package main + +import ( + "strings" +) + +type Quirks struct { + SendNamesOnWho bool + RequireNickForGeneralMessages bool +} + +func GetQuirksForClient(ver string) Quirks { + if strings.Contains(ver, "HexChat") { + return Quirks{ + SendNamesOnWho: true, + } + + } else if strings.Contains(ver, "mIRC") || /*strings.Contains(ver, "Revolution") ||*/ strings.Contains(ver, "Atomic") { + return Quirks{ + RequireNickForGeneralMessages: true, + } + + } else { + return Quirks{} + } +} diff --git a/server.go b/server.go index bd19f7b..a4a1ced 100644 --- a/server.go +++ b/server.go @@ -24,7 +24,6 @@ import ( "io" "log" "net" - "regexp" "strings" "sync" "time" @@ -40,30 +39,6 @@ const ( CSJoined ) -type Quirks struct { - SendNamesOnWho bool -} - -func DefaultQuirks() Quirks { - return Quirks{ - SendNamesOnWho: false, - } -} - -func HexChatQuirks() Quirks { - return Quirks{ - SendNamesOnWho: true, - } -} - -func GetQuirksForClient(ver string) Quirks { - if strings.Contains(ver, "HexChat") { - return HexChatQuirks() - } else { - return DefaultQuirks() - } -} - type Server struct { name string motd string @@ -105,7 +80,7 @@ func NewServer(name string, upstream libnmdc.HubAddress, conn net.Conn) *Server }, upstreamEvents: make(chan libnmdc.HubEvent, 0), // unbuffered upstreamCloser: make(chan struct{}, 1), - quirks: DefaultQuirks(), + quirks: Quirks{}, } } @@ -191,7 +166,7 @@ func (s *Server) postGeneralMessageInRoom(msg string) { var sendAs = "" // Some clients can't handle blank nicks very well - if s.upstreamLauncher.Self.ClientTag == "mIRC" || s.upstreamLauncher.Self.ClientTag == "Atomic" { + if s.quirks.RequireNickForGeneralMessages { sendAs = s.hubSecNick + "!" + s.hubSecNick + "@" + s.hubSecNick } @@ -221,31 +196,6 @@ 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 { - - // "Greentext" filter - // TODO markdown filters - if len(body) > 0 && body[0] == '>' { - return "\x033" + strings.Replace(body, "implying", "\x02implying\x02", -1) - - } else { - return body - } -} - func (s *Server) upstreamWorker() { // Read loop