author | Mikael Berthe <mikael@lilotux.net> |
Mon, 25 Jul 2005 21:46:35 +0100 | |
changeset 374 | bd5638c21834 |
parent 364 | 33b8e801ffa6 |
child 378 | 2e6c7b1440d1 |
permissions | -rw-r--r-- |
113 | 1 |
/* |
2 |
* hooks.c -- Hooks layer |
|
3 |
* |
|
4 |
* Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net> |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation; either version 2 of the License, or (at |
|
9 |
* your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but |
|
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|
19 |
* USA |
|
20 |
*/ |
|
21 |
||
160 | 22 |
#include <sys/types.h> |
23 |
#include <unistd.h> |
|
113 | 24 |
#include <screen.h> |
25 |
||
26 |
#include "hooks.h" |
|
27 |
#include "roster.h" |
|
28 |
#include "histolog.h" |
|
115 | 29 |
#include "utf8.h" |
325
ff6fb51bfd78
Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents:
318
diff
changeset
|
30 |
#include "hbuf.h" |
113 | 31 |
|
355
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
32 |
static char *extcmd; |
113 | 33 |
|
325
ff6fb51bfd78
Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents:
318
diff
changeset
|
34 |
inline void hk_message_in(const char *jid, time_t timestamp, const char *msg, |
ff6fb51bfd78
Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents:
318
diff
changeset
|
35 |
const char *type) |
113 | 36 |
{ |
141 | 37 |
int new_guy = FALSE; |
325
ff6fb51bfd78
Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents:
318
diff
changeset
|
38 |
int message_flags; |
141 | 39 |
|
40 |
// If this user isn't in the roster, we add it |
|
41 |
if (!roster_exists(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT)) { |
|
42 |
roster_add_user(jid, NULL, NULL, ROSTER_TYPE_USER); |
|
43 |
new_guy = TRUE; |
|
44 |
} |
|
45 |
||
325
ff6fb51bfd78
Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents:
318
diff
changeset
|
46 |
if (type && !strcmp(type, "error")) { |
ff6fb51bfd78
Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents:
318
diff
changeset
|
47 |
message_flags = HBB_PREFIX_ERR | HBB_PREFIX_IN; |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
48 |
scr_LogPrint(LPRINT_LOGNORM, "Error message received from <%s>", jid); |
325
ff6fb51bfd78
Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents:
318
diff
changeset
|
49 |
} else |
ff6fb51bfd78
Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents:
318
diff
changeset
|
50 |
message_flags = 0; |
ff6fb51bfd78
Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents:
318
diff
changeset
|
51 |
|
178 | 52 |
// Note: the hlog_write should not be called first, because in some |
53 |
// cases scr_WriteIncomingMessage() will load the history and we'd |
|
54 |
// have the message twice... |
|
325
ff6fb51bfd78
Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents:
318
diff
changeset
|
55 |
scr_WriteIncomingMessage(jid, msg, timestamp, message_flags); |
ff6fb51bfd78
Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents:
318
diff
changeset
|
56 |
// We don't log the message if it is an error message |
ff6fb51bfd78
Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents:
318
diff
changeset
|
57 |
if (!(message_flags & HBB_PREFIX_ERR)) |
ff6fb51bfd78
Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents:
318
diff
changeset
|
58 |
hlog_write_message(jid, timestamp, FALSE, msg); |
355
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
59 |
// External command |
160 | 60 |
hk_ext_cmd(jid, 'M', 'R', NULL); |
146 | 61 |
// We need to rebuild the list if the sender is unknown or |
62 |
// if the sender is offline/invisible and hide_offline_buddies is set |
|
63 |
if (new_guy || |
|
64 |
(roster_getstatus(jid) == offline && buddylist_get_hide_offline_buddies())) |
|
65 |
{ |
|
141 | 66 |
buddylist_build(); |
67 |
update_roster = TRUE; |
|
68 |
} |
|
113 | 69 |
} |
70 |
||
71 |
inline void hk_message_out(const char *jid, time_t timestamp, const char *msg) |
|
72 |
{ |
|
73 |
scr_WriteOutgoingMessage(jid, msg); |
|
74 |
hlog_write_message(jid, timestamp, TRUE, msg); |
|
355
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
75 |
// External command |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
76 |
hk_ext_cmd(jid, 'M', 'S', NULL); |
113 | 77 |
} |
78 |
||
79 |
inline void hk_statuschange(const char *jid, time_t timestamp, |
|
221 | 80 |
enum imstatus status, const char *status_msg) |
113 | 81 |
{ |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
82 |
scr_LogPrint(LPRINT_LOGNORM, "Buddy status has changed: [%c>%c] <%s> %s", |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
83 |
imstatus2char[roster_getstatus(jid)], imstatus2char[status], jid, |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
84 |
((status_msg) ? status_msg : "")); |
221 | 85 |
roster_setstatus(jid, status, status_msg); |
120 | 86 |
buddylist_build(); |
87 |
scr_DrawRoster(); |
|
221 | 88 |
hlog_write_status(jid, 0, status, status_msg); |
355
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
89 |
// External command |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
90 |
hk_ext_cmd(jid, 'S', imstatus2char[status], NULL); |
113 | 91 |
} |
92 |
||
116 | 93 |
inline void hk_mystatuschange(time_t timestamp, |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
94 |
enum imstatus old_status, enum imstatus new_status, const char *msg) |
116 | 95 |
{ |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
96 |
if (!msg && (old_status == new_status)) |
116 | 97 |
return; |
98 |
||
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
99 |
scr_LogPrint(LPRINT_LOGNORM, "Your status has changed: [%c>%c] %s", |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
100 |
imstatus2char[old_status], imstatus2char[new_status], |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
101 |
((msg) ? msg : "")); |
116 | 102 |
//hlog_write_status(NULL, 0, status); |
103 |
} |
|
104 |
||
160 | 105 |
|
106 |
/* External commands */ |
|
107 |
||
108 |
// hk_ext_cmd_init() |
|
109 |
// Initialize external command variable. |
|
110 |
// Can be called with parameter NULL to reset and free memory. |
|
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
221
diff
changeset
|
111 |
void hk_ext_cmd_init(const char *command) |
160 | 112 |
{ |
355
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
113 |
if (extcmd) { |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
114 |
g_free(extcmd); |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
115 |
extcmd = NULL; |
160 | 116 |
} |
117 |
if (command) |
|
355
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
118 |
extcmd = g_strdup(command); |
160 | 119 |
} |
120 |
||
121 |
// hk_ext_cmd() |
|
122 |
// Launch an external command (process) for the given event. |
|
123 |
// For now, data should be NULL. |
|
124 |
void hk_ext_cmd(const char *jid, guchar type, guchar info, const char *data) |
|
125 |
{ |
|
126 |
pid_t pid; |
|
355
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
127 |
char *arg_type = NULL; |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
128 |
char *arg_info = NULL; |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
129 |
char *arg_data = NULL; |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
130 |
char status_str[2]; |
160 | 131 |
|
355
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
132 |
if (!extcmd) return; |
160 | 133 |
|
355
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
134 |
// Prepare arg_* (external command parameters) |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
135 |
switch (type) { |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
136 |
case 'M': |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
137 |
arg_type = "MSG"; |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
138 |
if (info == 'R') |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
139 |
arg_info = "IN"; |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
140 |
else if (info == 'S') |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
141 |
arg_info = "OUT"; |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
142 |
|
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
143 |
break; |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
144 |
case 'S': |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
145 |
arg_type = "STATUS"; |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
146 |
if (strchr(imstatus2char, tolower(info))) { |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
147 |
status_str[0] = toupper(info); |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
148 |
status_str[1] = 0; |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
149 |
arg_info = status_str; |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
150 |
} |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
151 |
break; |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
152 |
default: |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
153 |
return; |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
154 |
} |
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
155 |
|
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
156 |
if (!arg_type || !arg_info) return; |
160 | 157 |
|
158 |
if ((pid=fork()) == -1) { |
|
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
159 |
scr_LogPrint(LPRINT_LOGNORM, "Fork error, cannot launch external command."); |
160 | 160 |
return; |
161 |
} |
|
162 |
||
163 |
if (pid == 0) { // child |
|
355
c5a7a7273986
Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents:
325
diff
changeset
|
164 |
if (execl(extcmd, extcmd, arg_type, arg_info, jid, arg_data) == -1) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
165 |
// scr_LogPrint(LPRINT_LOGNORM, "Cannot execute external command."); |
160 | 166 |
exit(1); |
167 |
} |
|
168 |
} |
|
169 |
} |
|
170 |