2010-11-21 19:53:14 +00:00
|
|
|
package client
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2010-11-03 23:48:28 +00:00
|
|
|
import "strings"
|
|
|
|
|
2009-11-29 20:23:15 +00:00
|
|
|
// this file contains the various commands you can
|
2009-12-17 17:22:31 +00:00
|
|
|
// send to the server using an Conn connection
|
2009-11-29 20:23:15 +00:00
|
|
|
|
|
|
|
// This could be a lot less ugly with the ability to manipulate
|
|
|
|
// the symbol table and add methods/functions on the fly
|
2009-12-17 21:30:18 +00:00
|
|
|
// [ CMD, FMT, FMTARGS ] etc.
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2013-03-06 21:46:10 +00:00
|
|
|
// safe removes CR/LF to avoid IRC command injection.
|
|
|
|
func safe(s string) string {
|
|
|
|
s = strings.Replace(s, "\r", "", -1)
|
|
|
|
s = strings.Replace(s, "\n", "", -1)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2011-11-06 04:56:46 +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.
|
|
|
|
func (conn *Conn) Raw(rawline string) { conn.out <- rawline }
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Pass() sends a PASS command to the server
|
2013-03-06 21:46:10 +00:00
|
|
|
func (conn *Conn) Pass(password string) { conn.out <- safe("PASS " + password) }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Nick() sends a NICK command to the server
|
2013-03-06 21:46:10 +00:00
|
|
|
func (conn *Conn) Nick(nick string) { conn.out <- safe("NICK " + nick) }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// User() sends a USER command to the server
|
|
|
|
func (conn *Conn) User(ident, name string) {
|
2013-03-06 21:46:10 +00:00
|
|
|
conn.out <- safe("USER " + ident + " 12 * :" + name)
|
2009-12-18 22:39:22 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Join() sends a JOIN command to the server
|
2013-03-06 21:46:10 +00:00
|
|
|
func (conn *Conn) Join(channel string) { conn.out <- safe("JOIN " + channel) }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Part() sends a PART command to the server with an optional part 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-06 21:46:10 +00:00
|
|
|
conn.out <- safe("PART " + channel + msg)
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-12-19 22:15:11 +00:00
|
|
|
// Kick() sends a KICK command to remove a nick from a channel
|
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-06 21:46:10 +00:00
|
|
|
conn.out <- safe("KICK " + channel + " " + nick + msg)
|
2009-12-19 22:15:11 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Quit() sends a QUIT command to the server with an optional 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-06 21:46:10 +00:00
|
|
|
conn.out <- safe("QUIT :" + msg)
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Whois() sends a WHOIS command to the server
|
2013-03-06 21:46:10 +00:00
|
|
|
func (conn *Conn) Whois(nick string) { conn.out <- safe("WHOIS " + nick) }
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
//Who() sends a WHO command to the server
|
2013-03-06 21:46:10 +00:00
|
|
|
func (conn *Conn) Who(nick string) { conn.out <- safe("WHO " + nick) }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Privmsg() sends a PRIVMSG to the target t
|
2013-03-06 21:46:10 +00:00
|
|
|
func (conn *Conn) Privmsg(t, msg string) { conn.out <- safe("PRIVMSG " + t + " :" + msg) }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Notice() sends a NOTICE to the target t
|
2013-03-06 21:46:10 +00:00
|
|
|
func (conn *Conn) Notice(t, msg string) { conn.out <- safe("NOTICE " + t + " :" + msg) }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Ctcp() sends a (generic) CTCP message to the target t
|
|
|
|
// with an optional argument
|
2010-08-21 18:23:07 +00:00
|
|
|
func (conn *Conn) Ctcp(t, ctcp string, arg ...string) {
|
2010-11-03 23:48:28 +00:00
|
|
|
msg := strings.Join(arg, " ")
|
2009-11-29 20:23:15 +00:00
|
|
|
if msg != "" {
|
|
|
|
msg = " " + msg
|
|
|
|
}
|
2011-08-21 12:22:26 +00:00
|
|
|
conn.Privmsg(t, "\001"+strings.ToUpper(ctcp)+msg+"\001")
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// CtcpReply() sends a generic CTCP reply to the target t
|
|
|
|
// with an optional argument
|
2010-08-21 18:23:07 +00:00
|
|
|
func (conn *Conn) CtcpReply(t, ctcp string, arg ...string) {
|
2010-11-03 23:48:28 +00:00
|
|
|
msg := strings.Join(arg, " ")
|
2009-11-29 20:23:15 +00:00
|
|
|
if msg != "" {
|
|
|
|
msg = " " + msg
|
|
|
|
}
|
2011-08-21 12:22:26 +00:00
|
|
|
conn.Notice(t, "\001"+strings.ToUpper(ctcp)+msg+"\001")
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Version() sends a CTCP "VERSION" to the target t
|
2009-12-17 21:30:18 +00:00
|
|
|
func (conn *Conn) Version(t string) { conn.Ctcp(t, "VERSION") }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Action() sends a CTCP "ACTION" to the target t
|
2009-12-17 21:30:18 +00:00
|
|
|
func (conn *Conn) Action(t, msg string) { conn.Ctcp(t, "ACTION", msg) }
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Topic() sends a TOPIC command to the channel
|
|
|
|
// Topic(channel) retrieves the current channel topic (see "332" handler)
|
|
|
|
// Topic(channel, topic) sets the topic for the channel
|
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-06 21:46:10 +00:00
|
|
|
conn.out <- safe("TOPIC " + channel + t)
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
2009-12-19 22:15:11 +00:00
|
|
|
// Mode() sends a MODE command to the server. This one can get complicated if
|
|
|
|
// we try to be too clever, so it's deliberately simple:
|
2009-12-18 22:39:22 +00:00
|
|
|
// Mode(t) retrieves the user or channel modes for target t
|
|
|
|
// Mode(t, "modestring") sets user or channel modes for target t, where...
|
|
|
|
// 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
|
|
|
|
// the state tracking and ChanMode/NickMode/ChanPrivs objects later...
|
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-06 21:46:10 +00:00
|
|
|
conn.out <- safe("MODE " + t + mode)
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-12-19 22:15:11 +00:00
|
|
|
// Away() sends an AWAY command to the server
|
|
|
|
// Away() resets away status
|
|
|
|
// Away(message) sets away with the given 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-06 21:46:10 +00:00
|
|
|
conn.out <- safe("AWAY" + msg)
|
2009-12-19 22:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invite() sends an INVITE command to the server
|
|
|
|
func (conn *Conn) Invite(nick, channel string) {
|
2013-03-06 21:46:10 +00:00
|
|
|
conn.out <- safe("INVITE " + nick + " " + channel)
|
2009-12-19 22:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Oper() sends an OPER command to the server
|
|
|
|
func (conn *Conn) Oper(user, pass string) {
|
2013-03-06 21:46:10 +00:00
|
|
|
conn.out <- safe("OPER " + user + " " + pass)
|
2009-12-19 22:15:11 +00:00
|
|
|
}
|