2009-11-29 20:23:15 +00:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2009-12-17 21:30:18 +00:00
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"net"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2009-11-29 20:23:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// the IRC connection object
|
2009-12-17 17:22:31 +00:00
|
|
|
type Conn struct {
|
|
|
|
// Hostname, Nickname, etc.
|
2009-12-17 21:30:18 +00:00
|
|
|
Host string
|
|
|
|
Me *Nick
|
2009-12-17 17:22:31 +00:00
|
|
|
|
|
|
|
// I/O stuff to server
|
2009-12-17 21:30:18 +00:00
|
|
|
sock *net.TCPConn
|
|
|
|
io *bufio.ReadWriter
|
|
|
|
in chan *Line
|
|
|
|
out chan string
|
|
|
|
connected bool
|
2009-12-17 17:22:31 +00:00
|
|
|
|
|
|
|
// Error channel to transmit any fail back to the user
|
2009-12-17 21:30:18 +00:00
|
|
|
Err chan os.Error
|
2009-12-17 17:22:31 +00:00
|
|
|
|
|
|
|
// Event handler mapping
|
2009-12-17 21:30:18 +00:00
|
|
|
events map[string][]func(*Conn, *Line)
|
2009-12-17 17:22:31 +00:00
|
|
|
|
|
|
|
// Map of channels we're on
|
2009-12-17 21:30:18 +00:00
|
|
|
chans map[string]*Channel
|
2009-12-17 17:22:31 +00:00
|
|
|
|
|
|
|
// Map of nicks we know about
|
2009-12-17 21:30:18 +00:00
|
|
|
nicks map[string]*Nick
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll parse an incoming line into this struct
|
|
|
|
// raw =~ ":nick!user@host cmd args[] :text"
|
|
|
|
// src == "nick!user@host"
|
2009-12-17 17:22:31 +00:00
|
|
|
type Line struct {
|
2009-12-17 21:30:18 +00:00
|
|
|
Nick, Ident, Host, Src string
|
|
|
|
Cmd, Text, Raw string
|
|
|
|
Args []string
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// construct a new IRC Connection object
|
|
|
|
func New(nick, user, name string) *Conn {
|
2009-12-17 21:30:18 +00:00
|
|
|
conn := new(Conn)
|
|
|
|
conn.initialise()
|
|
|
|
conn.Me = conn.NewNick(nick, user, name, "")
|
|
|
|
conn.setupEvents()
|
|
|
|
return conn
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) initialise() {
|
2009-11-29 20:23:15 +00:00
|
|
|
// allocate meh some memoraaaahh
|
2009-12-17 21:30:18 +00:00
|
|
|
fmt.Println("irc.initialise(): initialising...")
|
|
|
|
conn.nicks = make(map[string]*Nick)
|
|
|
|
conn.chans = make(map[string]*Channel)
|
|
|
|
conn.in = make(chan *Line, 32)
|
|
|
|
conn.out = make(chan string, 32)
|
|
|
|
conn.Err = make(chan os.Error, 4)
|
|
|
|
conn.io = nil
|
|
|
|
conn.sock = nil
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// connect the IRC connection object to a host
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) Connect(host, pass string) os.Error {
|
|
|
|
if conn.connected {
|
2009-12-17 21:30:18 +00:00
|
|
|
return os.NewError(fmt.Sprintf("irc.Connect(): already connected to %s, cannot connect to %s", conn.Host, host))
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
if !hasPort(host) {
|
2009-12-17 21:30:18 +00:00
|
|
|
host += ":6667"
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
2009-12-17 17:22:31 +00:00
|
|
|
|
|
|
|
if addr, err := net.ResolveTCPAddr(host); err != nil {
|
|
|
|
return err
|
|
|
|
} else if conn.sock, err = net.DialTCP("tcp", nil, addr); err != nil {
|
2009-11-29 20:23:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
fmt.Println("irc.Connect(): connected happily...")
|
|
|
|
conn.Host = host
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
conn.io = bufio.NewReadWriter(
|
|
|
|
bufio.NewReader(conn.sock),
|
2009-12-17 21:30:18 +00:00
|
|
|
bufio.NewWriter(conn.sock))
|
|
|
|
go conn.send()
|
|
|
|
go conn.recv()
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2009-11-29 20:23:15 +00:00
|
|
|
if pass != "" {
|
|
|
|
conn.Pass(pass)
|
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.Nick(conn.Me.Nick)
|
|
|
|
conn.User(conn.Me.Ident, conn.Me.Name)
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-17 21:30:18 +00:00
|
|
|
go conn.runLoop()
|
|
|
|
fmt.Println("irc.Connect(): launched runLoop() goroutine.")
|
|
|
|
return nil
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// dispatch a nicely formatted os.Error to the error channel
|
2009-12-17 21:30:18 +00:00
|
|
|
func (conn *Conn) error(s string, a ...) { conn.Err <- os.NewError(fmt.Sprintf(s, a)) }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
|
|
|
// copied from http.client for great justice
|
2009-12-17 21:30:18 +00:00
|
|
|
func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// dispatch input from channel as \r\n terminated line to peer
|
|
|
|
func (conn *Conn) send() {
|
|
|
|
for {
|
2009-12-17 21:30:18 +00:00
|
|
|
line := <-conn.out
|
2009-12-17 17:22:31 +00:00
|
|
|
if closed(conn.out) {
|
2009-12-17 21:30:18 +00:00
|
|
|
break
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
if err := conn.io.WriteString(line + "\r\n"); err != nil {
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.error("irc.send(): %s", err.String())
|
|
|
|
conn.shutdown()
|
|
|
|
break
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.io.Flush()
|
|
|
|
fmt.Println("-> " + line)
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// receive one \r\n terminated line from peer, parse and dispatch it
|
|
|
|
func (conn *Conn) recv() {
|
|
|
|
for {
|
2009-12-17 21:30:18 +00:00
|
|
|
s, err := conn.io.ReadString('\n')
|
2009-12-17 17:22:31 +00:00
|
|
|
if err != nil {
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.error("irc.recv(): %s", err.String())
|
|
|
|
conn.shutdown()
|
|
|
|
break
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
// chop off \r\n
|
2009-12-17 21:30:18 +00:00
|
|
|
s = s[0 : len(s)-2]
|
|
|
|
fmt.Println("<- " + s)
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2009-12-17 21:30:18 +00:00
|
|
|
line := &Line{Raw: s}
|
2009-12-17 17:22:31 +00:00
|
|
|
if s[0] == ':' {
|
|
|
|
// remove a source and parse it
|
|
|
|
if idx := strings.Index(s, " "); idx != -1 {
|
2009-12-17 21:30:18 +00:00
|
|
|
line.Src, s = s[1:idx], s[idx+1:len(s)]
|
2009-12-17 17:22:31 +00:00
|
|
|
} else {
|
|
|
|
// pretty sure we shouldn't get here ...
|
2009-12-17 21:30:18 +00:00
|
|
|
line.Src = s[1:len(s)]
|
|
|
|
conn.in <- line
|
|
|
|
continue
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// src can be the hostname of the irc server or a nick!user@host
|
2009-12-17 21:30:18 +00:00
|
|
|
line.Host = line.Src
|
|
|
|
nidx, uidx := strings.Index(line.Src, "!"), strings.Index(line.Src, "@")
|
2009-12-17 17:22:31 +00:00
|
|
|
if uidx != -1 && nidx != -1 {
|
2009-12-17 21:30:18 +00:00
|
|
|
line.Nick = line.Src[0:nidx]
|
|
|
|
line.Ident = line.Src[nidx+1 : uidx]
|
|
|
|
line.Host = line.Src[uidx+1 : len(line.Src)]
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we're here, we've parsed a :nick!user@host or :server off
|
|
|
|
// s should contain "cmd args[] :text"
|
2009-12-17 21:30:18 +00:00
|
|
|
args := strings.Split(s, " :", 2)
|
2009-12-17 17:22:31 +00:00
|
|
|
if len(args) > 1 {
|
2009-12-17 21:30:18 +00:00
|
|
|
line.Text = args[1]
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
args = strings.Split(args[0], " ", 0)
|
|
|
|
line.Cmd = strings.ToUpper(args[0])
|
2009-12-17 17:22:31 +00:00
|
|
|
if len(args) > 1 {
|
2009-12-17 21:30:18 +00:00
|
|
|
line.Args = args[1:len(args)]
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
conn.in <- line
|
|
|
|
}
|
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) runLoop() {
|
|
|
|
for {
|
|
|
|
if closed(conn.in) {
|
2009-12-17 21:30:18 +00:00
|
|
|
break
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
select {
|
2009-12-17 21:30:18 +00:00
|
|
|
case line := <-conn.in:
|
|
|
|
conn.dispatchEvent(line)
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
fmt.Println("irc.runLoop(): Exited runloop...")
|
2009-12-17 17:22:31 +00:00
|
|
|
// if we fall off the end here due to shutdown,
|
|
|
|
// reinit everything once the runloop is done
|
|
|
|
// so that Connect() can be called again.
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.initialise()
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Conn) shutdown() {
|
2009-12-17 21:30:18 +00:00
|
|
|
close(conn.in)
|
|
|
|
close(conn.out)
|
|
|
|
close(conn.Err)
|
|
|
|
conn.connected = false
|
|
|
|
conn.sock.Close()
|
|
|
|
fmt.Println("irc.shutdown(): shut down sockets and channels!")
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) String() string {
|
2009-12-17 21:30:18 +00:00
|
|
|
str := "GoIRC Connection\n"
|
|
|
|
str += "----------------\n\n"
|
2009-12-17 17:22:31 +00:00
|
|
|
if conn.connected {
|
|
|
|
str += "Connected to " + conn.Host + "\n\n"
|
|
|
|
} else {
|
2009-12-17 21:30:18 +00:00
|
|
|
str += "Not currently connected!\n\n"
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
str += conn.Me.String() + "\n"
|
|
|
|
str += "GoIRC Channels\n"
|
|
|
|
str += "--------------\n\n"
|
2009-12-17 17:22:31 +00:00
|
|
|
for _, ch := range conn.chans {
|
|
|
|
str += ch.String() + "\n"
|
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
str += "GoIRC NickNames\n"
|
|
|
|
str += "---------------\n\n"
|
2009-12-17 17:22:31 +00:00
|
|
|
for _, n := range conn.nicks {
|
|
|
|
if n != conn.Me {
|
|
|
|
str += n.String() + "\n"
|
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
return str
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|