comparison mcabber/src/hooks.c @ 524:05c0e55c4bb1

Pass message body to external command Introduce 2 new options: event_log_files & event_log_dir
author Mikael Berthe <mikael@lilotux.net>
date Sun, 20 Nov 2005 00:08:49 +0100
parents e5008032edb8
children 56f641155579
comparison
equal deleted inserted replaced
523:fc6bc26f891e 524:05c0e55c4bb1
19 * USA 19 * USA
20 */ 20 */
21 21
22 #include <sys/types.h> 22 #include <sys/types.h>
23 #include <unistd.h> 23 #include <unistd.h>
24 #include <stdlib.h>
24 25
25 #include "hooks.h" 26 #include "hooks.h"
26 #include "screen.h" 27 #include "screen.h"
27 #include "roster.h" 28 #include "roster.h"
28 #include "histolog.h" 29 #include "histolog.h"
29 #include "hbuf.h" 30 #include "hbuf.h"
30 #include "settings.h" 31 #include "settings.h"
32 #include "utils.h"
31 33
32 static char *extcmd; 34 static char *extcmd;
33 35
34 inline void hk_message_in(const char *jid, const char *resname, 36 inline void hk_message_in(const char *jid, const char *resname,
35 time_t timestamp, const char *msg, const char *type) 37 time_t timestamp, const char *msg, const char *type)
99 101
100 // External command 102 // External command
101 // - We do not call hk_ext_cmd() for history lines in MUC 103 // - We do not call hk_ext_cmd() for history lines in MUC
102 // - We do call hk_ext_cmd() for private messages in a room 104 // - We do call hk_ext_cmd() for private messages in a room
103 if ((is_groupchat && !timestamp) || !is_groupchat) 105 if ((is_groupchat && !timestamp) || !is_groupchat)
104 hk_ext_cmd(jid, (is_groupchat ? 'G' : 'M'), 'R', NULL); 106 hk_ext_cmd(jid, (is_groupchat ? 'G' : 'M'), 'R', wmsg);
105 107
106 // We need to rebuild the list if the sender is unknown or 108 // We need to rebuild the list if the sender is unknown or
107 // if the sender is offline/invisible and hide_offline_buddies is set 109 // if the sender is offline/invisible and hide_offline_buddies is set
108 if (new_guy || 110 if (new_guy ||
109 (buddy_getstatus(roster_usr->data, NULL) == offline && 111 (buddy_getstatus(roster_usr->data, NULL) == offline &&
231 pid_t pid; 233 pid_t pid;
232 char *arg_type = NULL; 234 char *arg_type = NULL;
233 char *arg_info = NULL; 235 char *arg_info = NULL;
234 char *arg_data = NULL; 236 char *arg_data = NULL;
235 char status_str[2]; 237 char status_str[2];
238 char *datafname = NULL;
236 239
237 if (!extcmd) return; 240 if (!extcmd) return;
238 241
239 // Prepare arg_* (external command parameters) 242 // Prepare arg_* (external command parameters)
240 switch (type) { 243 switch (type) {
261 return; 264 return;
262 } 265 }
263 266
264 if (!arg_type || !arg_info) return; 267 if (!arg_type || !arg_info) return;
265 268
269 if (strchr("MG", type) && data && settings_opt_get_int("event_log_files")) {
270 int fd;
271 const char *prefix;
272 prefix = settings_opt_get("event_log_dir");
273 if (!prefix)
274 prefix = ut_get_tmpdir();
275 datafname = g_strdup_printf("%s/mcabber-%d.XXXXXX", prefix, getpid());
276 // XXX Some old systems may require us to set umask first.
277 fd = mkstemp(datafname);
278 if (fd == -1) {
279 g_free(datafname);
280 datafname = NULL;
281 scr_LogPrint(LPRINT_LOGNORM,
282 "Unable to create temp file for external command.");
283 }
284 write(fd, data, strlen(data));
285 write(fd, "\n", 1);
286 close(fd);
287 arg_data = datafname;
288 }
289
266 if ((pid=fork()) == -1) { 290 if ((pid=fork()) == -1) {
267 scr_LogPrint(LPRINT_LOGNORM, "Fork error, cannot launch external command."); 291 scr_LogPrint(LPRINT_LOGNORM, "Fork error, cannot launch external command.");
292 if (datafname)
293 g_free(datafname);
268 return; 294 return;
269 } 295 }
270 296
271 if (pid == 0) { // child 297 if (pid == 0) { // child
272 if (execl(extcmd, extcmd, arg_type, arg_info, jid, arg_data, NULL) == -1) { 298 if (execl(extcmd, extcmd, arg_type, arg_info, jid, arg_data, NULL) == -1) {
273 // scr_LogPrint(LPRINT_LOGNORM, "Cannot execute external command."); 299 // scr_LogPrint(LPRINT_LOGNORM, "Cannot execute external command.");
274 exit(1); 300 exit(1);
275 } 301 }
276 } 302 }
277 } 303 if (datafname)
278 304 g_free(datafname);
305 }