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	"strconv"
13
14	"github.com/coyim/coyim/xmpp/data"
15)
16
17func (c *conn) sendPresence(presence *data.ClientPresence) error {
18	if len(presence.ID) == 0 {
19		presence.ID = strconv.FormatUint(uint64(c.getCookie()), 10)
20	}
21
22	//TODO: do we have a method for this?
23	return xml.NewEncoder(c.out).Encode(presence)
24}
25
26// SendPresence sends a presence stanza. If id is empty, a unique id is
27// generated.
28func (c *conn) SendPresence(to, typ, id, status string) error {
29	p := &data.ClientPresence{
30		ID:   id,
31		To:   to,
32		Type: typ,
33	}
34
35	if typ == "subscribe" && status != "" {
36		p.Status = status
37	}
38
39	return c.sendPresence(p)
40}
41
42// SignalPresence will signal the current presence
43func (c *conn) SignalPresence(state string) error {
44	//We dont use c.sendPresence() because this presence does not have `id` (why?)
45	_, err := fmt.Fprintf(c.out, "<presence><show>%s</show></presence>", xmlEscape(state))
46	return err
47}
48