Merge pull request #49 from Caerostris/master
minor adjustments for multiple connections
This commit is contained in:
commit
5e6704d895
22
irc.go
22
irc.go
@ -75,7 +75,7 @@ func (irc *Connection) readLoop() {
|
|||||||
|
|
||||||
irc.lastMessage = time.Now()
|
irc.lastMessage = time.Now()
|
||||||
msg = msg[:len(msg)-2] //Remove \r\n
|
msg = msg[:len(msg)-2] //Remove \r\n
|
||||||
event := &Event{Raw: msg}
|
event := &Event{Raw: msg, Connection: irc}
|
||||||
if msg[0] == ':' {
|
if msg[0] == ':' {
|
||||||
if i := strings.Index(msg, " "); i > -1 {
|
if i := strings.Index(msg, " "); i > -1 {
|
||||||
event.Source = msg[1:i]
|
event.Source = msg[1:i]
|
||||||
@ -318,31 +318,31 @@ func (irc *Connection) Disconnect() {
|
|||||||
// Reconnect to a server using the current connection.
|
// Reconnect to a server using the current connection.
|
||||||
func (irc *Connection) Reconnect() error {
|
func (irc *Connection) Reconnect() error {
|
||||||
irc.end = make(chan struct{})
|
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.
|
// Connect to a given server using the current connection configuration.
|
||||||
// This function also takes care of identification if a password is provided.
|
// This function also takes care of identification if a password is provided.
|
||||||
// RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.1
|
// RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.1
|
||||||
func (irc *Connection) Connect(server string) error {
|
func (irc *Connection) Connect(server string) error {
|
||||||
irc.server = server
|
irc.Server = server
|
||||||
irc.stopped = false
|
irc.stopped = false
|
||||||
|
|
||||||
// make sure everything is ready for connection
|
// make sure everything is ready for connection
|
||||||
if len(irc.server) == 0 {
|
if len(irc.Server) == 0 {
|
||||||
return errors.New("empty 'server'")
|
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")
|
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")
|
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")
|
return errors.New("port missing")
|
||||||
}
|
}
|
||||||
// check for valid range
|
// check for valid range
|
||||||
ports := strings.Split(irc.server, ":")[1]
|
ports := strings.Split(irc.Server, ":")[1]
|
||||||
port, err := strconv.Atoi(ports)
|
port, err := strconv.Atoi(ports)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("extracting port failed")
|
return errors.New("extracting port failed")
|
||||||
@ -362,14 +362,14 @@ func (irc *Connection) Connect(server string) error {
|
|||||||
|
|
||||||
if irc.UseTLS {
|
if irc.UseTLS {
|
||||||
dialer := &net.Dialer{Timeout: irc.Timeout}
|
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 {
|
} else {
|
||||||
irc.socket, err = net.DialTimeout("tcp", irc.server, irc.Timeout)
|
irc.socket, err = net.DialTimeout("tcp", irc.Server, irc.Timeout)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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.pwrite = make(chan string, 10)
|
||||||
irc.Error = make(chan error, 2)
|
irc.Error = make(chan error, 2)
|
||||||
|
@ -23,6 +23,7 @@ type Connection struct {
|
|||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
PingFreq time.Duration
|
PingFreq time.Duration
|
||||||
KeepAlive time.Duration
|
KeepAlive time.Duration
|
||||||
|
Server string
|
||||||
|
|
||||||
socket net.Conn
|
socket net.Conn
|
||||||
pwrite chan string
|
pwrite chan string
|
||||||
@ -32,7 +33,6 @@ type Connection struct {
|
|||||||
nickcurrent string //The nickname we currently have.
|
nickcurrent string //The nickname we currently have.
|
||||||
user string
|
user string
|
||||||
registered bool
|
registered bool
|
||||||
server string
|
|
||||||
events map[string]map[string]func(*Event)
|
events map[string]map[string]func(*Event)
|
||||||
|
|
||||||
lastMessage time.Time
|
lastMessage time.Time
|
||||||
@ -45,13 +45,14 @@ type Connection struct {
|
|||||||
|
|
||||||
// A struct to represent an event.
|
// A struct to represent an event.
|
||||||
type Event struct {
|
type Event struct {
|
||||||
Code string
|
Code string
|
||||||
Raw string
|
Raw string
|
||||||
Nick string //<nick>
|
Nick string //<nick>
|
||||||
Host string //<nick>!<usr>@<host>
|
Host string //<nick>!<usr>@<host>
|
||||||
Source string //<host>
|
Source string //<host>
|
||||||
User string //<usr>
|
User string //<usr>
|
||||||
Arguments []string
|
Arguments []string
|
||||||
|
Connection *Connection
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve the last message from Event arguments.
|
// Retrieve the last message from Event arguments.
|
||||||
|
Loading…
Reference in New Issue
Block a user