2
* jabberd - Jabber Open Source Server
3
* Copyright (c) 2007 Tomasz Sterna
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; version 2 of the License
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
20
* this sx plugin implements stanza acknowledgements
21
* as described in XEP-0198: Stanza Acknowledgements
26
#define STREAM_ACK_NS_DECL " xmlns:ack='" uri_ACK "'"
28
static void _sx_ack_header(sx_t s, sx_plugin_t p, sx_buf_t buf) {
30
log_debug(ZONE, "hacking ack namespace decl onto stream header");
32
/* get enough space */
33
_sx_buffer_alloc_margin(buf, 0, strlen(STREAM_ACK_NS_DECL) + 2);
35
/* overwrite the trailing ">" with a decl followed by a new ">" */
36
memcpy(&buf->data[buf->len - 1], STREAM_ACK_NS_DECL ">", strlen(STREAM_ACK_NS_DECL)+1);
37
buf->len += strlen(STREAM_ACK_NS_DECL);
40
/** sx features callback */
41
static void _sx_ack_features(sx_t s, sx_plugin_t p, nad_t nad) {
42
/* offer feature only when authenticated and not enabled yet */
43
if(s->state == state_OPEN && s->plugin_data[p->index] == NULL)
44
nad_append_elem(nad, -1, "ack:ack", 1);
47
/** process handshake packets from the client */
48
static int _sx_ack_process(sx_t s, sx_plugin_t p, nad_t nad) {
51
/* not interested if we're not a server */
52
if(s->type != type_SERVER)
55
/* only want ack packets */
56
if((NAD_ENS(nad, 0) < 0 || NAD_NURI_L(nad, NAD_ENS(nad, 0)) != strlen(uri_ACK) || strncmp(NAD_NURI(nad, NAD_ENS(nad, 0)), uri_ACK, strlen(uri_COMPRESS)) != 0))
60
if(NAD_ENAME_L(nad, 0) == 4 && strncmp(NAD_ENAME(nad, 0), "ping", 4) == 0) {
61
jqueue_push(s->wbufq, _sx_buffer_new("<ack:pong/>", 11, NULL, NULL), 0);
64
/* handled the packet */
69
/* enable only when authenticated */
70
if(s->state == state_OPEN && NAD_ENAME_L(nad, 0) == 6 && strncmp(NAD_ENAME(nad, 0), "enable", 6) == 0) {
71
jqueue_push(s->wbufq, _sx_buffer_new("<ack:enabled/>", 14, NULL, NULL), 254);
74
s->plugin_data[p->index] = (void *) 1;
76
/* handled the packet */
81
/* 'r' or 'a' when enabled */
82
if(s->plugin_data[p->index] != NULL && NAD_ENAME_L(nad, 0) == 1 && (strncmp(NAD_ENAME(nad, 0), "r", 1) == 0 || strncmp(NAD_ENAME(nad, 0), "a", 1) == 0) ) {
83
attr = nad_find_attr(nad, 0, -1, "c", NULL);
85
char *buf = (char *) malloc(sizeof(char) * (NAD_AVAL_L(nad, attr) + 13 + 1));
86
snprintf(buf, NAD_AVAL_L(nad, attr) + 13 + 1, "<ack:a b='%.*s'/>", NAD_AVAL_L(nad, attr), NAD_AVAL(nad, attr));
87
jqueue_push(s->wbufq, _sx_buffer_new(buf, NAD_AVAL_L(nad, attr) + 13, NULL, NULL), 255);
92
/* handled the packet */
97
_sx_debug(ZONE, "unhandled ack namespace element '%.*s', dropping packet", NAD_ENAME_L(nad, 0), NAD_ENAME(nad, 0));
103
int sx_ack_init(sx_env_t env, sx_plugin_t p, va_list args) {
104
log_debug(ZONE, "initialising stanza acknowledgements sx plugin");
106
p->header = _sx_ack_header;
107
p->features = _sx_ack_features;
108
p->process = _sx_ack_process;