@@ -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+
12011278static xmpp_stanza_t * _stanza_new_with_attrs (xmpp_ctx_t * ctx ,
12021279 const char * const name ,
12031280 const char * const type ,
0 commit comments