minor adjustments for multiple connections

This commit is contained in:
Keno Schwalb 2014-11-03 15:21:48 +00:00
parent 9c4d007b9f
commit 4e1ebe0162
2 changed files with 20 additions and 19 deletions

22
irc.go
View File

@ -75,7 +75,7 @@ func (irc *Connection) readLoop() {
irc.lastMessage = time.Now()
msg = msg[:len(msg)-2] //Remove \r\n
event := &Event{Raw: msg}
event := &Event{Raw: msg, Connection: irc}
if msg[0] == ':' {
if i := strings.Index(msg, " "); i > -1 {
event.Source = msg[1:i]
@ -318,31 +318,31 @@ func (irc *Connection) Disconnect() {
// Reconnect to a server using the current connection.
func (irc *Connection) Reconnect() error {
irc.end = make(chan struct{})
return irc.Connect(irc.server)
return irc.Connect(irc.Server)
}
// Connect to a given server using the current connection configuration.
// This function also takes care of identification if a password is provided.
// RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.1
func (irc *Connection) Connect(server string) error {
irc.server = server
irc.Server = server
irc.stopped = false
// make sure everything is ready for connection
if len(irc.server) == 0 {
if len(irc.Server) == 0 {
return errors.New("empty 'server'")
}
if strings.Count(irc.server, ":") != 1 {
if strings.Count(irc.Server, ":") != 1 {
return errors.New("wrong number of ':' in address")
}
if strings.Index(irc.server, ":") == 0 {
if strings.Index(irc.Server, ":") == 0 {
return errors.New("hostname is missing")
}
if strings.Index(irc.server, ":") == len(irc.server)-1 {
if strings.Index(irc.Server, ":") == len(irc.Server)-1 {
return errors.New("port missing")
}
// check for valid range
ports := strings.Split(irc.server, ":")[1]
ports := strings.Split(irc.Server, ":")[1]
port, err := strconv.Atoi(ports)
if err != nil {
return errors.New("extracting port failed")
@ -362,14 +362,14 @@ func (irc *Connection) Connect(server string) error {
if irc.UseTLS {
dialer := &net.Dialer{Timeout: irc.Timeout}
irc.socket, err = tls.DialWithDialer(dialer, "tcp", irc.server, irc.TLSConfig)
irc.socket, err = tls.DialWithDialer(dialer, "tcp", irc.Server, irc.TLSConfig)
} else {
irc.socket, err = net.DialTimeout("tcp", irc.server, irc.Timeout)
irc.socket, err = net.DialTimeout("tcp", irc.Server, irc.Timeout)
}
if err != nil {
return err
}
irc.Log.Printf("Connected to %s (%s)\n", irc.server, irc.socket.RemoteAddr())
irc.Log.Printf("Connected to %s (%s)\n", irc.Server, irc.socket.RemoteAddr())
irc.pwrite = make(chan string, 10)
irc.Error = make(chan error, 2)

View File

@ -23,6 +23,7 @@ type Connection struct {
Timeout time.Duration
PingFreq time.Duration
KeepAlive time.Duration
Server string
socket net.Conn
pwrite chan string
@ -32,7 +33,6 @@ type Connection struct {
nickcurrent string //The nickname we currently have.
user string
registered bool
server string
events map[string]map[string]func(*Event)
lastMessage time.Time
@ -45,13 +45,14 @@ type Connection struct {
// A struct to represent an event.
type Event struct {
Code string
Raw string
Nick string //<nick>
Host string //<nick>!<usr>@<host>
Source string //<host>
User string //<usr>
Arguments []string
Code string
Raw string
Nick string //<nick>
Host string //<nick>!<usr>@<host>
Source string //<host>
User string //<usr>
Arguments []string
Connection *Connection
}
// Retrieve the last message from Event arguments.