New module: show_mdr
authorMikael Berthe <mikael@lilotux.net>
Sun, 06 Jul 2014 17:02:23 +0200
changeset 48 06495be653fd
parent 47 dcef1b8f2e05
child 49 a276e2435f04
New module: show_mdr The module show_mdr displays the details of the received Delivery Messages when the contact has several connected resources.
Makefile.am
configure.ac
show_mdr/Makefile.am
show_mdr/show_mdr.c
--- a/Makefile.am	Sat Oct 13 17:33:01 2012 +0200
+++ b/Makefile.am	Sun Jul 06 17:02:23 2014 +0200
@@ -1,1 +1,1 @@
-SUBDIRS = clock comment extsay-ng ignore_auth info_msgcount killpresence lastmsg
+SUBDIRS = clock comment extsay-ng ignore_auth info_msgcount killpresence lastmsg show_mdr
--- a/configure.ac	Sat Oct 13 17:33:01 2012 +0200
+++ b/configure.ac	Sun Jul 06 17:02:23 2014 +0200
@@ -101,31 +101,42 @@
               AC_HELP_STRING([--enable-module-clock],
                              [enable module clock]),
               enable_module_clock=$enableval)
+
 AC_ARG_ENABLE(module-comment,
               AC_HELP_STRING([--enable-module-comment],
                              [enable module comment]),
               enable_module_comment=$enableval)
+
 AC_ARG_ENABLE(module-extsayng,
               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]),
               enable_module_info_msgcount=$enableval)
+
 AC_ARG_ENABLE(module-killpresence,
               AC_HELP_STRING([--enable-module-killpresence],
                              [enable module killpresence]),
               enable_module_lastmsg=$enableval)
+
 AC_ARG_ENABLE(module-lastmsg,
               AC_HELP_STRING([--enable-module-lastmsg],
                              [enable module lastmsg]),
               enable_module_lastmsg=$enableval)
 
+AC_ARG_ENABLE(module-show_mdr,
+              AC_HELP_STRING([--enable-module-show_mdr],
+                             [enable module show_mdr]),
+              enable_module_show_mdr=$enableval)
+
 AM_CONDITIONAL([INSTALL_MODULE_CLOCK],
                [test x"${enable_all_modules}" = x"yes" -o \
                      x"${enable_module_clock}" = x"yes"])
@@ -154,6 +165,10 @@
                [test x"${enable_all_modules}" = x"yes" -o \
                      x"${enable_module_lastmsg}" = x"yes"])
 
+AM_CONDITIONAL([INSTALL_MODULE_SHOW_MDR],
+               [test x"${enable_all_modules}" = x"yes" -o \
+                     x"${enable_module_show_mdr}" = x"yes"])
+
 AC_CONFIG_FILES([clock/Makefile
                  comment/Makefile
                  extsay-ng/Makefile
@@ -161,5 +176,6 @@
                  info_msgcount/Makefile
                  killpresence/Makefile
                  lastmsg/Makefile
+                 show_mdr/Makefile
                  Makefile])
 AC_OUTPUT
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/show_mdr/Makefile.am	Sun Jul 06 17:02:23 2014 +0200
@@ -0,0 +1,11 @@
+
+if INSTALL_MODULE_SHOW_MDR
+
+pkglib_LTLIBRARIES = libshow_mdr.la
+libshow_mdr_la_SOURCES = show_mdr.c
+libshow_mdr_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/show_mdr/show_mdr.c	Sun Jul 06 17:02:23 2014 +0200
@@ -0,0 +1,116 @@
+/*
+ *  Module "show_mdr"       -- Show Message Delivery Receipts
+ *
+ *  This module displays the message "delivery receipts" (XEP 184)
+ *  received from contacts to the log window.
+ *
+ * Copyright (C) 2013,2014 Mikael Berthe <mikael@lilotux.net>
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <mcabber/modules.h>
+#include <mcabber/logprint.h>
+#include <mcabber/hooks.h>
+#include <mcabber/roster.h>
+#include <mcabber/utils.h>
+
+static void show_mdr_init(void);
+static void show_mdr_uninit(void);
+
+/* Module description */
+module_info_t info_show_mdr = {
+  /*
+        .branch         = MCABBER_BRANCH,
+        .api            = MCABBER_API_VERSION,
+  */
+        .branch         = "dev",
+        .api            = 33,     // HOOK_MDR_RECEIVED was included in dev-33
+        .version        = "0.01",
+        .description    = "Show delivery receipts in the log window.",
+        .requires       = NULL,
+        .init           = show_mdr_init,
+        .uninit         = show_mdr_uninit,
+        .next           = NULL,
+};
+
+// Hook handler id
+static guint mdr_hid;
+
+static int number_of_resources(const char *fjid)
+{
+  GSList *sl_user;
+  gpointer bud;
+  GSList *resources, *p_res;
+  char *bjid;
+  int n = 0;
+
+  if (!fjid) return -1;
+
+  bjid = jidtodisp(fjid);
+  sl_user = roster_find(bjid, jidsearch, ROSTER_TYPE_USER);
+  g_free(bjid);
+
+  if (!sl_user) return -1;
+
+  bud = sl_user->data;
+  resources = buddy_getresources(bud);
+
+  for (p_res = resources ; p_res ; p_res = g_slist_next(p_res))
+    n++;
+
+  return n;
+}
+
+// Event handler for delivery receipts events
+static guint mdr_hh(const gchar *hookname, hk_arg_t *args,
+                    gpointer userdata)
+{
+  for ( ; args->name; args++) {
+    if (!g_strcmp0(args->name, "jid")) {
+      int nres;
+      // Note: we could use a whitelist...
+
+      scr_log_print(LPRINT_DEBUG, "Received MDR from %s", args->value);
+
+      /* What we do: we check the number N of resources from the contact and
+         display the MDR sender only if N > 1
+       */
+      nres = number_of_resources(args->value);
+      if (nres > 1)
+        scr_log_print(LPRINT_NORMAL, "Received MDR from %s", args->value);
+    }
+  }
+
+  return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+}
+
+// Initialization
+static void show_mdr_init(void)
+{
+  // Add hook handler for delivery receipts
+  mdr_hid = hk_add_handler(mdr_hh, HOOK_MDR_RECEIVED,
+                           G_PRIORITY_DEFAULT_IDLE, NULL);
+}
+
+// Uninitialization
+static void show_mdr_uninit(void)
+{
+  // Unregister handler
+  hk_del_handler(HOOK_MDR_RECEIVED, mdr_hid);
+}
+
+/* vim: set et cindent cinoptions=>2\:2(0 ts=2 sw=2:  For Vim users... */