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"
35 #include "window-items.h"
36 #include "fe-queries.h"
37 #include "hilight-text.h"
38 #include "printtext.h"
40 #define ishighalnum(c) ((unsigned char) (c) >= 128 || i_isalnum(c))
42 static GHashTable *printnicks;
44 /* convert _underlined_ and *bold* words (and phrases) to use real
45 underlining or bolding */
46 char *expand_emphasis(WI_ITEM_REC *item, const char *text)
52 g_return_val_if_fail(text != NULL, NULL);
54 str = g_string_new(text);
56 for (pos = 0; pos < str->len; pos++) {
57 char type, *bgn, *end;
64 type = 31; /* underlined */
68 /* check that the beginning marker starts a word, and
69 that the matching end marker ends a word */
70 if ((pos > 0 && !i_isspace(bgn[-1])) || !ishighalnum(bgn[1]))
72 if ((end = strchr(bgn+1, *bgn)) == NULL)
74 if (!ishighalnum(end[-1]) || ishighalnum(end[1]) ||
75 end[1] == type || end[1] == '*' || end[1] == '_')
78 if (IS_CHANNEL(item)) {
79 /* check that this isn't a _nick_, we don't want to
80 use emphasis on them. */
86 found = nicklist_find(CHANNEL(item), bgn) != NULL;
91 /* allow only *word* emphasis, not *multiple words* */
92 if (!settings_get_bool("emphasis_multiword")) {
94 for (c = bgn+1; c != end; c++) {
98 if (c != end) continue;
101 if (settings_get_bool("emphasis_replace")) {
105 g_string_insert_c(str, pos, type);
106 pos += (end - bgn) + 2;
107 g_string_insert_c(str, pos++, type);
112 g_string_free(str, FALSE);
116 static char *channel_get_nickmode_rec(NICK_REC *nickrec)
120 if (!settings_get_bool("show_nickmode"))
123 emptystr = settings_get_bool("show_nickmode_empty") ? " " : "";
125 return nickrec == NULL ? emptystr :
127 nickrec->halfop ? "%" :
128 nickrec->voice ? "+" :
132 char *channel_get_nickmode(CHANNEL_REC *channel, const char *nick)
134 g_return_val_if_fail(nick != NULL, NULL);
136 return channel_get_nickmode_rec(channel == NULL ? NULL :
137 nicklist_find(channel, nick));
140 static void sig_message_public(SERVER_REC *server, const char *msg,
141 const char *nick, const char *address,
142 const char *target, NICK_REC *nickrec)
144 CHANNEL_REC *chanrec;
145 const char *nickmode, *printnick;
146 int for_me, print_channel, level;
147 char *color, *freemsg = NULL;
149 /* NOTE: this may return NULL if some channel is just closed with
150 /WINDOW CLOSE and server still sends the few last messages */
151 chanrec = channel_find(server, target);
152 if (nickrec == NULL && chanrec != NULL)
153 nickrec = nicklist_find(chanrec, nick);
155 for_me = !settings_get_bool("hilight_nick_matches") ? FALSE :
156 nick_match_msg(chanrec, msg, server->nick);
157 color = for_me ? NULL :
158 hilight_match_nick(server, target, nick, address, MSGLEVEL_PUBLIC, msg);
160 print_channel = chanrec == NULL ||
161 !window_item_is_active((WI_ITEM_REC *) chanrec);
162 if (!print_channel && settings_get_bool("print_active_channel") &&
163 window_item_window((WI_ITEM_REC *) chanrec)->items->next != NULL)
164 print_channel = TRUE;
166 level = MSGLEVEL_PUBLIC;
167 if (for_me || color != NULL)
168 level |= MSGLEVEL_HILIGHT;
170 if (settings_get_bool("emphasis"))
171 msg = freemsg = expand_emphasis((WI_ITEM_REC *) chanrec, msg);
173 /* get nick mode & nick what to print the msg with
174 (in case there's multiple identical nicks) */
175 nickmode = channel_get_nickmode_rec(nickrec);
176 printnick = nickrec == NULL ? nick :
177 g_hash_table_lookup(printnicks, nickrec);
178 if (printnick == NULL)
181 if (!print_channel) {
182 /* message to active channel in window */
184 /* highlighted nick */
185 printformat(server, target, level,
187 color, printnick, msg, nickmode);
189 printformat(server, target, level,
190 for_me ? TXT_PUBMSG_ME : TXT_PUBMSG,
191 printnick, msg, nickmode);
194 /* message to not existing/active channel */
196 /* highlighted nick */
197 printformat(server, target, level,
198 TXT_PUBMSG_HILIGHT_CHANNEL,
199 color, printnick, target, msg, nickmode);
201 printformat(server, target, level,
202 for_me ? TXT_PUBMSG_ME_CHANNEL :
204 printnick, target, msg, nickmode);
208 g_free_not_null(freemsg);
209 g_free_not_null(color);
212 static void sig_message_private(SERVER_REC *server, const char *msg,
213 const char *nick, const char *address)
216 char *freemsg = NULL;
218 query = query_find(server, nick);
220 if (settings_get_bool("emphasis"))
221 msg = freemsg = expand_emphasis((WI_ITEM_REC *) query, msg);
223 printformat(server, nick, MSGLEVEL_MSGS,
224 query == NULL ? TXT_MSG_PRIVATE :
225 TXT_MSG_PRIVATE_QUERY, nick, address, msg);
227 g_free_not_null(freemsg);
230 static void sig_message_own_public(SERVER_REC *server, const char *msg,
234 CHANNEL_REC *channel;
235 const char *nickmode;
236 char *freemsg = NULL;
239 channel = channel_find(server, target);
241 target = channel->visible_name;
243 nickmode = channel_get_nickmode(channel, server->nick);
245 window = channel == NULL ? NULL :
246 window_item_window((WI_ITEM_REC *) channel);
248 print_channel = window == NULL ||
249 window->active != (WI_ITEM_REC *) channel;
251 if (!print_channel && settings_get_bool("print_active_channel") &&
252 window != NULL && g_slist_length(window->items) > 1)
253 print_channel = TRUE;
255 if (settings_get_bool("emphasis"))
256 msg = freemsg = expand_emphasis((WI_ITEM_REC *) channel, msg);
258 if (!print_channel) {
259 printformat(server, target, MSGLEVEL_PUBLIC | MSGLEVEL_NOHILIGHT | MSGLEVEL_NO_ACT,
260 TXT_OWN_MSG, server->nick, msg, nickmode);
262 printformat(server, target, MSGLEVEL_PUBLIC | MSGLEVEL_NOHILIGHT | MSGLEVEL_NO_ACT,
263 TXT_OWN_MSG_CHANNEL, server->nick, target, msg, nickmode);
266 g_free_not_null(freemsg);
269 static void sig_message_own_private(SERVER_REC *server, const char *msg,
270 const char *target, const char *origtarget)
273 char *freemsg = NULL;
275 g_return_if_fail(server != NULL);
276 g_return_if_fail(msg != NULL);
278 if (target == NULL) {
279 /* this should only happen if some special target failed and
280 we should display some error message. currently the special
281 targets are only ',' and '.'. */
282 g_return_if_fail(strcmp(origtarget, ",") == 0 ||
283 strcmp(origtarget, ".") == 0);
285 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
286 *origtarget == ',' ? TXT_NO_MSGS_GOT :
292 query = privmsg_get_query(server, target, TRUE, MSGLEVEL_MSGS);
294 if (settings_get_bool("emphasis"))
295 msg = freemsg = expand_emphasis((WI_ITEM_REC *) query, msg);
297 printformat(server, target,
298 MSGLEVEL_MSGS | MSGLEVEL_NOHILIGHT | MSGLEVEL_NO_ACT,
299 query == NULL ? TXT_OWN_MSG_PRIVATE :
300 TXT_OWN_MSG_PRIVATE_QUERY, target, msg, server->nick);
302 g_free_not_null(freemsg);
305 static void sig_message_join(SERVER_REC *server, const char *channel,
306 const char *nick, const char *address)
308 printformat(server, channel, MSGLEVEL_JOINS,
309 TXT_JOIN, nick, address, channel);
312 static void sig_message_part(SERVER_REC *server, const char *channel,
313 const char *nick, const char *address,
316 printformat(server, channel, MSGLEVEL_PARTS,
317 TXT_PART, nick, address, channel, reason);
320 static void sig_message_quit(SERVER_REC *server, const char *nick,
321 const char *address, const char *reason)
325 GSList *tmp, *windows;
329 if (ignore_check(server, nick, address, NULL, reason, MSGLEVEL_QUITS))
332 print_channel = NULL;
333 once = settings_get_bool("show_quit_once");
335 count = 0; windows = NULL;
336 chans = g_string_new(NULL);
337 for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
338 CHANNEL_REC *rec = tmp->data;
340 if (!nicklist_find(rec, nick))
343 if (ignore_check(server, nick, address, rec->visible_name,
344 reason, MSGLEVEL_QUITS)) {
349 if (print_channel == NULL ||
350 active_win->active == (WI_ITEM_REC *) rec)
351 print_channel = rec->visible_name;
354 g_string_sprintfa(chans, "%s,", rec->visible_name);
356 window = window_item_window((WI_ITEM_REC *) rec);
357 if (g_slist_find(windows, window) == NULL) {
358 windows = g_slist_append(windows, window);
359 printformat(server, rec->visible_name,
361 TXT_QUIT, nick, address, reason,
367 g_slist_free(windows);
370 /* check if you had query with the nick and
371 display the quit there too */
372 QUERY_REC *query = query_find(server, nick);
374 printformat(server, nick, MSGLEVEL_QUITS,
375 TXT_QUIT, nick, address, reason, "");
379 if (once || count == 0) {
381 g_string_truncate(chans, chans->len-1);
382 printformat(server, print_channel, MSGLEVEL_QUITS,
383 count <= 1 ? TXT_QUIT : TXT_QUIT_ONCE,
384 nick, address, reason, chans->str);
386 g_string_free(chans, TRUE);
389 static void sig_message_kick(SERVER_REC *server, const char *channel,
390 const char *nick, const char *kicker,
391 const char *address, const char *reason)
393 printformat(server, channel, MSGLEVEL_KICKS,
394 TXT_KICK, nick, channel, kicker, reason, address);
397 static void print_nick_change_channel(SERVER_REC *server, const char *channel,
398 const char *newnick, const char *oldnick,
404 if (ignore_check(server, oldnick, address,
405 channel, newnick, MSGLEVEL_NICKS))
408 level = MSGLEVEL_NICKS;
409 if (ownnick) level |= MSGLEVEL_NO_ACT;
411 printformat(server, channel, level,
412 ownnick ? TXT_YOUR_NICK_CHANGED : TXT_NICK_CHANGED,
413 oldnick, newnick, channel, address);
416 static void print_nick_change(SERVER_REC *server, const char *newnick,
417 const char *oldnick, const char *address,
420 GSList *tmp, *windows;
425 /* Print to each channel/query where the nick is.
426 Don't print more than once to the same window. */
428 for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
429 CHANNEL_REC *channel = tmp->data;
431 window_item_window((WI_ITEM_REC *) channel);
433 if (nicklist_find(channel, newnick) == NULL ||
434 g_slist_find(windows, window) != NULL)
437 windows = g_slist_append(windows, window);
438 print_nick_change_channel(server, channel->visible_name,
439 newnick, oldnick, address, ownnick);
443 g_slist_free(windows);
445 if (!msgprint && ownnick) {
446 printformat(server, NULL, MSGLEVEL_NICKS,
447 TXT_YOUR_NICK_CHANGED, oldnick, newnick, "",
452 static void sig_message_nick(SERVER_REC *server, const char *newnick,
453 const char *oldnick, const char *address)
455 print_nick_change(server, newnick, oldnick, address, FALSE);
458 static void sig_message_own_nick(SERVER_REC *server, const char *newnick,
459 const char *oldnick, const char *address)
461 if (!settings_get_bool("show_own_nickchange_once"))
462 print_nick_change(server, newnick, oldnick, address, TRUE);
464 printformat(server, NULL, MSGLEVEL_NICKS,
465 TXT_YOUR_NICK_CHANGED, oldnick, newnick, "",
470 static void sig_message_invite(SERVER_REC *server, const char *channel,
471 const char *nick, const char *address)
475 str = show_lowascii(channel);
476 printformat(server, NULL, MSGLEVEL_INVITES,
477 TXT_INVITE, nick, str, address);
481 static void sig_message_topic(SERVER_REC *server, const char *channel,
483 const char *nick, const char *address)
485 printformat(server, channel, MSGLEVEL_TOPICS,
486 *topic != '\0' ? TXT_NEW_TOPIC : TXT_TOPIC_UNSET,
487 nick, channel, topic, address);
490 static int printnick_exists(NICK_REC *first, NICK_REC *ignore,
495 while (first != NULL) {
496 if (first != ignore) {
497 printnick = g_hash_table_lookup(printnicks, first);
498 if (printnick != NULL && strcmp(printnick, nick) == 0)
508 static NICK_REC *printnick_find_original(NICK_REC *nick)
510 while (nick != NULL) {
511 if (g_hash_table_lookup(printnicks, nick) == NULL)
520 static void sig_nicklist_new(CHANNEL_REC *channel, NICK_REC *nick)
527 if (nick->host == NULL)
530 firstnick = g_hash_table_lookup(channel->nicks, nick->nick);
531 if (firstnick->next == NULL)
534 if (nick == channel->ownnick) {
535 /* own nick is being added, might be a nick change and
536 someone else having the original nick already in use.. */
537 nick = printnick_find_original(firstnick->next);
539 return; /* nope, we have it */
542 /* identical nick already exists, have to change it somehow.. */
543 p = strchr(nick->host, '@');
544 if (p == NULL) p = nick->host; else p++;
546 nickhost = g_strdup_printf("%s@%s", nick->nick, p);
547 p = strchr(nickhost+strlen(nick->nick), '.');
548 if (p != NULL) *p = '\0';
550 if (!printnick_exists(firstnick, nick, nickhost)) {
552 g_hash_table_insert(printnicks, nick, nickhost);
556 newnick = g_string_new(NULL);
559 g_string_sprintf(newnick, "%s%d", nickhost, n);
561 } while (printnick_exists(firstnick, nick, newnick->str));
563 g_hash_table_insert(printnicks, nick, newnick->str);
564 g_string_free(newnick, FALSE);
568 static void sig_nicklist_remove(CHANNEL_REC *channel, NICK_REC *nick)
572 nickname = g_hash_table_lookup(printnicks, nick);
573 if (nickname != NULL) {
575 g_hash_table_remove(printnicks, nick);
579 static void sig_nicklist_changed(CHANNEL_REC *channel, NICK_REC *nick)
581 sig_nicklist_remove(channel, nick);
582 sig_nicklist_new(channel, nick);
585 static void sig_channel_joined(CHANNEL_REC *channel)
590 /* channel->ownnick is set at this point - check if our own nick
591 has been changed, if it was set it back to the original nick and
592 change the previous original to something else */
594 nickname = g_hash_table_lookup(printnicks, channel->ownnick);
595 if (nickname == NULL)
599 g_hash_table_remove(printnicks, channel->ownnick);
601 /* our own nick is guaranteed to be the first in list */
602 nick = channel->ownnick->next;
603 while (nick != NULL) {
604 if (g_hash_table_lookup(printnicks, nick) == NULL) {
605 sig_nicklist_new(channel, nick);
612 static void g_hash_free_value(void *key, void *value)
617 void fe_messages_init(void)
619 printnicks = g_hash_table_new((GHashFunc) g_direct_hash,
620 (GCompareFunc) g_direct_equal);
622 settings_add_bool("lookandfeel", "hilight_nick_matches", TRUE);
623 settings_add_bool("lookandfeel", "emphasis", TRUE);
624 settings_add_bool("lookandfeel", "emphasis_replace", FALSE);
625 settings_add_bool("lookandfeel", "emphasis_multiword", FALSE);
626 settings_add_bool("lookandfeel", "show_nickmode", TRUE);
627 settings_add_bool("lookandfeel", "show_nickmode_empty", TRUE);
628 settings_add_bool("lookandfeel", "print_active_channel", FALSE);
629 settings_add_bool("lookandfeel", "show_quit_once", FALSE);
630 settings_add_bool("lookandfeel", "show_own_nickchange_once", FALSE);
632 signal_add_last("message public", (SIGNAL_FUNC) sig_message_public);
633 signal_add_last("message private", (SIGNAL_FUNC) sig_message_private);
634 signal_add_last("message own_public", (SIGNAL_FUNC) sig_message_own_public);
635 signal_add_last("message own_private", (SIGNAL_FUNC) sig_message_own_private);
636 signal_add_last("message join", (SIGNAL_FUNC) sig_message_join);
637 signal_add_last("message part", (SIGNAL_FUNC) sig_message_part);
638 signal_add_last("message quit", (SIGNAL_FUNC) sig_message_quit);
639 signal_add_last("message kick", (SIGNAL_FUNC) sig_message_kick);
640 signal_add_last("message nick", (SIGNAL_FUNC) sig_message_nick);
641 signal_add_last("message own_nick", (SIGNAL_FUNC) sig_message_own_nick);
642 signal_add_last("message invite", (SIGNAL_FUNC) sig_message_invite);
643 signal_add_last("message topic", (SIGNAL_FUNC) sig_message_topic);
645 signal_add("nicklist new", (SIGNAL_FUNC) sig_nicklist_new);
646 signal_add("nicklist remove", (SIGNAL_FUNC) sig_nicklist_remove);
647 signal_add("nicklist changed", (SIGNAL_FUNC) sig_nicklist_changed);
648 signal_add("nicklist host changed", (SIGNAL_FUNC) sig_nicklist_new);
649 signal_add("channel joined", (SIGNAL_FUNC) sig_channel_joined);
652 void fe_messages_deinit(void)
654 g_hash_table_foreach(printnicks, (GHFunc) g_hash_free_value, NULL);
655 g_hash_table_destroy(printnicks);
657 signal_remove("message public", (SIGNAL_FUNC) sig_message_public);
658 signal_remove("message private", (SIGNAL_FUNC) sig_message_private);
659 signal_remove("message own_public", (SIGNAL_FUNC) sig_message_own_public);
660 signal_remove("message own_private", (SIGNAL_FUNC) sig_message_own_private);
661 signal_remove("message join", (SIGNAL_FUNC) sig_message_join);
662 signal_remove("message part", (SIGNAL_FUNC) sig_message_part);
663 signal_remove("message quit", (SIGNAL_FUNC) sig_message_quit);
664 signal_remove("message kick", (SIGNAL_FUNC) sig_message_kick);
665 signal_remove("message nick", (SIGNAL_FUNC) sig_message_nick);
666 signal_remove("message own_nick", (SIGNAL_FUNC) sig_message_own_nick);
667 signal_remove("message invite", (SIGNAL_FUNC) sig_message_invite);
668 signal_remove("message topic", (SIGNAL_FUNC) sig_message_topic);
670 signal_remove("nicklist new", (SIGNAL_FUNC) sig_nicklist_new);
671 signal_remove("nicklist remove", (SIGNAL_FUNC) sig_nicklist_remove);
672 signal_remove("nicklist changed", (SIGNAL_FUNC) sig_nicklist_changed);
673 signal_remove("nicklist host changed", (SIGNAL_FUNC) sig_nicklist_new);
674 signal_remove("channel joined", (SIGNAL_FUNC) sig_channel_joined);