jabberd2  2.2.16
util/stanza.c
Go to the documentation of this file.
00001 /*
00002  * jabberd - Jabber Open Source Server
00003  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
00004  *                    Ryan Eatmon, Robert Norris
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
00019  */
00020 
00021 #include "util.h"
00022 
00024 struct _stanza_error_st _stanza_errors[] = {
00025     { "bad-request",                "modify",   "400" },    /* stanza_err_BAD_REQUEST */
00026     { "conflict",                   "cancel",   "409" },    /* stanza_err_CONFLICT */
00027     { "feature-not-implemented",    "cancel",   "501" },    /* stanza_err_FEATURE_NOT_IMPLEMENTED */
00028     { "forbidden",                  "auth",     "403" },    /* stanza_err_FORBIDDEN */
00029     { "gone",                       "modify",   "302" },    /* stanza_err_GONE */
00030     { "internal-server-error",      "wait",     "500" },    /* stanza_err_INTERNAL_SERVER_ERROR */
00031     { "item-not-found",             "cancel",   "404" },    /* stanza_err_ITEM_NOT_FOUND */
00032     { "jid-malformed",              "modify",   "400" },    /* stanza_err_JID_MALFORMED */
00033     { "not-acceptable",             "cancel",   "406" },    /* stanza_err_NOT_ACCEPTABLE */
00034     { "not-allowed",                "cancel",   "405" },    /* stanza_err_NOT_ALLOWED */
00035     { "payment-required",           "auth",     "402" },    /* stanza_err_PAYMENT_REQUIRED */
00036     { "recipient-unavailable",      "wait",     "404" },    /* stanza_err_RECIPIENT_UNAVAILABLE */
00037     { "redirect",                   "modify",   "302" },    /* stanza_err_REDIRECT */
00038     { "registration-required",      "auth",     "407" },    /* stanza_err_REGISTRATION_REQUIRED */
00039     { "remote-server-not-found",    "cancel",   "404" },    /* stanza_err_REMOTE_SERVER_NOT_FOUND */
00040     { "remote-server-timeout",      "wait",     "502" },    /* stanza_err_REMOTE_SERVER_TIMEOUT */
00041     { "resource-constraint",        "wait",     "500" },    /* stanza_err_RESOURCE_CONSTRAINT */
00042     { "service-unavailable",        "cancel",   "503" },    /* stanza_err_SERVICE_UNAVAILABLE */
00043     { "subscription-required",      "auth",     "407" },    /* stanza_err_SUBSCRIPTION_REQUIRED */
00044     { "undefined-condition",        NULL,       "500" },    /* stanza_err_UNDEFINED_CONDITION */
00045     { "unexpected-request",         "wait",     "400" },    /* stanza_err_UNEXPECTED_REQUEST */
00046     { NULL,                         NULL,       "401" },    /* stanza_err_OLD_UNAUTH */
00047     { "unknown-sender",             "modify",   "400" },    /* stanza_err_UNKNOWN_SENDER */
00048     { NULL,                         NULL,       NULL  }
00049 };
00050 
00052 nad_t stanza_error(nad_t nad, int elem, int err) {
00053     int ns;
00054 
00055     assert((int) (nad != NULL));
00056     assert((int) (elem >= 0));
00057     assert((int) (err >= stanza_err_BAD_REQUEST && err < stanza_err_LAST));
00058 
00059     err = err - stanza_err_BAD_REQUEST;
00060 
00061     nad_set_attr(nad, elem, -1, "type", "error", 5);
00062 
00063     elem = nad_insert_elem(nad, elem, 0, "error", NULL);
00064     if(_stanza_errors[err].code != NULL)
00065     nad_set_attr(nad, elem, -1, "code", _stanza_errors[err].code, 0);
00066     if(_stanza_errors[err].type != NULL)
00067         nad_set_attr(nad, elem, -1, "type", _stanza_errors[err].type, 0);
00068 
00069     if(_stanza_errors[err].name != NULL) {
00070         ns = nad_add_namespace(nad, uri_STANZA_ERR, NULL);
00071         nad_insert_elem(nad, elem, ns, _stanza_errors[err].name, NULL);
00072     }
00073 
00074     return nad;
00075 }
00076 
00078 nad_t stanza_tofrom(nad_t nad, int elem) {
00079     int attr;
00080     char to[3072], from[3072];
00081 
00082     assert((int) (nad != NULL));
00083 
00084     to[0] = '\0';
00085     from[0] = '\0';
00086 
00087     attr = nad_find_attr(nad, elem, -1, "to", NULL);
00088     if(attr >= 0)
00089         snprintf(to, 3072, "%.*s", NAD_AVAL_L(nad, attr), NAD_AVAL(nad, attr));
00090     
00091     attr = nad_find_attr(nad, elem, -1, "from", NULL);
00092     if(attr >= 0)
00093         snprintf(from, 3072, "%.*s", NAD_AVAL_L(nad, attr), NAD_AVAL(nad, attr));
00094 
00095     nad_set_attr(nad, elem, -1, "to", from[0] != '\0' ? from : NULL, 0);
00096     nad_set_attr(nad, elem, -1, "from", to[0] != '\0' ? to : NULL, 0);
00097 
00098     return nad;
00099 }