modularize-extcmd.diff
changeset 36 b8854e2fe147
parent 29 335662766a88
child 55 8f5cf5969e25
equal deleted inserted replaced
35:c80eb5663234 36:b8854e2fe147
       
     1 Convert extcmd into a module
       
     2 
       
     3 diff -r 27d19b6bc194 mcabber/configure.ac
       
     4 --- a/mcabber/configure.ac	Mon Apr 30 23:36:55 2012 +0300
       
     5 +++ b/mcabber/configure.ac	Mon Apr 30 23:37:25 2012 +0300
       
     6 @@ -283,6 +283,7 @@
       
     7                   modules/beep/Makefile
       
     8                   modules/xttitle/Makefile
       
     9                   modules/fifo/Makefile
       
    10 +                 modules/eventcmd/Makefile
       
    11                   modules/urlregex/Makefile
       
    12                   doc/Makefile
       
    13                   doc/guide/Makefile
       
    14 diff -r 27d19b6bc194 mcabber/mcabber/Makefile.am
       
    15 --- a/mcabber/mcabber/Makefile.am	Mon Apr 30 23:36:55 2012 +0300
       
    16 +++ b/mcabber/mcabber/Makefile.am	Mon Apr 30 23:37:25 2012 +0300
       
    17 @@ -7,7 +7,7 @@
       
    18  		  xmpp.c xmpp.h xmpp_helper.c xmpp_helper.h xmpp_defines.h \
       
    19  		  xmpp_iq.c xmpp_iq.h xmpp_iqrequest.c xmpp_iqrequest.h \
       
    20  		  xmpp_muc.c xmpp_muc.h xmpp_s10n.c xmpp_s10n.h \
       
    21 -		  caps.c caps.h help.c help.h extcmd.c extcmd.h
       
    22 +		  caps.c caps.h help.c help.h
       
    23  
       
    24  if OTR
       
    25  mcabber_SOURCES += otr.c otr.h nohtml.c nohtml.h
       
    26 @@ -55,7 +55,7 @@
       
    27  
       
    28  mcabberincludedir = $(includedir)/mcabber
       
    29  else
       
    30 -mcabber_SOURCES += fifo.c fifo.h
       
    31 +mcabber_SOURCES += fifo.c fifo.h extcmd.c extcmd.h
       
    32  endif
       
    33  
       
    34  #SUBDIRS =
       
    35 diff -r 27d19b6bc194 mcabber/mcabber/hooks.c
       
    36 --- a/mcabber/mcabber/hooks.c	Mon Apr 30 23:36:55 2012 +0300
       
    37 +++ b/mcabber/mcabber/hooks.c	Mon Apr 30 23:37:25 2012 +0300
       
    38 @@ -36,7 +36,10 @@
       
    39  #include "utf8.h"
       
    40  #include "commands.h"
       
    41  #include "main.h"
       
    42 -#include "extcmd.h"
       
    43 +
       
    44 +#ifndef MODULES_ENABLE
       
    45 +# include "extcmd.h"
       
    46 +#endif
       
    47  
       
    48  #ifdef MODULES_ENABLE
       
    49  #include <glib.h>
       
    50 @@ -189,7 +192,9 @@
       
    51    int is_groupchat = FALSE; // groupchat message
       
    52    int is_room = FALSE;      // window is a room window
       
    53    int log_muc_conf = FALSE;
       
    54 +#ifndef MODULES_ENABLE
       
    55    int active_window = FALSE;
       
    56 +#endif
       
    57    int message_flags = 0;
       
    58    guint rtype = ROSTER_TYPE_USER;
       
    59    char *wmsg = NULL, *bmsg = NULL, *mmsg = NULL;
       
    60 @@ -397,6 +402,7 @@
       
    61    }
       
    62  #endif
       
    63  
       
    64 +#ifndef MODULES_ENABLE
       
    65    if (settings_opt_get_int("events_ignore_active_window") &&
       
    66        current_buddy && scr_get_chatmode()) {
       
    67      gpointer bud = BUDDATA(current_buddy);
       
    68 @@ -415,6 +421,7 @@
       
    69      hk_ext_cmd(bjid, EXT_CMD_TYPE_MESSAGE,
       
    70                 is_groupchat ? EXT_CMD_INFO_GROUPCHAT : EXT_CMD_INFO_RECEIVED,
       
    71                 wmsg);
       
    72 +#endif
       
    73  
       
    74    // Beep, if enabled:
       
    75    // - if it's a private message
       
    76 @@ -491,8 +498,10 @@
       
    77    }
       
    78  #endif
       
    79  
       
    80 +#ifndef MODULES_ENABLE
       
    81    // External command
       
    82    hk_ext_cmd(bjid, EXT_CMD_TYPE_MESSAGE, EXT_CMD_INFO_SENT, NULL);
       
    83 +#endif
       
    84  
       
    85    g_free(bmsg);
       
    86    g_free(mmsg);
       
    87 @@ -578,9 +587,11 @@
       
    88    }
       
    89  #endif
       
    90  
       
    91 +#ifndef MODULES_ENABLE
       
    92    // External command
       
    93    newstatus[0] = toupper(newstatus[0]);
       
    94    hk_ext_cmd(bjid, EXT_CMD_TYPE_STATUS, newstatus, status_msg);
       
    95 +#endif
       
    96  }
       
    97  
       
    98  void hk_mystatuschange(time_t timestamp, enum imstatus old_status,
       
    99 @@ -704,11 +715,13 @@
       
   100    prev_muc_unread    = muc_unread;
       
   101    prev_muc_attention = muc_attention;
       
   102  
       
   103 +#ifndef MODULES_ENABLE
       
   104    /* Call external command */
       
   105    str_unread = g_strdup_printf("%u %u %u %u", unread_count, attention_count,
       
   106                                 muc_unread, muc_attention);
       
   107    hk_ext_cmd("", EXT_CMD_TYPE_UNREAD, str_unread, NULL);
       
   108    g_free(str_unread);
       
   109 +#endif
       
   110  }
       
   111  
       
   112  //  hk_presence_subscription_request(jid, message)
       
   113 diff -r 27d19b6bc194 mcabber/mcabber/main.c
       
   114 --- a/mcabber/mcabber/main.c	Mon Apr 30 23:36:55 2012 +0300
       
   115 +++ b/mcabber/mcabber/main.c	Mon Apr 30 23:37:25 2012 +0300
       
   116 @@ -44,7 +44,10 @@
       
   117  #include "xmpp.h"
       
   118  #include "help.h"
       
   119  #include "events.h"
       
   120 -#include "extcmd.h"
       
   121 +
       
   122 +#ifndef MODULES_ENABLE
       
   123 +# include "extcmd.h"
       
   124 +#endif
       
   125  
       
   126  #ifndef MODULES_ENABLE
       
   127  # include "fifo.h"
       
   128 @@ -443,9 +446,11 @@
       
   129    }
       
   130  #endif
       
   131  
       
   132 +#ifndef MODULES_ENABLE
       
   133    optstring = settings_opt_get("events_command");
       
   134    if (optstring)
       
   135      hk_ext_cmd_init(optstring);
       
   136 +#endif
       
   137  
       
   138    optstring = settings_opt_get("roster_display_filter");
       
   139    if (optstring)
       
   140 diff -r 27d19b6bc194 mcabber/modules/Makefile.am
       
   141 --- a/mcabber/modules/Makefile.am	Mon Apr 30 23:36:55 2012 +0300
       
   142 +++ b/mcabber/modules/Makefile.am	Mon Apr 30 23:37:25 2012 +0300
       
   143 @@ -1,1 +1,1 @@
       
   144 -SUBDIRS = beep xttitle fifo urlregex
       
   145 +SUBDIRS = beep xttitle fifo eventcmd urlregex
       
   146 diff -r 27d19b6bc194 mcabber/modules/eventcmd/Makefile.am
       
   147 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
       
   148 +++ b/mcabber/modules/eventcmd/Makefile.am	Mon Apr 30 23:37:25 2012 +0300
       
   149 @@ -0,0 +1,12 @@
       
   150 +
       
   151 +if INSTALL_HEADERS
       
   152 +pkglib_LTLIBRARIES = libeventcmd.la
       
   153 +libeventcmd_la_SOURCES = eventcmd.c $(top_srcdir)/mcabber/extcmd.c $(top_srcdir)/mcabber/extcmd.h
       
   154 +libeventcmd_la_LDFLAGS = -module -avoid-version -shared
       
   155 +
       
   156 +LDADD = $(GLIB_LIBS)
       
   157 +AM_CPPFLAGS = -I$(top_srcdir) $(GLIB_CFLAGS) $(LOUDMOUTH_CFLAGS) \
       
   158 +				$(GPGME_CFLAGS) $(LIBOTR_CFLAGS) \
       
   159 +				$(ENCHANT_CFLAGS)
       
   160 +endif
       
   161 +
       
   162 diff -r 27d19b6bc194 mcabber/modules/eventcmd/eventcmd.c
       
   163 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
       
   164 +++ b/mcabber/modules/eventcmd/eventcmd.c	Mon Apr 30 23:37:25 2012 +0300
       
   165 @@ -0,0 +1,221 @@
       
   166 +
       
   167 +/* Copyright 2009 Myhailo Danylenko
       
   168 + *
       
   169 + * This file is part of mcabber
       
   170 + *
       
   171 + * mcabber is free software: you can redistribute it and/or modify
       
   172 + * it under the terms of the GNU General Public License as published by
       
   173 + * the Free Software Foundation, either version 2 of the License, or
       
   174 + * (at your option) any later version.
       
   175 + *
       
   176 + * This program is distributed in the hope that it will be useful,
       
   177 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
   178 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
   179 + * GNU General Public License for more details.
       
   180 + *
       
   181 + * You should have received a copy of the GNU General Public License
       
   182 + * along with this program.  If not, see <http://www.gnu.org/licenses/>. */
       
   183 +
       
   184 +#include <glib.h>
       
   185 +#include <gmodule.h>
       
   186 +#include <string.h>
       
   187 +#include <stdlib.h>
       
   188 +#include <ctype.h> // tolower
       
   189 +#include <unistd.h>
       
   190 +
       
   191 +#include <mcabber/settings.h>
       
   192 +#include <mcabber/hooks.h>
       
   193 +#include <mcabber/roster.h>
       
   194 +#include <mcabber/utils.h>
       
   195 +#include <mcabber/logprint.h>
       
   196 +#include <mcabber/extcmd.h>
       
   197 +#include <mcabber/modules.h>
       
   198 +#include <mcabber/screen.h>
       
   199 +#include <mcabber/config.h>
       
   200 +
       
   201 +static void ecm_init(void);
       
   202 +static void ecm_uninit(void);
       
   203 +
       
   204 +module_info_t info_eventcmd = {
       
   205 +  .branch          = MCABBER_BRANCH,
       
   206 +  .api             = MCABBER_API_VERSION,
       
   207 +  .version         = MCABBER_VERSION,
       
   208 +  .requires        = NULL,
       
   209 +  .init            = ecm_init,
       
   210 +  .uninit          = ecm_uninit,
       
   211 +  .description     = "External command execution on events\n"
       
   212 +          "Recognizes options events_command (required), events_ignore_active_window, "
       
   213 +          "event_log_files, event_log_dir, eventcmd_use_nickname, eventcmd_checkstatus.",
       
   214 +  .next = NULL,
       
   215 +};
       
   216 +
       
   217 +static guint eventcmd_hpmi_hid = 0;
       
   218 +static guint eventcmd_hmo_hid  = 0;
       
   219 +static guint eventcmd_hsc_hid  = 0;
       
   220 +static guint eventcmd_hulc_hid = 0;
       
   221 +
       
   222 +static guint eventcmd_hpmih(const gchar *name, hk_arg_t *args,
       
   223 +                            gpointer userdata)
       
   224 +{
       
   225 +  gboolean    active_window = FALSE;
       
   226 +  gboolean    is_groupchat  = FALSE;
       
   227 +  gboolean    timestamp     = FALSE;
       
   228 +  const char *bjid          = NULL;
       
   229 +  hk_arg_t   *arg           = args;
       
   230 +  const char *wmsg          = NULL;
       
   231 +
       
   232 +  while (arg->name) {
       
   233 +    if (!strcmp(arg->name, "jid"))
       
   234 +      bjid = arg->value;
       
   235 +    else if (!strcmp(arg->name, "groupchat"))
       
   236 +      is_groupchat = strcmp(arg->value, "true") ? FALSE : TRUE;
       
   237 +    else if (!strcmp(arg->name, "delayed") && *(arg->value))
       
   238 +      timestamp = TRUE;
       
   239 +    else if (!strcmp(arg->name, "message"))
       
   240 +      wmsg = arg->value;
       
   241 +    arg++;
       
   242 +  }
       
   243 +
       
   244 +  if (!bjid) {
       
   245 +    scr_LogPrint(LPRINT_LOGNORM, "eventcmd: post-message-in: No jid found.");
       
   246 +    return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   247 +  }
       
   248 +
       
   249 +  if (settings_opt_get_int("events_ignore_active_window") &&
       
   250 +      current_buddy && scr_get_chatmode()) {
       
   251 +    gpointer bud = BUDDATA(current_buddy);
       
   252 +    if (bud) {
       
   253 +      const char *cjid = buddy_getjid(bud);
       
   254 +      if (cjid && !strcasecmp(cjid, bjid))
       
   255 +        active_window = TRUE;
       
   256 +    }
       
   257 +  }
       
   258 +
       
   259 +  if (!active_window && ((is_groupchat && !timestamp) || !is_groupchat))
       
   260 +    hk_ext_cmd(bjid, EXT_CMD_TYPE_MESSAGE,
       
   261 +               is_groupchat ? EXT_CMD_INFO_GROUPCHAT : EXT_CMD_INFO_RECEIVED, wmsg);
       
   262 +
       
   263 +  return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   264 +}
       
   265 +
       
   266 +static guint eventcmd_hmoh(const gchar *name, hk_arg_t *args, gpointer userdata)
       
   267 +{
       
   268 +  const char *bjid = NULL;
       
   269 +  hk_arg_t   *arg  = args;
       
   270 +
       
   271 +  while (arg->name) {
       
   272 +    if (!strcmp(arg->name, "jid")) {
       
   273 +      bjid = arg->value;
       
   274 +      break;
       
   275 +    }
       
   276 +    arg++;
       
   277 +  }
       
   278 +
       
   279 +  if (!bjid) {
       
   280 +    scr_LogPrint(LPRINT_LOGNORM, "eventcmd: message-out: No jid found.");
       
   281 +    return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   282 +  }
       
   283 +
       
   284 +  hk_ext_cmd(bjid, EXT_CMD_TYPE_MESSAGE, EXT_CMD_INFO_SENT, NULL);
       
   285 +
       
   286 +  return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   287 +}
       
   288 +
       
   289 +static guint eventcmd_hsch(const gchar *name, hk_arg_t *args, gpointer userdata)
       
   290 +{
       
   291 +  const char *bjid      = NULL;
       
   292 +  char        status[2] = { '?', '\0' };
       
   293 +  hk_arg_t   *arg       = args;
       
   294 +  const char *message   = NULL;
       
   295 +
       
   296 +  while (arg->name) {
       
   297 +    if (!strcmp(arg->name, "jid"))
       
   298 +      bjid = arg->value;
       
   299 +    else if (!strcmp(arg->name, "new_status"))
       
   300 +      status[0] = toupper (arg->value[0]);
       
   301 +    else if (!strcmp(arg->name, "message") && arg->value && *(arg->value))
       
   302 +      message = arg->value;
       
   303 +    arg++;
       
   304 +  }
       
   305 +
       
   306 +  if (!bjid) {
       
   307 +    scr_LogPrint(LPRINT_LOGNORM, "eventcmd: status-change: No jid found.");
       
   308 +    return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   309 +  }
       
   310 +
       
   311 +  hk_ext_cmd(bjid, EXT_CMD_TYPE_STATUS, status, message);
       
   312 +
       
   313 +  return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   314 +}
       
   315 +
       
   316 +static guint eventcmd_hulch(const gchar *name, hk_arg_t *args, gpointer userdata)
       
   317 +{
       
   318 +  hk_arg_t    *arg           = args;
       
   319 +  const gchar *unread        = NULL;
       
   320 +  const gchar *attention     = NULL;
       
   321 +  const gchar *muc_unread    = NULL;
       
   322 +  const gchar *muc_attention = NULL;
       
   323 +
       
   324 +  while (arg->name) {
       
   325 +    if (!strcmp(arg->name, "unread"))
       
   326 +      unread = arg->value;
       
   327 +    else if (!strcmp(arg->name, "attention"))
       
   328 +      attention = arg->value;
       
   329 +    else if (!strcmp(arg->name, "muc_unread"))
       
   330 +      muc_unread = arg->value;
       
   331 +    else if (!strcmp(arg->name, "muc_attention"))
       
   332 +      muc_attention = arg->value;
       
   333 +    arg++;
       
   334 +  }
       
   335 +
       
   336 +  if (!unread || !attention || !muc_unread || !muc_attention) {
       
   337 +    scr_LogPrint(LPRINT_LOGNORM, "eventcmd: unread-list-change: Missing parameter.");
       
   338 +    return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   339 +  }
       
   340 +
       
   341 +  {
       
   342 +    gchar *str = g_strdup_printf("%s %s %s %s", unread, attention, muc_unread,
       
   343 +                                 muc_attention);
       
   344 +    hk_ext_cmd("", EXT_CMD_TYPE_UNREAD, str, NULL);
       
   345 +    g_free(str);
       
   346 +  }
       
   347 +
       
   348 +  return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   349 +}
       
   350 +
       
   351 +const gchar *g_module_check_init(GModule *module)
       
   352 +{
       
   353 +  const char *command = settings_opt_get("events_command");
       
   354 +  if (!command)
       
   355 +    return "events_command not set";
       
   356 +  hk_ext_cmd_init(command);
       
   357 +  return NULL;
       
   358 +}
       
   359 +
       
   360 +void g_module_unload(GModule *module)
       
   361 +{
       
   362 +  hk_ext_cmd_init(NULL);
       
   363 +}
       
   364 +
       
   365 +static void ecm_init(void)
       
   366 +{
       
   367 +  eventcmd_hpmi_hid = hk_add_handler(eventcmd_hpmih, HOOK_POST_MESSAGE_IN,
       
   368 +                                     G_PRIORITY_LOW, NULL);
       
   369 +  eventcmd_hmo_hid  = hk_add_handler(eventcmd_hmoh,  HOOK_MESSAGE_OUT,
       
   370 +                                     G_PRIORITY_LOW, NULL);
       
   371 +  eventcmd_hsc_hid  = hk_add_handler(eventcmd_hsch,  HOOK_STATUS_CHANGE,
       
   372 +                                     G_PRIORITY_LOW, NULL);
       
   373 +  eventcmd_hulc_hid = hk_add_handler(eventcmd_hulch, HOOK_UNREAD_LIST_CHANGE,
       
   374 +                                     G_PRIORITY_LOW, NULL);
       
   375 +
       
   376 +}
       
   377 +
       
   378 +static void ecm_uninit(void)
       
   379 +{
       
   380 +  hk_del_handler(HOOK_POST_MESSAGE_IN,    eventcmd_hpmi_hid);
       
   381 +  hk_del_handler(HOOK_MESSAGE_OUT,        eventcmd_hmo_hid);
       
   382 +  hk_del_handler(HOOK_STATUS_CHANGE,      eventcmd_hsc_hid);
       
   383 +  hk_del_handler(HOOK_UNREAD_LIST_CHANGE, eventcmd_hulc_hid);
       
   384 +}
       
   385 +
       
   386 +/* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2:  For Vim users... */