Merge pull request #70 from ripcurld00d/privmsg_enhanced

Add variadic Privmsg functions.
This commit is contained in:
Alex Bee 2015-10-15 20:36:05 +01:00
commit 038549c7e6
2 changed files with 56 additions and 4 deletions

View File

@ -1,6 +1,9 @@
package client package client
import "strings" import (
"fmt"
"strings"
)
const ( const (
REGISTER = "REGISTER" REGISTER = "REGISTER"
@ -73,7 +76,7 @@ func splitMessage(msg string, splitLen int) (msgs []string) {
if idx < 0 { if idx < 0 {
idx = splitLen - 3 idx = splitLen - 3
} }
msgs = append(msgs, msg[:idx] + "...") msgs = append(msgs, msg[:idx]+"...")
msg = msg[idx:] msg = msg[idx:]
} }
return append(msgs, msg) return append(msgs, msg)
@ -151,13 +154,34 @@ func (conn *Conn) Who(nick string) { conn.Raw(WHO + " " + nick) }
// Privmsg sends a PRIVMSG to the target nick or channel t. // Privmsg sends a PRIVMSG to the target nick or channel t.
// If msg is longer than Config.SplitLen characters, multiple PRIVMSGs // If msg is longer than Config.SplitLen characters, multiple PRIVMSGs
// will be sent to the target containing sequential parts of msg. // will be sent to the target containing sequential parts of msg.
// PRIVMSG t :msg // PRIVMSG t :msg
func (conn *Conn) Privmsg(t, msg string) { func (conn *Conn) Privmsg(t, msg string) {
prefix := PRIVMSG + " " + t + " :"
for _, s := range splitMessage(msg, conn.cfg.SplitLen) { for _, s := range splitMessage(msg, conn.cfg.SplitLen) {
conn.Raw(PRIVMSG + " " + t + " :" + s) conn.Raw(prefix + s)
} }
} }
// Privmsgln is the variadic version of Privmsg that formats the message
// that is sent to the target nick or channel t using the
// fmt.Sprintln function.
// Note: Privmsgln doesn't add the '\n' character at the end of the message.
func (conn *Conn) Privmsgln(t string, a ...interface{}) {
msg := fmt.Sprintln(a...)
// trimming the new-line character added by the fmt.Sprintln function,
// since it's irrelevant.
msg = msg[:len(msg)-1]
conn.Privmsg(t, msg)
}
// Privmsgf is the variadic version of Privmsg that formats the message
// that is sent to the target nick or channel t using the
// fmt.Sprintf function.
func (conn *Conn) Privmsgf(t, format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
conn.Privmsg(t, msg)
}
// Notice sends a NOTICE to the target nick or channel t. // Notice sends a NOTICE to the target nick or channel t.
// If msg is longer than Config.SplitLen characters, multiple NOTICEs // If msg is longer than Config.SplitLen characters, multiple NOTICEs
// will be sent to the target containing sequential parts of msg. // will be sent to the target containing sequential parts of msg.

View File

@ -118,11 +118,39 @@ func TestClientCommands(t *testing.T) {
c.Privmsg("#foo", "bar") c.Privmsg("#foo", "bar")
s.nc.Expect("PRIVMSG #foo :bar") s.nc.Expect("PRIVMSG #foo :bar")
c.Privmsgln("#foo", "bar")
s.nc.Expect("PRIVMSG #foo :bar")
c.Privmsgf("#foo", "say %s", "foo")
s.nc.Expect("PRIVMSG #foo :say foo")
c.Privmsgln("#foo", "bar", 1, 3.54, []int{24, 36})
s.nc.Expect("PRIVMSG #foo :bar 1 3.54 [24 36]")
c.Privmsgf("#foo", "user %d is at %s", 2, "home")
s.nc.Expect("PRIVMSG #foo :user 2 is at home")
// 0123456789012345678901234567890123 // 0123456789012345678901234567890123
c.Privmsg("#foo", "foo bar baz blorp. woo woobly woo.") c.Privmsg("#foo", "foo bar baz blorp. woo woobly woo.")
s.nc.Expect("PRIVMSG #foo :foo bar baz blorp. ...") s.nc.Expect("PRIVMSG #foo :foo bar baz blorp. ...")
s.nc.Expect("PRIVMSG #foo :woo woobly woo.") s.nc.Expect("PRIVMSG #foo :woo woobly woo.")
c.Privmsgln("#foo", "foo bar baz blorp. woo woobly woo.")
s.nc.Expect("PRIVMSG #foo :foo bar baz blorp. ...")
s.nc.Expect("PRIVMSG #foo :woo woobly woo.")
c.Privmsgf("#foo", "%s %s", "foo bar baz blorp.", "woo woobly woo.")
s.nc.Expect("PRIVMSG #foo :foo bar baz blorp. ...")
s.nc.Expect("PRIVMSG #foo :woo woobly woo.")
c.Privmsgln("#foo", "foo bar", 3.54, "blorp.", "woo", "woobly", []int{1, 2})
s.nc.Expect("PRIVMSG #foo :foo bar 3.54 blorp. ...")
s.nc.Expect("PRIVMSG #foo :woo woobly [1 2]")
c.Privmsgf("#foo", "%s %.2f %s %s %s %v", "foo bar", 3.54, "blorp.", "woo", "woobly", []int{1, 2})
s.nc.Expect("PRIVMSG #foo :foo bar 3.54 blorp. ...")
s.nc.Expect("PRIVMSG #foo :woo woobly [1 2]")
c.Notice("somebody", "something") c.Notice("somebody", "something")
s.nc.Expect("NOTICE somebody :something") s.nc.Expect("NOTICE somebody :something")