-
Notifications
You must be signed in to change notification settings - Fork 0
/
private.go
117 lines (104 loc) · 2.97 KB
/
private.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2022 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.
// Package privatexml implements storage of arbitrary data on the server.
//
// New uses of this package should likely use mellium.im/xmpp/pubsub instead
// unless backwards compatibility with other legacy specifications is desired.
package privatexml // import "mellium.im/legacy/privatexml"
import (
"context"
"encoding/xml"
"fmt"
"io"
"mellium.im/xmlstream"
"mellium.im/xmpp"
"mellium.im/xmpp/stanza"
)
// NS is the namespace used by this package.
const NS = `jabber:iq:private`
// Set stores the XML copied from r on the server for later retrieval.
func Set(ctx context.Context, s *xmpp.Session, r xml.TokenReader) error {
return SetIQ(ctx, stanza.IQ{}, s, r)
}
// SetIQ is like Set except that the IQ stanza can be customized.
// Changing the type of the stanza has no effect.
func SetIQ(ctx context.Context, iq stanza.IQ, s *xmpp.Session, r xml.TokenReader) error {
iq.Type = stanza.SetIQ
return s.UnmarshalIQElement(ctx, xmlstream.Wrap(
r,
xml.StartElement{Name: xml.Name{Space: NS, Local: "query"}},
), iq, nil)
}
// Get requests XML that was previously stored on the server.
func Get(ctx context.Context, s *xmpp.Session, name xml.Name) (xmlstream.TokenReadCloser, error) {
return GetIQ(ctx, stanza.IQ{}, s, name)
}
type readCloser struct {
TokenReader xml.TokenReader
Closer io.Closer
closed bool
}
func (r *readCloser) Token() (xml.Token, error) {
tok, err := r.TokenReader.Token()
// Close early if we finish reading the stream.
if err == io.EOF {
e := r.Closer.Close()
if e != nil {
return tok, e
}
}
return tok, err
}
func (r *readCloser) Close() error {
if r.closed {
return nil
}
r.closed = true
return r.Closer.Close()
}
// GetIQ is like Get except that the IQ stanza can be customized.
// Changing the type of the stanza has no effect.
func GetIQ(ctx context.Context, iq stanza.IQ, s *xmpp.Session, name xml.Name) (xmlstream.TokenReadCloser, error) {
iq.Type = stanza.GetIQ
resp, err := s.SendIQElement(ctx, xmlstream.Wrap(
xmlstream.Wrap(
nil,
xml.StartElement{Name: name},
),
xml.StartElement{Name: xml.Name{Space: NS, Local: "query"}},
), iq)
if err != nil {
return nil, err
}
tok, err := resp.Token()
if err != nil {
return nil, err
}
start, ok := tok.(xml.StartElement)
if !ok {
/* #nosec */
resp.Close()
return nil, fmt.Errorf("privatexml: expected IQ start token, got %T %[1]v", tok)
}
_, err = stanza.UnmarshalIQError(resp, start)
if err != nil {
/* #nosec */
resp.Close()
return nil, err
}
tok, err = resp.Token()
if err != nil {
return nil, err
}
start, ok = tok.(xml.StartElement)
if !ok || start.Name.Space != NS || start.Name.Local != "query" {
/* #nosec */
resp.Close()
return nil, fmt.Errorf("privatexml: expected query payload, got %T %[1]v", tok)
}
return &readCloser{
TokenReader: xmlstream.Inner(resp),
Closer: resp,
}, nil
}