From e975eae5bace80a4ca6e898ecc8b5a9aa359a037 Mon Sep 17 00:00:00 2001 From: Iceflower Date: Tue, 26 Jun 2018 08:48:51 +0200 Subject: [PATCH] Add method to get message without formatting --- irc_struct.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/irc_struct.go b/irc_struct.go index 3e51bb8..6cf44ca 100644 --- a/irc_struct.go +++ b/irc_struct.go @@ -8,6 +8,7 @@ import ( "crypto/tls" "log" "net" + "regexp" "sync" "time" ) @@ -17,7 +18,7 @@ type Connection struct { sync.WaitGroup Debug bool Error chan error - WebIRC string + WebIRC string Password string UseTLS bool UseSASL bool @@ -72,7 +73,7 @@ type Event struct { } // 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. func (e *Event) Message() string { if len(e.Arguments) == 0 { @@ -80,3 +81,17 @@ func (e *Event) Message() string { } 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], "") +}