2010-11-21 19:53:14 +00:00
|
|
|
package client
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2015-10-12 13:48:56 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
2010-11-03 23:48:28 +00:00
|
|
|
|
2013-03-08 01:33:56 +00:00
|
|
|
const (
|
|
|
|
REGISTER = "REGISTER"
|
|
|
|
CONNECTED = "CONNECTED"
|
|
|
|
DISCONNECTED = "DISCONNECTED"
|
|
|
|
ACTION = "ACTION"
|
|
|
|
AWAY = "AWAY"
|
2016-01-11 22:20:06 +00:00
|
|
|
CAP = "CAP"
|
2013-03-08 01:33:56 +00:00
|
|
|
CTCP = "CTCP"
|
|
|
|
CTCPREPLY = "CTCPREPLY"
|
2016-11-28 22:50:19 +00:00
|
|
|
ERROR = "ERROR"
|
2013-03-08 01:33:56 +00:00
|
|
|
INVITE = "INVITE"
|
|
|
|
JOIN = "JOIN"
|
|
|
|
KICK = "KICK"
|
|
|
|
MODE = "MODE"
|
|
|
|
NICK = "NICK"
|
|
|
|
NOTICE = "NOTICE"
|
|
|
|
OPER = "OPER"
|
|
|
|
PART = "PART"
|
|
|
|
PASS = "PASS"
|
|
|
|
PING = "PING"
|
|
|
|
PONG = "PONG"
|
|
|
|
PRIVMSG = "PRIVMSG"
|
|
|
|
QUIT = "QUIT"
|
|
|
|
TOPIC = "TOPIC"
|
|
|
|
USER = "USER"
|
|
|
|
VERSION = "VERSION"
|
2013-03-15 17:40:28 +00:00
|
|
|
VHOST = "VHOST"
|
2013-03-08 01:33:56 +00:00
|
|
|
WHO = "WHO"
|
|
|
|
WHOIS = "WHOIS"
|
2015-04-15 21:27:50 +00:00
|
|
|
defaultSplit = 450
|
2013-03-08 01:33:56 +00:00
|
|
|
)
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// cutNewLines() pares down a string to the part before the first "\r" or "\n".
|
2013-03-07 10:54:00 +00:00
|
|
|
func cutNewLines(s string) string {
|
2013-03-10 12:17:16 +00:00
|
|
|
r := strings.SplitN(s, "\r", 2)
|
|
|
|
r = strings.SplitN(r[0], "\n", 2)
|
2013-03-07 10:54:00 +00:00
|
|
|
return r[0]
|
2013-03-06 21:46:10 +00:00
|
|
|
}
|
|
|
|
|
2013-03-17 01:21:09 +00:00
|
|
|
// indexFragment looks for the last sentence split-point (defined as one of
|
|
|
|
// the punctuation characters .:;,!?"' followed by a space) in the string s
|
|
|
|
// and returns the index in the string after that split-point. If no split-
|
|
|
|
// point is found it returns the index after the last space in s, or -1.
|
|
|
|
func indexFragment(s string) int {
|
|
|
|
max := -1
|
|
|
|
for _, sep := range []string{". ", ": ", "; ", ", ", "! ", "? ", "\" ", "' "} {
|
|
|
|
if idx := strings.LastIndex(s, sep); idx > max {
|
|
|
|
max = idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if max > 0 {
|
|
|
|
return max + 2
|
|
|
|
}
|
|
|
|
if idx := strings.LastIndex(s, " "); idx > 0 {
|
|
|
|
return idx + 1
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// splitMessage splits a message > splitLen chars at:
|
|
|
|
// 1. the end of the last sentence fragment before splitLen
|
|
|
|
// 2. the end of the last word before splitLen
|
|
|
|
// 3. splitLen itself
|
|
|
|
func splitMessage(msg string, splitLen int) (msgs []string) {
|
|
|
|
// This is quite short ;-)
|
2015-04-14 10:18:11 +00:00
|
|
|
if splitLen < 13 {
|
2015-04-15 21:27:50 +00:00
|
|
|
splitLen = defaultSplit
|
2013-03-17 01:21:09 +00:00
|
|
|
}
|
|
|
|
for len(msg) > splitLen {
|
2015-04-14 10:18:11 +00:00
|
|
|
idx := indexFragment(msg[:splitLen-3])
|
2013-03-17 01:21:09 +00:00
|
|
|
if idx < 0 {
|
2015-04-14 10:18:11 +00:00
|
|
|
idx = splitLen - 3
|
2013-03-17 01:21:09 +00:00
|
|
|
}
|
2015-10-12 13:48:56 +00:00
|
|
|
msgs = append(msgs, msg[:idx]+"...")
|
2013-03-17 01:21:09 +00:00
|
|
|
msg = msg[idx:]
|
|
|
|
}
|
|
|
|
return append(msgs, msg)
|
|
|
|
}
|
|
|
|
|
2022-03-06 22:20:06 +00:00
|
|
|
func splitArgs(args []string, maxLen int) []string {
|
|
|
|
res := make([]string, 0)
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for i < len(args) {
|
|
|
|
currArg := args[i]
|
|
|
|
i++
|
|
|
|
|
|
|
|
for i < len(args) && len(currArg)+len(args[i])+1 < maxLen {
|
|
|
|
currArg += " " + args[i]
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
res = append(res, currArg)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Raw sends a raw line to the server, should really only be used for
|
2009-12-18 22:39:22 +00:00
|
|
|
// debugging purposes but may well come in handy.
|
2013-03-07 10:54:00 +00:00
|
|
|
func (conn *Conn) Raw(rawline string) {
|
|
|
|
// Avoid command injection by enforcing one command per line.
|
|
|
|
conn.out <- cutNewLines(rawline)
|
|
|
|
}
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Pass sends a PASS command to the server.
|
|
|
|
// PASS password
|
2013-03-08 22:59:54 +00:00
|
|
|
func (conn *Conn) Pass(password string) { conn.Raw(PASS + " " + password) }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Nick sends a NICK command to the server.
|
|
|
|
// NICK nick
|
2013-03-08 22:59:54 +00:00
|
|
|
func (conn *Conn) Nick(nick string) { conn.Raw(NICK + " " + nick) }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// User sends a USER command to the server.
|
|
|
|
// USER ident 12 * :name
|
2009-12-18 22:39:22 +00:00
|
|
|
func (conn *Conn) User(ident, name string) {
|
2013-03-08 22:59:54 +00:00
|
|
|
conn.Raw(USER + " " + ident + " 12 * :" + name)
|
2009-12-18 22:39:22 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Join sends a JOIN command to the server with an optional key.
|
|
|
|
// JOIN channel [key]
|
2014-12-23 18:21:53 +00:00
|
|
|
func (conn *Conn) Join(channel string, key ...string) {
|
|
|
|
k := ""
|
|
|
|
if len(key) > 0 {
|
|
|
|
k = " " + key[0]
|
|
|
|
}
|
|
|
|
conn.Raw(JOIN + " " + channel + k)
|
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Part sends a PART command to the server with an optional part message.
|
|
|
|
// PART channel [:message]
|
2010-08-21 18:23:07 +00:00
|
|
|
func (conn *Conn) Part(channel string, message ...string) {
|
2010-11-03 23:48:28 +00:00
|
|
|
msg := strings.Join(message, " ")
|
2009-11-29 20:23:15 +00:00
|
|
|
if msg != "" {
|
|
|
|
msg = " :" + msg
|
|
|
|
}
|
2013-03-08 22:59:54 +00:00
|
|
|
conn.Raw(PART + " " + channel + msg)
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Kick sends a KICK command to remove a nick from a channel.
|
|
|
|
// KICK channel nick [:message]
|
2010-08-21 18:23:07 +00:00
|
|
|
func (conn *Conn) Kick(channel, nick string, message ...string) {
|
2010-11-03 23:48:28 +00:00
|
|
|
msg := strings.Join(message, " ")
|
2009-12-19 22:15:11 +00:00
|
|
|
if msg != "" {
|
|
|
|
msg = " :" + msg
|
|
|
|
}
|
2013-03-08 22:59:54 +00:00
|
|
|
conn.Raw(KICK + " " + channel + " " + nick + msg)
|
2009-12-19 22:15:11 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Quit sends a QUIT command to the server with an optional quit message.
|
|
|
|
// QUIT [:message]
|
2010-08-21 18:23:07 +00:00
|
|
|
func (conn *Conn) Quit(message ...string) {
|
2010-11-03 23:48:28 +00:00
|
|
|
msg := strings.Join(message, " ")
|
2009-11-29 20:23:15 +00:00
|
|
|
if msg == "" {
|
2013-02-18 06:28:22 +00:00
|
|
|
msg = conn.cfg.QuitMessage
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
2013-03-08 22:59:54 +00:00
|
|
|
conn.Raw(QUIT + " :" + msg)
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Whois sends a WHOIS command to the server.
|
|
|
|
// WHOIS nick
|
2013-03-08 22:59:54 +00:00
|
|
|
func (conn *Conn) Whois(nick string) { conn.Raw(WHOIS + " " + nick) }
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Who sends a WHO command to the server.
|
|
|
|
// WHO nick
|
2013-03-08 22:59:54 +00:00
|
|
|
func (conn *Conn) Who(nick string) { conn.Raw(WHO + " " + nick) }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Privmsg sends a PRIVMSG to the target nick or channel t.
|
|
|
|
// If msg is longer than Config.SplitLen characters, multiple PRIVMSGs
|
|
|
|
// will be sent to the target containing sequential parts of msg.
|
2015-10-12 13:48:56 +00:00
|
|
|
// PRIVMSG t :msg
|
2013-03-17 01:21:09 +00:00
|
|
|
func (conn *Conn) Privmsg(t, msg string) {
|
2015-10-12 13:48:56 +00:00
|
|
|
prefix := PRIVMSG + " " + t + " :"
|
2013-03-17 01:21:09 +00:00
|
|
|
for _, s := range splitMessage(msg, conn.cfg.SplitLen) {
|
2015-10-12 13:48:56 +00:00
|
|
|
conn.Raw(prefix + s)
|
2013-03-17 01:21:09 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2015-10-12 13:48:56 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Notice sends a NOTICE to the target nick or channel t.
|
|
|
|
// If msg is longer than Config.SplitLen characters, multiple NOTICEs
|
|
|
|
// will be sent to the target containing sequential parts of msg.
|
|
|
|
// NOTICE t :msg
|
2013-03-17 01:21:09 +00:00
|
|
|
func (conn *Conn) Notice(t, msg string) {
|
|
|
|
for _, s := range splitMessage(msg, conn.cfg.SplitLen) {
|
|
|
|
conn.Raw(NOTICE + " " + t + " :" + s)
|
|
|
|
}
|
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Ctcp sends a (generic) CTCP message to the target nick
|
|
|
|
// or channel t, with an optional argument.
|
|
|
|
// PRIVMSG t :\001CTCP arg\001
|
2010-08-21 18:23:07 +00:00
|
|
|
func (conn *Conn) Ctcp(t, ctcp string, arg ...string) {
|
2013-03-17 01:21:09 +00:00
|
|
|
// We need to split again here to ensure
|
|
|
|
for _, s := range splitMessage(strings.Join(arg, " "), conn.cfg.SplitLen) {
|
|
|
|
if s != "" {
|
|
|
|
s = " " + s
|
|
|
|
}
|
|
|
|
// Using Raw rather than PRIVMSG here to avoid double-split problems.
|
|
|
|
conn.Raw(PRIVMSG + " " + t + " :\001" + strings.ToUpper(ctcp) + s + "\001")
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// CtcpReply sends a (generic) CTCP reply to the target nick
|
|
|
|
// or channel t, with an optional argument.
|
|
|
|
// NOTICE t :\001CTCP arg\001
|
2010-08-21 18:23:07 +00:00
|
|
|
func (conn *Conn) CtcpReply(t, ctcp string, arg ...string) {
|
2013-03-17 01:21:09 +00:00
|
|
|
for _, s := range splitMessage(strings.Join(arg, " "), conn.cfg.SplitLen) {
|
|
|
|
if s != "" {
|
|
|
|
s = " " + s
|
|
|
|
}
|
|
|
|
// Using Raw rather than NOTICE here to avoid double-split problems.
|
|
|
|
conn.Raw(NOTICE + " " + t + " :\001" + strings.ToUpper(ctcp) + s + "\001")
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Version sends a CTCP "VERSION" to the target nick or channel t.
|
2013-03-08 01:33:56 +00:00
|
|
|
func (conn *Conn) Version(t string) { conn.Ctcp(t, VERSION) }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Action sends a CTCP "ACTION" to the target nick or channel t.
|
2013-03-08 01:33:56 +00:00
|
|
|
func (conn *Conn) Action(t, msg string) { conn.Ctcp(t, ACTION, msg) }
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Topic() sends a TOPIC command for a channel.
|
|
|
|
// If no topic is provided this requests that a 332 response is sent by the
|
|
|
|
// server for that channel, which can then be handled to retrieve the current
|
|
|
|
// channel topic. If a topic is provided the channel's topic will be set.
|
|
|
|
// TOPIC channel
|
|
|
|
// TOPIC channel :topic
|
2010-08-21 18:23:07 +00:00
|
|
|
func (conn *Conn) Topic(channel string, topic ...string) {
|
2010-11-03 23:48:28 +00:00
|
|
|
t := strings.Join(topic, " ")
|
2009-12-18 22:39:22 +00:00
|
|
|
if t != "" {
|
|
|
|
t = " :" + t
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2013-03-08 22:59:54 +00:00
|
|
|
conn.Raw(TOPIC + " " + channel + t)
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Mode sends a MODE command for a target nick or channel t.
|
|
|
|
// If no mode strings are provided this requests that a 324 response is sent
|
|
|
|
// by the server for the target. Otherwise the mode strings are concatenated
|
|
|
|
// with spaces and sent to the server. This allows e.g.
|
|
|
|
// conn.Mode("#channel", "+nsk", "mykey")
|
|
|
|
//
|
|
|
|
// MODE t
|
|
|
|
// MODE t modestring
|
2010-08-21 18:23:07 +00:00
|
|
|
func (conn *Conn) Mode(t string, modestring ...string) {
|
2010-11-03 23:48:28 +00:00
|
|
|
mode := strings.Join(modestring, " ")
|
2009-12-17 17:22:31 +00:00
|
|
|
if mode != "" {
|
|
|
|
mode = " " + mode
|
|
|
|
}
|
2013-03-08 22:59:54 +00:00
|
|
|
conn.Raw(MODE + " " + t + mode)
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Away sends an AWAY command to the server.
|
|
|
|
// If a message is provided it sets the client's away status with that message,
|
|
|
|
// otherwise it resets the client's away status.
|
|
|
|
// AWAY
|
|
|
|
// AWAY :message
|
2010-08-21 18:23:07 +00:00
|
|
|
func (conn *Conn) Away(message ...string) {
|
2010-11-03 23:48:28 +00:00
|
|
|
msg := strings.Join(message, " ")
|
2009-12-19 22:15:11 +00:00
|
|
|
if msg != "" {
|
2010-11-04 00:25:46 +00:00
|
|
|
msg = " :" + msg
|
2009-12-19 22:15:11 +00:00
|
|
|
}
|
2013-03-08 22:59:54 +00:00
|
|
|
conn.Raw(AWAY + msg)
|
2009-12-19 22:15:11 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Invite sends an INVITE command to the server.
|
|
|
|
// INVITE nick channel
|
2009-12-19 22:15:11 +00:00
|
|
|
func (conn *Conn) Invite(nick, channel string) {
|
2013-03-08 22:59:54 +00:00
|
|
|
conn.Raw(INVITE + " " + nick + " " + channel)
|
2009-12-19 22:15:11 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Oper sends an OPER command to the server.
|
|
|
|
// OPER user pass
|
2013-03-08 22:59:54 +00:00
|
|
|
func (conn *Conn) Oper(user, pass string) { conn.Raw(OPER + " " + user + " " + pass) }
|
2013-03-08 01:33:56 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// VHost sends a VHOST command to the server.
|
|
|
|
// VHOST user pass
|
2013-03-15 17:40:28 +00:00
|
|
|
func (conn *Conn) VHost(user, pass string) { conn.Raw(VHOST + " " + user + " " + pass) }
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Ping sends a PING command to the server, which should PONG.
|
|
|
|
// PING :message
|
2013-03-08 22:59:54 +00:00
|
|
|
func (conn *Conn) Ping(message string) { conn.Raw(PING + " :" + message) }
|
2013-03-08 01:33:56 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Pong sends a PONG command to the server.
|
|
|
|
// PONG :message
|
2013-03-08 22:59:54 +00:00
|
|
|
func (conn *Conn) Pong(message string) { conn.Raw(PONG + " :" + message) }
|
2016-01-11 22:20:06 +00:00
|
|
|
|
|
|
|
// Cap sends a CAP command to the server.
|
2016-01-11 22:25:17 +00:00
|
|
|
// CAP subcommand
|
|
|
|
// CAP subcommand :message
|
2016-01-11 22:50:32 +00:00
|
|
|
func (conn *Conn) Cap(subcommmand string, capabilities ...string) {
|
|
|
|
if len(capabilities) == 0 {
|
2016-01-11 22:20:06 +00:00
|
|
|
conn.Raw(CAP + " " + subcommmand)
|
|
|
|
} else {
|
2022-03-06 22:20:06 +00:00
|
|
|
cmdPrefix := CAP + " " + subcommmand + " :"
|
|
|
|
for _, args := range splitArgs(capabilities, defaultSplit-len(cmdPrefix)) {
|
|
|
|
conn.Raw(cmdPrefix + args)
|
|
|
|
}
|
2016-01-11 22:20:06 +00:00
|
|
|
}
|
|
|
|
}
|