Merge pull request #107 from IceflowRE/master

Add method to get message without formatting
This commit is contained in:
Thomas Jager 2018-08-16 06:25:45 +02:00 committed by GitHub
commit f25dab053f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,6 +8,7 @@ import (
"crypto/tls" "crypto/tls"
"log" "log"
"net" "net"
"regexp"
"sync" "sync"
"time" "time"
) )
@ -17,7 +18,7 @@ type Connection struct {
sync.WaitGroup sync.WaitGroup
Debug bool Debug bool
Error chan error Error chan error
WebIRC string WebIRC string
Password string Password string
UseTLS bool UseTLS bool
UseSASL bool UseSASL bool
@ -72,7 +73,7 @@ type Event struct {
} }
// Retrieve the last message from Event arguments. // Retrieve the last message from Event arguments.
// This function leaves the arguments untouched and // This function leaves the arguments untouched and
// returns an empty string if there are none. // returns an empty string if there are none.
func (e *Event) Message() string { func (e *Event) Message() string {
if len(e.Arguments) == 0 { if len(e.Arguments) == 0 {
@ -80,3 +81,17 @@ func (e *Event) Message() string {
} }
return e.Arguments[len(e.Arguments)-1] return e.Arguments[len(e.Arguments)-1]
} }
// https://stackoverflow.com/a/10567935/6754440
// Regex of IRC formatting.
var ircFormat = regexp.MustCompile(`[\x02\x1F\x0F\x16\x1D]|\x03(\d\d?(,\d\d?)?)?`)
// Retrieve the last message from Event arguments, but without IRC formatting (color.
// This function leaves the arguments untouched and
// returns an empty string if there are none.
func (e *Event) MessageWithoutFormat() string {
if len(e.Arguments) == 0 {
return ""
}
return ircFormat.ReplaceAllString(e.Arguments[len(e.Arguments)-1], "")
}