xmpp.go
changeset 98 c9cc4eda6dce
parent 72 53f15893a1a7
child 100 24231ff0016c
--- a/xmpp.go	Mon Jan 23 21:54:41 2012 -0700
+++ b/xmpp.go	Sun Dec 16 13:03:03 2012 -0700
@@ -8,13 +8,13 @@
 
 import (
 	"bytes"
+	"encoding/xml"
+	"errors"
 	"fmt"
 	"io"
+	"log/syslog"
 	"net"
-	"os"
 	"sync"
-	"syslog"
-	"xml"
 )
 
 const (
@@ -22,6 +22,7 @@
 	Version = "1.0"
 
 	// Various XML namespaces.
+	NsClient  = "jabber:client"
 	NsStreams = "urn:ietf:params:xml:ns:xmpp-streams"
 	NsStream  = "http://etherx.jabber.org/streams"
 	NsTLS     = "urn:ietf:params:xml:ns:xmpp-tls"
@@ -106,8 +107,7 @@
 // has completed. The negotiation will occur asynchronously, and any
 // send operation to Client.Out will block until negotiation (resource
 // binding) is complete.
-func NewClient(jid *JID, password string, exts []Extension) (*Client,
-os.Error) {
+func NewClient(jid *JID, password string, exts []Extension) (*Client, error) {
 	// Include the mandatory extensions.
 	exts = append(exts, rosterExt)
 	exts = append(exts, bindExt)
@@ -115,8 +115,8 @@
 	// Resolve the domain in the JID.
 	_, srvs, err := net.LookupSRV(clientSrv, "tcp", jid.Domain)
 	if err != nil {
-		return nil, os.NewError("LookupSrv " + jid.Domain +
-			": " + err.String())
+		return nil, errors.New("LookupSrv " + jid.Domain +
+			": " + err.Error())
 	}
 
 	var tcp *net.TCPConn
@@ -124,14 +124,14 @@
 		addrStr := fmt.Sprintf("%s:%d", srv.Target, srv.Port)
 		addr, err := net.ResolveTCPAddr("tcp", addrStr)
 		if err != nil {
-			err = os.NewError(fmt.Sprintf("ResolveTCPAddr(%s): %s",
-				addrStr, err.String()))
+			err = fmt.Errorf("ResolveTCPAddr(%s): %s",
+				addrStr, err.Error())
 			continue
 		}
 		tcp, err = net.DialTCP("tcp", nil, addr)
 		if err != nil {
-			err = os.NewError(fmt.Sprintf("DialTCP(%s): %s",
-				addr, err.String()))
+			err = fmt.Errorf("DialTCP(%s): %s",
+				addr, err)
 			continue
 		}
 	}
@@ -271,17 +271,25 @@
 // session, retrieve the roster, and broadcast an initial
 // presence. The presence can be as simple as a newly-initialized
 // Presence struct.  See RFC 3921, Section 3.
-func (cl *Client) StartSession(getRoster bool, pr *Presence) os.Error {
+func (cl *Client) StartSession(getRoster bool, pr *Presence) error {
 	id := <-Id
 	iq := &Iq{To: cl.Jid.Domain, Id: id, Type: "set", Nested: []interface{}{Generic{XMLName: xml.Name{Space: NsSession, Local: "session"}}}}
-	ch := make(chan os.Error)
+	ch := make(chan error)
 	f := func(st Stanza) bool {
-		if st.GetType() == "error" {
+		iq, ok := st.(*Iq)
+		if !ok {
+			if Log != nil {
+				Log.Err("iq reply not iq; can't start session")
+			}
+			ch <- errors.New("bad session start reply")
+			return false
+		}
+		if iq.Type == "error" {
 			if Log != nil {
 				Log.Err(fmt.Sprintf("Can't start session: %v",
-					st))
+					iq))
 			}
-			ch <- st.GetError()
+			ch <- iq.Error
 			return false
 		}
 		ch <- nil