4 Copyright (C) 1999-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"
30 #include "chat-protocols.h"
34 #include "channels-setup.h"
37 #include "fe-windows.h"
38 #include "fe-channels.h"
39 #include "window-items.h"
40 #include "printtext.h"
42 static void signal_channel_created(CHANNEL_REC *channel, void *automatic)
44 if (window_item_window(channel) == NULL) {
45 window_item_create((WI_ITEM_REC *) channel,
46 GPOINTER_TO_INT(automatic));
50 static void signal_channel_created_curwin(CHANNEL_REC *channel)
52 g_return_if_fail(channel != NULL);
54 window_item_add(active_win, (WI_ITEM_REC *) channel, FALSE);
57 static void signal_channel_destroyed(CHANNEL_REC *channel)
61 g_return_if_fail(channel != NULL);
63 window = window_item_window((WI_ITEM_REC *) channel);
67 window_item_destroy((WI_ITEM_REC *) channel);
69 if (channel->joined && !channel->left &&
70 channel->server != NULL) {
71 /* kicked out from channel */
72 window_bind_add(window, channel->server->tag,
74 } else if (!channel->joined || channel->left)
75 window_auto_destroy(window);
78 static void sig_disconnected(SERVER_REC *server)
83 g_return_if_fail(IS_SERVER(server));
85 for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
86 CHANNEL_REC *channel = tmp->data;
88 window = window_item_window((WI_ITEM_REC *) channel);
89 window_bind_add(window, server->tag, channel->name);
93 static void signal_window_item_changed(WINDOW_REC *window, WI_ITEM_REC *item)
95 g_return_if_fail(window != NULL);
96 if (item == NULL) return;
98 if (g_slist_length(window->items) > 1 && IS_CHANNEL(item)) {
99 printformat(item->server, item->name, MSGLEVEL_CLIENTNOTICE,
100 TXT_TALKING_IN, item->name);
105 static void cmd_wjoin_pre(const char *data)
111 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
112 PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
113 "join", &optlist, &nick))
116 if (g_hash_table_lookup(optlist, "window") != NULL) {
117 signal_add("channel created",
118 (SIGNAL_FUNC) signal_channel_created_curwin);
120 cmd_params_free(free_arg);
123 static void cmd_join(const char *data, SERVER_REC *server)
126 CHANNEL_REC *channel;
128 if (strchr(data, ' ') != NULL || strchr(data, ',') != NULL)
131 channel = channel_find(server, data);
135 window = window_item_window(channel);
137 if (window == active_win) {
138 /* channel is in active window, set it active */
139 window_item_set_active(active_win,
140 (WI_ITEM_REC *) channel);
142 /* notify user how to move the channel to active
143 window. this was used to be done automatically
144 but it just confused everyone who did it
146 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
147 TXT_CHANNEL_MOVE_NOTIFY, channel->name,
152 static void cmd_wjoin_post(const char *data)
158 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
159 PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
160 "join", &optlist, &nick))
163 if (g_hash_table_lookup(optlist, "window") != NULL) {
164 signal_remove("channel created",
165 (SIGNAL_FUNC) signal_channel_created_curwin);
167 cmd_params_free(free_arg);
170 static void cmd_channel_list_joined(void)
172 CHANNEL_REC *channel;
174 GSList *nicklist, *tmp, *ntmp;
176 if (channels == NULL) {
177 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_NOT_IN_CHANNELS);
181 /* print active channel */
182 channel = CHANNEL(active_win->active);
184 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CURRENT_CHANNEL, channel->name);
186 /* print list of all channels, their modes, server tags and nicks */
187 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANLIST_HEADER);
188 for (tmp = channels; tmp != NULL; tmp = tmp->next) {
191 nicklist = nicklist_getnicks(channel);
192 nicks = g_string_new(NULL);
193 for (ntmp = nicklist; ntmp != NULL; ntmp = ntmp->next) {
194 NICK_REC *rec = ntmp->data;
196 g_string_sprintfa(nicks, "%s ", rec->nick);
199 if (nicks->len > 1) g_string_truncate(nicks, nicks->len-1);
200 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANLIST_LINE,
201 channel->name, channel->mode, channel->server->tag, nicks->str);
203 g_slist_free(nicklist);
204 g_string_free(nicks, TRUE);
208 /* SYNTAX: CHANNEL LIST */
209 static void cmd_channel_list(void)
214 str = g_string_new(NULL);
215 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANSETUP_HEADER);
216 for (tmp = setupchannels; tmp != NULL; tmp = tmp->next) {
217 CHANNEL_SETUP_REC *rec = tmp->data;
219 g_string_truncate(str, 0);
221 g_string_append(str, "autojoin, ");
222 if (rec->botmasks != NULL && *rec->botmasks != '\0')
223 g_string_sprintfa(str, "bots: %s, ", rec->botmasks);
224 if (rec->autosendcmd != NULL && *rec->autosendcmd != '\0')
225 g_string_sprintfa(str, "botcmd: %s, ", rec->autosendcmd);
227 if (str->len > 2) g_string_truncate(str, str->len-2);
228 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANSETUP_LINE,
229 rec->name, rec->chatnet == NULL ? "" : rec->chatnet,
230 rec->password == NULL ? "" : rec->password, str->str);
232 g_string_free(str, TRUE);
233 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANSETUP_FOOTER);
236 static void cmd_channel(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
239 cmd_channel_list_joined();
240 else if (server != NULL && server_ischannel(server, data)) {
241 signal_emit("command join", 3, data, server, item);
243 command_runsub("channel", data, server, item);
247 /* SYNTAX: CHANNEL ADD [-auto | -noauto] [-bots <masks>] [-botcmd <command>]
248 <channel> <chatnet> [<password>] */
249 static void cmd_channel_add(const char *data)
252 CHATNET_REC *chatnetrec;
253 CHANNEL_SETUP_REC *rec;
254 char *botarg, *botcmdarg, *chatnet, *channel, *password;
257 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTIONS,
258 "channel add", &optlist, &channel, &chatnet, &password))
261 if (*chatnet == '\0' || *channel == '\0')
262 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
264 chatnetrec = chatnet_find(chatnet);
265 if (chatnetrec == NULL) {
266 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
267 TXT_UNKNOWN_CHATNET, chatnet);
268 cmd_params_free(free_arg);
272 botarg = g_hash_table_lookup(optlist, "bots");
273 botcmdarg = g_hash_table_lookup(optlist, "botcmd");
275 rec = channel_setup_find(channel, chatnet);
277 rec = CHAT_PROTOCOL(chatnetrec)->create_channel_setup();
278 rec->name = g_strdup(channel);
279 rec->chatnet = g_strdup(chatnet);
281 if (g_hash_table_lookup(optlist, "bots")) g_free_and_null(rec->botmasks);
282 if (g_hash_table_lookup(optlist, "botcmd")) g_free_and_null(rec->autosendcmd);
283 if (*password != '\0') g_free_and_null(rec->password);
285 if (g_hash_table_lookup(optlist, "auto")) rec->autojoin = TRUE;
286 if (g_hash_table_lookup(optlist, "noauto")) rec->autojoin = FALSE;
287 if (botarg != NULL && *botarg != '\0') rec->botmasks = g_strdup(botarg);
288 if (botcmdarg != NULL && *botcmdarg != '\0') rec->autosendcmd = g_strdup(botcmdarg);
289 if (*password != '\0' && strcmp(password, "-") != 0) rec->password = g_strdup(password);
291 signal_emit("channel add fill", 2, rec, optlist);
293 channel_setup_create(rec);
294 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
295 TXT_CHANSETUP_ADDED, channel, chatnet);
297 cmd_params_free(free_arg);
300 /* SYNTAX: CHANNEL REMOVE <channel> <chatnet> */
301 static void cmd_channel_remove(const char *data)
303 CHANNEL_SETUP_REC *rec;
304 char *chatnet, *channel;
307 if (!cmd_get_params(data, &free_arg, 2, &channel, &chatnet))
309 if (*chatnet == '\0' || *channel == '\0')
310 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
312 rec = channel_setup_find(channel, chatnet);
314 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CHANSETUP_NOT_FOUND, channel, chatnet);
316 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CHANSETUP_REMOVED, channel, chatnet);
317 channel_setup_remove(rec);
319 cmd_params_free(free_arg);
322 static int get_nick_length(void *data)
324 return strlen(((NICK_REC *) data)->nick);
327 static void display_sorted_nicks(CHANNEL_REC *channel, GSList *nicklist)
333 char *format, *stripped, *prefix_format;
334 char *linebuf, nickmode[2] = { 0, 0 };
335 int *columns, cols, rows, last_col_rows, col, row, max_width;
336 int item_extra, linebuf_size, formatnum;
338 window = window_find_closest(channel->server, channel->name,
339 MSGLEVEL_CLIENTCRAP);
340 max_width = window->width;
342 /* get the length of item extra stuff ("[ ] ") */
343 format = format_get_text(MODULE_NAME, NULL,
344 channel->server, channel->name,
345 TXT_NAMES_NICK, " ", "");
346 stripped = strip_codes(format);
347 item_extra = strlen(stripped);
351 if (settings_get_int("names_max_width") > 0 &&
352 settings_get_int("names_max_width") < max_width)
353 max_width = settings_get_int("names_max_width");
355 /* remove width of the timestamp from max_width */
356 format_create_dest(&dest, channel->server, channel->name,
357 MSGLEVEL_CLIENTCRAP, NULL);
358 format = format_get_line_start(current_theme, &dest, time(NULL));
359 if (format != NULL) {
360 stripped = strip_codes(format);
361 max_width -= strlen(stripped);
366 /* remove width of the prefix from max_width */
367 prefix_format = format_get_text(MODULE_NAME, NULL,
368 channel->server, channel->name,
369 TXT_NAMES_PREFIX, channel->name);
370 if (prefix_format != NULL) {
371 stripped = strip_codes(prefix_format);
372 max_width -= strlen(stripped);
376 if (max_width <= 0) {
377 /* we should always have at least some space .. if we
378 really don't, it won't show properly anyway. */
382 /* calculate columns */
383 cols = get_max_column_count(nicklist, get_nick_length, max_width,
384 settings_get_int("names_max_columns"),
385 item_extra, 3, &columns, &rows);
386 nicklist = columns_sort_list(nicklist, rows);
388 /* rows in last column */
389 last_col_rows = rows-(cols*rows-g_slist_length(nicklist));
390 if (last_col_rows == 0)
391 last_col_rows = rows;
393 str = g_string_new(prefix_format);
394 linebuf_size = max_width+1; linebuf = g_malloc(linebuf_size);
397 for (tmp = nicklist; tmp != NULL; tmp = tmp->next) {
398 NICK_REC *rec = tmp->data;
402 else if (rec->halfop)
409 if (linebuf_size < columns[col]-item_extra+1) {
410 linebuf_size = (columns[col]-item_extra+1)*2;
411 linebuf = g_realloc(linebuf, linebuf_size);
413 memset(linebuf, ' ', columns[col]-item_extra);
414 linebuf[columns[col]-item_extra] = '\0';
415 memcpy(linebuf, rec->nick, strlen(rec->nick));
417 formatnum = rec->op ? TXT_NAMES_NICK_OP :
418 rec->halfop ? TXT_NAMES_NICK_HALFOP :
419 rec->voice ? TXT_NAMES_NICK_VOICE :
421 format = format_get_text(MODULE_NAME, NULL,
422 channel->server, channel->name,
423 formatnum, nickmode, linebuf);
424 g_string_append(str, format);
428 printtext(channel->server, channel->name,
429 MSGLEVEL_CLIENTCRAP, "%s", str->str);
430 g_string_truncate(str, 0);
431 if (prefix_format != NULL)
432 g_string_assign(str, prefix_format);
435 if (row == last_col_rows)
440 if (str->len > strlen(prefix_format)) {
441 printtext(channel->server, channel->name,
442 MSGLEVEL_CLIENTCRAP, "%s", str->str);
445 g_slist_free(nicklist);
446 g_string_free(str, TRUE);
447 g_free_not_null(columns);
448 g_free_not_null(prefix_format);
452 void fe_channels_nicklist(CHANNEL_REC *channel, int flags)
455 GSList *tmp, *nicklist, *sorted;
456 int nicks, normal, voices, halfops, ops;
458 nicks = normal = voices = halfops = ops = 0;
459 nicklist = nicklist_getnicks(channel);
462 /* sort the nicklist */
463 for (tmp = nicklist; tmp != NULL; tmp = tmp->next) {
469 if ((flags & CHANNEL_NICKLIST_FLAG_OPS) == 0)
471 } else if (nick->halfop) {
473 if ((flags & CHANNEL_NICKLIST_FLAG_HALFOPS) == 0)
475 } else if (nick->voice) {
477 if ((flags & CHANNEL_NICKLIST_FLAG_VOICES) == 0)
481 if ((flags & CHANNEL_NICKLIST_FLAG_NORMAL) == 0)
485 sorted = g_slist_insert_sorted(sorted, nick, (GCompareFunc)
488 g_slist_free(nicklist);
490 /* display the nicks */
491 if ((flags & CHANNEL_NICKLIST_FLAG_COUNT) == 0) {
492 printformat(channel->server, channel->name,
493 MSGLEVEL_CLIENTCRAP, TXT_NAMES, channel->name, nicks, ops, halfops, voices, normal);
494 display_sorted_nicks(channel, sorted);
496 g_slist_free(sorted);
498 printformat(channel->server, channel->name,
499 MSGLEVEL_CLIENTNOTICE, TXT_ENDOFNAMES,
500 channel->name, nicks, ops, halfops, voices, normal);
503 /* SYNTAX: NAMES [-count | -ops -halfops -voices -normal] [<channels> | **] */
504 static void cmd_names(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
506 CHANNEL_REC *chanrec;
509 char *channel, **channels, **tmp;
513 g_return_if_fail(data != NULL);
514 if (!IS_SERVER(server) || !server->connected)
515 cmd_return_error(CMDERR_NOT_CONNECTED);
517 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
518 "names", &optlist, &channel))
521 if (strcmp(channel, "*") == 0 || *channel == '\0') {
522 if (!IS_CHANNEL(item))
523 cmd_param_error(CMDERR_NOT_JOINED);
525 channel = item->name;
529 if (g_hash_table_lookup(optlist, "ops") != NULL)
530 flags |= CHANNEL_NICKLIST_FLAG_OPS;
531 if (g_hash_table_lookup(optlist, "halfops") != NULL)
532 flags |= CHANNEL_NICKLIST_FLAG_HALFOPS;
533 if (g_hash_table_lookup(optlist, "voices") != NULL)
534 flags |= CHANNEL_NICKLIST_FLAG_VOICES;
535 if (g_hash_table_lookup(optlist, "normal") != NULL)
536 flags |= CHANNEL_NICKLIST_FLAG_NORMAL;
537 if (g_hash_table_lookup(optlist, "count") != NULL)
538 flags |= CHANNEL_NICKLIST_FLAG_COUNT;
540 if (flags == 0) flags = CHANNEL_NICKLIST_FLAG_ALL;
542 unknowns = g_string_new(NULL);
544 channels = g_strsplit(channel, ",", -1);
545 for (tmp = channels; *tmp != NULL; tmp++) {
546 chanrec = channel_find(server, *tmp);
548 g_string_sprintfa(unknowns, "%s,", *tmp);
550 fe_channels_nicklist(chanrec, flags);
554 g_strfreev(channels);
556 if (unknowns->len > 1)
557 g_string_truncate(unknowns, unknowns->len-1);
559 if (unknowns->len > 0 && strcmp(channel, unknowns->str) != 0)
560 signal_emit("command names", 3, unknowns->str, server, item);
561 g_string_free(unknowns, TRUE);
563 cmd_params_free(free_arg);
566 /* SYNTAX: CYCLE [<channel>] [<message>] */
567 static void cmd_cycle(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
569 CHANNEL_REC *chanrec;
570 char *channame, *msg, *joindata;
573 g_return_if_fail(data != NULL);
574 if (!IS_SERVER(server) || !server->connected)
575 cmd_return_error(CMDERR_NOT_CONNECTED);
577 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTCHAN,
578 item, &channame, &msg))
580 if (*channame == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
582 chanrec = channel_find(server, channame);
583 if (chanrec == NULL) cmd_param_error(CMDERR_CHAN_NOT_FOUND);
585 joindata = chanrec->get_join_data(chanrec);
586 window_bind_add(window_item_window(chanrec),
587 chanrec->server->tag, chanrec->name);
589 /* FIXME: kludgy kludgy... and it relies on channel not
590 being destroyed immediately.. */
591 signal_emit("command part", 3, data, server, item);
592 chanrec->left = TRUE;
593 channel_destroy(chanrec);
595 server->channels_join(server, joindata, FALSE);
598 cmd_params_free(free_arg);
601 void fe_channels_init(void)
603 settings_add_bool("lookandfeel", "autoclose_windows", TRUE);
604 settings_add_int("lookandfeel", "names_max_columns", 6);
605 settings_add_int("lookandfeel", "names_max_width", 0);
607 signal_add("channel created", (SIGNAL_FUNC) signal_channel_created);
608 signal_add("channel destroyed", (SIGNAL_FUNC) signal_channel_destroyed);
609 signal_add_last("window item changed", (SIGNAL_FUNC) signal_window_item_changed);
610 signal_add_last("server disconnected", (SIGNAL_FUNC) sig_disconnected);
612 command_bind_first("join", NULL, (SIGNAL_FUNC) cmd_wjoin_pre);
613 command_bind("join", NULL, (SIGNAL_FUNC) cmd_join);
614 command_bind_last("join", NULL, (SIGNAL_FUNC) cmd_wjoin_post);
615 command_bind("channel", NULL, (SIGNAL_FUNC) cmd_channel);
616 command_bind("channel add", NULL, (SIGNAL_FUNC) cmd_channel_add);
617 command_bind("channel remove", NULL, (SIGNAL_FUNC) cmd_channel_remove);
618 command_bind("channel list", NULL, (SIGNAL_FUNC) cmd_channel_list);
619 command_bind("names", NULL, (SIGNAL_FUNC) cmd_names);
620 command_bind("cycle", NULL, (SIGNAL_FUNC) cmd_cycle);
622 command_set_options("channel add", "auto noauto -bots -botcmd");
623 command_set_options("names", "count ops halfops voices normal");
624 command_set_options("join", "window");
627 void fe_channels_deinit(void)
629 signal_remove("channel created", (SIGNAL_FUNC) signal_channel_created);
630 signal_remove("channel destroyed", (SIGNAL_FUNC) signal_channel_destroyed);
631 signal_remove("window item changed", (SIGNAL_FUNC) signal_window_item_changed);
632 signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
634 command_unbind("join", (SIGNAL_FUNC) cmd_wjoin_pre);
635 command_unbind("join", (SIGNAL_FUNC) cmd_join);
636 command_unbind("join", (SIGNAL_FUNC) cmd_wjoin_post);
637 command_unbind("channel", (SIGNAL_FUNC) cmd_channel);
638 command_unbind("channel add", (SIGNAL_FUNC) cmd_channel_add);
639 command_unbind("channel remove", (SIGNAL_FUNC) cmd_channel_remove);
640 command_unbind("channel list", (SIGNAL_FUNC) cmd_channel_list);
641 command_unbind("names", (SIGNAL_FUNC) cmd_names);
642 command_unbind("cycle", (SIGNAL_FUNC) cmd_cycle);