From 05e3500a3c636cb2e0629e9412d766ab58582c17 Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Mon, 30 Aug 2010 12:16:20 +0100 Subject: [PATCH] Enable SSL IRC for goirc. --- client.go | 4 ++-- irc/connection.go | 36 +++++++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/client.go b/client.go index c2dc7e3..289971d 100644 --- a/client.go +++ b/client.go @@ -15,7 +15,7 @@ func main() { func(conn *irc.Conn, line *irc.Line) { conn.Join("#go-nuts") }) // 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) return } @@ -82,7 +82,7 @@ func main() { break } 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) break } diff --git a/irc/connection.go b/irc/connection.go index cd98da4..42d7f70 100644 --- a/irc/connection.go +++ b/irc/connection.go @@ -4,6 +4,7 @@ import ( "bufio" "os" "net" + "crypto/tls" "fmt" "strings" "time" @@ -17,17 +18,20 @@ type Conn struct { Me *Nick // I/O stuff to server - sock *net.TCPConn + sock net.Conn io *bufio.ReadWriter in chan *Line out chan string connected bool + // Are we connecting via SSL? + SSL bool + // Error channel to transmit any fail back to the user Err chan os.Error // Set this to true to disable flood protection and false to re-enable - Flood bool; + Flood bool // Event handler mapping 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 -// a hostname or an IP address, with an optional port defaulting to 6667. -// You can also provide an optional connect password. -func (conn *Conn) Connect(host string, pass ...string) os.Error { +// a hostname or an IP address, with an optional port. To enable explicit SSL +// on the connection to the IRC server, set ssl to true. The port will default +// 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 { return os.NewError(fmt.Sprintf("irc.Connect(): already connected to %s, cannot connect to %s", conn.Host, host)) } if !hasPort(host) { - host += ":6667" + if ssl { + host += ":6697" + } else { + host += ":6667" + } } - if addr, err := net.ResolveTCPAddr(host); err != nil { - return err - } else if conn.sock, err = net.DialTCP("tcp", nil, addr); err != nil { + var sock net.Conn; + var err os.Error; + if ssl { + sock, err = tls.Dial("tcp", "", host) + } else { + sock, err = net.Dial("tcp", "", host) + } + if err != nil { return err } + conn.Host = host + conn.SSL = ssl + conn.sock = sock conn.io = bufio.NewReadWriter( bufio.NewReader(conn.sock),