Add ignore_auth module
authorfranky
Sat, 06 Nov 2010 20:50:52 +0100
changeset 38 3dad6bd6cde2
parent 37 a05815df848c
child 39 aa40c940b180
Add ignore_auth module
Makefile.am
configure.ac
ignore_auth/Makefile.am
ignore_auth/ignore_auth.c
ignore_auth/ignore_auth.rc
--- a/Makefile.am	Sun Apr 11 15:27:59 2010 +0200
+++ b/Makefile.am	Sat Nov 06 20:50:52 2010 +0100
@@ -1,1 +1,1 @@
-SUBDIRS = clock comment extsay-ng info_msgcount killpresence lastmsg
+SUBDIRS = clock comment extsay-ng ignore_auth info_msgcount killpresence lastmsg
--- a/configure.ac	Sun Apr 11 15:27:59 2010 +0200
+++ b/configure.ac	Sat Nov 06 20:50:52 2010 +0100
@@ -109,6 +109,10 @@
               AC_HELP_STRING([--enable-module-extsayng],
                              [enable module extsayng]),
               enable_module_extsayng=$enableval)
+AC_ARG_ENABLE(module-ignore_auth,
+              AC_HELP_STRING([--enable-module-ignore_auth],
+                             [enable module ignore_auth]),
+              enable_module_ignore_auth=$enableval)
 AC_ARG_ENABLE(module-info_msgcount,
               AC_HELP_STRING([--enable-module-info_msgcount],
                              [enable module info_msgcount]),
@@ -134,6 +138,10 @@
                [test x"${enable_all_modules}" = x"yes" -o \
                      x"${enable_module_extsayng}" = x"yes"])
 
+AM_CONDITIONAL([INSTALL_MODULE_IGNORE_AUTH],
+               [test x"${enable_all_modules}" = x"yes" -o \
+                     x"${enable_module_ignore_auth}" = x"yes"])
+
 AM_CONDITIONAL([INSTALL_MODULE_INFO_MSGCOUNT],
                [test x"${enable_all_modules}" = x"yes" -o \
                      x"${enable_module_info_msgcount}" = x"yes"])
