Mercurial > hg
annotate mcabber/src/hooks.c @ 322:da138cdebf04
Implement auto-away mode
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Fri, 15 Jul 2005 22:08:53 +0100 |
parents | 45076d02eeef |
children | ff6fb51bfd78 |
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" |
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 { |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
68 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
|
69 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
|
70 ((status_msg) ? status_msg : "")); |
221 | 71 roster_setstatus(jid, status, status_msg); |
120 | 72 buddylist_build(); |
73 scr_DrawRoster(); | |
221 | 74 hlog_write_status(jid, 0, status, status_msg); |
113 | 75 } |
76 | |
116 | 77 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
|
78 enum imstatus old_status, enum imstatus new_status, const char *msg) |
116 | 79 { |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
80 if (!msg && (old_status == new_status)) |
116 | 81 return; |
82 | |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
83 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
|
84 imstatus2char[old_status], imstatus2char[new_status], |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
85 ((msg) ? msg : "")); |
116 | 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. | |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
221
diff
changeset
|
95 void hk_ext_cmd_init(const char *command) |
160 | 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 |