From b257c5955e9b4511e9bb22a26a97735417879584 Mon Sep 17 00:00:00 2001 From: Daniel Oaks Date: Fri, 20 Jan 2017 23:51:36 +1000 Subject: [PATCH] client: Add SendForceTrailing function, to force the last param to be a trailing --- irc/client.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/irc/client.go b/irc/client.go index 42cf0944..1e2da8cc 100644 --- a/irc/client.go +++ b/irc/client.go @@ -12,6 +12,7 @@ import ( "net" "runtime/debug" "strconv" + "strings" "time" "github.com/DanielOaks/girc-go/ircmsg" @@ -535,6 +536,16 @@ func (client *Client) SendFromClient(msgid string, from *Client, tags *map[strin // Send sends an IRC line to the client. func (client *Client) Send(tags *map[string]ircmsg.TagValue, prefix string, command string, params ...string) error { + return client.send(false, tags, prefix, command, params...) +} + +// SendForceTrailing sends an IRC line to the client, forcing the last param to be a trailing. +// This is a hack. However, there are clients that treat special chars differently. +func (client *Client) SendForceTrailing(tags *map[string]ircmsg.TagValue, prefix string, command string, params ...string) error { + return client.send(true, tags, prefix, command, params...) +} + +func (client *Client) send(forceTrailing bool, tags *map[string]ircmsg.TagValue, prefix string, command string, params ...string) error { // attach server-time if client.capabilities[ServerTime] { if tags == nil { @@ -544,6 +555,16 @@ func (client *Client) Send(tags *map[string]ircmsg.TagValue, prefix string, comm } } + // force trailing + var usedSpaceHack bool + if forceTrailing && len(params) > 0 { + lastParam := params[len(params)-1] + if !strings.Contains(lastParam, " ") { + params[len(params)-1] = lastParam + " " + usedSpaceHack = true + } + } + // send out the message message := ircmsg.MakeMessage(tags, prefix, command, params...) maxlenTags, maxlenRest := client.maxlens() @@ -559,6 +580,12 @@ func (client *Client) Send(tags *map[string]ircmsg.TagValue, prefix string, comm client.socket.Write(line) return err } + + // strip space hack if we used it + if usedSpaceHack { + line = line[:len(line)-3] + "\r\n" + } + client.socket.Write(line) return nil }