mcabber/src/hooks.c
changeset 1607 14690e624e9d
parent 1604 351427ef0b4b
child 1611 f9bf561e54d0
equal deleted inserted replaced
1606:d7f26538c24c 1607:14690e624e9d
    33 #include "settings.h"
    33 #include "settings.h"
    34 #include "utils.h"
    34 #include "utils.h"
    35 #include "utf8.h"
    35 #include "utf8.h"
    36 #include "commands.h"
    36 #include "commands.h"
    37 #include "fifo.h"
    37 #include "fifo.h"
       
    38 
       
    39 #ifdef MODULES_ENABLE
       
    40 #include <glib.h>
       
    41 
       
    42 typedef struct {
       
    43   hk_handler_t handler;
       
    44   gpointer     userdata;
       
    45 } hook_list_data_t;
       
    46 
       
    47 static GSList *hk_handler_queue = NULL;
       
    48 
       
    49 void hk_add_handler (hk_handler_t handler, gpointer userdata)
       
    50 {
       
    51   hook_list_data_t *h = g_new (hook_list_data_t, 1);
       
    52   h->handler  = handler;
       
    53   h->userdata = userdata;
       
    54   hk_handler_queue = g_slist_append (hk_handler_queue, h);
       
    55 }
       
    56 
       
    57 static gint hk_queue_search_cb (hook_list_data_t *a, hook_list_data_t *b)
       
    58 {
       
    59   if (a->handler == b->handler && a->userdata == b->userdata)
       
    60     return 0;
       
    61   else
       
    62     return 1;
       
    63 }
       
    64 
       
    65 void hk_del_handler (hk_handler_t handler, gpointer userdata)
       
    66 {
       
    67   hook_list_data_t h = { handler, userdata };
       
    68   GSList *el = g_slist_find_custom (hk_handler_queue, &h, (GCompareFunc) hk_queue_search_cb);
       
    69   if (el) {
       
    70     g_free (el->data);
       
    71     hk_handler_queue = g_slist_delete_link (hk_handler_queue, el);
       
    72   }
       
    73 }
       
    74 #endif
    38 
    75 
    39 static char *extcmd;
    76 static char *extcmd;
    40 
    77 
    41 static const char *COMMAND_ME = "/me ";
    78 static const char *COMMAND_ME = "/me ";
    42 
    79 
   207   }
   244   }
   208 
   245 
   209   if (settings_opt_get_int("eventcmd_use_nickname"))
   246   if (settings_opt_get_int("eventcmd_use_nickname"))
   210     ename = roster_getname(bjid);
   247     ename = roster_getname(bjid);
   211 
   248 
       
   249 #ifdef MODULES_ENABLE
       
   250   {
       
   251     GSList *h = hk_handler_queue;
       
   252     if (h) {
       
   253 #if 0
       
   254       hk_arg_t *args = g_new (hk_arg_t, 5);
       
   255       args[0].name = "hook";
       
   256       args[0].value = "hook-message-in";
       
   257       args[1].name = "jid";
       
   258       args[1].value = bjid;
       
   259       args[2].name = "message";
       
   260       args[2].value = wmsg;
       
   261       args[3].name = "groupchat";
       
   262       args[3].value = is_groupchat ? "true" : "false";
       
   263       args[4].name = NULL;
       
   264       args[4].value = NULL;
       
   265 #else
       
   266       // We can use a const array for keys/static values, so modules
       
   267       // can do fast known to them args check by just comparing pointers...
       
   268       hk_arg_t args[] = {
       
   269         { "hook", "hook-message-in" },
       
   270         { "jid", bjid },
       
   271         { "message", wmsg },
       
   272         { "groupchat", is_groupchat ? "true" : "false" },
       
   273         { NULL, NULL },
       
   274       };
       
   275 #endif
       
   276       while (h) {
       
   277         hook_list_data_t *data = h->data;
       
   278         (data->handler) (args, data->userdata);
       
   279         h = g_slist_next (h);
       
   280       }
       
   281     }
       
   282   }
       
   283 #endif
       
   284 
   212   // External command
   285   // External command
   213   // - We do not call hk_ext_cmd() for history lines in MUC
   286   // - We do not call hk_ext_cmd() for history lines in MUC
   214   // - We do call hk_ext_cmd() for private messages in a room
   287   // - We do call hk_ext_cmd() for private messages in a room
   215   // - We do call hk_ext_cmd() for messages to the current window
   288   // - We do call hk_ext_cmd() for messages to the current window
   216   if (!active_window && ((is_groupchat && !timestamp) || !is_groupchat))
   289   if (!active_window && ((is_groupchat && !timestamp) || !is_groupchat))
   284   scr_WriteOutgoingMessage(bjid, wmsg, cryptflag, xep184);
   357   scr_WriteOutgoingMessage(bjid, wmsg, cryptflag, xep184);
   285 
   358 
   286   // We don't log private messages
   359   // We don't log private messages
   287   if (!nick)
   360   if (!nick)
   288     hlog_write_message(bjid, timestamp, 1, msg);
   361     hlog_write_message(bjid, timestamp, 1, msg);
       
   362 
       
   363 #ifdef MODULES_ENABLE
       
   364   {
       
   365     GSList *h = hk_handler_queue;
       
   366     if (h) {
       
   367       hk_arg_t args[] = {
       
   368         { "hook", "hook-message-out" },
       
   369         { "jid", bjid },
       
   370         { "message", wmsg },
       
   371         { NULL, NULL },
       
   372       };
       
   373       while (h) {
       
   374         hook_list_data_t *data = h->data;
       
   375         (data->handler) (args, data->userdata);
       
   376         h = g_slist_next (h);
       
   377       }
       
   378     }
       
   379   }
       
   380 #endif
   289 
   381 
   290   // External command
   382   // External command
   291   hk_ext_cmd(bjid, 'M', 'S', NULL);
   383   hk_ext_cmd(bjid, 'M', 'S', NULL);
   292 
   384 
   293   g_free(bmsg);
   385   g_free(bmsg);
   355   roster_setstatus(bjid, rn, prio, status, status_msg, timestamp,
   447   roster_setstatus(bjid, rn, prio, status, status_msg, timestamp,
   356                    role_none, affil_none, NULL);
   448                    role_none, affil_none, NULL);
   357   buddylist_build();
   449   buddylist_build();
   358   scr_DrawRoster();
   450   scr_DrawRoster();
   359   hlog_write_status(bjid, timestamp, status, status_msg);
   451   hlog_write_status(bjid, timestamp, status, status_msg);
       
   452 
       
   453 #ifdef MODULES_ENABLE
       
   454   {
       
   455     GSList *h = hk_handler_queue;
       
   456     if (h) {
       
   457       char os[2] = " \0";
       
   458       char ns[2] = " \0";
       
   459       hk_arg_t args[] = {
       
   460         { "hook", "hook-status-change" },
       
   461         { "jid", bjid },
       
   462         { "resource", rn },
       
   463         { "old_status", os },
       
   464         { "new_status", ns },
       
   465         { "message", status_msg ? status_msg : "" },
       
   466         { NULL, NULL },
       
   467       };
       
   468       os[0] = imstatus2char[oldstat];
       
   469       ns[0] = imstatus2char[status];
       
   470       while (h) {
       
   471         hook_list_data_t *data = h->data;
       
   472         (data->handler) (args, data->userdata);
       
   473         h = g_slist_next (h);
       
   474       }
       
   475     }
       
   476   }
       
   477 #endif
       
   478 
   360   // External command
   479   // External command
   361   hk_ext_cmd(ename ? ename : bjid, 'S', imstatus2char[status], NULL);
   480   hk_ext_cmd(ename ? ename : bjid, 'S', imstatus2char[status], NULL);
   362 }
   481 }
   363 
   482 
   364 void hk_mystatuschange(time_t timestamp, enum imstatus old_status,
   483 void hk_mystatuschange(time_t timestamp, enum imstatus old_status,
   365                               enum imstatus new_status, const char *msg)
   484                               enum imstatus new_status, const char *msg)
   366 {
   485 {
   367   scr_LogPrint(LPRINT_LOGNORM, "Your status has been set: [%c>%c] %s",
   486   scr_LogPrint(LPRINT_LOGNORM, "Your status has been set: [%c>%c] %s",
   368                imstatus2char[old_status], imstatus2char[new_status],
   487                imstatus2char[old_status], imstatus2char[new_status],
   369                (msg ? msg : ""));
   488                (msg ? msg : ""));
       
   489 
       
   490 #ifdef MODULES_ENABLE
       
   491   {
       
   492     GSList *h = hk_handler_queue;
       
   493     if (h) {
       
   494       char ns[2] = " \0";
       
   495       hk_arg_t args[] = {
       
   496         { "hook", "hook-my-status-change" },
       
   497         { "new_status", ns },
       
   498         { "message", msg ? msg : "" },
       
   499         { NULL, NULL },
       
   500       };
       
   501       ns[0] = imstatus2char[new_status];
       
   502       while (h) {
       
   503         hook_list_data_t *data = h->data;
       
   504         (data->handler) (args, data->userdata);
       
   505         h = g_slist_next (h);
       
   506       }
       
   507     }
       
   508   }
       
   509 #endif
       
   510 
   370   //hlog_write_status(NULL, 0, status);
   511   //hlog_write_status(NULL, 0, status);
   371 }
   512 }
   372 
   513 
   373 
   514 
   374 /* Internal commands */
   515 /* Internal commands */
   387   scr_LogPrint(LPRINT_LOGNORM, "%s", buf);
   528   scr_LogPrint(LPRINT_LOGNORM, "%s", buf);
   388 
   529 
   389   cmdline = from_utf8(hook_command);
   530   cmdline = from_utf8(hook_command);
   390   if (process_command(cmdline, TRUE) == 255)
   531   if (process_command(cmdline, TRUE) == 255)
   391     mcabber_set_terminate_ui();
   532     mcabber_set_terminate_ui();
       
   533 
       
   534 #ifdef MODULES_ENABLE
       
   535   {
       
   536     GSList *h = hk_handler_queue;
       
   537     if (h) {
       
   538       hk_arg_t args[] = {
       
   539         { "hook", hookname },
       
   540         { NULL, NULL },
       
   541       };
       
   542       while (h) {
       
   543         hook_list_data_t *data = h->data;
       
   544         (data->handler) (args, data->userdata);
       
   545         h = g_slist_next (h);
       
   546       }
       
   547     }
       
   548   }
       
   549 #endif
   392 
   550 
   393   g_free(cmdline);
   551   g_free(cmdline);
   394   g_free(buf);
   552   g_free(buf);
   395 }
   553 }
   396 
   554