jabberd2  2.6.1
stanza.c
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
4  * Ryan Eatmon, Robert Norris
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19  */
20 
21 #include "util.h"
22 
25  { "bad-request", "modify", "400" }, /* stanza_err_BAD_REQUEST */
26  { "conflict", "cancel", "409" }, /* stanza_err_CONFLICT */
27  { "feature-not-implemented", "cancel", "501" }, /* stanza_err_FEATURE_NOT_IMPLEMENTED */
28  { "forbidden", "auth", "403" }, /* stanza_err_FORBIDDEN */
29  { "gone", "modify", "302" }, /* stanza_err_GONE */
30  { "internal-server-error", "wait", "500" }, /* stanza_err_INTERNAL_SERVER_ERROR */
31  { "item-not-found", "cancel", "404" }, /* stanza_err_ITEM_NOT_FOUND */
32  { "jid-malformed", "modify", "400" }, /* stanza_err_JID_MALFORMED */
33  { "not-acceptable", "cancel", "406" }, /* stanza_err_NOT_ACCEPTABLE */
34  { "not-allowed", "cancel", "405" }, /* stanza_err_NOT_ALLOWED */
35  { "payment-required", "auth", "402" }, /* stanza_err_PAYMENT_REQUIRED */
36  { "recipient-unavailable", "wait", "404" }, /* stanza_err_RECIPIENT_UNAVAILABLE */
37  { "redirect", "modify", "302" }, /* stanza_err_REDIRECT */
38  { "registration-required", "auth", "407" }, /* stanza_err_REGISTRATION_REQUIRED */
39  { "remote-server-not-found", "cancel", "404" }, /* stanza_err_REMOTE_SERVER_NOT_FOUND */
40  { "remote-server-timeout", "wait", "502" }, /* stanza_err_REMOTE_SERVER_TIMEOUT */
41  { "resource-constraint", "wait", "500" }, /* stanza_err_RESOURCE_CONSTRAINT */
42  { "service-unavailable", "cancel", "503" }, /* stanza_err_SERVICE_UNAVAILABLE */
43  { "subscription-required", "auth", "407" }, /* stanza_err_SUBSCRIPTION_REQUIRED */
44  { "undefined-condition", NULL, "500" }, /* stanza_err_UNDEFINED_CONDITION */
45  { "unexpected-request", "wait", "400" }, /* stanza_err_UNEXPECTED_REQUEST */
46  { NULL, NULL, "401" }, /* stanza_err_OLD_UNAUTH */
47  { "unknown-sender", "modify", "400" }, /* stanza_err_UNKNOWN_SENDER */
48  { NULL, NULL, NULL }
49 };
50 
52 nad_t stanza_error(nad_t nad, int elem, int err) {
53  int ns;
54 
55  assert((int) (nad != NULL));
56  assert((int) (elem >= 0));
57  assert((int) (err >= stanza_err_BAD_REQUEST && err < stanza_err_LAST));
58 
59  err = err - stanza_err_BAD_REQUEST;
60 
61  nad_set_attr(nad, elem, -1, "type", "error", 5);
62 
63  elem = nad_insert_elem(nad, elem, 0, "error", NULL);
64  if(_stanza_errors[err].code != NULL)
65  nad_set_attr(nad, elem, -1, "code", _stanza_errors[err].code, 0);
66  if(_stanza_errors[err].type != NULL)
67  nad_set_attr(nad, elem, -1, "type", _stanza_errors[err].type, 0);
68 
69  if(_stanza_errors[err].name != NULL) {
70  ns = nad_add_namespace(nad, uri_STANZA_ERR, NULL);
71  nad_insert_elem(nad, elem, ns, _stanza_errors[err].name, NULL);
72  }
73 
74  return nad;
75 }
76 
78 nad_t stanza_tofrom(nad_t nad, int elem) {
79  int attr;
80  char to[3072], from[3072];
81 
82  assert((int) (nad != NULL));
83 
84  to[0] = '\0';
85  from[0] = '\0';
86 
87  attr = nad_find_attr(nad, elem, -1, "to", NULL);
88  if(attr >= 0)
89  snprintf(to, 3072, "%.*s", NAD_AVAL_L(nad, attr), NAD_AVAL(nad, attr));
90 
91  attr = nad_find_attr(nad, elem, -1, "from", NULL);
92  if(attr >= 0)
93  snprintf(from, 3072, "%.*s", NAD_AVAL_L(nad, attr), NAD_AVAL(nad, attr));
94 
95  nad_set_attr(nad, elem, -1, "to", from[0] != '\0' ? from : NULL, 0);
96  nad_set_attr(nad, elem, -1, "from", to[0] != '\0' ? to : NULL, 0);
97 
98  return nad;
99 }
Definition: nad.h:93
struct _stanza_error_st _stanza_errors[]
if you change these, reflect your changes in the defines in util.h
Definition: stanza.c:24
#define uri_STANZA_ERR
Definition: uri.h:51
int nad_find_attr(nad_t nad, unsigned int elem, int ns, const char *name, const char *val)
get a matching attr on this elem, both name and optional val
Definition: nad.c:237
void nad_set_attr(nad_t nad, unsigned int elem, int ns, const char *name, const char *val, int vallen)
create, update, or zap any matching attr on this elem
Definition: nad.c:407
int nad_add_namespace(nad_t nad, const char *uri, const char *prefix)
bring a new namespace into scope
Definition: nad.c:778
const char * code
Definition: util.h:398
int nad_insert_elem(nad_t nad, unsigned int parent, int ns, const char *name, const char *cdata)
shove in a new child elem after the given one
Definition: nad.c:437
const char * name
Definition: util.h:396
#define stanza_err_BAD_REQUEST
Definition: util.h:367
nad_t stanza_tofrom(nad_t nad, int elem)
flip the to and from attributes on this elem
Definition: stanza.c:78
#define NAD_AVAL_L(N, A)
Definition: nad.h:190
nad_t stanza_error(nad_t nad, int elem, int err)
error the packet
Definition: stanza.c:52
#define NAD_AVAL(N, A)
Definition: nad.h:189
#define stanza_err_LAST
Definition: util.h:390
const char * type
Definition: util.h:397