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