summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordefanor <defanor@uberspace.net>2020-03-12 12:53:32 +0300
committerdefanor <defanor@uberspace.net>2020-03-12 12:53:32 +0300
commit7b3d46b995bc7cc8ac02533822cadaa3e3b4458b (patch)
tree98c29598a7479eafa597a26ba510524002315374
parentbc8aecf0c58fba2c04df2e04089562f311fea8f1 (diff)
Add feature configuration
Many features may better be handled by an application rather than by a library, yet for less sophisticated clients it is better if a library implements those. A common solution is to implement optional features as loadable modules (e.g., a collection of hooks and some state), which is also a nice way to structure them. But module APIs tend to be restrictive, or ad hoc (aiming to cover just the desired modules), or flexible and reminiscent of "Greenspun's tenth rule", particularly when done in C. The solution chosen here for now is to introduce configurable flags: an application can enable or disable features that way, while the control flow inside the library should be more explicit and not restrictive.
-rw-r--r--src/rexmpp.c17
-rw-r--r--src/rexmpp.h7
2 files changed, 14 insertions, 10 deletions
diff --git a/src/rexmpp.c b/src/rexmpp.c
index 7e48bab..f57c611 100644
--- a/src/rexmpp.c
+++ b/src/rexmpp.c
@@ -79,6 +79,8 @@ rexmpp_err_t rexmpp_init (rexmpp_t *s,
s->manual_direct_tls = 0;
s->socks_host = NULL;
s->server_host = NULL;
+ s->enable_carbons = 1;
+ s->enable_service_discovery = 1;
s->send_buffer = NULL;
s->send_queue = NULL;
s->server_srv = NULL;
@@ -225,9 +227,6 @@ void rexmpp_cleanup (rexmpp_t *s) {
s->server_srv_tls_cur = NULL;
}
s->sm_state = REXMPP_SM_INACTIVE;
- if (s->carbons_state != REXMPP_CARBONS_DISABLED) {
- s->carbons_state = REXMPP_CARBONS_INACTIVE;
- }
}
/* Frees the things that persist through reconnects. */
@@ -1021,7 +1020,7 @@ void rexmpp_discovery_info (rexmpp_t *s, xmlNodePtr req, xmlNodePtr response) {
if (rexmpp_xml_match(child, "http://jabber.org/protocol/disco#info",
"feature")) {
char *var = xmlGetProp(child, "var");
- if (s->carbons_state != REXMPP_CARBONS_DISABLED &&
+ if (s->enable_carbons &&
strcmp(var, "urn:xmpp:carbons:2") == 0) {
xmlNodePtr carbons_enable = xmlNewNode(NULL, "enable");
xmlNewNs(carbons_enable, "urn:xmpp:carbons:2", NULL);
@@ -1039,10 +1038,12 @@ void rexmpp_stream_is_ready(rexmpp_t *s) {
s->stream_state = REXMPP_STREAM_READY;
rexmpp_resend_stanzas(s);
rexmpp_send(s, xmlNewNode(NULL, "presence"));
- xmlNodePtr disco_query = xmlNewNode(NULL, "query");
- xmlNewNs(disco_query, "http://jabber.org/protocol/disco#info", NULL);
- rexmpp_iq_new(s, "get", jid_bare_to_host(s->initial_jid),
- disco_query, rexmpp_discovery_info);
+ if (s->enable_service_discovery) {
+ xmlNodePtr disco_query = xmlNewNode(NULL, "query");
+ xmlNewNs(disco_query, "http://jabber.org/protocol/disco#info", NULL);
+ rexmpp_iq_new(s, "get", jid_bare_to_host(s->initial_jid),
+ disco_query, rexmpp_discovery_info);
+ }
}
/* Resource binding,
diff --git a/src/rexmpp.h b/src/rexmpp.h
index 254bf08..5156142 100644
--- a/src/rexmpp.h
+++ b/src/rexmpp.h
@@ -150,8 +150,7 @@ enum sm_st {
enum carbons_st {
REXMPP_CARBONS_INACTIVE,
REXMPP_CARBONS_NEGOTIATION,
- REXMPP_CARBONS_ACTIVE,
- REXMPP_CARBONS_DISABLED
+ REXMPP_CARBONS_ACTIVE
};
/** Error codes. */
@@ -221,6 +220,10 @@ struct rexmpp
const char *socks_host;
uint16_t socks_port;
+ /* Various knobs (these are used instead of loadable modules). */
+ int enable_carbons;
+ int enable_service_discovery;
+
/* Resource limits. */
uint32_t stanza_queue_size;
uint32_t send_queue_size;