Skip to content

Commit

Permalink
use go-utils lib for stripping newlines from a message
Browse files Browse the repository at this point in the history
  • Loading branch information
cfindlayisme committed Dec 7, 2024
1 parent b3e03ea commit d15aadb
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
require (
github.com/bytedance/sonic v1.11.8 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cfindlayisme/go-utils v0.0.3 // indirect
github.com/cfindlayisme/go-utils v0.0.12 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3z
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/cfindlayisme/go-utils v0.0.3 h1:lTZ49JPTa3+25Fx1K+aPlF4aE2p9saXsjo8r7N8TZJk=
github.com/cfindlayisme/go-utils v0.0.3/go.mod h1:oFFLbVTq59qrLqhYl+9u/T35Je6MI136eZZhzwHkDpI=
github.com/cfindlayisme/go-utils v0.0.12 h1:rr89se3INELE0VAijzu2HrnaA1PvBS/FkEqMdevqLxM=
github.com/cfindlayisme/go-utils v0.0.12/go.mod h1:oFFLbVTq59qrLqhYl+9u/T35Je6MI136eZZhzwHkDpI=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
Expand Down
12 changes: 7 additions & 5 deletions ircclient/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"log"
"net"

goutilsstrings "github.com/cfindlayisme/go-utils/strings"
)

func SetNick(conn net.Conn, nick string) error {
Expand Down Expand Up @@ -31,7 +33,7 @@ func SetMode(conn net.Conn, channel string, mode string) error {
}

func SetTopic(conn net.Conn, channel string, topic string) error {
cleanTopic := CleanMessage(topic)
cleanTopic := goutilsstrings.StripNewlines(topic)
_, err := fmt.Fprintf(conn, "TOPIC "+channel+" "+cleanTopic+"\r\n")
log.Println("TOPIC command sent for channel: ", channel, " with topic: ", cleanTopic)
return err
Expand Down Expand Up @@ -59,15 +61,15 @@ func Quote(conn net.Conn, command string) error {
}

func SendMessage(conn net.Conn, target string, message string) error {
ircMessage := CleanMessage(message)
ircMessage := goutilsstrings.StripNewlines(message)

_, err := fmt.Fprintf(conn, "PRIVMSG "+target+" :"+ircMessage+"\r\n")
log.Println("Sent message to ", target, ": ", ircMessage)
return err
}

func SendNotice(conn net.Conn, target string, message string) error {
ircMessage := CleanMessage(message)
ircMessage := goutilsstrings.StripNewlines(message)

_, err := fmt.Fprintf(conn, "NOTICE "+target+" :"+ircMessage+"\r\n")
log.Println("Sent notice to ", target, ": ", ircMessage)
Expand All @@ -81,13 +83,13 @@ func SetUser(conn net.Conn) error {
}

func SendQuit(conn net.Conn, quitMessage string) error {
_, err := fmt.Fprintf(conn, "QUIT :%s\r\n", CleanMessage(quitMessage))
_, err := fmt.Fprintf(conn, "QUIT :%s\r\n", goutilsstrings.StripNewlines(quitMessage))
log.Println("Sent QUIT command")
return err
}

func SendCTCPReply(conn net.Conn, target, command, response string) error {
ctcpMessage := fmt.Sprintf("\x01%s %s\x01", command, CleanMessage(response))
ctcpMessage := fmt.Sprintf("\x01%s %s\x01", command, goutilsstrings.StripNewlines(response))
_, err := fmt.Fprintf(conn, "NOTICE %s :%s\r\n", target, ctcpMessage)
return err
}
10 changes: 0 additions & 10 deletions ircclient/format.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ircclient

import (
"strings"

"github.com/cfindlayisme/wmb/model"
)

Expand Down Expand Up @@ -74,11 +72,3 @@ func FormatMessage(msg model.IncomingMessage) string {

return ircMessage
}

func CleanMessage(message string) string {
// Strip newlines to prevent chaining of commands, ie, QUIT to the end
message = strings.ReplaceAll(message, "\n", "")
message = strings.ReplaceAll(message, "\r", "")

return message
}
5 changes: 3 additions & 2 deletions ircclient/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

goutilsstrings "github.com/cfindlayisme/go-utils/strings"
"github.com/cfindlayisme/wmb/ircclient"
"github.com/cfindlayisme/wmb/model"
"github.com/stretchr/testify/require"
Expand All @@ -13,13 +14,13 @@ func TestCleanMessage(t *testing.T) {
// Test case with newlines and carriage returns
input := "Hello\nWorld\r\n"
expected := "HelloWorld"
result := ircclient.CleanMessage(input)
result := goutilsstrings.StripNewlines(input)
require.Equal(t, expected, result, "The message should be cleaned")

// Test case with no newlines or carriage returns
input = "Hello World"
expected = "Hello World"
result = ircclient.CleanMessage(input)
result = goutilsstrings.StripNewlines(input)
require.Equal(t, expected, result, "The message should be unchanged")
}

Expand Down

0 comments on commit d15aadb

Please sign in to comment.