about summary refs log tree commit diff stats
path: root/src/xmpp
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2021-07-01 18:02:03 +0200
committerMichael Vetter <jubalh@iodoru.org>2021-07-01 18:02:03 +0200
commit06482fdaefc2ae8651bcdd719936cb9c7bcfb10f (patch)
tree3b2e577ff352dadaf8827539b64e60b039a560e0 /src/xmpp
parent226cffe75be5d408e1d357337eb4ed921100f3b7 (diff)
downloadprofani-tty-06482fdaefc2ae8651bcdd719936cb9c7bcfb10f.tar.gz
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
Diffstat (limited to 'src/xmpp')
-rw-r--r--src/xmpp/message.c22
1 files changed, 22 insertions, 0 deletions
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;
+}