Enable SSL IRC for goirc.

This commit is contained in:
Alex Bramley 2010-08-30 12:16:20 +01:00
parent c4d09cd228
commit 05e3500a3c
2 changed files with 29 additions and 11 deletions

View File

@ -15,7 +15,7 @@ func main() {
func(conn *irc.Conn, line *irc.Line) { conn.Join("#go-nuts") }) func(conn *irc.Conn, line *irc.Line) { conn.Join("#go-nuts") })
// connect to server // connect to server
if err := c.Connect("irc.freenode.net", ""); err != nil { if err := c.Connect("irc.freenode.net", false); err != nil {
fmt.Printf("Connection error: %s\n", err) fmt.Printf("Connection error: %s\n", err)
return return
} }
@ -82,7 +82,7 @@ func main() {
break break
} }
fmt.Println("Reconnecting...") fmt.Println("Reconnecting...")
if err := c.Connect("irc.freenode.net", ""); err != nil { if err := c.Connect("irc.freenode.net", false); err != nil {
fmt.Printf("Connection error: %s\n", err) fmt.Printf("Connection error: %s\n", err)
break break
} }

View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"os" "os"
"net" "net"
"crypto/tls"
"fmt" "fmt"
"strings" "strings"
"time" "time"
@ -17,17 +18,20 @@ 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
// Set this to true to disable flood protection and false to re-enable // Set this to true to disable flood protection and false to re-enable
Flood bool; Flood bool
// Event handler mapping // Event handler mapping
events map[string][]func(*Conn, *Line) events map[string][]func(*Conn, *Line)
@ -78,22 +82,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),