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