2009-11-29 20:23:15 +00:00
|
|
|
package irc
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
import (
|
2009-12-17 21:30:18 +00:00
|
|
|
// "fmt";
|
|
|
|
"reflect"
|
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
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// send a raw line to the server for debugging etc
|
2009-12-17 21:30:18 +00:00
|
|
|
func (conn *Conn) Raw(s string) { conn.out <- s }
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2009-11-29 20:23:15 +00:00
|
|
|
// send a PASS command to the server
|
2009-12-17 21:30:18 +00:00
|
|
|
func (conn *Conn) Pass(p string) { conn.out <- "PASS "+p }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
|
|
|
// send a NICK command to the server
|
2009-12-17 21:30:18 +00:00
|
|
|
func (conn *Conn) Nick(n string) { conn.out <- "NICK "+n }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
|
|
|
// send a USER command to the server
|
2009-12-17 21:30:18 +00:00
|
|
|
func (conn *Conn) User(u, n string) { conn.out <- "USER "+u+" 12 * :"+n }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
|
|
|
// send a JOIN command to the server
|
2009-12-17 21:30:18 +00:00
|
|
|
func (conn *Conn) Join(c string) { conn.out <- "JOIN "+c }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
|
|
|
// send a PART command to the server
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) Part(c string, a ...) {
|
2009-12-17 21:30:18 +00:00
|
|
|
msg := getStringMsg(a)
|
2009-11-29 20:23:15 +00:00
|
|
|
if msg != "" {
|
|
|
|
msg = " :" + msg
|
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.out <- "PART "+c+msg
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send a QUIT command to the server
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) Quit(a ...) {
|
2009-12-17 21:30:18 +00:00
|
|
|
msg := getStringMsg(a)
|
2009-11-29 20:23:15 +00:00
|
|
|
if msg == "" {
|
|
|
|
msg = "GoBye!"
|
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.out <- "QUIT :"+msg
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send a WHOIS command to the server
|
2009-12-17 21:30:18 +00:00
|
|
|
func (conn *Conn) Whois(t string) { conn.out <- "WHOIS "+t }
|
2009-12-17 17:22:31 +00:00
|
|
|
|
|
|
|
// send a WHO command to the server
|
2009-12-17 21:30:18 +00:00
|
|
|
func (conn *Conn) Who(t string) { conn.out <- "WHO "+t }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
|
|
|
// send a PRIVMSG to the target t
|
2009-12-17 21:30:18 +00:00
|
|
|
func (conn *Conn) Privmsg(t, msg string) { conn.out <- "PRIVMSG "+t+" :"+msg }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
|
|
|
// send a NOTICE to the target t
|
2009-12-17 21:30:18 +00:00
|
|
|
func (conn *Conn) Notice(t, msg string) { conn.out <- "NOTICE "+t+" :"+msg }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
|
|
|
// send a (generic) CTCP to the target t
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) Ctcp(t, ctcp string, a ...) {
|
2009-12-17 21:30:18 +00:00
|
|
|
msg := getStringMsg(a)
|
2009-11-29 20:23:15 +00:00
|
|
|
if msg != "" {
|
|
|
|
msg = " " + msg
|
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.Privmsg(t, "\001"+ctcp+msg+"\001")
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send a generic CTCP reply to the target t
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) CtcpReply(t, ctcp string, a ...) {
|
2009-12-17 21:30:18 +00:00
|
|
|
msg := getStringMsg(a)
|
2009-11-29 20:23:15 +00:00
|
|
|
if msg != "" {
|
|
|
|
msg = " " + msg
|
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.Notice(t, "\001"+ctcp+msg+"\001")
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send 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
|
|
|
|
|
|
|
// send a CTCP "ACTION" to the target t -- /me does stuff!
|
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
|
|
|
|
|
|
|
// send a TOPIC command to the channel c
|
|
|
|
func (conn *Conn) Topic(c string, a ...) {
|
2009-12-17 21:30:18 +00:00
|
|
|
topic := getStringMsg(a)
|
2009-12-17 17:22:31 +00:00
|
|
|
if topic != "" {
|
|
|
|
topic = " :" + topic
|
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.out <- "TOPIC "+c+topic
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send a MODE command (this one gets complicated)
|
|
|
|
// Mode(t) retrieves the user or channel modes for target t
|
|
|
|
// Mode(t, "string"
|
|
|
|
func (conn *Conn) Mode(t string, a ...) {
|
2009-12-17 21:30:18 +00:00
|
|
|
mode := getStringMsg(a)
|
2009-12-17 17:22:31 +00:00
|
|
|
if mode != "" {
|
|
|
|
mode = " " + mode
|
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.out <- "MODE "+t+mode
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getStringMsg(a ...) (msg string) {
|
|
|
|
// dealing with functions with a variable parameter list is nasteeh :-(
|
2009-12-17 17:22:31 +00:00
|
|
|
// the below stolen and munged from fmt/print.go func getString()
|
|
|
|
if v := reflect.NewValue(a).(*reflect.StructValue); v.NumField() > 0 {
|
|
|
|
if s, ok := v.Field(0).(*reflect.StringValue); ok {
|
2009-12-17 21:30:18 +00:00
|
|
|
return s.Get()
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
if b, ok := v.Interface().([]byte); ok {
|
|
|
|
return string(b)
|
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
2009-12-17 17:22:31 +00:00
|
|
|
return ""
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|