move chatformatting, quirks to separate package

--HG--
branch : nmdc-ircfrontend
This commit is contained in:
mappu 2018-03-24 14:37:40 +13:00
parent 371a7eb9e4
commit 0e7e2b629c
3 changed files with 61 additions and 52 deletions

33
chatFormatting.go Normal file
View File

@ -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
}
}

26
quirks.go Normal file
View File

@ -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{}
}
}

View File

@ -24,7 +24,6 @@ import (
"io" "io"
"log" "log"
"net" "net"
"regexp"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -40,30 +39,6 @@ const (
CSJoined 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 { type Server struct {
name string name string
motd 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 upstreamEvents: make(chan libnmdc.HubEvent, 0), // unbuffered
upstreamCloser: make(chan struct{}, 1), upstreamCloser: make(chan struct{}, 1),
quirks: DefaultQuirks(), quirks: Quirks{},
} }
} }
@ -191,7 +166,7 @@ func (s *Server) postGeneralMessageInRoom(msg string) {
var sendAs = "" var sendAs = ""
// Some clients can't handle blank nicks very well // 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 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() { func (s *Server) upstreamWorker() {
// Read loop // Read loop