xmpp.go
changeset 102 872e936f9f3f
parent 100 24231ff0016c
child 105 aa895dfae3f6
equal deleted inserted replaced
101:5d721a565503 102:872e936f9f3f
    10 	"bytes"
    10 	"bytes"
    11 	"encoding/xml"
    11 	"encoding/xml"
    12 	"errors"
    12 	"errors"
    13 	"fmt"
    13 	"fmt"
    14 	"io"
    14 	"io"
    15 	"log"
       
    16 	"net"
    15 	"net"
    17 	"sync"
    16 	"sync"
    18 )
    17 )
    19 
    18 
    20 const (
    19 const (
    32 	NsRoster  = "jabber:iq:roster"
    31 	NsRoster  = "jabber:iq:roster"
    33 
    32 
    34 	// DNS SRV names
    33 	// DNS SRV names
    35 	serverSrv = "xmpp-server"
    34 	serverSrv = "xmpp-server"
    36 	clientSrv = "xmpp-client"
    35 	clientSrv = "xmpp-client"
    37 )
       
    38 
       
    39 var (
       
    40 	// If any of these are non-nil when NewClient() is called,
       
    41 	// they will be used to log messages of the indicated
       
    42 	// severity.
       
    43 	Warn *log.Logger
       
    44 	Info *log.Logger
       
    45 	Debug *log.Logger
       
    46 )
    36 )
    47 
    37 
    48 // This channel may be used as a convenient way to generate a unique
    38 // This channel may be used as a convenient way to generate a unique
    49 // id for an iq, message, or presence stanza.
    39 // id for an iq, message, or presence stanza.
    50 var Id <-chan string
    40 var Id <-chan string
   248 		if n == 0 {
   238 		if n == 0 {
   249 			break
   239 			break
   250 		}
   240 		}
   251 		buf.Write(c[:n])
   241 		buf.Write(c[:n])
   252 		if c[0] == '\n' || c[0] == '>' {
   242 		if c[0] == '\n' || c[0] == '>' {
   253 			Debug.Print(buf)
   243 			Debug.Log(buf)
   254 			buf.Reset()
   244 			buf.Reset()
   255 		}
   245 		}
   256 	}
   246 	}
   257 	leftover := buf.String()
   247 	leftover := buf.String()
   258 	if leftover != "" {
   248 	if leftover != "" {
   259 		Debug.Print(buf)
   249 		Debug.Log(buf)
   260 	}
   250 	}
   261 }
   251 }
   262 
   252 
   263 // bindDone is called when we've finished resource binding (and all
   253 // bindDone is called when we've finished resource binding (and all
   264 // the negotiations that precede it). Now we can start accepting
   254 // the negotiations that precede it). Now we can start accepting
   277 	iq := &Iq{To: cl.Jid.Domain, Id: id, Type: "set", Nested: []interface{}{Generic{XMLName: xml.Name{Space: NsSession, Local: "session"}}}}
   267 	iq := &Iq{To: cl.Jid.Domain, Id: id, Type: "set", Nested: []interface{}{Generic{XMLName: xml.Name{Space: NsSession, Local: "session"}}}}
   278 	ch := make(chan error)
   268 	ch := make(chan error)
   279 	f := func(st Stanza) bool {
   269 	f := func(st Stanza) bool {
   280 		iq, ok := st.(*Iq)
   270 		iq, ok := st.(*Iq)
   281 		if !ok {
   271 		if !ok {
   282 			Warnf("iq reply not iq; can't start session")
   272 			Warn.Logf("iq reply not iq; can't start session")
   283 			ch <- errors.New("bad session start reply")
   273 			ch <- errors.New("bad session start reply")
   284 			return false
   274 			return false
   285 		}
   275 		}
   286 		if iq.Type == "error" {
   276 		if iq.Type == "error" {
   287 			Warnf("Can't start session: %v", iq)
   277 			Warn.Logf("Can't start session: %v", iq)
   288 			ch <- iq.Error
   278 			ch <- iq.Error
   289 			return false
   279 			return false
   290 		}
   280 		}
   291 		ch <- nil
   281 		ch <- nil
   292 		return false
   282 		return false
   317 // channel closes, the filter should close its output channel.
   307 // channel closes, the filter should close its output channel.
   318 func (cl *Client) AddFilter(out <-chan Stanza) <-chan Stanza {
   308 func (cl *Client) AddFilter(out <-chan Stanza) <-chan Stanza {
   319 	cl.filterOut <- out
   309 	cl.filterOut <- out
   320 	return <-cl.filterIn
   310 	return <-cl.filterIn
   321 }
   311 }
   322 
       
   323 func Warnf(msg string, args ...interface{}) {
       
   324 	if Warn != nil {
       
   325 		Warn.Printf(msg, args)
       
   326 	}
       
   327 }
       
   328 
       
   329 func Infof(msg string, args ...interface{}) {
       
   330 	if Info != nil {
       
   331 		Info.Printf(msg, args)
       
   332 	}
       
   333 }
       
   334 
       
   335 func Debugf(msg string, args ...interface{}) {
       
   336 	if Debug != nil {
       
   337 		Debug.Printf(msg, args)
       
   338 	}
       
   339 }