Fixes for recent versions of Go.

Simplifying getStringMsg().
This commit is contained in:
Yves Junqueira 2010-08-22 02:23:07 +08:00 committed by Alex Bramley
parent cc35817517
commit 1503d7b9db
2 changed files with 16 additions and 28 deletions

View File

@ -3,10 +3,6 @@ package irc
// 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
import (
"reflect"
)
// This could be a lot less ugly with the ability to manipulate // This could be a lot less ugly with the ability to manipulate
// the symbol table and add methods/functions on the fly // the symbol table and add methods/functions on the fly
// [ CMD, FMT, FMTARGS ] etc. // [ CMD, FMT, FMTARGS ] etc.
@ -30,7 +26,7 @@ func (conn *Conn) User(ident, name string) {
func (conn *Conn) Join(channel string) { conn.out <- "JOIN "+channel } 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 ...) { func (conn *Conn) Part(channel string, message ...string) {
msg := getStringMsg(message) msg := getStringMsg(message)
if msg != "" { if msg != "" {
msg = " :" + msg msg = " :" + msg
@ -39,7 +35,7 @@ func (conn *Conn) Part(channel string, message ...) {
} }
// 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 ...) { func (conn *Conn) Kick(channel, nick string, message ...string) {
msg := getStringMsg(message) msg := getStringMsg(message)
if msg != "" { if msg != "" {
msg = " :" + msg msg = " :" + msg
@ -48,7 +44,7 @@ func (conn *Conn) Kick(channel, nick string, message ...) {
} }
// 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 ...) { func (conn *Conn) Quit(message ...string) {
msg := getStringMsg(message) msg := getStringMsg(message)
if msg == "" { if msg == "" {
msg = "GoBye!" msg = "GoBye!"
@ -70,7 +66,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 ...) { func (conn *Conn) Ctcp(t, ctcp string, arg ...string) {
msg := getStringMsg(arg) msg := getStringMsg(arg)
if msg != "" { if msg != "" {
msg = " " + msg msg = " " + msg
@ -80,7 +76,7 @@ func (conn *Conn) Ctcp(t, ctcp string, arg ...) {
// 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 ...) { func (conn *Conn) CtcpReply(t, ctcp string, arg ...string) {
msg := getStringMsg(arg) msg := getStringMsg(arg)
if msg != "" { if msg != "" {
msg = " " + msg msg = " " + msg
@ -97,7 +93,7 @@ func (conn *Conn) Action(t, msg string) { conn.Ctcp(t, "ACTION", msg) }
// Topic() sends a TOPIC command to the channel // Topic() sends a TOPIC command to the channel
// 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 ...) { func (conn *Conn) Topic(channel string, topic ...string) {
t := getStringMsg(topic) t := getStringMsg(topic)
if t != "" { if t != "" {
t = " :" + t t = " :" + t
@ -112,7 +108,7 @@ func (conn *Conn) Topic(channel string, topic ...) {
// modestring == e.g. "+o <nick>" or "+ntk <key>" or "-is" // modestring == e.g. "+o <nick>" or "+ntk <key>" or "-is"
// 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 ...) { func (conn *Conn) Mode(t string, modestring ...string) {
mode := getStringMsg(modestring) mode := getStringMsg(modestring)
if mode != "" { if mode != "" {
mode = " " + mode mode = " " + mode
@ -123,7 +119,7 @@ func (conn *Conn) Mode(t string, modestring ...) {
// Away() sends an AWAY command to the server // Away() sends an AWAY command to the server
// 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 ...) { func (conn *Conn) Away(message ...string) {
msg := getStringMsg(message) msg := getStringMsg(message)
if msg != "" { if msg != "" {
msg = " :"+msg msg = " :"+msg
@ -141,16 +137,9 @@ func (conn *Conn) Oper(user, pass string) {
conn.out <- "OPER "+user+" "+pass conn.out <- "OPER "+user+" "+pass
} }
func getStringMsg(a ...) (msg string) { func getStringMsg(a ...string) (msg string) {
// dealing with functions with a variable parameter list is nasteeh :-( if len(a) > 0 {
// the below stolen and munged from fmt/print.go func getString() return a[0]
if v := reflect.NewValue(a).(*reflect.StructValue); v.NumField() > 0 {
if s, ok := v.Field(0).(*reflect.StringValue); ok {
return s.Get()
}
if b, ok := v.Interface().([]byte); ok {
return string(b)
}
} }
return "" return ""
} }

View File

@ -80,7 +80,7 @@ func (conn *Conn) initialise() {
// Connect the IRC connection object to "host[:port]" which should be either // Connect the IRC connection object to "host[:port]" which should be either
// a hostname or an IP address, with an optional port defaulting to 6667. // a hostname or an IP address, with an optional port defaulting to 6667.
// You can also provide an optional connect password. // You can also provide an optional connect password.
func (conn *Conn) Connect(host string, pass ...) os.Error { func (conn *Conn) Connect(host string, pass ...string) os.Error {
if conn.connected { if conn.connected {
return os.NewError(fmt.Sprintf("irc.Connect(): already connected to %s, cannot connect to %s", conn.Host, host)) return os.NewError(fmt.Sprintf("irc.Connect(): already connected to %s, cannot connect to %s", conn.Host, host))
} }
@ -113,7 +113,7 @@ func (conn *Conn) Connect(host string, pass ...) os.Error {
} }
// dispatch a nicely formatted os.Error to the error channel // dispatch a nicely formatted os.Error to the error channel
func (conn *Conn) error(s string, a ...) { conn.Err <- os.NewError(fmt.Sprintf(s, a)) } func (conn *Conn) error(s string, a ...interface{}) { conn.Err <- os.NewError(fmt.Sprintf(s, a)) }
// copied from http.client for great justice // copied from http.client for great justice
func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") } func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }
@ -142,7 +142,7 @@ func (conn *Conn) send() {
// so sleep for the current line's time value before sending it // so sleep for the current line's time value before sending it
time.Sleep(linetime) time.Sleep(linetime)
} }
if err := conn.io.WriteString(line + "\r\n"); err != nil { if _, err := conn.io.WriteString(line + "\r\n"); err != nil {
conn.error("irc.send(): %s", err.String()) conn.error("irc.send(): %s", err.String())
conn.shutdown() conn.shutdown()
break break
@ -161,8 +161,7 @@ func (conn *Conn) recv() {
conn.shutdown() conn.shutdown()
break break
} }
// chop off \r\n s = strings.Trim(s, "\r\n")
s = s[0 : len(s)-2]
fmt.Println("<- " + s) fmt.Println("<- " + s)
line := &Line{Raw: s} line := &Line{Raw: s}
@ -193,7 +192,7 @@ func (conn *Conn) recv() {
if len(args) > 1 { if len(args) > 1 {
line.Text = args[1] line.Text = args[1]
} }
args = strings.Split(args[0], " ", 0) args = strings.Split(args[0], " ", -1)
line.Cmd = strings.ToUpper(args[0]) line.Cmd = strings.ToUpper(args[0])
if len(args) > 1 { if len(args) > 1 {
line.Args = args[1:len(args)] line.Args = args[1:len(args)]