merge #34 changes into #32

This commit is contained in:
StalkR 2013-03-08 23:59:54 +01:00
commit 9c832fdbdb
4 changed files with 81 additions and 50 deletions

View File

@ -2,6 +2,34 @@ package client
import "strings" import "strings"
const (
REGISTER = "REGISTER"
CONNECTED = "CONNECTED"
DISCONNECTED = "DISCONNECTED"
ACTION = "ACTION"
AWAY = "AWAY"
CTCP = "CTCP"
CTCPREPLY = "CTCPREPLY"
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"
WHO = "WHO"
WHOIS = "WHOIS"
)
// 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
@ -23,18 +51,18 @@ func (conn *Conn) Raw(rawline string) {
} }
// Pass() sends a PASS command to the server // Pass() sends a PASS command to the server
func (conn *Conn) Pass(password string) { conn.Raw("PASS " + password) } func (conn *Conn) Pass(password string) { conn.Raw(PASS + " " + password) }
// Nick() sends a NICK command to the server // Nick() sends a NICK command to the server
func (conn *Conn) Nick(nick string) { conn.Raw("NICK " + nick) } func (conn *Conn) Nick(nick string) { conn.Raw(NICK + " " + nick) }
// User() sends a USER command to the server // User() sends a USER command to the server
func (conn *Conn) User(ident, name string) { func (conn *Conn) User(ident, name string) {
conn.Raw("USER " + ident + " 12 * :" + name) conn.Raw(USER + " " + ident + " 12 * :" + name)
} }
// Join() sends a JOIN command to the server // Join() sends a JOIN command to the server
func (conn *Conn) Join(channel string) { conn.Raw("JOIN " + channel) } func (conn *Conn) Join(channel string) { conn.Raw(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 ...string) { func (conn *Conn) Part(channel string, message ...string) {
@ -42,7 +70,7 @@ func (conn *Conn) Part(channel string, message ...string) {
if msg != "" { if msg != "" {
msg = " :" + msg msg = " :" + msg
} }
conn.Raw("PART " + channel + msg) conn.Raw(PART + " " + channel + msg)
} }
// Kick() sends a KICK command to remove a nick from a channel // Kick() sends a KICK command to remove a nick from a channel
@ -51,7 +79,7 @@ func (conn *Conn) Kick(channel, nick string, message ...string) {
if msg != "" { if msg != "" {
msg = " :" + msg msg = " :" + msg
} }
conn.Raw("KICK " + channel + " " + nick + msg) conn.Raw(KICK + " " + channel + " " + nick + msg)
} }
// 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
@ -60,20 +88,20 @@ func (conn *Conn) Quit(message ...string) {
if msg == "" { if msg == "" {
msg = conn.cfg.QuitMessage msg = conn.cfg.QuitMessage
} }
conn.Raw("QUIT :" + msg) conn.Raw(QUIT + " :" + msg)
} }
// Whois() sends a WHOIS command to the server // Whois() sends a WHOIS command to the server
func (conn *Conn) Whois(nick string) { conn.Raw("WHOIS " + nick) } func (conn *Conn) Whois(nick string) { conn.Raw(WHOIS + " " + nick) }
//Who() sends a WHO command to the server //Who() sends a WHO command to the server
func (conn *Conn) Who(nick string) { conn.Raw("WHO " + nick) } func (conn *Conn) Who(nick string) { conn.Raw(WHO + " " + nick) }
// Privmsg() sends a PRIVMSG to the target t // Privmsg() sends a PRIVMSG to the target t
func (conn *Conn) Privmsg(t, msg string) { conn.Raw("PRIVMSG " + t + " :" + msg) } func (conn *Conn) Privmsg(t, msg string) { conn.Raw(PRIVMSG + " " + t + " :" + msg) }
// Notice() sends a NOTICE to the target t // Notice() sends a NOTICE to the target t
func (conn *Conn) Notice(t, msg string) { conn.Raw("NOTICE " + t + " :" + msg) } func (conn *Conn) Notice(t, msg string) { conn.Raw(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
@ -96,10 +124,10 @@ func (conn *Conn) CtcpReply(t, ctcp string, arg ...string) {
} }
// Version() sends a CTCP "VERSION" to the target t // Version() sends a CTCP "VERSION" to the target t
func (conn *Conn) Version(t string) { conn.Ctcp(t, "VERSION") } func (conn *Conn) Version(t string) { conn.Ctcp(t, VERSION) }
// Action() sends a CTCP "ACTION" to the target t // Action() sends a CTCP "ACTION" to the target t
func (conn *Conn) Action(t, msg string) { conn.Ctcp(t, "ACTION", msg) } 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)
@ -109,7 +137,7 @@ func (conn *Conn) Topic(channel string, topic ...string) {
if t != "" { if t != "" {
t = " :" + t t = " :" + t
} }
conn.Raw("TOPIC " + channel + t) conn.Raw(TOPIC + " " + channel + t)
} }
// Mode() sends a MODE command to the server. This one can get complicated if // Mode() sends a MODE command to the server. This one can get complicated if
@ -124,7 +152,7 @@ func (conn *Conn) Mode(t string, modestring ...string) {
if mode != "" { if mode != "" {
mode = " " + mode mode = " " + mode
} }
conn.Raw("MODE " + t + mode) conn.Raw(MODE + " " + t + mode)
} }
// Away() sends an AWAY command to the server // Away() sends an AWAY command to the server
@ -135,15 +163,20 @@ func (conn *Conn) Away(message ...string) {
if msg != "" { if msg != "" {
msg = " :" + msg msg = " :" + msg
} }
conn.Raw("AWAY" + msg) conn.Raw(AWAY + msg)
} }
// Invite() sends an INVITE command to the server // Invite() sends an INVITE command to the server
func (conn *Conn) Invite(nick, channel string) { func (conn *Conn) Invite(nick, channel string) {
conn.Raw("INVITE " + nick + " " + channel) conn.Raw(INVITE + " " + nick + " " + channel)
} }
// Oper() sends an OPER command to the server // Oper() sends an OPER command to the server
func (conn *Conn) Oper(user, pass string) { func (conn *Conn) Oper(user, pass string) { conn.Raw(OPER + " " + user + " " + pass) }
conn.Raw("OPER " + user + " " + pass)
} // Ping() sends a PING command to the server
// A PONG response is to be expected afterwards
func (conn *Conn) Ping(message string) { conn.Raw(PING + " :" + message) }
// Pong() sends a PONG command to the server
func (conn *Conn) Pong(message string) { conn.Raw(PONG + " :" + message) }

