ignore_auth/ignore_auth.c
changeset 38 3dad6bd6cde2
child 40 5cd4b42ada91
equal deleted inserted replaced
37:a05815df848c 38:3dad6bd6cde2
       
     1 /*
       
     2  *  Module "ignore_auth" -- ignore subscription requests
       
     3  *
       
     4  * Copyright 2010 Frank Zschockelt <mcabber@freakysoft.de>
       
     5  *
       
     6  * This module is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License as published by
       
     8  * the Free Software Foundation; either version 2 of the License, or (at
       
     9  * your option) any later version.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful, but
       
    12  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14  * General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU General Public License
       
    17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18  */
       
    19 #include <string.h>
       
    20 
       
    21 #include <mcabber/commands.h>
       
    22 #include <mcabber/hooks.h>
       
    23 #include <mcabber/modules.h>
       
    24 #include <mcabber/screen.h>
       
    25 #include <mcabber/settings.h>
       
    26 #include <mcabber/xmpp.h>
       
    27 
       
    28 static void ignore_auth_init   (void);
       
    29 static void ignore_auth_uninit (void);
       
    30 
       
    31 /* Module description */
       
    32 module_info_t info_ignore_auth = {
       
    33         .branch          = MCABBER_BRANCH,
       
    34         .api             = MCABBER_API_VERSION,
       
    35         .version         = MCABBER_VERSION,
       
    36         .description     = "ignore auth requests by specifying a jid regex",
       
    37         .requires        = NULL,
       
    38         .init            = ignore_auth_init,
       
    39         .uninit          = ignore_auth_uninit,
       
    40         .next            = NULL,
       
    41 };
       
    42 
       
    43 GSList *regexlist = NULL;
       
    44 static guint ignore_auth_hid = 0;  /* Hook handler id */
       
    45 
       
    46 static guint ignore_hh(const gchar *hookname, hk_arg_t *args, gpointer userdata)
       
    47 {
       
    48   guint subscription;
       
    49   const char *bjid = NULL, *type = NULL, *msg = NULL;
       
    50 
       
    51   if (settings_opt_get_int("ignore_auth")) {
       
    52     int i;
       
    53     for (i = 0; args[i].name; i++) {
       
    54       if (!strcmp(args[i].name, "type"))
       
    55         type = args[i].value;
       
    56       if (!strcmp(args[i].name, "message"))
       
    57         msg = args[i].value;
       
    58       if (!strcmp(args[i].name, "jid"))
       
    59         bjid = args[i].value;
       
    60     }
       
    61     /* shouldn't happen */
       
    62     if (!bjid || !type || !msg)
       
    63       return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
    64 
       
    65     subscription = roster_getsubscription(bjid);
       
    66     /* only ignore subscription requests when no subscription exists */
       
    67     if ((subscription == sub_none) && (!strcmp(type, "subscribe"))) {
       
    68       GSList *head;
       
    69       int ignore_it = FALSE;
       
    70 
       
    71       /* try to match against one of our regular expressions */
       
    72       for (head = regexlist; head && !ignore_it; head = g_slist_next(head)) {
       
    73         GMatchInfo *match_info;
       
    74         g_regex_match(head->data, bjid, 0, &match_info);
       
    75         if (g_match_info_matches(match_info))
       
    76           ignore_it = TRUE;
       
    77         g_match_info_free (match_info);
       
    78       }
       
    79       if(ignore_it) {
       
    80         xmpp_send_s10n(bjid, LM_MESSAGE_SUB_TYPE_UNSUBSCRIBED);
       
    81         scr_log_print(LPRINT_NORMAL, "Ignored auth request from %s (%s)",
       
    82                       bjid, msg);
       
    83         return HOOK_HANDLER_RESULT_NO_MORE_HANDLER_DROP_DATA;
       
    84       }
       
    85     }
       
    86   }
       
    87   /* we're done, let the other handlers do their job! */
       
    88   return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
    89 }
       
    90 
       
    91 /* ignore command handler */
       
    92 static void do_ignore_auth(char *args)
       
    93 {
       
    94   GRegex *new_regex;
       
    95   if (!args || !*args) {
       
    96     GSList *head;
       
    97     for (head = regexlist; head; head = g_slist_next(head))
       
    98       scr_log_print(LPRINT_NORMAL, "Ignoring %s",
       
    99                     g_regex_get_pattern(head->data));
       
   100     return;
       
   101   }
       
   102   new_regex = g_regex_new(args, G_REGEX_OPTIMIZE, 0, NULL);
       
   103   if (!new_regex) {
       
   104     scr_log_print(LPRINT_NORMAL, "That wasn't a glib regex");
       
   105     return;
       
   106   }
       
   107   regexlist = g_slist_append(regexlist, new_regex);
       
   108 }
       
   109 
       
   110 /* Initialization */
       
   111 static void ignore_auth_init(void)
       
   112 {
       
   113   /* Add command */
       
   114   cmd_add("ignore_auth", "", 0, 0, do_ignore_auth, NULL);
       
   115   /* Add handler */
       
   116   ignore_auth_hid = hk_add_handler(ignore_hh, HOOK_SUBSCRIPTION,
       
   117                                    G_PRIORITY_DEFAULT_IDLE, NULL);
       
   118   settings_set(SETTINGS_TYPE_OPTION, "ignore_auth", "1");
       
   119 }
       
   120 
       
   121 /* Uninitialization */
       
   122 static void ignore_auth_uninit(void)
       
   123 {
       
   124   GSList *head;
       
   125   /* Unregister command */
       
   126   cmd_del("ignore_auth");
       
   127   /* Unregister event handler */
       
   128   hk_del_handler(HOOK_SUBSCRIPTION, ignore_auth_hid);
       
   129   /* unref every regex */
       
   130   for (head = regexlist; head; head = g_slist_next(head))
       
   131     g_regex_unref(head->data);
       
   132   g_slist_free(regexlist);
       
   133   regexlist = NULL;
       
   134 }
       
   135 
       
   136 /* vim: set et cindent cinoptions=>2\:2(0 ts=2 sw=2:  For Vim users... */