Enable SSL IRC for goirc.

This commit is contained in:
Alex Bramley 2010-08-30 12:16:20 +01:00 committed by raylu
parent 1a5b744150
commit 10de1b570a
1 changed files with 26 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"os" "os"
"net" "net"
"crypto/tls"
"fmt" "fmt"
"strings" "strings"
"time" "time"
@ -17,12 +18,15 @@ type Conn struct {
Me *Nick Me *Nick
// I/O stuff to server // I/O stuff to server
sock *net.TCPConn sock net.Conn
io *bufio.ReadWriter io *bufio.ReadWriter
in chan *Line in chan *Line
out chan string out chan string
connected bool connected bool
// Are we connecting via SSL?
SSL bool
// Error channel to transmit any fail back to the user // Error channel to transmit any fail back to the user
Err chan os.Error Err chan os.Error
@ -80,22 +84,36 @@ func (conn *Conn) initialise() {
} }
// Connect the IRC connection object to "host[:port]" which should be either // Connect the IRC connection object to "host[:port]" which should be either
// a hostname or an IP address, with an optional port defaulting to 6667. // a hostname or an IP address, with an optional port. To enable explicit SSL
// You can also provide an optional connect password. // on the connection to the IRC server, set ssl to true. The port will default
func (conn *Conn) Connect(host string, pass string) os.Error { // to 6697 if ssl is enabled, and 6667 otherwise. You can also provide an
// optional connect password.
func (conn *Conn) Connect(host string, ssl bool, pass string) os.Error {
if conn.connected { if conn.connected {
return os.NewError(fmt.Sprintf("irc.Connect(): already connected to %s, cannot connect to %s", conn.Host, host)) return os.NewError(fmt.Sprintf("irc.Connect(): already connected to %s, cannot connect to %s", conn.Host, host))
} }
if !hasPort(host) { if !hasPort(host) {
host += ":6667" if ssl {
host += ":6697"
} else {
host += ":6667"
}
} }
if addr, err := net.ResolveTCPAddr(host); err != nil { var sock net.Conn;
return err var err os.Error;
} else if conn.sock, err = net.DialTCP("tcp", nil, addr); err != nil { if ssl {
sock, err = tls.Dial("tcp", "", host)
} else {
sock, err = net.Dial("tcp", "", host)
}
if err != nil {
return err return err
} }
conn.Host = host conn.Host = host
conn.SSL = ssl
conn.sock = sock
conn.io = bufio.NewReadWriter( conn.io = bufio.NewReadWriter(
bufio.NewReader(conn.sock), bufio.NewReader(conn.sock),