xmpp.go
changeset 100 24231ff0016c
parent 98 c9cc4eda6dce
child 102 872e936f9f3f
equal deleted inserted replaced
98:c9cc4eda6dce 100:24231ff0016c
    10 	"bytes"
    10 	"bytes"
    11 	"encoding/xml"
    11 	"encoding/xml"
    12 	"errors"
    12 	"errors"
    13 	"fmt"
    13 	"fmt"
    14 	"io"
    14 	"io"
    15 	"log/syslog"
    15 	"log"
    16 	"net"
    16 	"net"
    17 	"sync"
    17 	"sync"
    18 )
    18 )
    19 
    19 
    20 const (
    20 const (
    35 	serverSrv = "xmpp-server"
    35 	serverSrv = "xmpp-server"
    36 	clientSrv = "xmpp-client"
    36 	clientSrv = "xmpp-client"
    37 )
    37 )
    38 
    38 
    39 var (
    39 var (
    40 	// If non-nil when NewClient() is called, log messages will be
    40 	// If any of these are non-nil when NewClient() is called,
    41 	// sent to this writer.
    41 	// they will be used to log messages of the indicated
    42 	Log *syslog.Writer
    42 	// severity.
    43 	// Threshold for which messages are logged.
    43 	Warn *log.Logger
    44 	Loglevel syslog.Priority = syslog.LOG_NOTICE
    44 	Info *log.Logger
       
    45 	Debug *log.Logger
    45 )
    46 )
    46 
    47 
    47 // This channel may be used as a convenient way to generate a unique
    48 // This channel may be used as a convenient way to generate a unique
    48 // id for an iq, message, or presence stanza.
    49 // id for an iq, message, or presence stanza.
    49 var Id <-chan string
    50 var Id <-chan string
   247 		if n == 0 {
   248 		if n == 0 {
   248 			break
   249 			break
   249 		}
   250 		}
   250 		buf.Write(c[:n])
   251 		buf.Write(c[:n])
   251 		if c[0] == '\n' || c[0] == '>' {
   252 		if c[0] == '\n' || c[0] == '>' {
   252 			Log.Debug(buf.String())
   253 			Debug.Print(buf)
   253 			buf.Reset()
   254 			buf.Reset()
   254 		}
   255 		}
   255 	}
   256 	}
   256 	leftover := buf.String()
   257 	leftover := buf.String()
   257 	if leftover != "" {
   258 	if leftover != "" {
   258 		Log.Debug(buf.String())
   259 		Debug.Print(buf)
   259 	}
   260 	}
   260 }
   261 }
   261 
   262 
   262 // bindDone is called when we've finished resource binding (and all
   263 // bindDone is called when we've finished resource binding (and all
   263 // the negotiations that precede it). Now we can start accepting
   264 // the negotiations that precede it). Now we can start accepting
   276 	iq := &Iq{To: cl.Jid.Domain, Id: id, Type: "set", Nested: []interface{}{Generic{XMLName: xml.Name{Space: NsSession, Local: "session"}}}}
   277 	iq := &Iq{To: cl.Jid.Domain, Id: id, Type: "set", Nested: []interface{}{Generic{XMLName: xml.Name{Space: NsSession, Local: "session"}}}}
   277 	ch := make(chan error)
   278 	ch := make(chan error)
   278 	f := func(st Stanza) bool {
   279 	f := func(st Stanza) bool {
   279 		iq, ok := st.(*Iq)
   280 		iq, ok := st.(*Iq)
   280 		if !ok {
   281 		if !ok {
   281 			if Log != nil {
   282 			Warnf("iq reply not iq; can't start session")
   282 				Log.Err("iq reply not iq; can't start session")
       
   283 			}
       
   284 			ch <- errors.New("bad session start reply")
   283 			ch <- errors.New("bad session start reply")
   285 			return false
   284 			return false
   286 		}
   285 		}
   287 		if iq.Type == "error" {
   286 		if iq.Type == "error" {
   288 			if Log != nil {
   287 			Warnf("Can't start session: %v", iq)
   289 				Log.Err(fmt.Sprintf("Can't start session: %v",
       
   290 					iq))
       
   291 			}
       
   292 			ch <- iq.Error
   288 			ch <- iq.Error
   293 			return false
   289 			return false
   294 		}
   290 		}
   295 		ch <- nil
   291 		ch <- nil
   296 		return false
   292 		return false
   321 // channel closes, the filter should close its output channel.
   317 // channel closes, the filter should close its output channel.
   322 func (cl *Client) AddFilter(out <-chan Stanza) <-chan Stanza {
   318 func (cl *Client) AddFilter(out <-chan Stanza) <-chan Stanza {
   323 	cl.filterOut <- out
   319 	cl.filterOut <- out
   324 	return <-cl.filterIn
   320 	return <-cl.filterIn
   325 }
   321 }
       
   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 }