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" |
113
|
30 |
|
160
|
31 |
static char *extcommand; |
113
|
32 |
|
|
33 |
inline void hk_message_in(const char *jid, time_t timestamp, const char *msg) |
|
34 |
{ |
115
|
35 |
char *buffer = utf8_decode(msg); |
141
|
36 |
int new_guy = FALSE; |
|
37 |
|
|
38 |
// If this user isn't in the roster, we add it |
|
39 |
if (!roster_exists(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT)) { |
|
40 |
roster_add_user(jid, NULL, NULL, ROSTER_TYPE_USER); |
|
41 |
new_guy = TRUE; |
|
42 |
} |
|
43 |
|
115
|
44 |
scr_WriteIncomingMessage(jid, buffer); |
|
45 |
hlog_write_message(jid, timestamp, FALSE, buffer); |
160
|
46 |
hk_ext_cmd(jid, 'M', 'R', NULL); |
115
|
47 |
free(buffer); |
146
|
48 |
// We need to rebuild the list if the sender is unknown or |
|
49 |
// if the sender is offline/invisible and hide_offline_buddies is set |
|
50 |
if (new_guy || |
|
51 |
(roster_getstatus(jid) == offline && buddylist_get_hide_offline_buddies())) |
|
52 |
{ |
141
|
53 |
buddylist_build(); |
|
54 |
update_roster = TRUE; |
|
55 |
} |
113
|
56 |
} |
|
57 |
|
|
58 |
inline void hk_message_out(const char *jid, time_t timestamp, const char *msg) |
|
59 |
{ |
|
60 |
scr_WriteOutgoingMessage(jid, msg); |
|
61 |
hlog_write_message(jid, timestamp, TRUE, msg); |
|
62 |
} |
|
63 |
|
|
64 |
inline void hk_statuschange(const char *jid, time_t timestamp, |
|
65 |
enum imstatus status) |
|
66 |
{ |
|
67 |
scr_LogPrint("Buddy status has changed: [%c>%c] <%s>", |
|
68 |
imstatus2char[roster_getstatus(jid)], imstatus2char[status], jid); |
|
69 |
roster_setstatus(jid, status); |
120
|
70 |
buddylist_build(); |
|
71 |
scr_DrawRoster(); |
113
|
72 |
hlog_write_status(jid, 0, status); |
|
73 |
} |
|
74 |
|
116
|
75 |
inline void hk_mystatuschange(time_t timestamp, |
|
76 |
enum imstatus old_status, enum imstatus new_status) |
|
77 |
{ |
|
78 |
if (old_status == new_status) |
|
79 |
return; |
|
80 |
|
118
|
81 |
scr_LogPrint("Your status has changed: [%c>%c]", |
116
|
82 |
imstatus2char[old_status], imstatus2char[new_status]); |
|
83 |
//hlog_write_status(NULL, 0, status); |
|
84 |
} |
|
85 |
|
160
|
86 |
|
|
87 |
/* External commands */ |
|
88 |
|
|
89 |
// hk_ext_cmd_init() |
|
90 |
// Initialize external command variable. |
|
91 |
// Can be called with parameter NULL to reset and free memory. |
|
92 |
void hk_ext_cmd_init(char *command) |
|
93 |
{ |
|
94 |
if (extcommand) { |
|
95 |
g_free(extcommand); |
|
96 |
extcommand = NULL; |
|
97 |
} |
|
98 |
if (command) |
|
99 |
extcommand = g_strdup(command); |
|
100 |
} |
|
101 |
|
|
102 |
// hk_ext_cmd() |
|
103 |
// Launch an external command (process) for the given event. |
|
104 |
// For now, data should be NULL. |
|
105 |
void hk_ext_cmd(const char *jid, guchar type, guchar info, const char *data) |
|
106 |
{ |
|
107 |
pid_t pid; |
|
108 |
|
|
109 |
if (!extcommand) return; |
|
110 |
|
|
111 |
// For now we'll only handle incoming messages |
|
112 |
if (type != 'M') return; |
|
113 |
if (info != 'R') return; |
|
114 |
|
|
115 |
if ((pid=fork()) == -1) { |
|
116 |
scr_LogPrint("Fork error, cannot launch external command."); |
|
117 |
return; |
|
118 |
} |
|
119 |
|
|
120 |
// I don't remember what I should do with the parent process... |
|
121 |
if (pid == 0) { // child |
|
122 |
if (execl(extcommand, extcommand, "MSG", "IN", jid, NULL) == -1) { |
|
123 |
// ut_WriteLog("Cannot execute external command.\n"); |
|
124 |
exit(1); |
|
125 |
} |
|
126 |
} |
|
127 |
} |
|
128 |
|