comparison mcabber/src/hooks.c @ 355:c5a7a7273986

Add some external actions Now the external command can get the following parameters: - MSG IN jid - MSG OUT jid - STATUS $S jid ($S == status uppercase letter: O, I, F, D, N, A, _)
author Mikael Berthe <mikael@lilotux.net>
date Fri, 22 Jul 2005 21:45:45 +0100
parents ff6fb51bfd78
children 33b8e801ffa6
comparison
equal deleted inserted replaced
354:80ccf8e6c23f 355:c5a7a7273986
27 #include "roster.h" 27 #include "roster.h"
28 #include "histolog.h" 28 #include "histolog.h"
29 #include "utf8.h" 29 #include "utf8.h"
30 #include "hbuf.h" 30 #include "hbuf.h"
31 31
32 static char *extcommand; 32 static char *extcmd;
33 33
34 inline void hk_message_in(const char *jid, time_t timestamp, const char *msg, 34 inline void hk_message_in(const char *jid, time_t timestamp, const char *msg,
35 const char *type) 35 const char *type)
36 { 36 {
37 int new_guy = FALSE; 37 int new_guy = FALSE;
54 // have the message twice... 54 // have the message twice...
55 scr_WriteIncomingMessage(jid, msg, timestamp, message_flags); 55 scr_WriteIncomingMessage(jid, msg, timestamp, message_flags);
56 // We don't log the message if it is an error message 56 // We don't log the message if it is an error message
57 if (!(message_flags & HBB_PREFIX_ERR)) 57 if (!(message_flags & HBB_PREFIX_ERR))
58 hlog_write_message(jid, timestamp, FALSE, msg); 58 hlog_write_message(jid, timestamp, FALSE, msg);
59 // External command
59 hk_ext_cmd(jid, 'M', 'R', NULL); 60 hk_ext_cmd(jid, 'M', 'R', NULL);
60 // We need to rebuild the list if the sender is unknown or 61 // We need to rebuild the list if the sender is unknown or
61 // if the sender is offline/invisible and hide_offline_buddies is set 62 // if the sender is offline/invisible and hide_offline_buddies is set
62 if (new_guy || 63 if (new_guy ||
63 (roster_getstatus(jid) == offline && buddylist_get_hide_offline_buddies())) 64 (roster_getstatus(jid) == offline && buddylist_get_hide_offline_buddies()))
69 70
70 inline void hk_message_out(const char *jid, time_t timestamp, const char *msg) 71 inline void hk_message_out(const char *jid, time_t timestamp, const char *msg)
71 { 72 {
72 scr_WriteOutgoingMessage(jid, msg); 73 scr_WriteOutgoingMessage(jid, msg);
73 hlog_write_message(jid, timestamp, TRUE, msg); 74 hlog_write_message(jid, timestamp, TRUE, msg);
75 // External command
76 hk_ext_cmd(jid, 'M', 'S', NULL);
74 } 77 }
75 78
76 inline void hk_statuschange(const char *jid, time_t timestamp, 79 inline void hk_statuschange(const char *jid, time_t timestamp,
77 enum imstatus status, const char *status_msg) 80 enum imstatus status, const char *status_msg)
78 { 81 {
81 ((status_msg) ? status_msg : "")); 84 ((status_msg) ? status_msg : ""));
82 roster_setstatus(jid, status, status_msg); 85 roster_setstatus(jid, status, status_msg);
83 buddylist_build(); 86 buddylist_build();
84 scr_DrawRoster(); 87 scr_DrawRoster();
85 hlog_write_status(jid, 0, status, status_msg); 88 hlog_write_status(jid, 0, status, status_msg);
89 // External command
90 hk_ext_cmd(jid, 'S', imstatus2char[status], NULL);
86 } 91 }
87 92
88 inline void hk_mystatuschange(time_t timestamp, 93 inline void hk_mystatuschange(time_t timestamp,
89 enum imstatus old_status, enum imstatus new_status, const char *msg) 94 enum imstatus old_status, enum imstatus new_status, const char *msg)
90 { 95 {
103 // hk_ext_cmd_init() 108 // hk_ext_cmd_init()
104 // Initialize external command variable. 109 // Initialize external command variable.
105 // Can be called with parameter NULL to reset and free memory. 110 // Can be called with parameter NULL to reset and free memory.
106 void hk_ext_cmd_init(const char *command) 111 void hk_ext_cmd_init(const char *command)
107 { 112 {
108 if (extcommand) { 113 if (extcmd) {
109 g_free(extcommand); 114 g_free(extcmd);
110 extcommand = NULL; 115 extcmd = NULL;
111 } 116 }
112 if (command) 117 if (command)
113 extcommand = g_strdup(command); 118 extcmd = g_strdup(command);
114 } 119 }
115 120
116 // hk_ext_cmd() 121 // hk_ext_cmd()
117 // Launch an external command (process) for the given event. 122 // Launch an external command (process) for the given event.
118 // For now, data should be NULL. 123 // For now, data should be NULL.
119 void hk_ext_cmd(const char *jid, guchar type, guchar info, const char *data) 124 void hk_ext_cmd(const char *jid, guchar type, guchar info, const char *data)
120 { 125 {
121 pid_t pid; 126 pid_t pid;
127 char *arg_type = NULL;
128 char *arg_info = NULL;
129 char *arg_data = NULL;
130 char status_str[2];
122 131
123 if (!extcommand) return; 132 if (!extcmd) return;
124 133
125 // For now we'll only handle incoming messages 134 // Prepare arg_* (external command parameters)
126 if (type != 'M') return; 135 switch (type) {
127 if (info != 'R') return; 136 case 'M':
137 arg_type = "MSG";
138 if (info == 'R')
139 arg_info = "IN";
140 else if (info == 'S')
141 arg_info = "OUT";
142
143 break;
144 case 'S':
145 arg_type = "STATUS";
146 if (strchr(imstatus2char, tolower(info))) {
147 status_str[0] = toupper(info);
148 status_str[1] = 0;
149 arg_info = status_str;
150 }
151 break;
152 default:
153 return;
154 }
155
156 if (!arg_type || !arg_info) return;
128 157
129 if ((pid=fork()) == -1) { 158 if ((pid=fork()) == -1) {
130 scr_LogPrint("Fork error, cannot launch external command."); 159 scr_LogPrint("Fork error, cannot launch external command.");
131 return; 160 return;
132 } 161 }
133 162
134 // I don't remember what I should do with the parent process...
135 if (pid == 0) { // child 163 if (pid == 0) { // child
136 if (execl(extcommand, extcommand, "MSG", "IN", jid, NULL) == -1) { 164 if (execl(extcmd, extcmd, arg_type, arg_info, jid, arg_data) == -1) {
137 // ut_WriteLog("Cannot execute external command.\n"); 165 // ut_WriteLog("Cannot execute external command.\n");
138 exit(1); 166 exit(1);
139 } 167 }
140 } 168 }
141 } 169 }