-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapi.go
346 lines (282 loc) · 7.4 KB
/
api.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package ejabberd
import (
"encoding/json"
"fmt"
"net/url"
"strings"
)
// Response is the common interface for all ejabberd API call results.
type Response interface {
JSON() string
}
// request is the common interface to all ejabberd requests. It is
// passed to the ejabberd.Client Call methods to get parameters to
// make the call and parse responses from the server.
type request interface {
params() (apiParams, error)
parseResponse([]byte) (Response, error)
}
// apiParams gathers all values needed by the client to encode actual
// ejabberd API call. An ejabberd API commands should return apiParams
// struct when being issued params call.
type apiParams struct {
name string
version int
admin bool // = Flag to mark if API requires admin header
method string
query url.Values
body []byte
}
//==============================================================================
// TODO: Move into a api_stats file
// Wraps various ejabberd call that all returns stats
// From ejabberd mod_admin_extra
// Stats is the data structure returned by ejabberd Stats API call.
type Stats struct {
Name string `json:"name"`
Value int `json:"stat"`
}
// JSON converts Stats data structure to JSON string.
func (s Stats) JSON() string {
body, _ := json.Marshal(s)
return string(body)
}
// String represents Stats data structure as a human readable value.
func (s Stats) String() string {
return fmt.Sprintf("%d", s.Value)
}
type statsRequest struct {
Name string `json:"name"`
}
func (s statsRequest) params() (apiParams, error) {
switch s.Name {
case "":
return apiParams{}, fmt.Errorf("required argument 'name' not provided")
case "registeredusers", "onlineusers", "onlineusersnode", "uptimeseconds", "processes":
return s.paramsStats()
default:
return apiParams{}, fmt.Errorf("unknow statistic: %s", s.Name)
}
}
func (s statsRequest) paramsStats() (apiParams, error) {
var query url.Values
body, err := json.Marshal(s)
if err != nil {
return apiParams{}, err
}
return apiParams{
name: "stats",
version: 1,
admin: true,
method: "POST",
query: query,
body: body,
}, nil
}
func (s statsRequest) parseResponse(body []byte) (Response, error) {
var resp Stats
err := json.Unmarshal(body, &resp)
if err != nil {
return resp, APIError{Code: 99, Message: err.Error()}
}
resp.Name = s.Name
return resp, err
}
//==============================================================================
// From ejabberd_admin
// Register contains the message return by server after successful
// user registration.
type Register string
// JSON represents Register result as a JSON string. Can be useful for
// further processing.
func (r Register) JSON() string {
body, _ := json.Marshal(r)
return string(body)
}
type registerRequest struct {
JID string `json:"jid"`
Password string `json:"password"`
}
func (r registerRequest) params() (apiParams, error) {
var query url.Values
jid, err := parseJID(r.JID)
if err != nil {
return apiParams{}, err
}
// Actual parameters for ejabberd. We expose JID string as it is
// easier to manipulate from a client.
type register struct {
User string `json:"user"`
Host string `json:"host"`
Password string `json:"password"`
}
data := register{
User: jid.username,
Host: jid.domain,
Password: r.Password,
}
body, err := json.Marshal(data)
if err != nil {
return apiParams{}, err
}
return apiParams{
name: "register",
version: 1,
admin: true,
method: "POST",
query: query,
body: body,
}, nil
}
func (r registerRequest) parseResponse(body []byte) (Response, error) {
var resp Register
err := json.Unmarshal(body, &resp)
if err != nil {
return resp, APIError{Code: 99, Message: err.Error()}
}
return resp, nil
}
//==============================================================================
// OfflineCount contains the result of the call to ejabberd
// get_offline_count API.
type OfflineCount struct {
Name string `json:"name"`
JID string `json:"jid"`
Value int `json:"value"`
}
// JSON represents OfflineCount as a JSON string, for further
// processing with other tools.
func (o OfflineCount) JSON() string {
body, _ := json.Marshal(o)
return string(body)
}
type offlineCountRequest struct {
JID string `json:"jid"`
}
func (o offlineCountRequest) params() (apiParams, error) {
var query url.Values
jid, err := parseJID(o.JID)
if err != nil {
return apiParams{}, err
}
type offlineCount struct {
User string `json:"user"`
Server string `json:"server"`
}
data := offlineCount{
User: jid.username,
Server: jid.domain,
}
body, err := json.Marshal(data)
if err != nil {
return apiParams{}, err
}
if err != nil {
return apiParams{}, err
}
return apiParams{
name: "get_offline_count",
version: 1,
method: "POST",
query: query,
body: body,
}, nil
}
func (o offlineCountRequest) parseResponse(body []byte) (Response, error) {
var resp OfflineCount
err := json.Unmarshal(body, &resp)
if err != nil {
return resp, APIError{Code: 99, Message: err.Error()}
}
resp.Name = "offline_count"
resp.JID = o.JID
return resp, nil
}
func (o OfflineCount) String() string {
return fmt.Sprintf("%d", o.Value)
}
//==============================================================================
// UserResources contains the result of the call to ejabberd
// user_resources API.
type UserResources struct {
JID string `json:"jid"`
Resources []string `json:"resources"`
}
// JSON represents UserResources as a JSON string, for further
// processing with other tools.
func (u UserResources) JSON() string {
body, _ := json.Marshal(u)
return string(body)
}
type userResourcesRequest struct {
JID string `json:"jid"`
}
func (u userResourcesRequest) params() (apiParams, error) {
var query url.Values
jid, err := parseJID(u.JID)
if err != nil {
return apiParams{}, err
}
type userResources struct {
User string `json:"user"`
Server string `json:"server"`
}
data := userResources{
User: jid.username,
Server: jid.domain,
}
body, err := json.Marshal(data)
if err != nil {
return apiParams{}, err
}
if err != nil {
return apiParams{}, err
}
return apiParams{
name: "user_resources",
version: 1,
method: "POST",
query: query,
body: body,
}, nil
}
func (u userResourcesRequest) parseResponse(body []byte) (Response, error) {
var resp UserResources
var data []string
err := json.Unmarshal(body, &data)
if err != nil {
return resp, APIError{Code: 99, Message: err.Error()}
}
resp.JID = u.JID
resp.Resources = data
return resp, nil
}
func (u UserResources) String() string {
resources := strings.Join(u.Resources, ",")
return fmt.Sprintf("%s", resources)
}
//==============================================================================
// APIError represents ejabberd error returned by the server as result
// of ejabberd API calls.
type APIError struct {
Status string `json:"status"`
Code int `json:"code"`
Message string `json:"message"`
}
func parseError(body []byte) (APIError, error) {
var resp APIError
err := json.Unmarshal(body, &resp)
if err != nil {
return resp, APIError{Code: 99, Message: err.Error()}
}
return resp, nil
}
// JSON represents ejabberd error response as a JSON string, for further
// processing with other tools.
func (e APIError) JSON() string {
body, _ := json.Marshal(e)
return string(body)
}
func (e APIError) Error() string {
return fmt.Sprintf("Error %d: %s", e.Code, e.Message)
}