4 Copyright (C) 2000 Timo Sirainen
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
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
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 USA
22 #include "module-formats.h"
27 #include "special-vars.h"
36 #include "window-items.h"
37 #include "fe-queries.h"
38 #include "hilight-text.h"
39 #include "printtext.h"
41 #define ishighalnum(c) ((unsigned char) (c) >= 128 || i_isalnum(c))
42 #define isnickchar(a) \
43 (i_isalnum(a) || (a) == '`' || (a) == '-' || (a) == '_' || \
44 (a) == '[' || (a) == ']' || (a) == '{' || (a) == '}' || \
45 (a) == '|' || (a) == '\\' || (a) == '^')
47 GHashTable *printnicks;
49 /* convert _underlined_ and *bold* words (and phrases) to use real
50 underlining or bolding */
51 char *expand_emphasis(WI_ITEM_REC *item, const char *text)
57 g_return_val_if_fail(text != NULL, NULL);
59 str = g_string_new(text);
61 for (pos = 0; pos < str->len; pos++) {
62 char type, *bgn, *end;
69 type = 31; /* underlined */
73 /* check that the beginning marker starts a word, and
74 that the matching end marker ends a word */
75 if ((pos > 0 && bgn[-1] != ' ') || !ishighalnum(bgn[1]))
77 if ((end = strchr(bgn+1, *bgn)) == NULL)
79 if (!ishighalnum(end[-1]) || ishighalnum(end[1]) ||
80 end[1] == type || end[1] == '*' || end[1] == '_')
83 if (IS_CHANNEL(item)) {
84 /* check that this isn't a _nick_, we don't want to
85 use emphasis on them. */
90 /* check if _foo_ is a nick */
93 found = nicklist_find(CHANNEL(item), bgn) != NULL;
97 /* check if the whole 'word' (e.g. "_foo_^") is a nick
98 in "_foo_^ ", end will be the second _, end2 the ^ */
100 while (isnickchar(end2[1]))
104 found = nicklist_find(CHANNEL(item), bgn) != NULL;
109 /* allow only *word* emphasis, not *multiple words* */
110 if (!settings_get_bool("emphasis_multiword")) {
112 for (c = bgn+1; c != end; c++) {
113 if (!ishighalnum(*c))
116 if (c != end) continue;
119 if (settings_get_bool("emphasis_replace")) {
123 g_string_insert_c(str, pos, type);
124 pos += (end - bgn) + 2;
125 g_string_insert_c(str, pos++, type);
130 g_string_free(str, FALSE);
134 static char *channel_get_nickmode_rec(NICK_REC *nickrec)
137 static char nickmode[2]; /* FIXME: bad */
139 if (!settings_get_bool("show_nickmode"))
142 emptystr = settings_get_bool("show_nickmode_empty") ? " " : "";
144 if (nickrec != NULL && nickrec->other) {
145 nickmode[0] = nickrec->other;
150 return nickrec == NULL ? emptystr :
152 nickrec->halfop ? "%" :
153 nickrec->voice ? "+" :
157 char *channel_get_nickmode(CHANNEL_REC *channel, const char *nick)
159 g_return_val_if_fail(nick != NULL, NULL);
161 return channel_get_nickmode_rec(channel == NULL ? NULL :
162 nicklist_find(channel, nick));
165 static void sig_message_public(SERVER_REC *server, const char *msg,
166 const char *nick, const char *address,
167 const char *target, NICK_REC *nickrec)
169 CHANNEL_REC *chanrec;
170 const char *nickmode, *printnick;
171 int for_me, print_channel, level;
172 char *color, *freemsg = NULL;
173 HILIGHT_REC *hilight;
175 /* NOTE: this may return NULL if some channel is just closed with
176 /WINDOW CLOSE and server still sends the few last messages */
177 chanrec = channel_find(server, target);
178 if (nickrec == NULL && chanrec != NULL)
179 nickrec = nicklist_find(chanrec, nick);
181 for_me = !settings_get_bool("hilight_nick_matches") ? FALSE :
182 nick_match_msg(chanrec, msg, server->nick);
183 hilight = for_me ? NULL :
184 hilight_match_nick(server, target, nick, address, MSGLEVEL_PUBLIC, msg);
185 color = (hilight == NULL) ? NULL : hilight_get_color(hilight);
187 print_channel = chanrec == NULL ||
188 !window_item_is_active((WI_ITEM_REC *) chanrec);
189 if (!print_channel && settings_get_bool("print_active_channel") &&
190 window_item_window((WI_ITEM_REC *) chanrec)->items->next != NULL)
191 print_channel = TRUE;
193 level = MSGLEVEL_PUBLIC;
195 level |= MSGLEVEL_HILIGHT;
197 if (settings_get_bool("emphasis"))
198 msg = freemsg = expand_emphasis((WI_ITEM_REC *) chanrec, msg);
200 /* get nick mode & nick what to print the msg with
201 (in case there's multiple identical nicks) */
202 nickmode = channel_get_nickmode_rec(nickrec);
203 printnick = nickrec == NULL ? nick :
204 g_hash_table_lookup(printnicks, nickrec);
205 if (printnick == NULL)
209 /* highlighted nick */
211 format_create_dest(&dest, server, target, level, NULL);
212 hilight_update_text_dest(&dest,hilight);
213 if (!print_channel) /* message to active channel in window */
214 printformat_dest(&dest, TXT_PUBMSG_HILIGHT, color,
215 printnick, msg, nickmode);
216 else /* message to not existing/active channel */
217 printformat_dest(&dest, TXT_PUBMSG_HILIGHT_CHANNEL,
218 color, printnick, target, msg,
222 printformat(server, target, level,
223 for_me ? TXT_PUBMSG_ME : TXT_PUBMSG,
224 printnick, msg, nickmode);
226 printformat(server, target, level,
227 for_me ? TXT_PUBMSG_ME_CHANNEL :
229 printnick, target, msg, nickmode);
232 g_free_not_null(freemsg);
233 g_free_not_null(color);
236 static void sig_message_private(SERVER_REC *server, const char *msg,
237 const char *nick, const char *address)
240 char *freemsg = NULL;
242 query = query_find(server, nick);
244 if (settings_get_bool("emphasis"))
245 msg = freemsg = expand_emphasis((WI_ITEM_REC *) query, msg);
247 printformat(server, nick, MSGLEVEL_MSGS,
248 query == NULL ? TXT_MSG_PRIVATE :
249 TXT_MSG_PRIVATE_QUERY, nick, address, msg);
251 g_free_not_null(freemsg);
254 static void sig_message_own_public(SERVER_REC *server, const char *msg,
258 CHANNEL_REC *channel;
259 const char *nickmode;
260 char *freemsg = NULL, *recoded;
262 channel = channel_find(server, target);
264 target = channel->visible_name;
266 nickmode = channel_get_nickmode(channel, server->nick);
268 window = channel == NULL ? NULL :
269 window_item_window((WI_ITEM_REC *) channel);
271 print_channel = window == NULL ||
272 window->active != (WI_ITEM_REC *) channel;
274 if (!print_channel && settings_get_bool("print_active_channel") &&
275 window != NULL && g_slist_length(window->items) > 1)
276 print_channel = TRUE;
278 if (settings_get_bool("emphasis"))
279 msg = freemsg = expand_emphasis((WI_ITEM_REC *) channel, msg);
281 /* ugly: recode the sent message back for printing */
282 recoded = recode_in(server, msg, target);
284 if (!print_channel) {
285 printformat(server, target, MSGLEVEL_PUBLIC | MSGLEVEL_NOHILIGHT | MSGLEVEL_NO_ACT,
286 TXT_OWN_MSG, server->nick, recoded, nickmode);
288 printformat(server, target, MSGLEVEL_PUBLIC | MSGLEVEL_NOHILIGHT | MSGLEVEL_NO_ACT,
289 TXT_OWN_MSG_CHANNEL, server->nick, target, recoded, nickmode);
293 g_free_not_null(freemsg);
296 static void sig_message_own_private(SERVER_REC *server, const char *msg,
297 const char *target, const char *origtarget)
300 char *freemsg = NULL, *recoded;
302 g_return_if_fail(server != NULL);
303 g_return_if_fail(msg != NULL);
304 if (target == NULL) {
305 /* this should only happen if some special target failed and
306 we should display some error message. currently the special
307 targets are only ',' and '.'. */
308 g_return_if_fail(strcmp(origtarget, ",") == 0 ||
309 strcmp(origtarget, ".") == 0);
311 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
312 *origtarget == ',' ? TXT_NO_MSGS_GOT :
318 query = privmsg_get_query(server, target, TRUE, MSGLEVEL_MSGS);
320 if (settings_get_bool("emphasis"))
321 msg = freemsg = expand_emphasis((WI_ITEM_REC *) query, msg);
323 /* ugly: recode the sent message back for printing */
324 recoded = recode_in(server, msg, target);
326 printformat(server, target,
327 MSGLEVEL_MSGS | MSGLEVEL_NOHILIGHT | MSGLEVEL_NO_ACT,
328 query == NULL ? TXT_OWN_MSG_PRIVATE :
329 TXT_OWN_MSG_PRIVATE_QUERY, target, recoded, server->nick);
332 g_free_not_null(freemsg);
335 static void sig_message_join(SERVER_REC *server, const char *channel,
336 const char *nick, const char *address)
338 printformat(server, channel, MSGLEVEL_JOINS,
339 TXT_JOIN, nick, address, channel);
342 static void sig_message_part(SERVER_REC *server, const char *channel,
343 const char *nick, const char *address,
346 printformat(server, channel, MSGLEVEL_PARTS,
347 TXT_PART, nick, address, channel, reason);
350 static void sig_message_quit(SERVER_REC *server, const char *nick,
351 const char *address, const char *reason)
355 GSList *tmp, *windows;
356 char *print_channel, *recoded;
359 if (ignore_check(server, nick, address, NULL, reason, MSGLEVEL_QUITS))
362 print_channel = NULL;
363 once = settings_get_bool("show_quit_once");
365 count = 0; windows = NULL;
366 chans = g_string_new(NULL);
367 for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
368 CHANNEL_REC *rec = tmp->data;
370 if (!nicklist_find(rec, nick))
373 if (ignore_check(server, nick, address, rec->visible_name,
374 reason, MSGLEVEL_QUITS)) {
379 if (print_channel == NULL ||
380 active_win->active == (WI_ITEM_REC *) rec)
381 print_channel = rec->visible_name;
384 g_string_sprintfa(chans, "%s,", rec->visible_name);
386 window = window_item_window((WI_ITEM_REC *) rec);
387 if (g_slist_find(windows, window) == NULL) {
388 windows = g_slist_append(windows, window);
389 recoded = recode_in(server, reason, rec->visible_name);
390 printformat(server, rec->visible_name,
392 TXT_QUIT, nick, address, recoded,
399 g_slist_free(windows);
402 /* check if you had query with the nick and
403 display the quit there too */
404 QUERY_REC *query = query_find(server, nick);
406 recoded = recode_in(server, reason, nick);
407 printformat(server, nick, MSGLEVEL_QUITS,
408 TXT_QUIT, nick, address, recoded, "");
413 if (once || count == 0) {
415 g_string_truncate(chans, chans->len-1);
416 /* at least recode_fallback will be used */
417 recoded = recode_in(server, reason, nick);
418 printformat(server, print_channel, MSGLEVEL_QUITS,
419 count <= 1 ? TXT_QUIT : TXT_QUIT_ONCE,
420 nick, address, recoded, chans->str);
423 g_string_free(chans, TRUE);
426 static void sig_message_kick(SERVER_REC *server, const char *channel,
427 const char *nick, const char *kicker,
428 const char *address, const char *reason)
430 printformat(server, channel, MSGLEVEL_KICKS,
431 TXT_KICK, nick, channel, kicker, reason, address);
434 static void print_nick_change_channel(SERVER_REC *server, const char *channel,
435 const char *newnick, const char *oldnick,
441 if (ignore_check(server, oldnick, address,
442 channel, newnick, MSGLEVEL_NICKS))
445 level = MSGLEVEL_NICKS;
446 if (ownnick) level |= MSGLEVEL_NO_ACT;
448 printformat(server, channel, level,
449 ownnick ? TXT_YOUR_NICK_CHANGED : TXT_NICK_CHANGED,
450 oldnick, newnick, channel, address);
453 static void print_nick_change(SERVER_REC *server, const char *newnick,
454 const char *oldnick, const char *address,
457 GSList *tmp, *windows;
462 /* Print to each channel/query where the nick is.
463 Don't print more than once to the same window. */
465 for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
466 CHANNEL_REC *channel = tmp->data;
468 window_item_window((WI_ITEM_REC *) channel);
470 if (nicklist_find(channel, newnick) == NULL ||
471 g_slist_find(windows, window) != NULL)
474 windows = g_slist_append(windows, window);
475 print_nick_change_channel(server, channel->visible_name,
476 newnick, oldnick, address, ownnick);
480 g_slist_free(windows);
482 if (!msgprint && ownnick) {
483 printformat(server, NULL, MSGLEVEL_NICKS,
484 TXT_YOUR_NICK_CHANGED, oldnick, newnick, "",
489 static void sig_message_nick(SERVER_REC *server, const char *newnick,
490 const char *oldnick, const char *address)
492 print_nick_change(server, newnick, oldnick, address, FALSE);
495 static void sig_message_own_nick(SERVER_REC *server, const char *newnick,
496 const char *oldnick, const char *address)
498 if (!settings_get_bool("show_own_nickchange_once"))
499 print_nick_change(server, newnick, oldnick, address, TRUE);
501 printformat(server, NULL, MSGLEVEL_NICKS,
502 TXT_YOUR_NICK_CHANGED, oldnick, newnick, "",
507 static void sig_message_invite(SERVER_REC *server, const char *channel,
508 const char *nick, const char *address)
512 str = show_lowascii(channel);
513 printformat(server, NULL, MSGLEVEL_INVITES,
514 TXT_INVITE, nick, str, address);
518 static void sig_message_topic(SERVER_REC *server, const char *channel,
520 const char *nick, const char *address)
522 printformat(server, channel, MSGLEVEL_TOPICS,
523 *topic != '\0' ? TXT_NEW_TOPIC : TXT_TOPIC_UNSET,
524 nick, channel, topic, address);
527 static int printnick_exists(NICK_REC *first, NICK_REC *ignore,
532 while (first != NULL) {
533 if (first != ignore) {
534 printnick = g_hash_table_lookup(printnicks, first);
535 if (printnick != NULL && strcmp(printnick, nick) == 0)
545 static NICK_REC *printnick_find_original(NICK_REC *nick)
547 while (nick != NULL) {
548 if (g_hash_table_lookup(printnicks, nick) == NULL)
557 static void sig_nicklist_new(CHANNEL_REC *channel, NICK_REC *nick)
564 if (nick->host == NULL)
567 firstnick = g_hash_table_lookup(channel->nicks, nick->nick);
568 if (firstnick->next == NULL)
571 if (nick == channel->ownnick) {
572 /* own nick is being added, might be a nick change and
573 someone else having the original nick already in use.. */
574 nick = printnick_find_original(firstnick->next);
576 return; /* nope, we have it */
579 /* identical nick already exists, have to change it somehow.. */
580 p = strchr(nick->host, '@');
581 if (p == NULL) p = nick->host; else p++;
583 nickhost = g_strdup_printf("%s@%s", nick->nick, p);
584 p = strchr(nickhost+strlen(nick->nick), '.');
585 if (p != NULL) *p = '\0';
587 if (!printnick_exists(firstnick, nick, nickhost)) {
589 g_hash_table_insert(printnicks, nick, nickhost);
593 newnick = g_string_new(NULL);
596 g_string_sprintf(newnick, "%s%d", nickhost, n);
598 } while (printnick_exists(firstnick, nick, newnick->str));
600 g_hash_table_insert(printnicks, nick, newnick->str);
601 g_string_free(newnick, FALSE);
605 static void sig_nicklist_remove(CHANNEL_REC *channel, NICK_REC *nick)
609 nickname = g_hash_table_lookup(printnicks, nick);
610 if (nickname != NULL) {
612 g_hash_table_remove(printnicks, nick);
616 static void sig_nicklist_changed(CHANNEL_REC *channel, NICK_REC *nick)
618 sig_nicklist_remove(channel, nick);
619 sig_nicklist_new(channel, nick);
622 static void sig_channel_joined(CHANNEL_REC *channel)
627 /* channel->ownnick is set at this point - check if our own nick
628 has been changed, if it was set it back to the original nick and
629 change the previous original to something else */
631 nickname = g_hash_table_lookup(printnicks, channel->ownnick);
632 if (nickname == NULL)
636 g_hash_table_remove(printnicks, channel->ownnick);
638 /* our own nick is guaranteed to be the first in list */
639 nick = channel->ownnick->next;
640 while (nick != NULL) {
641 if (g_hash_table_lookup(printnicks, nick) == NULL) {
642 sig_nicklist_new(channel, nick);
649 static void g_hash_free_value(void *key, void *value)
654 void fe_messages_init(void)
656 printnicks = g_hash_table_new((GHashFunc) g_direct_hash,
657 (GCompareFunc) g_direct_equal);
659 settings_add_bool("lookandfeel", "hilight_nick_matches", TRUE);
660 settings_add_bool("lookandfeel", "emphasis", TRUE);
661 settings_add_bool("lookandfeel", "emphasis_replace", FALSE);
662 settings_add_bool("lookandfeel", "emphasis_multiword", FALSE);
663 settings_add_bool("lookandfeel", "show_nickmode", TRUE);
664 settings_add_bool("lookandfeel", "show_nickmode_empty", TRUE);
665 settings_add_bool("lookandfeel", "print_active_channel", FALSE);
666 settings_add_bool("lookandfeel", "show_quit_once", FALSE);
667 settings_add_bool("lookandfeel", "show_own_nickchange_once", FALSE);
669 signal_add_last("message public", (SIGNAL_FUNC) sig_message_public);
670 signal_add_last("message private", (SIGNAL_FUNC) sig_message_private);
671 signal_add_last("message own_public", (SIGNAL_FUNC) sig_message_own_public);
672 signal_add_last("message own_private", (SIGNAL_FUNC) sig_message_own_private);
673 signal_add_last("message join", (SIGNAL_FUNC) sig_message_join);
674 signal_add_last("message part", (SIGNAL_FUNC) sig_message_part);
675 signal_add_last("message quit", (SIGNAL_FUNC) sig_message_quit);
676 signal_add_last("message kick", (SIGNAL_FUNC) sig_message_kick);
677 signal_add_last("message nick", (SIGNAL_FUNC) sig_message_nick);
678 signal_add_last("message own_nick", (SIGNAL_FUNC) sig_message_own_nick);
679 signal_add_last("message invite", (SIGNAL_FUNC) sig_message_invite);
680 signal_add_last("message topic", (SIGNAL_FUNC) sig_message_topic);
682 signal_add("nicklist new", (SIGNAL_FUNC) sig_nicklist_new);
683 signal_add("nicklist remove", (SIGNAL_FUNC) sig_nicklist_remove);
684 signal_add("nicklist changed", (SIGNAL_FUNC) sig_nicklist_changed);
685 signal_add("nicklist host changed", (SIGNAL_FUNC) sig_nicklist_new);
686 signal_add("channel joined", (SIGNAL_FUNC) sig_channel_joined);
689 void fe_messages_deinit(void)
691 g_hash_table_foreach(printnicks, (GHFunc) g_hash_free_value, NULL);
692 g_hash_table_destroy(printnicks);
694 signal_remove("message public", (SIGNAL_FUNC) sig_message_public);
695 signal_remove("message private", (SIGNAL_FUNC) sig_message_private);
696 signal_remove("message own_public", (SIGNAL_FUNC) sig_message_own_public);
697 signal_remove("message own_private", (SIGNAL_FUNC) sig_message_own_private);
698 signal_remove("message join", (SIGNAL_FUNC) sig_message_join);
699 signal_remove("message part", (SIGNAL_FUNC) sig_message_part);
700 signal_remove("message quit", (SIGNAL_FUNC) sig_message_quit);
701 signal_remove("message kick", (SIGNAL_FUNC) sig_message_kick);
702 signal_remove("message nick", (SIGNAL_FUNC) sig_message_nick);
703 signal_remove("message own_nick", (SIGNAL_FUNC) sig_message_own_nick);
704 signal_remove("message invite", (SIGNAL_FUNC) sig_message_invite);
705 signal_remove("message topic", (SIGNAL_FUNC) sig_message_topic);
707 signal_remove("nicklist new", (SIGNAL_FUNC) sig_nicklist_new);
708 signal_remove("nicklist remove", (SIGNAL_FUNC) sig_nicklist_remove);
709 signal_remove("nicklist changed", (SIGNAL_FUNC) sig_nicklist_changed);
710 signal_remove("nicklist host changed", (SIGNAL_FUNC) sig_nicklist_new);
711 signal_remove("channel joined", (SIGNAL_FUNC) sig_channel_joined);