Skip to content

Commit 7c68ace

Browse files
committed
stanza: add xmpp_stanza_reply_error()
This function makes an error reply stanza as described in https://tools.ietf.org/html/rfc6120#section-8.3
1 parent 197896b commit 7c68ace

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- xmpp_stanza_new_from_string()
1818
- xmpp_stanza_add_child_ex()
1919
- xmpp_stanza_get_context()
20+
- xmpp_stanza_reply_error()
2021
- xmpp_global_timed_handler_add()
2122
- xmpp_global_timed_handler_delete()
2223

src/stanza.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,83 @@ xmpp_stanza_t *xmpp_stanza_reply(xmpp_stanza_t *const stanza)
11981198
return NULL;
11991199
}
12001200

1201+
/** Create an error stanza in reply to the provided stanza.
1202+
*
1203+
* Check https://tools.ietf.org/html/rfc6120#section-8.3 for details.
1204+
*
1205+
* @param stanza a Strophe stanza object
1206+
* @param error_type type attribute in the <error/> child element
1207+
* @param condition the defined-condition (e.g. "item-not-found")
1208+
* @param text optional description, may be NULL
1209+
*
1210+
* @return a new Strophe stanza object
1211+
*
1212+
* @ingroup Stanza
1213+
*/
1214+
xmpp_stanza_t *xmpp_stanza_reply_error(xmpp_stanza_t *const stanza,
1215+
const char *const error_type,
1216+
const char *const condition,
1217+
const char *const text)
1218+
{
1219+
xmpp_ctx_t *ctx = stanza->ctx;
1220+
xmpp_stanza_t *reply = NULL;
1221+
xmpp_stanza_t *error;
1222+
xmpp_stanza_t *item;
1223+
xmpp_stanza_t *text_stanza;
1224+
const char *to;
1225+
1226+
if (!error_type || !condition)
1227+
goto quit_err;
1228+
1229+
reply = xmpp_stanza_reply(stanza);
1230+
if (!reply)
1231+
goto quit_err;
1232+
1233+
xmpp_stanza_set_type(reply, "error");
1234+
to = xmpp_stanza_get_to(stanza);
1235+
if (to)
1236+
xmpp_stanza_set_from(reply, to);
1237+
1238+
error = xmpp_stanza_new(ctx);
1239+
if (!error)
1240+
goto quit_err;
1241+
xmpp_stanza_set_name(error, "error");
1242+
xmpp_stanza_set_type(error, error_type);
1243+
xmpp_stanza_add_child(reply, error);
1244+
xmpp_stanza_release(error);
1245+
1246+
item = xmpp_stanza_new(ctx);
1247+
if (!item)
1248+
goto quit_err;
1249+
xmpp_stanza_set_name(item, condition);
1250+
xmpp_stanza_set_ns(item, XMPP_NS_STANZAS_IETF);
1251+
xmpp_stanza_add_child(error, item);
1252+
xmpp_stanza_release(item);
1253+
1254+
if (text) {
1255+
item = xmpp_stanza_new(ctx);
1256+
if (!item)
1257+
goto quit_err;
1258+
xmpp_stanza_set_name(item, "text");
1259+
xmpp_stanza_set_ns(item, XMPP_NS_STANZAS_IETF);
1260+
xmpp_stanza_add_child(error, item);
1261+
xmpp_stanza_release(item);
1262+
text_stanza = xmpp_stanza_new(ctx);
1263+
if (!text_stanza)
1264+
goto quit_err;
1265+
xmpp_stanza_set_text(text_stanza, text);
1266+
xmpp_stanza_add_child(item, text_stanza);
1267+
xmpp_stanza_release(text_stanza);
1268+
}
1269+
1270+
return reply;
1271+
1272+
quit_err:
1273+
if (reply)
1274+
xmpp_stanza_release(reply);
1275+
return NULL;
1276+
}
1277+
12011278
static xmpp_stanza_t *_stanza_new_with_attrs(xmpp_ctx_t *ctx,
12021279
const char *const name,
12031280
const char *const type,

strophe.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ extern "C" {
3939
* Namespace definition for 'urn:ietf:params:xml:ns:xmpp-streams'.
4040
*/
4141
#define XMPP_NS_STREAMS_IETF "urn:ietf:params:xml:ns:xmpp-streams"
42+
/** @def XMPP_NS_STANZAS_IETF
43+
* Namespace definition for 'urn:ietf:params:xml:ns:xmpp-stanzas'.
44+
*/
45+
#define XMPP_NS_STANZAS_IETF "urn:ietf:params:xml:ns:xmpp-stanzas"
4246
/** @def XMPP_NS_TLS
4347
* Namespace definition for 'url:ietf:params:xml:ns:xmpp-tls'.
4448
*/
@@ -402,6 +406,10 @@ int xmpp_stanza_set_from(xmpp_stanza_t *const stanza, const char *const from);
402406

403407
/* allocate and initialize a stanza in reply to another */
404408
xmpp_stanza_t *xmpp_stanza_reply(xmpp_stanza_t *const stanza);
409+
xmpp_stanza_t *xmpp_stanza_reply_error(xmpp_stanza_t *const stanza,
410+
const char *const error_type,
411+
const char *const condition,
412+
const char *const text);
405413

406414
/* stanza subclasses */
407415
xmpp_stanza_t *xmpp_message_new(xmpp_ctx_t *ctx,

0 commit comments

Comments
 (0)