Mercurial > hg
annotate mcabber/src/hooks.c @ 328:83d129adde03
Add set_current_buddy() function, to reduce code duplication
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 17 Jul 2005 19:06:32 +0100 |
parents | ff6fb51bfd78 |
children | c5a7a7273986 |
rev | line source |
---|---|
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 |
160 | 32 static char *extcommand; |
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; |
ff6fb51bfd78
Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents:
318
diff
changeset
|
48 scr_LogPrint("Error message received from <%s>", jid); |
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); |
160 | 59 hk_ext_cmd(jid, 'M', 'R', NULL); |
146 | 60 // 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 (new_guy || | |
63 (roster_getstatus(jid) == offline && buddylist_get_hide_offline_buddies())) | |
64 { | |
141 | 65 buddylist_build(); |
66 update_roster = TRUE; | |
67 } | |
113 | 68 } |
69 | |
70 inline void hk_message_out(const char *jid, time_t timestamp, const char *msg) | |
71 { | |
72 scr_WriteOutgoingMessage(jid, msg); | |
73 hlog_write_message(jid, timestamp, TRUE, msg); | |
74 } | |
75 | |
76 inline void hk_statuschange(const char *jid, time_t timestamp, | |
221 | 77 enum imstatus status, const char *status_msg) |
113 | 78 { |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
79 scr_LogPrint("Buddy status has changed: [%c>%c] <%s> %s", |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
80 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
|
81 ((status_msg) ? status_msg : "")); |
221 | 82 roster_setstatus(jid, status, status_msg); |
120 | 83 buddylist_build(); |
84 scr_DrawRoster(); | |
221 | 85 hlog_write_status(jid, 0, status, status_msg); |
113 | 86 } |
87 | |
116 | 88 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
|
89 enum imstatus old_status, enum imstatus new_status, const char *msg) |
116 | 90 { |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
91 if (!msg && (old_status == new_status)) |
116 | 92 return; |
93 | |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
94 scr_LogPrint("Your status has changed: [%c>%c] %s", |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
95 imstatus2char[old_status], imstatus2char[new_status], |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
96 ((msg) ? msg : "")); |
116 | 97 //hlog_write_status(NULL, 0, status); |
98 } | |
99 | |
160 | 100 |
101 /* External commands */ | |
102 | |
103 // hk_ext_cmd_init() | |
104 // Initialize external command variable. | |
105 // 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
|
106 void hk_ext_cmd_init(const char *command) |
160 | 107 { |
108 if (extcommand) { | |
109 g_free(extcommand); | |
110 extcommand = NULL; | |
111 } | |
112 if (command) | |
113 extcommand = g_strdup(command); | |
114 } | |
115 | |
116 // hk_ext_cmd() | |
117 // Launch an external command (process) for the given event. | |
118 // For now, data should be NULL. | |
119 void hk_ext_cmd(const char *jid, guchar type, guchar info, const char *data) | |
120 { | |
121 pid_t pid; | |
122 | |
123 if (!extcommand) return; | |
124 | |
125 // For now we'll only handle incoming messages | |
126 if (type != 'M') return; | |
127 if (info != 'R') return; | |
128 | |
129 if ((pid=fork()) == -1) { | |
130 scr_LogPrint("Fork error, cannot launch external command."); | |
131 return; | |
132 } | |
133 | |
134 // I don't remember what I should do with the parent process... | |
135 if (pid == 0) { // child | |
136 if (execl(extcommand, extcommand, "MSG", "IN", jid, NULL) == -1) { | |
137 // ut_WriteLog("Cannot execute external command.\n"); | |
138 exit(1); | |
139 } | |
140 } | |
141 } | |
142 |