From a50d706bcdf1ae2358a494e6f8ceab93e341bfbe Mon Sep 17 00:00:00 2001
From: Dmitry Podgorny
Date: Tue, 26 Apr 2016 18:18:11 +0000
Subject: [PATCH] stanza: added helpers for particular stanzas
These are wrappers for message, iq and presence stanzas.
---
ChangeLog | 5 ++
src/stanza.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++
strophe.h | 14 +++---
3 files changed, 149 insertions(+), 7 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 660f969..8bae501 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -13,6 +13,11 @@
- xmpp_stanza_set_to()
- xmpp_stanza_set_from()
- xmpp_stanza_reply()
+ - xmpp_message_new()
+ - xmpp_message_get_body()
+ - xmpp_message_set_body()
+ - xmpp_iq_new()
+ - xmpp_presence_new()
- Exposed private API:
- xmpp_jid_new()
- xmpp_jid_bare()
diff --git a/src/stanza.c b/src/stanza.c
index fe8364c..7da7d0e 100644
--- a/src/stanza.c
+++ b/src/stanza.c
@@ -1077,3 +1077,140 @@ copy_error:
if (copy) xmpp_stanza_release(copy);
return NULL;
}
+
+static xmpp_stanza_t *
+_stanza_new_with_attrs(xmpp_ctx_t *ctx, const char * const name,
+ const char * const type, const char * const id,
+ const char * const to)
+{
+ xmpp_stanza_t *stanza = xmpp_stanza_new(ctx);
+ int ret;
+
+ if (stanza) {
+ ret = xmpp_stanza_set_name(stanza, name);
+ if (ret == XMPP_EOK && type)
+ ret = xmpp_stanza_set_type(stanza, type);
+ if (ret == XMPP_EOK && id)
+ ret = xmpp_stanza_set_id(stanza, id);
+ if (ret == XMPP_EOK && to)
+ ret = xmpp_stanza_set_to(stanza, to);
+ if (ret != XMPP_EOK) {
+ xmpp_stanza_release(stanza);
+ stanza = NULL;
+ }
+ }
+ return stanza;
+}
+
+/** Create a stanza object with given attributes.
+ * Attributes are optional and may be NULL.
+ *
+ * @param type attribute 'type'
+ * @param to attribute 'to'
+ * @param id attribute 'id'
+ *
+ * @return a new Strophe stanza object
+ *
+ * @ingroup Stanza
+ */
+xmpp_stanza_t *xmpp_message_new(xmpp_ctx_t *ctx, const char * const type,
+ const char * const to, const char * const id)
+{
+ return _stanza_new_with_attrs(ctx, "message", type, id, to);
+}
+
+/** Get text from child element.
+ * This function returns new allocated string. The caller is responsible
+ * for freeing this string with xmpp_free().
+ *
+ * @param msg well formed stanza
+ *
+ * @return allocated string or NULL on failure (no