From 06482fdaefc2ae8651bcdd719936cb9c7bcfb10f Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Thu, 1 Jul 2021 18:02:03 +0200 Subject: Add option to only allow messages from jids in roster `/silence on` will throw away all messages (type: chat, normal) that come from jids that are not in the roster. Implement https://github.com/profanity-im/profanity/issues/955 --- src/xmpp/message.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/xmpp') diff --git a/src/xmpp/message.c b/src/xmpp/message.c index e14ae07d..a8613ef9 100644 --- a/src/xmpp/message.c +++ b/src/xmpp/message.c @@ -94,6 +94,7 @@ static gboolean _handle_mam(xmpp_stanza_t* const stanza); static void _handle_pubsub(xmpp_stanza_t* const stanza, xmpp_stanza_t* const event); static gboolean _handle_form(xmpp_stanza_t* const stanza); static gboolean _handle_jingle_message(xmpp_stanza_t* const stanza); +static gboolean _should_ignore_based_on_silence(xmpp_stanza_t* const stanza); #ifdef HAVE_LIBGPGME static xmpp_stanza_t* _openpgp_signcrypt(xmpp_ctx_t* ctx, const char* const to, const char* const text); @@ -171,6 +172,11 @@ _message_handler(xmpp_conn_t* const conn, xmpp_stanza_t* const stanza, void* con } else if (type == NULL || g_strcmp0(type, STANZA_TYPE_CHAT) == 0 || g_strcmp0(type, STANZA_TYPE_NORMAL) == 0) { // type: chat, normal (==NULL) + // ignore all messages from JIDs that are not in roster, if 'silence' is set + if (_should_ignore_based_on_silence(stanza)) { + return 1; + } + // XEP-0353: Jingle Message Initiation if (_handle_jingle_message(stanza)) { return 1; @@ -1687,3 +1693,19 @@ _handle_jingle_message(xmpp_stanza_t* const stanza) } return FALSE; } + +static gboolean +_should_ignore_based_on_silence(xmpp_stanza_t* const stanza) +{ + if (prefs_get_boolean(PREF_SILENCE_NON_ROSTER)) { + const char* const from = xmpp_stanza_get_from(stanza); + Jid* from_jid = jid_create(from); + PContact contact = roster_get_contact(from_jid->barejid); + jid_destroy(from_jid); + if (!contact) { + log_debug("[Silence] Ignoring message from: %s", from); + return TRUE; + } + } + return FALSE; +} -- cgit 1.4.1-2-gfad0