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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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))
41 #define isnickchar(a) \
42 (i_isalnum(a) || (a) == '`' || (a) == '-' || (a) == '_' || \
43 (a) == '[' || (a) == ']' || (a) == '{' || (a) == '}' || \
44 (a) == '|' || (a) == '\\' || (a) == '^')
46 GHashTable *printnicks;
48 /* convert _underlined_ and *bold* words (and phrases) to use real
49 underlining or bolding */
50 char *expand_emphasis(WI_ITEM_REC *item, const char *text)
56 g_return_val_if_fail(text != NULL, NULL);
58 str = g_string_new(text);
60 for (pos = 0; pos < str->len; pos++) {
61 char type, *bgn, *end;
68 type = 31; /* underlined */
72 /* check that the beginning marker starts a word, and
73 that the matching end marker ends a word */
74 if ((pos > 0 && bgn[-1] != ' ') || !ishighalnum(bgn[1]))
76 if ((end = strchr(bgn+1, *bgn)) == NULL)
78 if (!ishighalnum(end[-1]) || ishighalnum(end[1]) ||
79 end[1] == type || end[1] == '*' || end[1] == '_')
82 if (IS_CHANNEL(item)) {
83 /* check that this isn't a _nick_, we don't want to
84 use emphasis on them. */
89 /* check if _foo_ is a nick */
92 found = nicklist_find(CHANNEL(item), bgn) != NULL;
96 /* check if the whole 'word' (e.g. "_foo_^") is a nick
97 in "_foo_^ ", end will be the second _, end2 the ^ */
99 while (isnickchar(end2[1]))
103 found = nicklist_find(CHANNEL(item), bgn) != NULL;
108 /* allow only *word* emphasis, not *multiple words* */
109 if (!settings_get_bool("emphasis_multiword")) {
111 for (c = bgn+1; c != end; c++) {
112 if (!ishighalnum(*c))
115 if (c != end) continue;
118 if (settings_get_bool("emphasis_replace")) {
122 g_string_insert_c(str, pos, type);
123 pos += (end - bgn) + 2;
124 g_string_insert_c(str, pos++, type);
129 g_string_free(str, FALSE);
133 static char *channel_get_nickmode_rec(NICK_REC *nickrec)
138 if (!settings_get_bool("show_nickmode"))
141 emptystr = settings_get_bool("show_nickmode_empty") ? " " : "";
143 if (nickrec == NULL || nickrec->prefixes[0] == '\0')
144 nickmode = g_strdup(emptystr);
146 nickmode = g_malloc(2);
147 nickmode[0] = nickrec->prefixes[0];
153 char *channel_get_nickmode(CHANNEL_REC *channel, const char *nick)
155 g_return_val_if_fail(nick != NULL, NULL);
157 return channel_get_nickmode_rec(channel == NULL ? NULL :
158 nicklist_find(channel, nick));
161 static void sig_message_public(SERVER_REC *server, const char *msg,
162 const char *nick, const char *address,
163 const char *target, NICK_REC *nickrec)
165 CHANNEL_REC *chanrec;
166 const char *printnick;
167 int for_me, print_channel, level;
168 char *nickmode, *color, *freemsg = NULL;
169 HILIGHT_REC *hilight;
171 /* NOTE: this may return NULL if some channel is just closed with
172 /WINDOW CLOSE and server still sends the few last messages */
173 chanrec = channel_find(server, target);
174 if (nickrec == NULL && chanrec != NULL)
175 nickrec = nicklist_find(chanrec, nick);
177 for_me = !settings_get_bool("hilight_nick_matches") ? FALSE :
178 nick_match_msg(chanrec, msg, server->nick);
179 hilight = for_me ? NULL :
180 hilight_match_nick(server, target, nick, address, MSGLEVEL_PUBLIC, msg);
181 color = (hilight == NULL) ? NULL : hilight_get_color(hilight);
183 print_channel = chanrec == NULL ||
184 !window_item_is_active((WI_ITEM_REC *) chanrec);
185 if (!print_channel && settings_get_bool("print_active_channel") &&
186 window_item_window((WI_ITEM_REC *) chanrec)->items->next != NULL)
187 print_channel = TRUE;
189 level = MSGLEVEL_PUBLIC;
191 level |= MSGLEVEL_HILIGHT;
193 if (settings_get_bool("emphasis"))
194 msg = freemsg = expand_emphasis((WI_ITEM_REC *) chanrec, msg);
196 /* get nick mode & nick what to print the msg with
197 (in case there's multiple identical nicks) */
198 nickmode = channel_get_nickmode_rec(nickrec);
199 printnick = nickrec == NULL ? nick :
200 g_hash_table_lookup(printnicks, nickrec);
201 if (printnick == NULL)
205 /* highlighted nick */
207 format_create_dest(&dest, server, target, level, NULL);
208 hilight_update_text_dest(&dest,hilight);
209 if (!print_channel) /* message to active channel in window */
210 printformat_dest(&dest, TXT_PUBMSG_HILIGHT, color,
211 printnick, msg, nickmode);
212 else /* message to not existing/active channel */
213 printformat_dest(&dest, TXT_PUBMSG_HILIGHT_CHANNEL,
214 color, printnick, target, msg,
218 printformat(server, target, level,
219 for_me ? TXT_PUBMSG_ME : TXT_PUBMSG,
220 printnick, msg, nickmode);
222 printformat(server, target, level,
223 for_me ? TXT_PUBMSG_ME_CHANNEL :
225 printnick, target, msg, nickmode);
228 g_free_not_null(nickmode);
229 g_free_not_null(freemsg);
230 g_free_not_null(color);
233 static void sig_message_private(SERVER_REC *server, const char *msg,
234 const char *nick, const char *address)
237 char *freemsg = NULL;
239 query = query_find(server, nick);
241 if (settings_get_bool("emphasis"))
242 msg = freemsg = expand_emphasis((WI_ITEM_REC *) query, msg);
244 printformat(server, nick, MSGLEVEL_MSGS,
245 query == NULL ? TXT_MSG_PRIVATE :
246 TXT_MSG_PRIVATE_QUERY, nick, address, msg);
248 g_free_not_null(freemsg);
251 static void sig_message_own_public(SERVER_REC *server, const char *msg,
255 CHANNEL_REC *channel;
257 char *freemsg = NULL;
259 channel = channel_find(server, target);
261 target = channel->visible_name;
263 nickmode = channel_get_nickmode(channel, server->nick);
265 window = channel == NULL ? NULL :
266 window_item_window((WI_ITEM_REC *) channel);
268 print_channel = window == NULL ||
269 window->active != (WI_ITEM_REC *) channel;
271 if (!print_channel && settings_get_bool("print_active_channel") &&
272 window != NULL && g_slist_length(window->items) > 1)
273 print_channel = TRUE;
275 if (settings_get_bool("emphasis"))
276 msg = freemsg = expand_emphasis((WI_ITEM_REC *) channel, msg);
278 if (!print_channel) {
279 printformat(server, target, MSGLEVEL_PUBLIC | MSGLEVEL_NOHILIGHT | MSGLEVEL_NO_ACT,
280 TXT_OWN_MSG, server->nick, msg, nickmode);
282 printformat(server, target, MSGLEVEL_PUBLIC | MSGLEVEL_NOHILIGHT | MSGLEVEL_NO_ACT,
283 TXT_OWN_MSG_CHANNEL, server->nick, target, msg, nickmode);
286 g_free_not_null(nickmode);
287 g_free_not_null(freemsg);
290 static void sig_message_own_private(SERVER_REC *server, const char *msg,
291 const char *target, const char *origtarget)
294 char *freemsg = NULL;
296 g_return_if_fail(server != NULL);
297 g_return_if_fail(msg != NULL);
298 if (target == NULL) {
299 /* this should only happen if some special target failed and
300 we should display some error message. currently the special
301 targets are only ',' and '.'. */
302 g_return_if_fail(strcmp(origtarget, ",") == 0 ||
303 strcmp(origtarget, ".") == 0);
305 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
306 *origtarget == ',' ? TXT_NO_MSGS_GOT :
312 query = privmsg_get_query(server, target, TRUE, MSGLEVEL_MSGS);
314 if (settings_get_bool("emphasis"))
315 msg = freemsg = expand_emphasis((WI_ITEM_REC *) query, msg);
317 printformat(server, target,
318 MSGLEVEL_MSGS | MSGLEVEL_NOHILIGHT | MSGLEVEL_NO_ACT,
319 query == NULL ? TXT_OWN_MSG_PRIVATE :
320 TXT_OWN_MSG_PRIVATE_QUERY, target, msg, server->nick);
322 g_free_not_null(freemsg);
325 static void sig_message_join(SERVER_REC *server, const char *channel,
326 const char *nick, const char *address)
328 printformat(server, channel, MSGLEVEL_JOINS,
329 TXT_JOIN, nick, address, channel);
332 static void sig_message_part(SERVER_REC *server, const char *channel,
333 const char *nick, const char *address,
336 printformat(server, channel, MSGLEVEL_PARTS,
337 TXT_PART, nick, address, channel, reason);
340 static void sig_message_quit(SERVER_REC *server, const char *nick,
341 const char *address, const char *reason)
345 GSList *tmp, *windows;
349 if (ignore_check(server, nick, address, NULL, reason, MSGLEVEL_QUITS))
352 print_channel = NULL;
353 once = settings_get_bool("show_quit_once");
355 count = 0; windows = NULL;
356 chans = g_string_new(NULL);
357 for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
358 CHANNEL_REC *rec = tmp->data;
360 if (!nicklist_find(rec, nick))
363 if (ignore_check(server, nick, address, rec->visible_name,
364 reason, MSGLEVEL_QUITS)) {
369 if (print_channel == NULL ||
370 active_win->active == (WI_ITEM_REC *) rec)
371 print_channel = rec->visible_name;
374 g_string_append_printf(chans, "%s,", rec->visible_name);
376 window = window_item_window((WI_ITEM_REC *) rec);
377 if (g_slist_find(windows, window) == NULL) {
378 windows = g_slist_append(windows, window);
379 printformat(server, rec->visible_name,
381 TXT_QUIT, nick, address, reason,
387 g_slist_free(windows);
390 /* check if you had query with the nick and
391 display the quit there too */
392 QUERY_REC *query = query_find(server, nick);
394 printformat(server, nick, MSGLEVEL_QUITS,
395 TXT_QUIT, nick, address, reason, "");
399 if (once || count == 0) {
401 g_string_truncate(chans, chans->len-1);
402 printformat(server, print_channel, MSGLEVEL_QUITS,
403 count <= 1 ? TXT_QUIT : TXT_QUIT_ONCE,
404 nick, address, reason, chans->str);
406 g_string_free(chans, TRUE);
409 static void sig_message_kick(SERVER_REC *server, const char *channel,
410 const char *nick, const char *kicker,
411 const char *address, const char *reason)
413 printformat(server, channel, MSGLEVEL_KICKS,
414 TXT_KICK, nick, channel, kicker, reason, address);
417 static void print_nick_change_channel(SERVER_REC *server, const char *channel,
418 const char *newnick, const char *oldnick,
424 if (ignore_check(server, oldnick, address,
425 channel, newnick, MSGLEVEL_NICKS))
428 level = MSGLEVEL_NICKS;
429 if (ownnick) level |= MSGLEVEL_NO_ACT;
431 printformat(server, channel, level,
432 ownnick ? TXT_YOUR_NICK_CHANGED : TXT_NICK_CHANGED,
433 oldnick, newnick, channel, address);
436 static void print_nick_change(SERVER_REC *server, const char *newnick,
437 const char *oldnick, const char *address,
440 GSList *tmp, *windows;
445 /* Print to each channel where the nick is.
446 Don't print more than once to the same window. */
448 for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
449 CHANNEL_REC *channel = tmp->data;
451 window_item_window((WI_ITEM_REC *) channel);
453 if (nicklist_find(channel, newnick) == NULL ||
454 g_slist_find(windows, window) != NULL)
457 windows = g_slist_append(windows, window);
458 print_nick_change_channel(server, channel->visible_name,
459 newnick, oldnick, address, ownnick);
463 g_slist_free(windows);
465 if (!msgprint && ownnick) {
466 printformat(server, NULL, MSGLEVEL_NICKS,
467 TXT_YOUR_NICK_CHANGED, oldnick, newnick, "",
472 static void sig_message_nick(SERVER_REC *server, const char *newnick,
473 const char *oldnick, const char *address)
475 print_nick_change(server, newnick, oldnick, address, FALSE);
478 static void sig_message_own_nick(SERVER_REC *server, const char *newnick,
479 const char *oldnick, const char *address)
481 if (!settings_get_bool("show_own_nickchange_once"))
482 print_nick_change(server, newnick, oldnick, address, TRUE);
484 printformat(server, NULL, MSGLEVEL_NICKS,
485 TXT_YOUR_NICK_CHANGED, oldnick, newnick, "",
490 static void sig_message_invite(SERVER_REC *server, const char *channel,
491 const char *nick, const char *address)
495 str = show_lowascii(channel);
496 printformat(server, NULL, MSGLEVEL_INVITES,
497 TXT_INVITE, nick, str, address);
501 static void sig_message_topic(SERVER_REC *server, const char *channel,
503 const char *nick, const char *address)
505 printformat(server, channel, MSGLEVEL_TOPICS,
506 *topic != '\0' ? TXT_NEW_TOPIC : TXT_TOPIC_UNSET,
507 nick, channel, topic, address);
510 static int printnick_exists(NICK_REC *first, NICK_REC *ignore,
515 while (first != NULL) {
516 if (first != ignore) {
517 printnick = g_hash_table_lookup(printnicks, first);
518 if (printnick != NULL && strcmp(printnick, nick) == 0)
528 static NICK_REC *printnick_find_original(NICK_REC *nick)
530 while (nick != NULL) {
531 if (g_hash_table_lookup(printnicks, nick) == NULL)
540 static void sig_nicklist_new(CHANNEL_REC *channel, NICK_REC *nick)
547 if (nick->host == NULL)
550 firstnick = g_hash_table_lookup(channel->nicks, nick->nick);
551 if (firstnick->next == NULL)
554 if (nick == channel->ownnick) {
555 /* own nick is being added, might be a nick change and
556 someone else having the original nick already in use.. */
557 nick = printnick_find_original(firstnick->next);
559 return; /* nope, we have it */
562 /* identical nick already exists, have to change it somehow.. */
563 p = strchr(nick->host, '@');
564 if (p == NULL) p = nick->host; else p++;
566 nickhost = g_strdup_printf("%s@%s", nick->nick, p);
567 p = strchr(nickhost+strlen(nick->nick), '.');
568 if (p != NULL) *p = '\0';
570 if (!printnick_exists(firstnick, nick, nickhost)) {
572 g_hash_table_insert(printnicks, nick, nickhost);
576 newnick = g_string_new(NULL);
579 g_string_printf(newnick, "%s%d", nickhost, n);
581 } while (printnick_exists(firstnick, nick, newnick->str));
583 g_hash_table_insert(printnicks, nick, newnick->str);
584 g_string_free(newnick, FALSE);
588 static void sig_nicklist_remove(CHANNEL_REC *channel, NICK_REC *nick)
592 nickname = g_hash_table_lookup(printnicks, nick);
593 if (nickname != NULL) {
595 g_hash_table_remove(printnicks, nick);
599 static void sig_nicklist_changed(CHANNEL_REC *channel, NICK_REC *nick)
601 sig_nicklist_remove(channel, nick);
602 sig_nicklist_new(channel, nick);
605 static void sig_channel_joined(CHANNEL_REC *channel)
610 /* channel->ownnick is set at this point - check if our own nick
611 has been changed, if it was set it back to the original nick and
612 change the previous original to something else */
614 nickname = g_hash_table_lookup(printnicks, channel->ownnick);
615 if (nickname == NULL)
619 g_hash_table_remove(printnicks, channel->ownnick);
621 /* our own nick is guaranteed to be the first in list */
622 nick = channel->ownnick->next;
623 while (nick != NULL) {
624 if (g_hash_table_lookup(printnicks, nick) == NULL) {
625 sig_nicklist_new(channel, nick);
632 static void g_hash_free_value(void *key, void *value)
637 void fe_messages_init(void)
639 printnicks = g_hash_table_new((GHashFunc) g_direct_hash,
640 (GCompareFunc) g_direct_equal);
642 settings_add_bool("lookandfeel", "hilight_nick_matches", TRUE);
643 settings_add_bool("lookandfeel", "emphasis", TRUE);
644 settings_add_bool("lookandfeel", "emphasis_replace", FALSE);
645 settings_add_bool("lookandfeel", "emphasis_multiword", FALSE);
646 settings_add_bool("lookandfeel", "show_nickmode", TRUE);
647 settings_add_bool("lookandfeel", "show_nickmode_empty", TRUE);
648 settings_add_bool("lookandfeel", "print_active_channel", FALSE);
649 settings_add_bool("lookandfeel", "show_quit_once", FALSE);
650 settings_add_bool("lookandfeel", "show_own_nickchange_once", FALSE);
652 signal_add_last("message public", (SIGNAL_FUNC) sig_message_public);
653 signal_add_last("message private", (SIGNAL_FUNC) sig_message_private);
654 signal_add_last("message own_public", (SIGNAL_FUNC) sig_message_own_public);
655 signal_add_last("message own_private", (SIGNAL_FUNC) sig_message_own_private);
656 signal_add_last("message join", (SIGNAL_FUNC) sig_message_join);
657 signal_add_last("message part", (SIGNAL_FUNC) sig_message_part);
658 signal_add_last("message quit", (SIGNAL_FUNC) sig_message_quit);
659 signal_add_last("message kick", (SIGNAL_FUNC) sig_message_kick);
660 signal_add_last("message nick", (SIGNAL_FUNC) sig_message_nick);
661 signal_add_last("message own_nick", (SIGNAL_FUNC) sig_message_own_nick);
662 signal_add_last("message invite", (SIGNAL_FUNC) sig_message_invite);
663 signal_add_last("message topic", (SIGNAL_FUNC) sig_message_topic);
665 signal_add("nicklist new", (SIGNAL_FUNC) sig_nicklist_new);
666 signal_add("nicklist remove", (SIGNAL_FUNC) sig_nicklist_remove);
667 signal_add("nicklist changed", (SIGNAL_FUNC) sig_nicklist_changed);
668 signal_add("nicklist host changed", (SIGNAL_FUNC) sig_nicklist_new);
669 signal_add("channel joined", (SIGNAL_FUNC) sig_channel_joined);
672 void fe_messages_deinit(void)
674 g_hash_table_foreach(printnicks, (GHFunc) g_hash_free_value, NULL);
675 g_hash_table_destroy(printnicks);
677 signal_remove("message public", (SIGNAL_FUNC) sig_message_public);
678 signal_remove("message private", (SIGNAL_FUNC) sig_message_private);
679 signal_remove("message own_public", (SIGNAL_FUNC) sig_message_own_public);
680 signal_remove("message own_private", (SIGNAL_FUNC) sig_message_own_private);
681 signal_remove("message join", (SIGNAL_FUNC) sig_message_join);
682 signal_remove("message part", (SIGNAL_FUNC) sig_message_part);
683 signal_remove("message quit", (SIGNAL_FUNC) sig_message_quit);
684 signal_remove("message kick", (SIGNAL_FUNC) sig_message_kick);
685 signal_remove("message nick", (SIGNAL_FUNC) sig_message_nick);
686 signal_remove("message own_nick", (SIGNAL_FUNC) sig_message_own_nick);
687 signal_remove("message invite", (SIGNAL_FUNC) sig_message_invite);
688 signal_remove("message topic", (SIGNAL_FUNC) sig_message_topic);
690 signal_remove("nicklist new", (SIGNAL_FUNC) sig_nicklist_new);
691 signal_remove("nicklist remove", (SIGNAL_FUNC) sig_nicklist_remove);
692 signal_remove("nicklist changed", (SIGNAL_FUNC) sig_nicklist_changed);
693 signal_remove("nicklist host changed", (SIGNAL_FUNC) sig_nicklist_new);
694 signal_remove("channel joined", (SIGNAL_FUNC) sig_channel_joined);