diff --git a/irc.go b/irc.go index 7a3a3b3..ed04b27 100644 --- a/irc.go +++ b/irc.go @@ -20,6 +20,7 @@ package irc import ( "bufio" + "bytes" "crypto/tls" "errors" "fmt" @@ -245,6 +246,29 @@ func (irc *Connection) Privmsgf(target, format string, a ...interface{}) { irc.Privmsg(target, fmt.Sprintf(format, a...)) } +// Kick from with . For no message, pass empty string ("") +func (irc *Connection) Kick(user, channel, msg string) { + var cmd bytes.Buffer + cmd.WriteString(fmt.Sprintf("KICK %s %s", channel, user)) + if msg != "" { + cmd.WriteString(fmt.Sprintf(" :%s", msg)) + } + cmd.WriteString("\r\n") + irc.pwrite <- cmd.String() +} + +// Kick all from with . For no message, pass +// empty string ("") +func (irc *Connection) MultiKick(users []string, channel string, msg string) { + var cmd bytes.Buffer + cmd.WriteString(fmt.Sprintf("KICK %s %s", channel, strings.Join(users, ","))) + if msg != "" { + cmd.WriteString(fmt.Sprintf(" :%s", msg)) + } + cmd.WriteString("\r\n") + irc.pwrite <- cmd.String() +} + // Send raw string. func (irc *Connection) SendRaw(message string) { irc.pwrite <- message + "\r\n" diff --git a/irc_callback.go b/irc_callback.go index a7576c9..4b71b3b 100644 --- a/irc_callback.go +++ b/irc_callback.go @@ -80,8 +80,11 @@ func (irc *Connection) RunCallbacks(event *Event) { if event.Code == "PRIVMSG" && len(msg) > 2 && msg[0] == '\x01' { event.Code = "CTCP" //Unknown CTCP - if i := strings.LastIndex(msg, "\x01"); i > -1 { + if i := strings.LastIndex(msg, "\x01"); i > 0 { msg = msg[1:i] + } else { + irc.Log.Printf("Invalid CTCP Message: %s\n", strconv.Quote(msg)) + return } if msg == "VERSION" { @@ -161,14 +164,14 @@ func (irc *Connection) setupCallbacks() { irc.AddCallback("CTCP_PING", func(e *Event) { irc.SendRawf("NOTICE %s :\x01%s\x01", e.Nick, e.Message()) }) // 437: ERR_UNAVAILRESOURCE " :Nick/channel is temporarily unavailable" - // Add a _ to current nick. If irc.nickcurrent is empty this cannot - // work. It has to be set somewhere first in case the nick is already - // taken or unavailable from the beginning. - irc.AddCallback("437", func(e *Event) { - // If irc.nickcurrent hasn't been set yet, set to irc.nick - if irc.nickcurrent == "" { - irc.nickcurrent = irc.nick - } + // Add a _ to current nick. If irc.nickcurrent is empty this cannot + // work. It has to be set somewhere first in case the nick is already + // taken or unavailable from the beginning. + irc.AddCallback("437", func(e *Event) { + // If irc.nickcurrent hasn't been set yet, set to irc.nick + if irc.nickcurrent == "" { + irc.nickcurrent = irc.nick + } if len(irc.nickcurrent) > 8 { irc.nickcurrent = "_" + irc.nickcurrent @@ -181,10 +184,10 @@ func (irc *Connection) setupCallbacks() { // 433: ERR_NICKNAMEINUSE " :Nickname is already in use" // Add a _ to current nick. irc.AddCallback("433", func(e *Event) { - // If irc.nickcurrent hasn't been set yet, set to irc.nick - if irc.nickcurrent == "" { - irc.nickcurrent = irc.nick - } + // If irc.nickcurrent hasn't been set yet, set to irc.nick + if irc.nickcurrent == "" { + irc.nickcurrent = irc.nick + } if len(irc.nickcurrent) > 8 { irc.nickcurrent = "_" + irc.nickcurrent @@ -216,3 +219,7 @@ func (irc *Connection) setupCallbacks() { irc.nickcurrent = e.Arguments[0] }) } + +func init() { + rand.Seed(time.Now().UnixNano()) +}