comparison mcabber/src/hooks.c @ 1058:c0d44a9a99bc

Code cleanup Cosmetics. Mostly get rid of "jid" variables, as it is a structure pointer defined in libjabber/jabber.h.
author Mikael Berthe <mikael@lilotux.net>
date Thu, 30 Nov 2006 19:51:09 +0100
parents 6eb1efea75d0
children 1db3d045e95c
comparison
equal deleted inserted replaced
1057:4cdf19d9c74e 1058:c0d44a9a99bc
31 #include "settings.h" 31 #include "settings.h"
32 #include "utils.h" 32 #include "utils.h"
33 33
34 static char *extcmd; 34 static char *extcmd;
35 35
36 inline void hk_message_in(const char *jid, const char *resname, 36 inline void hk_message_in(const char *bjid, const char *resname,
37 time_t timestamp, const char *msg, const char *type, 37 time_t timestamp, const char *msg, const char *type,
38 guint encrypted) 38 guint encrypted)
39 { 39 {
40 int new_guy = FALSE; 40 int new_guy = FALSE;
41 int is_groupchat = FALSE; // groupchat message 41 int is_groupchat = FALSE; // groupchat message
63 wmsg = bmsg; 63 wmsg = bmsg;
64 if (!strncmp(msg, mkcmdstr("me "), strlen(mkcmdstr("me ")))) 64 if (!strncmp(msg, mkcmdstr("me "), strlen(mkcmdstr("me "))))
65 wmsg = mmsg = g_strdup_printf("*%s %s", resname, msg+4); 65 wmsg = mmsg = g_strdup_printf("*%s %s", resname, msg+4);
66 } else { 66 } else {
67 if (!strncmp(msg, mkcmdstr("me "), strlen(mkcmdstr("me ")))) 67 if (!strncmp(msg, mkcmdstr("me "), strlen(mkcmdstr("me "))))
68 wmsg = mmsg = g_strdup_printf("*%s %s", jid, msg+4); 68 wmsg = mmsg = g_strdup_printf("*%s %s", bjid, msg+4);
69 else 69 else
70 wmsg = (char*) msg; 70 wmsg = (char*) msg;
71 } 71 }
72 72
73 // If this user isn't in the roster, we add it 73 // If this user isn't in the roster, we add it
74 roster_usr = roster_find(jid, jidsearch, 0); 74 roster_usr = roster_find(bjid, jidsearch, 0);
75 if (!roster_usr) { 75 if (!roster_usr) {
76 new_guy = TRUE; 76 new_guy = TRUE;
77 roster_usr = roster_add_user(jid, NULL, NULL, rtype, sub_none); 77 roster_usr = roster_add_user(bjid, NULL, NULL, rtype, sub_none);
78 if (!roster_usr) { // Shouldn't happen... 78 if (!roster_usr) { // Shouldn't happen...
79 scr_LogPrint(LPRINT_LOGNORM, "ERROR: unable to add buddy!"); 79 scr_LogPrint(LPRINT_LOGNORM, "ERROR: unable to add buddy!");
80 g_free(bmsg); 80 g_free(bmsg);
81 g_free(mmsg); 81 g_free(mmsg);
82 return; 82 return;
109 } 109 }
110 } 110 }
111 111
112 if (type && !strcmp(type, "error")) { 112 if (type && !strcmp(type, "error")) {
113 message_flags = HBB_PREFIX_ERR | HBB_PREFIX_IN; 113 message_flags = HBB_PREFIX_ERR | HBB_PREFIX_IN;
114 scr_LogPrint(LPRINT_LOGNORM, "Error message received from <%s>", jid); 114 scr_LogPrint(LPRINT_LOGNORM, "Error message received from <%s>", bjid);
115 } 115 }
116 116
117 // Note: the hlog_write should not be called first, because in some 117 // Note: the hlog_write should not be called first, because in some
118 // cases scr_WriteIncomingMessage() will load the history and we'd 118 // cases scr_WriteIncomingMessage() will load the history and we'd
119 // have the message twice... 119 // have the message twice...
120 scr_WriteIncomingMessage(jid, wmsg, timestamp, message_flags); 120 scr_WriteIncomingMessage(bjid, wmsg, timestamp, message_flags);
121 121
122 // We don't log the modified message, but the original one 122 // We don't log the modified message, but the original one
123 if (wmsg == mmsg) 123 if (wmsg == mmsg)
124 wmsg = bmsg; 124 wmsg = bmsg;
125 125
127 // - We don't log the message if it is a private conf. message 127 // - We don't log the message if it is a private conf. message
128 // - We don't log the message if it is groupchat message and the log_muc_conf 128 // - We don't log the message if it is groupchat message and the log_muc_conf
129 // option is off (and it is not a history line) 129 // option is off (and it is not a history line)
130 if (!(message_flags & HBB_PREFIX_ERR) && 130 if (!(message_flags & HBB_PREFIX_ERR) &&
131 (!is_room || (is_groupchat && log_muc_conf && !timestamp))) 131 (!is_room || (is_groupchat && log_muc_conf && !timestamp)))
132 hlog_write_message(jid, timestamp, FALSE, wmsg); 132 hlog_write_message(bjid, timestamp, FALSE, wmsg);
133 133
134 // External command 134 // External command
135 // - We do not call hk_ext_cmd() for history lines in MUC 135 // - We do not call hk_ext_cmd() for history lines in MUC
136 // - We do call hk_ext_cmd() for private messages in a room 136 // - We do call hk_ext_cmd() for private messages in a room
137 if ((is_groupchat && !timestamp) || !is_groupchat) 137 if ((is_groupchat && !timestamp) || !is_groupchat)
138 hk_ext_cmd(jid, (is_groupchat ? 'G' : 'M'), 'R', wmsg); 138 hk_ext_cmd(bjid, (is_groupchat ? 'G' : 'M'), 'R', wmsg);
139 139
140 // Display the sender in the log window 140 // Display the sender in the log window
141 if ((!is_groupchat) && !(message_flags & HBB_PREFIX_ERR) && 141 if ((!is_groupchat) && !(message_flags & HBB_PREFIX_ERR) &&
142 settings_opt_get_int("log_display_sender")) { 142 settings_opt_get_int("log_display_sender")) {
143 const char *name = roster_getname(jid); 143 const char *name = roster_getname(bjid);
144 if (!name) name = ""; 144 if (!name) name = "";
145 scr_LogPrint(LPRINT_NORMAL, "Message received from %s <%s/%s>", 145 scr_LogPrint(LPRINT_NORMAL, "Message received from %s <%s/%s>",
146 name, jid, (resname ? resname : "")); 146 name, bjid, (resname ? resname : ""));
147 } 147 }
148 148
149 // Beep, if enabled 149 // Beep, if enabled
150 if ((!is_groupchat) && !(message_flags & HBB_PREFIX_ERR) && 150 if ((!is_groupchat) && !(message_flags & HBB_PREFIX_ERR) &&
151 settings_opt_get_int("beep_on_message")) { 151 settings_opt_get_int("beep_on_message")) {
166 } 166 }
167 167
168 // hk_message_out() 168 // hk_message_out()
169 // nick should be set for private messages in a chat room, and null for 169 // nick should be set for private messages in a chat room, and null for
170 // normal messages. 170 // normal messages.
171 inline void hk_message_out(const char *jid, const char *nick, 171 inline void hk_message_out(const char *bjid, const char *nick,
172 time_t timestamp, const char *msg, guint encrypted) 172 time_t timestamp, const char *msg, guint encrypted)
173 { 173 {
174 char *wmsg = NULL, *bmsg = NULL, *mmsg = NULL; 174 char *wmsg = NULL, *bmsg = NULL, *mmsg = NULL;
175 175
176 if (nick) { 176 if (nick) {
186 } 186 }
187 187
188 // Note: the hlog_write should not be called first, because in some 188 // Note: the hlog_write should not be called first, because in some
189 // cases scr_WriteOutgoingMessage() will load the history and we'd 189 // cases scr_WriteOutgoingMessage() will load the history and we'd
190 // have the message twice... 190 // have the message twice...
191 scr_WriteOutgoingMessage(jid, wmsg, (encrypted ? HBB_PREFIX_PGPCRYPT : 0)); 191 scr_WriteOutgoingMessage(bjid, wmsg, (encrypted ? HBB_PREFIX_PGPCRYPT : 0));
192 192
193 // We don't log private messages 193 // We don't log private messages
194 if (!nick) hlog_write_message(jid, timestamp, TRUE, msg); 194 if (!nick) hlog_write_message(bjid, timestamp, TRUE, msg);
195 195
196 // External command 196 // External command
197 hk_ext_cmd(jid, 'M', 'S', NULL); 197 hk_ext_cmd(bjid, 'M', 'S', NULL);
198 198
199 g_free(bmsg); 199 g_free(bmsg);
200 g_free(mmsg); 200 g_free(mmsg);
201 } 201 }
202 202
203 inline void hk_statuschange(const char *jid, const char *resname, gchar prio, 203 inline void hk_statuschange(const char *bjid, const char *resname, gchar prio,
204 time_t timestamp, enum imstatus status, 204 time_t timestamp, enum imstatus status,
205 const char *status_msg) 205 const char *status_msg)
206 { 206 {
207 int buddy_format; 207 int buddy_format;
208 int st_in_buf; 208 int st_in_buf;
212 const char *rn = (resname ? resname : ""); 212 const char *rn = (resname ? resname : "");
213 213
214 st_in_buf = settings_opt_get_int("show_status_in_buffer"); 214 st_in_buf = settings_opt_get_int("show_status_in_buffer");
215 buddy_format = settings_opt_get_int("buddy_format"); 215 buddy_format = settings_opt_get_int("buddy_format");
216 if (buddy_format) { 216 if (buddy_format) {
217 const char *name = roster_getname(jid); 217 const char *name = roster_getname(bjid);
218 if (name && strcmp(name, jid)) { 218 if (name && strcmp(name, bjid)) {
219 if (buddy_format == 1) 219 if (buddy_format == 1)
220 bn = g_strdup_printf("%s <%s/%s>", name, jid, rn); 220 bn = g_strdup_printf("%s <%s/%s>", name, bjid, rn);
221 else if (buddy_format == 2) 221 else if (buddy_format == 2)
222 bn = g_strdup_printf("%s/%s", name, rn); 222 bn = g_strdup_printf("%s/%s", name, rn);
223 else if (buddy_format == 3) 223 else if (buddy_format == 3)
224 bn = g_strdup_printf("%s", name); 224 bn = g_strdup_printf("%s", name);
225 } 225 }
226 } 226 }
227 227
228 if (!bn) { 228 if (!bn) {
229 bn = g_strdup_printf("<%s/%s>", jid, rn); 229 bn = g_strdup_printf("<%s/%s>", bjid, rn);
230 } 230 }
231 231
232 logsmsg = g_strdup(status_msg ? status_msg : ""); 232 logsmsg = g_strdup(status_msg ? status_msg : "");
233 replace_nl_with_dots(logsmsg); 233 replace_nl_with_dots(logsmsg);
234 234
235 oldstat = roster_getstatus(jid, resname); 235 oldstat = roster_getstatus(bjid, resname);
236 scr_LogPrint(LPRINT_LOGNORM, "Buddy status has changed: [%c>%c] %s %s", 236 scr_LogPrint(LPRINT_LOGNORM, "Buddy status has changed: [%c>%c] %s %s",
237 imstatus2char[oldstat], imstatus2char[status], bn, logsmsg); 237 imstatus2char[oldstat], imstatus2char[status], bn, logsmsg);
238 g_free(logsmsg); 238 g_free(logsmsg);
239 g_free(bn); 239 g_free(bn);
240 240
241 if (st_in_buf == 2 || 241 if (st_in_buf == 2 ||
242 (st_in_buf == 1 && (status == offline || oldstat == offline))) { 242 (st_in_buf == 1 && (status == offline || oldstat == offline))) {
243 // Write the status change in the buddy's buffer, only if it already exists 243 // Write the status change in the buddy's buffer, only if it already exists
244 if (scr_BuddyBufferExists(jid)) { 244 if (scr_BuddyBufferExists(bjid)) {
245 bn = g_strdup_printf("Buddy status has changed: [%c>%c] %s", 245 bn = g_strdup_printf("Buddy status has changed: [%c>%c] %s",
246 imstatus2char[oldstat], imstatus2char[status], 246 imstatus2char[oldstat], imstatus2char[status],
247 ((status_msg) ? status_msg : "")); 247 ((status_msg) ? status_msg : ""));
248 scr_WriteIncomingMessage(jid, bn, timestamp, 248 scr_WriteIncomingMessage(bjid, bn, timestamp,
249 HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG); 249 HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG);
250 g_free(bn); 250 g_free(bn);
251 } 251 }
252 } 252 }
253 253
254 roster_setstatus(jid, rn, prio, status, status_msg, timestamp, 254 roster_setstatus(bjid, rn, prio, status, status_msg, timestamp,
255 role_none, affil_none, NULL); 255 role_none, affil_none, NULL);
256 buddylist_build(); 256 buddylist_build();
257 scr_DrawRoster(); 257 scr_DrawRoster();
258 hlog_write_status(jid, timestamp, status, status_msg); 258 hlog_write_status(bjid, timestamp, status, status_msg);
259 // External command 259 // External command
260 hk_ext_cmd(jid, 'S', imstatus2char[status], NULL); 260 hk_ext_cmd(bjid, 'S', imstatus2char[status], NULL);
261 } 261 }
262 262
263 inline void hk_mystatuschange(time_t timestamp, enum imstatus old_status, 263 inline void hk_mystatuschange(time_t timestamp, enum imstatus old_status,
264 enum imstatus new_status, const char *msg) 264 enum imstatus new_status, const char *msg)
265 { 265 {
286 } 286 }
287 287
288 // hk_ext_cmd() 288 // hk_ext_cmd()
289 // Launch an external command (process) for the given event. 289 // Launch an external command (process) for the given event.
290 // For now, data should be NULL. 290 // For now, data should be NULL.
291 void hk_ext_cmd(const char *jid, guchar type, guchar info, const char *data) 291 void hk_ext_cmd(const char *bjid, guchar type, guchar info, const char *data)
292 { 292 {
293 pid_t pid; 293 pid_t pid;
294 char *arg_type = NULL; 294 char *arg_type = NULL;
295 char *arg_info = NULL; 295 char *arg_info = NULL;
296 char *arg_data = NULL; 296 char *arg_data = NULL;
361 if (pid == 0) { // child 361 if (pid == 0) { // child
362 // Close standard file descriptors 362 // Close standard file descriptors
363 close(STDIN_FILENO); 363 close(STDIN_FILENO);
364 close(STDOUT_FILENO); 364 close(STDOUT_FILENO);
365 close(STDERR_FILENO); 365 close(STDERR_FILENO);
366 if (execl(extcmd, extcmd, arg_type, arg_info, jid, arg_data, NULL) == -1) { 366 if (execl(extcmd, extcmd, arg_type, arg_info, bjid, arg_data, NULL) == -1) {
367 // scr_LogPrint(LPRINT_LOGNORM, "Cannot execute external command."); 367 // scr_LogPrint(LPRINT_LOGNORM, "Cannot execute external command.");
368 exit(1); 368 exit(1);
369 } 369 }
370 } 370 }
371 g_free(datafname); 371 g_free(datafname);