@@ -149,6 +157,7 @@
 AC_CONFIG_FILES([clock/Makefile
                  comment/Makefile
                  extsay-ng/Makefile
+                 ignore_auth/Makefile
                  info_msgcount/Makefile
                  killpresence/Makefile
                  lastmsg/Makefile
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ignore_auth/Makefile.am	Sat Nov 06 20:50:52 2010 +0100
@@ -0,0 +1,11 @@
+
+if INSTALL_MODULE_IGNORE_AUTH
+
+pkglib_LTLIBRARIES = libignore_auth.la
+libignore_auth_la_SOURCES = ignore_auth.c
+libignore_auth_la_LDFLAGS = -module -avoid-version -shared
+
+LDADD = $(GLIB_LIBS) $(MCABBER_LIBS)
+AM_CPPFLAGS = -I$(top_srcdir) $(GLIB_CFLAGS) $(MCABBER_CFLAGS)
+
+endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ignore_auth/ignore_auth.c	Sat Nov 06 20:50:52 2010 +0100
@@ -0,0 +1,136 @@
+/*
+ *  Module "ignore_auth" -- ignore subscription requests
+ *
+ * Copyright 2010 Frank Zschockelt <mcabber@freakysoft.de>
+ *
+ * This module is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+#include <mcabber/commands.h>
+#include <mcabber/hooks.h>
+#include <mcabber/modules.h>
+#include <mcabber/screen.h>
+#include <mcabber/settings.h>
+#include <mcabber/xmpp.h>
+
+static void ignore_auth_init   (void);
+static void ignore_auth_uninit (void);
+
+/* Module description */
+module_info_t info_ignore_auth = {
+        .branch          = MCABBER_BRANCH,
+        .api             = MCABBER_API_VERSION,
+        .version         = MCABBER_VERSION,
+        .description     = "ignore auth requests by specifying a jid regex",
+        .requires        = NULL,
+        .init            = ignore_auth_init,
+        .uninit          = ignore_auth_uninit,
+        .next            = NULL,
+};
+
+GSList *regexlist = NULL;
+static guint ignore_auth_hid = 0;  /* Hook handler id */
+
+static guint ignore_hh(const gchar *hookname, hk_arg_t *args, gpointer userdata)
+{
+  guint subscription;
+  const char *bjid = NULL, *type = NULL, *msg = NULL;
+
+  if (settings_opt_get_int("ignore_auth")) {
+    int i;
+    for (i = 0; args[i].name; i++) {
+      if (!strcmp(args[i].name, "type"))
+        type = args[i].value;
+      if (!strcmp(args[i].name, "message"))
+        msg = args[i].value;
+      if (!strcmp(args[i].name, "jid"))
+        bjid = args[i].value;
+    }
+    /* shouldn't happen */
+    if (!bjid || !type || !msg)
+      return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+
+    subscription = roster_getsubscription(bjid);
+    /* only ignore subscription requests when no subscription exists */
+    if ((subscription == sub_none) && (!strcmp(type, "subscribe"))) {
+      GSList *head;
+      int ignore_it = FALSE;
+
+      /* try to match against one of our regular expressions */
+      for (head = regexlist; head && !ignore_it; head = g_slist_next(head)) {
+        GMatchInfo *match_info;
+        g_regex_match(head->data, bjid, 0, &match_info);
+        if (g_match_info_matches(match_info))
+          ignore_it = TRUE;
+        g_match_info_free (match_info);
+      }
+      if(ignore_it) {
+        xmpp_send_s10n(bjid, LM_MESSAGE_SUB_TYPE_UNSUBSCRIBED);
+        scr_log_print(LPRINT_NORMAL, "Ignored auth request from %s (%s)",
+                      bjid, msg);
+        return HOOK_HANDLER_RESULT_NO_MORE_HANDLER_DROP_DATA;
+      }
+    }
+  }
+  /* we're done, let the other handlers do their job! */
+  return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+}
+
+/* ignore command handler */
+static void do_ignore_auth(char *args)
+{
+  GRegex *new_regex;
+  if (!args || !*args) {
+    GSList *head;
+    for (head = regexlist; head; head = g_slist_next(head))
+      scr_log_print(LPRINT_NORMAL, "Ignoring %s",
+                    g_regex_get_pattern(head->data));
+    return;
+  }
+  new_regex = g_regex_new(args, G_REGEX_OPTIMIZE, 0, NULL);
+  if (!new_regex) {
+    scr_log_print(LPRINT_NORMAL, "That wasn't a glib regex");
+    return;
+  }
+  regexlist = g_slist_append(regexlist, new_regex);
+}
+
+/* Initialization */
+static void ignore_auth_init(void)
+{
+  /* Add command */
+  cmd_add("ignore_auth", "", 0, 0, do_ignore_auth, NULL);
+  /* Add handler */
+  ignore_auth_hid = hk_add_handler(ignore_hh, HOOK_SUBSCRIPTION,
+                                   G_PRIORITY_DEFAULT_IDLE, NULL);
+  settings_set(SETTINGS_TYPE_OPTION, "ignore_auth", "1");
+}
+
+/* Uninitialization */
+static void ignore_auth_uninit(void)
+{
+  GSList *head;
+  /* Unregister command */
+  cmd_del("ignore_auth");
+  /* Unregister event handler */
+  hk_del_handler(HOOK_SUBSCRIPTION, ignore_auth_hid);
+  /* unref every regex */
+  for (head = regexlist; head; head = g_slist_next(head))
+    g_regex_unref(head->data);
+  g_slist_free(regexlist);
+  regexlist = NULL;
+}
+
+/* vim: set et cindent cinoptions=>2\:2(0 ts=2 sw=2:  For Vim users... */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ignore_auth/ignore_auth.rc	Sat Nov 06 20:50:52 2010 +0100
@@ -0,0 +1,7 @@
+load ignore_auth
+
+ignore_auth *@icq*
+ignore_auth spammer@jabber.org
+
+set ignore_auth = 1
+