Sneakier handling of variadic optional args for commands.

This commit is contained in:
Alex Bramley 2010-11-03 23:48:28 +00:00
parent f829eeab9b
commit a9d47d1a25
1 changed files with 10 additions and 15 deletions

View File

@ -1,5 +1,7 @@
package irc package irc
import "strings"
// this file contains the various commands you can // this file contains the various commands you can
// send to the server using an Conn connection // send to the server using an Conn connection
@ -27,7 +29,7 @@ func (conn *Conn) Join(channel string) { conn.out <- "JOIN "+channel }
// Part() sends a PART command to the server with an optional part message // Part() sends a PART command to the server with an optional part message
func (conn *Conn) Part(channel string, message ...string) { func (conn *Conn) Part(channel string, message ...string) {
msg := getStringMsg(message) msg := strings.Join(message, " ")
if msg != "" { if msg != "" {
msg = " :" + msg msg = " :" + msg
} }
@ -36,7 +38,7 @@ func (conn *Conn) Part(channel string, message ...string) {
// Kick() sends a KICK command to remove a nick from a channel // Kick() sends a KICK command to remove a nick from a channel
func (conn *Conn) Kick(channel, nick string, message ...string) { func (conn *Conn) Kick(channel, nick string, message ...string) {
msg := getStringMsg(message) msg := strings.Join(message, " ")
if msg != "" { if msg != "" {
msg = " :" + msg msg = " :" + msg
} }
@ -45,7 +47,7 @@ func (conn *Conn) Kick(channel, nick string, message ...string) {
// Quit() sends a QUIT command to the server with an optional quit message // Quit() sends a QUIT command to the server with an optional quit message
func (conn *Conn) Quit(message ...string) { func (conn *Conn) Quit(message ...string) {
msg := getStringMsg(message) msg := strings.Join(message, " ")
if msg == "" { if msg == "" {
msg = "GoBye!" msg = "GoBye!"
} }
@ -67,7 +69,7 @@ func (conn *Conn) Notice(t, msg string) { conn.out <- "NOTICE "+t+" :"+msg }
// Ctcp() sends a (generic) CTCP message to the target t // Ctcp() sends a (generic) CTCP message to the target t
// with an optional argument // with an optional argument
func (conn *Conn) Ctcp(t, ctcp string, arg ...string) { func (conn *Conn) Ctcp(t, ctcp string, arg ...string) {
msg := getStringMsg(arg) msg := strings.Join(arg, " ")
if msg != "" { if msg != "" {
msg = " " + msg msg = " " + msg
} }
@ -77,7 +79,7 @@ func (conn *Conn) Ctcp(t, ctcp string, arg ...string) {
// CtcpReply() sends a generic CTCP reply to the target t // CtcpReply() sends a generic CTCP reply to the target t
// with an optional argument // with an optional argument
func (conn *Conn) CtcpReply(t, ctcp string, arg ...string) { func (conn *Conn) CtcpReply(t, ctcp string, arg ...string) {
msg := getStringMsg(arg) msg := strings.Join(arg, " ")
if msg != "" { if msg != "" {
msg = " " + msg msg = " " + msg
} }
@ -94,7 +96,7 @@ func (conn *Conn) Action(t, msg string) { conn.Ctcp(t, "ACTION", msg) }
// Topic(channel) retrieves the current channel topic (see "332" handler) // Topic(channel) retrieves the current channel topic (see "332" handler)
// Topic(channel, topic) sets the topic for the channel // Topic(channel, topic) sets the topic for the channel
func (conn *Conn) Topic(channel string, topic ...string) { func (conn *Conn) Topic(channel string, topic ...string) {
t := getStringMsg(topic) t := strings.Join(topic, " ")
if t != "" { if t != "" {
t = " :" + t t = " :" + t
} }
@ -109,7 +111,7 @@ func (conn *Conn) Topic(channel string, topic ...string) {
// This means you'll need to do your own mode work. It may be linked in with // This means you'll need to do your own mode work. It may be linked in with
// the state tracking and ChanMode/NickMode/ChanPrivs objects later... // the state tracking and ChanMode/NickMode/ChanPrivs objects later...
func (conn *Conn) Mode(t string, modestring ...string) { func (conn *Conn) Mode(t string, modestring ...string) {
mode := getStringMsg(modestring) mode := strings.Join(modestring, " ")
if mode != "" { if mode != "" {
mode = " " + mode mode = " " + mode
} }
@ -120,7 +122,7 @@ func (conn *Conn) Mode(t string, modestring ...string) {
// Away() resets away status // Away() resets away status
// Away(message) sets away with the given message // Away(message) sets away with the given message
func (conn *Conn) Away(message ...string) { func (conn *Conn) Away(message ...string) {
msg := getStringMsg(message) msg := strings.Join(message, " ")
if msg != "" { if msg != "" {
msg = " :"+msg msg = " :"+msg
} }
@ -136,10 +138,3 @@ func (conn *Conn) Invite(nick, channel string) {
func (conn *Conn) Oper(user, pass string) { func (conn *Conn) Oper(user, pass string) {
conn.out <- "OPER "+user+" "+pass conn.out <- "OPER "+user+" "+pass
} }
func getStringMsg(a ...string) (msg string) {
if len(a) > 0 {
return a[0]
}
return ""
}