show_mdr/show_mdr.c
changeset 48 06495be653fd
child 49 a276e2435f04
equal deleted inserted replaced
47:dcef1b8f2e05 48:06495be653fd
       
     1 /*
       
     2  *  Module "show_mdr"       -- Show Message Delivery Receipts
       
     3  *
       
     4  *  This module displays the message "delivery receipts" (XEP 184)
       
     5  *  received from contacts to the log window.
       
     6  *
       
     7  * Copyright (C) 2013,2014 Mikael Berthe <mikael@lilotux.net>
       
     8  *
       
     9  * This module is free software; you can redistribute it and/or modify
       
    10  * it under the terms of the GNU General Public License as published by
       
    11  * the Free Software Foundation; either version 2 of the License, or (at
       
    12  * your option) any later version.
       
    13  *
       
    14  * This program is distributed in the hope that it will be useful, but
       
    15  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    17  * General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU General Public License
       
    20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    21  */
       
    22 
       
    23 #include <stdio.h>
       
    24 #include <stdlib.h>
       
    25 #include <mcabber/modules.h>
       
    26 #include <mcabber/logprint.h>
       
    27 #include <mcabber/hooks.h>
       
    28 #include <mcabber/roster.h>
       
    29 #include <mcabber/utils.h>
       
    30 
       
    31 static void show_mdr_init(void);
       
    32 static void show_mdr_uninit(void);
       
    33 
       
    34 /* Module description */
       
    35 module_info_t info_show_mdr = {
       
    36   /*
       
    37         .branch         = MCABBER_BRANCH,
       
    38         .api            = MCABBER_API_VERSION,
       
    39   */
       
    40         .branch         = "dev",
       
    41         .api            = 33,     // HOOK_MDR_RECEIVED was included in dev-33
       
    42         .version        = "0.01",
       
    43         .description    = "Show delivery receipts in the log window.",
       
    44         .requires       = NULL,
       
    45         .init           = show_mdr_init,
       
    46         .uninit         = show_mdr_uninit,
       
    47         .next           = NULL,
       
    48 };
       
    49 
       
    50 // Hook handler id
       
    51 static guint mdr_hid;
       
    52 
       
    53 static int number_of_resources(const char *fjid)
       
    54 {
       
    55   GSList *sl_user;
       
    56   gpointer bud;
       
    57   GSList *resources, *p_res;
       
    58   char *bjid;
       
    59   int n = 0;
       
    60 
       
    61   if (!fjid) return -1;
       
    62 
       
    63   bjid = jidtodisp(fjid);
       
    64   sl_user = roster_find(bjid, jidsearch, ROSTER_TYPE_USER);
       
    65   g_free(bjid);
       
    66 
       
    67   if (!sl_user) return -1;
       
    68 
       
    69   bud = sl_user->data;
       
    70   resources = buddy_getresources(bud);
       
    71 
       
    72   for (p_res = resources ; p_res ; p_res = g_slist_next(p_res))
       
    73     n++;
       
    74 
       
    75   return n;
       
    76 }
       
    77 
       
    78 // Event handler for delivery receipts events
       
    79 static guint mdr_hh(const gchar *hookname, hk_arg_t *args,
       
    80                     gpointer userdata)
       
    81 {
       
    82   for ( ; args->name; args++) {
       
    83     if (!g_strcmp0(args->name, "jid")) {
       
    84       int nres;
       
    85       // Note: we could use a whitelist...
       
    86 
       
    87       scr_log_print(LPRINT_DEBUG, "Received MDR from %s", args->value);
       
    88 
       
    89       /* What we do: we check the number N of resources from the contact and
       
    90          display the MDR sender only if N > 1
       
    91        */
       
    92       nres = number_of_resources(args->value);
       
    93       if (nres > 1)
       
    94         scr_log_print(LPRINT_NORMAL, "Received MDR from %s", args->value);
       
    95     }
       
    96   }
       
    97 
       
    98   return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
    99 }
       
   100 
       
   101 // Initialization
       
   102 static void show_mdr_init(void)
       
   103 {
       
   104   // Add hook handler for delivery receipts
       
   105   mdr_hid = hk_add_handler(mdr_hh, HOOK_MDR_RECEIVED,
       
   106                            G_PRIORITY_DEFAULT_IDLE, NULL);
       
   107 }
       
   108 
       
   109 // Uninitialization
       
   110 static void show_mdr_uninit(void)
       
   111 {
       
   112   // Unregister handler
       
   113   hk_del_handler(HOOK_MDR_RECEIVED, mdr_hid);
       
   114 }
       
   115 
       
   116 /* vim: set et cindent cinoptions=>2\:2(0 ts=2 sw=2:  For Vim users... */