From 289c9a88b4e0323f4b9d038e78219ab56a948073 Mon Sep 17 00:00:00 2001 From: defanor Date: Fri, 12 Feb 2021 22:10:14 +0300 Subject: Add a few XEP-0060 (pubsub) helper functions --- README | 2 +- src/Makefile.am | 3 +- src/rexmpp_openpgp.c | 49 ++++------------------------- src/rexmpp_pubsub.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/rexmpp_pubsub.h | 36 ++++++++++++++++++++++ 5 files changed, 132 insertions(+), 45 deletions(-) create mode 100644 src/rexmpp_pubsub.c create mode 100644 src/rexmpp_pubsub.h diff --git a/README b/README index 6f317ef..14516d1 100644 --- a/README +++ b/README @@ -89,7 +89,7 @@ A rough roadmap: [+] Display name establishment. [.] A console module. -[ ] Pubsub (XEP-0060) utility functions. +[.] XEP-0060 v1.19: Publish-Subscribe: helper functions. - Examples and application: diff --git a/src/Makefile.am b/src/Makefile.am index 52d6c4a..071c265 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -16,7 +16,8 @@ librexmpp_la_SOURCES = rexmpp_roster.h rexmpp_roster.c \ rexmpp_dns.h rexmpp_dns.c \ rexmpp_jid.h rexmpp_jid.c \ rexmpp_openpgp.h rexmpp_openpgp.c \ - rexmpp_console.h rexmpp_console.c + rexmpp_console.h rexmpp_console.c \ + rexmpp_pubsub.h rexmpp_pubsub.c include_HEADERS = rexmpp_roster.h rexmpp_tcp.h rexmpp_socks.h rexmpp.h \ rexmpp_dns.h rexmpp_jid.h rexmpp_openpgp.h rexmpp_console.h librexmpp_la_CFLAGS = $(AM_CFLAGS) $(LIBXML_CFLAGS) $(GNUTLS_CFLAGS) \ diff --git a/src/rexmpp_openpgp.c b/src/rexmpp_openpgp.c index 92d3f9a..d0fd506 100644 --- a/src/rexmpp_openpgp.c +++ b/src/rexmpp_openpgp.c @@ -49,6 +49,7 @@ Possible future improvements: #include "rexmpp.h" #include "rexmpp_openpgp.h" #include "rexmpp_jid.h" +#include "rexmpp_pubsub.h" void rexmpp_pgp_fp_reply (rexmpp_t *s, @@ -268,21 +269,8 @@ void rexmpp_pgp_key_fp_list_upload (rexmpp_t *s, xmlNodePtr metadata) { xmlNodePtr keylist = xmlNewNode(NULL, "public-keys-list"); xmlNewNs(keylist, "urn:xmpp:openpgp:0", NULL); xmlAddChild(keylist, metadata); - - xmlNodePtr item = xmlNewNode(NULL, "item"); - xmlNewNs(item, "http://jabber.org/protocol/pubsub", NULL); - xmlAddChild(item, keylist); - - xmlNodePtr publish = xmlNewNode(NULL, "publish"); - xmlNewNs(publish, "http://jabber.org/protocol/pubsub", NULL); - xmlNewProp(publish, "node", "urn:xmpp:openpgp:0:public-keys"); - xmlAddChild(publish, item); - - xmlNodePtr pubsub = xmlNewNode(NULL, "pubsub"); - xmlNewNs(pubsub, "http://jabber.org/protocol/pubsub", NULL); - xmlAddChild(pubsub, publish); - - rexmpp_iq_new(s, "set", NULL, pubsub, rexmpp_pgp_key_publish_list_iq); + rexmpp_pubsub_item_publish(s, NULL, "urn:xmpp:openpgp:0:public-keys", + NULL, keylist, rexmpp_pgp_key_publish_list_iq); } void rexmpp_pgp_key_delete_iq (rexmpp_t *s, @@ -344,19 +332,9 @@ void rexmpp_openpgp_retract_key (rexmpp_t *s, const char *fp) { if (new_fp_list != NULL) { rexmpp_pgp_key_fp_list_upload(s, new_fp_list); } - char node_str[72]; snprintf(node_str, 72, "urn:xmpp:openpgp:0:public-keys:%s", fp); - - xmlNodePtr delete = xmlNewNode(NULL, "delete"); - xmlNewNs(delete, "http://jabber.org/protocol/pubsub#owner", NULL); - xmlNewProp(delete, "node", node_str); - - xmlNodePtr pubsub = xmlNewNode(NULL, "pubsub"); - xmlNewNs(pubsub, "http://jabber.org/protocol/pubsub#owner", NULL); - xmlAddChild(pubsub, delete); - - rexmpp_iq_new(s, "set", NULL, pubsub, rexmpp_pgp_key_delete_iq); + rexmpp_pubsub_node_delete(s, NULL, node_str, rexmpp_pgp_key_delete_iq); } rexmpp_err_t rexmpp_openpgp_publish_key (rexmpp_t *s, const char *fp) { @@ -404,25 +382,10 @@ rexmpp_err_t rexmpp_openpgp_publish_key (rexmpp_t *s, const char *fp) { struct tm utc_time; gmtime_r(&t, &utc_time); strftime(time_str, 42, "%FT%TZ", &utc_time); - - xmlNodePtr item = xmlNewNode(NULL, "item"); - xmlNewNs(item, "http://jabber.org/protocol/pubsub", NULL); - xmlNewProp(item, "id", time_str); - xmlAddChild(item, pubkey); - char node_str[72]; snprintf(node_str, 72, "urn:xmpp:openpgp:0:public-keys:%s", fp); - xmlNodePtr publish = xmlNewNode(NULL, "publish"); - xmlNewNs(publish, "http://jabber.org/protocol/pubsub", NULL); - xmlNewProp(publish, "node", node_str); - xmlAddChild(publish, item); - - xmlNodePtr pubsub = xmlNewNode(NULL, "pubsub"); - xmlNewNs(pubsub, "http://jabber.org/protocol/pubsub", NULL); - xmlAddChild(pubsub, publish); - - rexmpp_iq_new(s, "set", NULL, pubsub, rexmpp_pgp_key_publish_iq); - + rexmpp_pubsub_item_publish(s, NULL, node_str, time_str, + pubkey, rexmpp_pgp_key_publish_iq); return REXMPP_SUCCESS; } diff --git a/src/rexmpp_pubsub.c b/src/rexmpp_pubsub.c new file mode 100644 index 0000000..6ed3f75 --- /dev/null +++ b/src/rexmpp_pubsub.c @@ -0,0 +1,87 @@ +/** + @file rexmpp_pubsub.c + @brief XEP-0060 helper functions + @author defanor + @date 2021 + @copyright MIT license. +*/ + +#include "rexmpp.h" + +void +rexmpp_pubsub_iq (rexmpp_t *s, + const char *iq_type, + const char *pubsub_namespace, + const char *service_jid, + xmlNodePtr payload, + rexmpp_iq_callback_t callback) +{ + xmlNodePtr pubsub = xmlNewNode(NULL, "pubsub"); + if (pubsub_namespace == NULL) { + xmlNewNs(pubsub, "http://jabber.org/protocol/pubsub", NULL); + } else { + xmlNewNs(pubsub, pubsub_namespace, NULL); + } + + xmlAddChild(pubsub, payload); + + rexmpp_iq_new(s, iq_type, service_jid, pubsub, callback); +} + +void +rexmpp_pubsub_item_publish (rexmpp_t *s, + const char *service_jid, + const char *node, + const char *item_id, + xmlNodePtr payload, + rexmpp_iq_callback_t callback) +{ + xmlNodePtr item = xmlNewNode(NULL, "item"); + xmlNewNs(item, "http://jabber.org/protocol/pubsub", NULL); + if (item_id != NULL) { + xmlNewProp(item, "id", item_id); + } + xmlAddChild(item, payload); + + xmlNodePtr publish = xmlNewNode(NULL, "publish"); + xmlNewNs(publish, "http://jabber.org/protocol/pubsub", NULL); + xmlNewProp(publish, "node", node); + xmlAddChild(publish, item); + + rexmpp_pubsub_iq(s, "set", NULL, service_jid, publish, callback); +} + +void +rexmpp_pubsub_item_retract (rexmpp_t *s, + const char *service_jid, + const char *node, + const char *item_id, + rexmpp_iq_callback_t callback) +{ + xmlNodePtr item = xmlNewNode(NULL, "item"); + xmlNewNs(item, "http://jabber.org/protocol/pubsub", NULL); + if (item_id != NULL) { + xmlNewProp(item, "id", item_id); + } + + xmlNodePtr retract = xmlNewNode(NULL, "retract"); + xmlNewNs(retract, "http://jabber.org/protocol/pubsub", NULL); + xmlNewProp(retract, "node", node); + xmlAddChild(retract, item); + + rexmpp_pubsub_iq(s, "set", NULL, service_jid, retract, callback); +} + +void +rexmpp_pubsub_node_delete (rexmpp_t *s, + const char *service_jid, + const char *node, + rexmpp_iq_callback_t callback) +{ + xmlNodePtr delete = xmlNewNode(NULL, "delete"); + xmlNewNs(delete, "http://jabber.org/protocol/pubsub#owner", NULL); + xmlNewProp(delete, "node", node); + + rexmpp_pubsub_iq(s, "set", "http://jabber.org/protocol/pubsub#owner", + service_jid, delete, callback); +} diff --git a/src/rexmpp_pubsub.h b/src/rexmpp_pubsub.h new file mode 100644 index 0000000..1c36fc2 --- /dev/null +++ b/src/rexmpp_pubsub.h @@ -0,0 +1,36 @@ +/** + @file rexmpp_pubsub.h + @brief XEP-0060 helper functions + @author defanor + @date 2021 + @copyright MIT license. +*/ + +void +rexmpp_pubsub_iq (rexmpp_t *s, + const char *iq_type, + const char *pubsub_namespace, + const char *service_jid, + xmlNodePtr payload, + rexmpp_iq_callback_t callback); + +void +rexmpp_pubsub_item_publish (rexmpp_t *s, + const char *service_jid, + const char *node, + const char *item_id, + xmlNodePtr payload, + rexmpp_iq_callback_t callback); + +void +rexmpp_pubsub_item_retract (rexmpp_t *s, + const char *service_jid, + const char *node, + const char *item_id, + rexmpp_iq_callback_t callback); + +void +rexmpp_pubsub_node_delete (rexmpp_t *s, + const char *service_jid, + const char *node, + rexmpp_iq_callback_t callback); -- cgit v1.2.3