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