View File

@ -272,7 +272,7 @@ func (conn *Conn) ping() {
for { for {
select { select {
case <-tick.C: case <-tick.C:
conn.Raw(fmt.Sprintf("PING :%d", time.Now().UnixNano())) conn.Ping(fmt.Sprintf("%d", time.Now().UnixNano()))
case <-conn.cPing: case <-conn.cPing:
tick.Stop() tick.Stop()
return return

View File

@ -7,20 +7,14 @@ import (
"strings" "strings"
) )
const (
REGISTER = "REGISTER"
CONNECTED = "CONNECTED"
DISCONNECTED = "DISCONNECTED"
)
// sets up the internal event handlers to do essential IRC protocol things // sets up the internal event handlers to do essential IRC protocol things
var intHandlers = map[string]HandlerFunc{ var intHandlers = map[string]HandlerFunc{
REGISTER: (*Conn).h_REGISTER, REGISTER: (*Conn).h_REGISTER,
"001": (*Conn).h_001, "001": (*Conn).h_001,
"433": (*Conn).h_433, "433": (*Conn).h_433,
"CTCP": (*Conn).h_CTCP, CTCP: (*Conn).h_CTCP,
"NICK": (*Conn).h_NICK, NICK: (*Conn).h_NICK,
"PING": (*Conn).h_PING, PING: (*Conn).h_PING,
} }
func (conn *Conn) addIntHandlers() { func (conn *Conn) addIntHandlers() {
@ -33,7 +27,7 @@ func (conn *Conn) addIntHandlers() {
// Basic ping/pong handler // Basic ping/pong handler
func (conn *Conn) h_PING(line *Line) { func (conn *Conn) h_PING(line *Line) {
conn.Raw("PONG :" + line.Args[0]) conn.Pong(line.Args[0])
} }
// Handler for initial registration with server once tcp connection is made. // Handler for initial registration with server once tcp connection is made.
@ -86,10 +80,10 @@ func (conn *Conn) h_433(line *Line) {
// Handle VERSION requests and CTCP PING // Handle VERSION requests and CTCP PING
func (conn *Conn) h_CTCP(line *Line) { func (conn *Conn) h_CTCP(line *Line) {
if line.Args[0] == "VERSION" { if line.Args[0] == VERSION {
conn.CtcpReply(line.Nick, "VERSION", conn.cfg.Version) conn.CtcpReply(line.Nick, VERSION, conn.cfg.Version)
} else if line.Args[0] == "PING" { } else if line.Args[0] == PING {
conn.CtcpReply(line.Nick, "PING", line.Args[2]) conn.CtcpReply(line.Nick, PING, line.Args[2])
} }
} }

View File

@ -158,23 +158,27 @@ func (conn *Conn) h_332(line *Line) {
// Handle 352 who reply // Handle 352 who reply
func (conn *Conn) h_352(line *Line) { func (conn *Conn) h_352(line *Line) {
if nk := conn.st.GetNick(line.Args[5]); nk != nil && nk != conn.Me() { nk := conn.st.GetNick(line.Args[5])
nk.Ident = line.Args[2] if nk == nil {
nk.Host = line.Args[3]
// XXX: do we care about the actual server the nick is on?
// or the hop count to this server?
// last arg contains "<hop count> <real name>"
a := strings.SplitN(line.Args[len(line.Args)-1], " ", 2)
nk.Name = a[1]
if idx := strings.Index(line.Args[6], "*"); idx != -1 {
nk.Modes.Oper = true
}
if idx := strings.Index(line.Args[6], "H"); idx != -1 {
nk.Modes.Invisible = true
}
} else {
logging.Warn("irc.352(): received WHO reply for unknown nick %s", logging.Warn("irc.352(): received WHO reply for unknown nick %s",
line.Args[5]) line.Args[5])
return
}
if nk == conn.Me() {
return
}
nk.Ident = line.Args[2]
nk.Host = line.Args[3]
// XXX: do we care about the actual server the nick is on?
// or the hop count to this server?
// last arg contains "<hop count> <real name>"
a := strings.SplitN(line.Args[len(line.Args)-1], " ", 2)
nk.Name = a[1]
if idx := strings.Index(line.Args[6], "*"); idx != -1 {
nk.Modes.Oper = true
}
if idx := strings.Index(line.Args[6], "H"); idx != -1 {
nk.Modes.Invisible = true
} }
} }