separate-extcmd.diff
changeset 36 b8854e2fe147
parent 29 335662766a88
child 55 8f5cf5969e25
equal deleted inserted replaced
35:c80eb5663234 36:b8854e2fe147
       
     1 Move extcmd code from hooks
       
     2 
       
     3 diff -r 64da54766f99 mcabber/mcabber/Makefile.am
       
     4 --- a/mcabber/mcabber/Makefile.am	Mon Apr 30 23:36:31 2012 +0300
       
     5 +++ b/mcabber/mcabber/Makefile.am	Mon Apr 30 23:36:55 2012 +0300
       
     6 @@ -7,7 +7,7 @@
       
     7  		  xmpp.c xmpp.h xmpp_helper.c xmpp_helper.h xmpp_defines.h \
       
     8  		  xmpp_iq.c xmpp_iq.h xmpp_iqrequest.c xmpp_iqrequest.h \
       
     9  		  xmpp_muc.c xmpp_muc.h xmpp_s10n.c xmpp_s10n.h \
       
    10 -		  caps.c caps.h help.c help.h
       
    11 +		  caps.c caps.h help.c help.h extcmd.c extcmd.h
       
    12  
       
    13  if OTR
       
    14  mcabber_SOURCES += otr.c otr.h nohtml.c nohtml.h
       
    15 @@ -42,6 +42,7 @@
       
    16  			 xmpp_iq.h xmpp_iqrequest.h \
       
    17  			 xmpp_muc.h xmpp_s10n.h \
       
    18  			 caps.h fifo.h help.h modules.h api.h \
       
    19 +			 extcmd.h \
       
    20  			 $(top_srcdir)/include/config.h
       
    21  
       
    22  if OTR
       
    23 diff -r 64da54766f99 mcabber/mcabber/extcmd.c
       
    24 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
       
    25 +++ b/mcabber/mcabber/extcmd.c	Mon Apr 30 23:36:55 2012 +0300
       
    26 @@ -0,0 +1,121 @@
       
    27 +/*
       
    28 + * extcmd.c      -- External event handler command
       
    29 + *
       
    30 + * Copyright (C) 2005-2010 Mikael Berthe <mikael@lilotux.net>
       
    31 + *
       
    32 + * This program is free software; you can redistribute it and/or modify
       
    33 + * it under the terms of the GNU General Public License as published by
       
    34 + * the Free Software Foundation; either version 2 of the License, or (at
       
    35 + * your option) any later version.
       
    36 + *
       
    37 + * This program is distributed in the hope that it will be useful, but
       
    38 + * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    39 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    40 + * General Public License for more details.
       
    41 + *
       
    42 + * You should have received a copy of the GNU General Public License
       
    43 + * along with this program; if not, write to the Free Software
       
    44 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
       
    45 + * USA
       
    46 + */
       
    47 +
       
    48 +#include <stdlib.h>
       
    49 +#include <string.h>
       
    50 +#include <sys/types.h>
       
    51 +#include <unistd.h>
       
    52 +#include <glib.h>
       
    53 +
       
    54 +#include "screen.h"
       
    55 +#include "roster.h"
       
    56 +#include "settings.h"
       
    57 +#include "utils.h"
       
    58 +#include "utf8.h"
       
    59 +
       
    60 +static char *extcmd = NULL;
       
    61 +
       
    62 +//  hk_ext_cmd_init()
       
    63 +// Initialize external command variable.
       
    64 +// Can be called with parameter NULL to reset and free memory
       
    65 +void hk_ext_cmd_init(const char *command)
       
    66 +{
       
    67 +  if (extcmd) {
       
    68 +    g_free(extcmd);
       
    69 +    extcmd = NULL;
       
    70 +  }
       
    71 +  if (command)
       
    72 +    extcmd = expand_filename(command);
       
    73 +}
       
    74 +
       
    75 +//  hk_ext_cmd()
       
    76 +// Launch an external command (process) for the given event.
       
    77 +// For now, data should be NULL.
       
    78 +void hk_ext_cmd(const char *name, const char *arg_type, const char *arg_info, const char *data)
       
    79 +{
       
    80 +  pid_t pid;
       
    81 +  char *arg_data = NULL;
       
    82 +  char *datafname = NULL;
       
    83 +
       
    84 +  if (!arg_type || !arg_info) return;
       
    85 +
       
    86 +  if (*name && settings_opt_get_int("eventcmd_use_nickname"))
       
    87 +    name = roster_getname(name);
       
    88 +
       
    89 +  if (data && settings_opt_get_int("event_log_files")) {
       
    90 +    int fd;
       
    91 +    const char *prefix;
       
    92 +    char *prefix_xp = NULL;
       
    93 +    char *data_locale;
       
    94 +
       
    95 +    data_locale = from_utf8(data);
       
    96 +    prefix = settings_opt_get("event_log_dir");
       
    97 +    if (prefix)
       
    98 +      prefix = prefix_xp = expand_filename(prefix);
       
    99 +    else
       
   100 +      prefix = ut_get_tmpdir();
       
   101 +    datafname = g_strdup_printf("%s/mcabber-%d.XXXXXX", prefix, getpid());
       
   102 +    g_free(prefix_xp);
       
   103 +
       
   104 +    // XXX Some old systems may require us to set umask first.
       
   105 +    fd = mkstemp(datafname);
       
   106 +    if (fd == -1) {
       
   107 +      g_free(datafname);
       
   108 +      datafname = NULL;
       
   109 +      scr_LogPrint(LPRINT_LOGNORM,
       
   110 +                   "Unable to create temp file for external command.");
       
   111 +    } else {
       
   112 +      size_t data_locale_len = strlen(data_locale);
       
   113 +      ssize_t a = write(fd, data_locale, data_locale_len);
       
   114 +      ssize_t b = write(fd, "\n", 1);
       
   115 +      if ((size_t)a != data_locale_len || b != 1) {
       
   116 +        g_free(datafname);
       
   117 +        datafname = NULL;
       
   118 +        scr_LogPrint(LPRINT_LOGNORM,
       
   119 +                     "Unable to write to temp file for external command.");
       
   120 +      }
       
   121 +      close(fd);
       
   122 +      arg_data = datafname;
       
   123 +    }
       
   124 +    g_free(data_locale);
       
   125 +  }
       
   126 +
       
   127 +  if ((pid=fork()) == -1) {
       
   128 +    scr_LogPrint(LPRINT_LOGNORM, "Fork error, cannot launch external command.");
       
   129 +    g_free(datafname);
       
   130 +    return;
       
   131 +  }
       
   132 +
       
   133 +  if (pid == 0) { // child
       
   134 +    // Close standard file descriptors
       
   135 +    close(STDIN_FILENO);
       
   136 +    close(STDOUT_FILENO);
       
   137 +    close(STDERR_FILENO);
       
   138 +    if (execl(extcmd, extcmd, arg_type, arg_info, name, arg_data,
       
   139 +              (char *)NULL) == -1) {
       
   140 +      // scr_LogPrint(LPRINT_LOGNORM, "Cannot execute external command.");
       
   141 +      exit(1);
       
   142 +    }
       
   143 +  }
       
   144 +  g_free(datafname);
       
   145 +}
       
   146 +
       
   147 +/* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */
       
   148 diff -r 64da54766f99 mcabber/mcabber/extcmd.h
       
   149 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
       
   150 +++ b/mcabber/mcabber/extcmd.h	Mon Apr 30 23:36:55 2012 +0300
       
   151 @@ -0,0 +1,15 @@
       
   152 +#ifndef __MCABBER_EXTCMD_H__
       
   153 +#define __MCABBER_EXTCMD_H__ 1
       
   154 +
       
   155 +#define EXT_CMD_TYPE_MESSAGE   "MSG"
       
   156 +#define EXT_CMD_TYPE_STATUS    "STATUS"
       
   157 +#define EXT_CMD_TYPE_UNREAD    "UNREAD"
       
   158 +
       
   159 +#define EXT_CMD_INFO_RECEIVED  "IN"
       
   160 +#define EXT_CMD_INFO_SENT      "OUT"
       
   161 +#define EXT_CMD_INFO_GROUPCHAT "MUC"
       
   162 +
       
   163 +void hk_ext_cmd_init(const char *command);
       
   164 +void hk_ext_cmd(const char *bjid, const char *type, const char *info, const char *data);
       
   165 +
       
   166 +#endif
       
   167 diff -r 64da54766f99 mcabber/mcabber/hooks.c
       
   168 --- a/mcabber/mcabber/hooks.c	Mon Apr 30 23:36:31 2012 +0300
       
   169 +++ b/mcabber/mcabber/hooks.c	Mon Apr 30 23:36:55 2012 +0300
       
   170 @@ -24,6 +24,7 @@
       
   171  #include <string.h>
       
   172  #include <sys/types.h>
       
   173  #include <unistd.h>
       
   174 +#include <ctype.h>
       
   175  
       
   176  #include "hooks.h"
       
   177  #include "screen.h"
       
   178 @@ -35,6 +36,7 @@
       
   179  #include "utf8.h"
       
   180  #include "commands.h"
       
   181  #include "main.h"
       
   182 +#include "extcmd.h"
       
   183  
       
   184  #ifdef MODULES_ENABLE
       
   185  #include <glib.h>
       
   186 @@ -177,8 +179,6 @@
       
   187  }
       
   188  #endif
       
   189  
       
   190 -static char *extcmd;
       
   191 -
       
   192  static const char *COMMAND_ME = "/me ";
       
   193  
       
   194  void hk_message_in(const char *bjid, const char *resname,
       
   195 @@ -195,7 +195,6 @@
       
   196    char *wmsg = NULL, *bmsg = NULL, *mmsg = NULL;
       
   197    GSList *roster_usr;
       
   198    unsigned mucnicklen = 0;
       
   199 -  const char *ename = NULL;
       
   200    gboolean attention = FALSE, mucprivmsg = FALSE;
       
   201    gboolean error_msg_subtype = (type == LM_MESSAGE_SUB_TYPE_ERROR);
       
   202  #ifdef MODULES_ENABLE
       
   203 @@ -373,19 +372,6 @@
       
   204        (!is_room || (is_groupchat && log_muc_conf && !timestamp)))
       
   205      hlog_write_message(bjid, timestamp, 0, wmsg);
       
   206  
       
   207 -  if (settings_opt_get_int("events_ignore_active_window") &&
       
   208 -      current_buddy && scr_get_chatmode()) {
       
   209 -    gpointer bud = BUDDATA(current_buddy);
       
   210 -    if (bud) {
       
   211 -      const char *cjid = buddy_getjid(bud);
       
   212 -      if (cjid && !strcasecmp(cjid, bjid))
       
   213 -        active_window = TRUE;
       
   214 -    }
       
   215 -  }
       
   216 -
       
   217 -  if (settings_opt_get_int("eventcmd_use_nickname"))
       
   218 -    ename = roster_getname(bjid);
       
   219 -
       
   220    // Display the sender in the log window
       
   221    if ((!is_groupchat) && !(message_flags & HBB_PREFIX_ERR) &&
       
   222        settings_opt_get_int("log_display_sender")) {
       
   223 @@ -411,12 +397,24 @@
       
   224    }
       
   225  #endif
       
   226  
       
   227 +  if (settings_opt_get_int("events_ignore_active_window") &&
       
   228 +      current_buddy && scr_get_chatmode()) {
       
   229 +    gpointer bud = BUDDATA(current_buddy);
       
   230 +    if (bud) {
       
   231 +      const char *cjid = buddy_getjid(bud);
       
   232 +      if (cjid && !strcasecmp(cjid, bjid))
       
   233 +        active_window = TRUE;
       
   234 +    }
       
   235 +  }
       
   236 +
       
   237    // External command
       
   238    // - We do not call hk_ext_cmd() for history lines in MUC
       
   239    // - We do call hk_ext_cmd() for private messages in a room
       
   240    // - We do call hk_ext_cmd() for messages to the current window
       
   241    if (!active_window && ((is_groupchat && !timestamp) || !is_groupchat))
       
   242 -    hk_ext_cmd(ename ? ename : bjid, (is_groupchat ? 'G' : 'M'), 'R', wmsg);
       
   243 +    hk_ext_cmd(bjid, EXT_CMD_TYPE_MESSAGE,
       
   244 +               is_groupchat ? EXT_CMD_INFO_GROUPCHAT : EXT_CMD_INFO_RECEIVED,
       
   245 +               wmsg);
       
   246  
       
   247    // Beep, if enabled:
       
   248    // - if it's a private message
       
   249 @@ -494,7 +492,7 @@
       
   250  #endif
       
   251  
       
   252    // External command
       
   253 -  hk_ext_cmd(bjid, 'M', 'S', NULL);
       
   254 +  hk_ext_cmd(bjid, EXT_CMD_TYPE_MESSAGE, EXT_CMD_INFO_SENT, NULL);
       
   255  
       
   256    g_free(bmsg);
       
   257    g_free(mmsg);
       
   258 @@ -509,10 +507,7 @@
       
   259    char *bn;
       
   260    char *logsmsg;
       
   261    const char *rn = (resname ? resname : "");
       
   262 -  const char *ename = NULL;
       
   263 -
       
   264 -  if (settings_opt_get_int("eventcmd_use_nickname"))
       
   265 -    ename = roster_getname(bjid);
       
   266 +  char newstatus[2] = { '?', '\0' };
       
   267  
       
   268    oldstat = roster_getstatus(bjid, resname);
       
   269  
       
   270 @@ -564,27 +559,28 @@
       
   271    scr_draw_roster();
       
   272    hlog_write_status(bjid, timestamp, status, status_msg);
       
   273  
       
   274 +  newstatus[0] = imstatus2char[status];
       
   275 +
       
   276  #ifdef MODULES_ENABLE
       
   277    {
       
   278      char os[2] = " \0";
       
   279 -    char ns[2] = " \0";
       
   280      hk_arg_t args[] = {
       
   281        { "jid", bjid },
       
   282        { "resource", rn },
       
   283        { "old_status", os },
       
   284 -      { "new_status", ns },
       
   285 +      { "new_status", newstatus },
       
   286        { "message", status_msg ? status_msg : "" },
       
   287        { NULL, NULL },
       
   288      };
       
   289      os[0] = imstatus2char[oldstat];
       
   290 -    ns[0] = imstatus2char[status];
       
   291  
       
   292      hk_run_handlers(HOOK_STATUS_CHANGE, args);
       
   293    }
       
   294  #endif
       
   295  
       
   296    // External command
       
   297 -  hk_ext_cmd(ename ? ename : bjid, 'S', imstatus2char[status], NULL);
       
   298 +  newstatus[0] = toupper(newstatus[0]);
       
   299 +  hk_ext_cmd(bjid, EXT_CMD_TYPE_STATUS, newstatus, status_msg);
       
   300  }
       
   301  
       
   302  void hk_mystatuschange(time_t timestamp, enum imstatus old_status,
       
   303 @@ -711,7 +707,7 @@
       
   304    /* Call external command */
       
   305    str_unread = g_strdup_printf("%u %u %u %u", unread_count, attention_count,
       
   306                                 muc_unread, muc_attention);
       
   307 -  hk_ext_cmd("", 'U', (guchar)MIN(255, unread_count), str_unread);
       
   308 +  hk_ext_cmd("", EXT_CMD_TYPE_UNREAD, str_unread, NULL);
       
   309    g_free(str_unread);
       
   310  }
       
   311  
       
   312 @@ -751,123 +747,4 @@
       
   313    return 0;
       
   314  }
       
   315  
       
   316 -
       
   317 -/* External commands */
       
   318 -
       
   319 -//  hk_ext_cmd_init()
       
   320 -// Initialize external command variable.
       
   321 -// Can be called with parameter NULL to reset and free memory.
       
   322 -void hk_ext_cmd_init(const char *command)
       
   323 -{
       
   324 -  if (extcmd) {
       
   325 -    g_free(extcmd);
       
   326 -    extcmd = NULL;
       
   327 -  }
       
   328 -  if (command)
       
   329 -    extcmd = expand_filename(command);
       
   330 -}
       
   331 -
       
   332 -//  hk_ext_cmd()
       
   333 -// Launch an external command (process) for the given event.
       
   334 -// For now, data should be NULL.
       
   335 -void hk_ext_cmd(const char *bjid, guchar type, guchar info, const char *data)
       
   336 -{
       
   337 -  pid_t pid;
       
   338 -  const char *arg_type = NULL;
       
   339 -  const char *arg_info = NULL;
       
   340 -  const char *arg_data = NULL;
       
   341 -  char status_str[2];
       
   342 -  char *datafname = NULL;
       
   343 -
       
   344 -  if (!extcmd) return;
       
   345 -
       
   346 -  // Prepare arg_* (external command parameters)
       
   347 -  switch (type) {
       
   348 -    case 'M': /* Normal message */
       
   349 -        arg_type = "MSG";
       
   350 -        if (info == 'R')
       
   351 -          arg_info = "IN";
       
   352 -        else if (info == 'S')
       
   353 -          arg_info = "OUT";
       
   354 -        break;
       
   355 -    case 'G': /* Groupchat message */
       
   356 -        arg_type = "MSG";
       
   357 -        arg_info = "MUC";
       
   358 -        break;
       
   359 -    case 'S': /* Status change */
       
   360 -        arg_type = "STATUS";
       
   361 -        if (strchr(imstatus2char, tolower(info))) {
       
   362 -          status_str[0] = toupper(info);
       
   363 -          status_str[1] = 0;
       
   364 -          arg_info = status_str;
       
   365 -        }
       
   366 -        break;
       
   367 -    case 'U': /* Unread buffer count */
       
   368 -        arg_type = "UNREAD";
       
   369 -        arg_info = data;
       
   370 -        break;
       
   371 -    default:
       
   372 -        return;
       
   373 -  }
       
   374 -
       
   375 -  if (!arg_type || !arg_info) return;
       
   376 -
       
   377 -  if (strchr("MG", type) && data && settings_opt_get_int("event_log_files")) {
       
   378 -    int fd;
       
   379 -    const char *prefix;
       
   380 -    char *prefix_xp = NULL;
       
   381 -    char *data_locale;
       
   382 -
       
   383 -    data_locale = from_utf8(data);
       
   384 -    prefix = settings_opt_get("event_log_dir");
       
   385 -    if (prefix)
       
   386 -      prefix = prefix_xp = expand_filename(prefix);
       
   387 -    else
       
   388 -      prefix = ut_get_tmpdir();
       
   389 -    datafname = g_strdup_printf("%s/mcabber-%d.XXXXXX", prefix, getpid());
       
   390 -    g_free(prefix_xp);
       
   391 -
       
   392 -    // XXX Some old systems may require us to set umask first.
       
   393 -    fd = mkstemp(datafname);
       
   394 -    if (fd == -1) {
       
   395 -      g_free(datafname);
       
   396 -      datafname = NULL;
       
   397 -      scr_LogPrint(LPRINT_LOGNORM,
       
   398 -                   "Unable to create temp file for external command.");
       
   399 -    } else {
       
   400 -      size_t data_locale_len = strlen(data_locale);
       
   401 -      ssize_t a = write(fd, data_locale, data_locale_len);
       
   402 -      ssize_t b = write(fd, "\n", 1);
       
   403 -      if ((size_t)a != data_locale_len || b != 1) {
       
   404 -        g_free(datafname);
       
   405 -        datafname = NULL;
       
   406 -        scr_LogPrint(LPRINT_LOGNORM,
       
   407 -                     "Unable to write to temp file for external command.");
       
   408 -      }
       
   409 -      close(fd);
       
   410 -      arg_data = datafname;
       
   411 -    }
       
   412 -    g_free(data_locale);
       
   413 -  }
       
   414 -
       
   415 -  if ((pid=fork()) == -1) {
       
   416 -    scr_LogPrint(LPRINT_LOGNORM, "Fork error, cannot launch external command.");
       
   417 -    g_free(datafname);
       
   418 -    return;
       
   419 -  }
       
   420 -
       
   421 -  if (pid == 0) { // child
       
   422 -    // Close standard file descriptors
       
   423 -    close(STDIN_FILENO);
       
   424 -    close(STDOUT_FILENO);
       
   425 -    close(STDERR_FILENO);
       
   426 -    if (execl(extcmd, extcmd, arg_type, arg_info, bjid, arg_data,
       
   427 -              (char *)NULL) == -1) {
       
   428 -      // scr_LogPrint(LPRINT_LOGNORM, "Cannot execute external command.");
       
   429 -      exit(1);
       
   430 -    }
       
   431 -  }
       
   432 -  g_free(datafname);
       
   433 -}
       
   434 -
       
   435  /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2:  For Vim users... */
       
   436 diff -r 64da54766f99 mcabber/mcabber/hooks.h
       
   437 --- a/mcabber/mcabber/hooks.h	Mon Apr 30 23:36:31 2012 +0300
       
   438 +++ b/mcabber/mcabber/hooks.h	Mon Apr 30 23:36:55 2012 +0300
       
   439 @@ -66,9 +66,6 @@
       
   440  guint hk_subscription(LmMessageSubType mstype, const gchar *bjid,
       
   441                        const gchar *msg);
       
   442  
       
   443 -void hk_ext_cmd_init(const char *command);
       
   444 -void hk_ext_cmd(const char *bjid, guchar type, guchar info, const char *data);
       
   445 -
       
   446  #endif /* __MCABBER_HOOKS_H__ */
       
   447  
       
   448  /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2:  For Vim users... */
       
   449 diff -r 64da54766f99 mcabber/mcabber/main.c
       
   450 --- a/mcabber/mcabber/main.c	Mon Apr 30 23:36:31 2012 +0300
       
   451 +++ b/mcabber/mcabber/main.c	Mon Apr 30 23:36:55 2012 +0300
       
   452 @@ -44,6 +44,7 @@
       
   453  #include "xmpp.h"
       
   454  #include "help.h"
       
   455  #include "events.h"
       
   456 +#include "extcmd.h"
       
   457  
       
   458  #ifndef MODULES_ENABLE
       
   459  # include "fifo.h"