1// Copyright 2013 The Go Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5// Package xmpp implements the XMPP IM protocol, as specified in RFC 6120 and 6// 6121. 7package xmpp 8 9import ( 10 "encoding/xml" 11 "fmt" 12 13 "github.com/coyim/coyim/xmpp/data" 14) 15 16type rawXML []byte 17 18// SendIQ sends an info/query message to the given user. It returns a channel 19// on which the reply can be read when received and a Cookie that can be used 20// to cancel the request. 21func (c *conn) SendIQ(to, typ string, value interface{}) (reply <-chan data.Stanza, cookie data.Cookie, err error) { 22 nextCookie := c.getCookie() 23 toAttr := "" 24 if len(to) > 0 { 25 toAttr = "to='" + xmlEscape(to) + "'" 26 } 27 28 if _, err = fmt.Fprintf(c.out, "<iq xmlns='jabber:client' %s from='%s' type='%s' id='%x'>", toAttr, xmlEscape(c.jid), xmlEscape(typ), nextCookie); err != nil { 29 return 30 } 31 32 switch v := value.(type) { 33 case data.EmptyReply: 34 //nothing 35 case rawXML: 36 _, err = c.out.Write(v) 37 default: 38 err = xml.NewEncoder(c.out).Encode(value) 39 } 40 41 if err != nil { 42 return 43 } 44 45 if _, err = fmt.Fprintf(c.out, "</iq>"); err != nil { 46 return 47 } 48 49 return c.createInflight(nextCookie, to) 50} 51 52// SendIQReply sends a reply to an IQ query. 53func (c *conn) SendIQReply(to, typ, id string, value interface{}) error { 54 if _, err := fmt.Fprintf(c.out, "<iq to='%s' from='%s' type='%s' id='%s'>", xmlEscape(to), xmlEscape(c.jid), xmlEscape(typ), xmlEscape(id)); err != nil { 55 return err 56 } 57 if _, ok := value.(data.EmptyReply); !ok { 58 if err := xml.NewEncoder(c.out).Encode(value); err != nil { 59 return err 60 } 61 } 62 _, err := fmt.Fprintf(c.out, "</iq>") 63 return err 64} 65