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"
33 #include "channels-setup.h"
36 #include "fe-windows.h"
37 #include "fe-channels.h"
38 #include "window-items.h"
39 #include "printtext.h"
41 static void signal_channel_created(CHANNEL_REC *channel, void *automatic)
43 if (window_item_window(channel) == NULL) {
44 window_item_create((WI_ITEM_REC *) channel,
45 GPOINTER_TO_INT(automatic));
49 static void signal_channel_created_curwin(CHANNEL_REC *channel)
51 g_return_if_fail(channel != NULL);
53 window_item_add(active_win, (WI_ITEM_REC *) channel, FALSE);
56 static void signal_channel_destroyed(CHANNEL_REC *channel)
60 g_return_if_fail(channel != NULL);
62 window = window_item_window((WI_ITEM_REC *) channel);
66 window_item_destroy((WI_ITEM_REC *) channel);
68 if (channel->joined && !channel->left &&
69 channel->server != NULL) {
70 /* kicked out from channel */
71 window_bind_add(window, channel->server->tag,
73 } else if (!channel->joined || channel->left)
74 window_auto_destroy(window);
77 static void signal_window_item_destroy(WINDOW_REC *window, WI_ITEM_REC *item)
81 g_return_if_fail(window != NULL);
83 channel = CHANNEL(item);
84 if (channel != NULL) channel_destroy(channel);
87 static void sig_disconnected(SERVER_REC *server)
92 g_return_if_fail(IS_SERVER(server));
94 for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
95 CHANNEL_REC *channel = tmp->data;
97 window = window_item_window((WI_ITEM_REC *) channel);
98 window_bind_add(window, server->tag, channel->name);
102 static void signal_window_item_changed(WINDOW_REC *window, WI_ITEM_REC *item)
104 g_return_if_fail(window != NULL);
105 if (item == NULL) return;
107 if (g_slist_length(window->items) > 1 && IS_CHANNEL(item)) {
108 printformat(item->server, item->name, MSGLEVEL_CLIENTNOTICE,
109 TXT_TALKING_IN, item->name);
114 static void cmd_wjoin_pre(const char *data)
120 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
121 PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
122 "join", &optlist, &nick))
125 if (g_hash_table_lookup(optlist, "window") != NULL) {
126 signal_add("channel created",
127 (SIGNAL_FUNC) signal_channel_created_curwin);
129 cmd_params_free(free_arg);
132 static void cmd_join(const char *data, SERVER_REC *server)
135 CHANNEL_REC *channel;
137 if (strchr(data, ' ') != NULL || strchr(data, ',') != NULL)
140 channel = channel_find(server, data);
144 window = window_item_window(channel);
146 if (window == active_win) {
147 /* channel is in active window, set it active */
148 window_item_set_active(active_win,
149 (WI_ITEM_REC *) channel);
151 /* notify user how to move the channel to active
152 window. this was used to be done automatically
153 but it just confused everyone who did it
155 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
156 TXT_CHANNEL_MOVE_NOTIFY, channel->name,
161 static void cmd_wjoin_post(const char *data)
167 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
168 PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
169 "join", &optlist, &nick))
172 if (g_hash_table_lookup(optlist, "window") != NULL) {
173 signal_remove("channel created",
174 (SIGNAL_FUNC) signal_channel_created_curwin);
176 cmd_params_free(free_arg);
179 static void cmd_channel_list_joined(void)
181 CHANNEL_REC *channel;
183 GSList *nicklist, *tmp, *ntmp;
185 if (channels == NULL) {
186 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_NOT_IN_CHANNELS);
190 /* print active channel */
191 channel = CHANNEL(active_win->active);
193 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CURRENT_CHANNEL, channel->name);
195 /* print list of all channels, their modes, server tags and nicks */
196 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANLIST_HEADER);
197 for (tmp = channels; tmp != NULL; tmp = tmp->next) {
200 nicklist = nicklist_getnicks(channel);
201 nicks = g_string_new(NULL);
202 for (ntmp = nicklist; ntmp != NULL; ntmp = ntmp->next) {
203 NICK_REC *rec = ntmp->data;
205 g_string_sprintfa(nicks, "%s ", rec->nick);
208 if (nicks->len > 1) g_string_truncate(nicks, nicks->len-1);
209 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANLIST_LINE,
210 channel->name, channel->mode, channel->server->tag, nicks->str);
212 g_slist_free(nicklist);
213 g_string_free(nicks, TRUE);
217 /* SYNTAX: CHANNEL LIST */
218 static void cmd_channel_list(void)
223 str = g_string_new(NULL);
224 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANSETUP_HEADER);
225 for (tmp = setupchannels; tmp != NULL; tmp = tmp->next) {
226 CHANNEL_SETUP_REC *rec = tmp->data;
228 g_string_truncate(str, 0);
230 g_string_append(str, "autojoin, ");
231 if (rec->botmasks != NULL && *rec->botmasks != '\0')
232 g_string_sprintfa(str, "bots: %s, ", rec->botmasks);
233 if (rec->autosendcmd != NULL && *rec->autosendcmd != '\0')
234 g_string_sprintfa(str, "botcmd: %s, ", rec->autosendcmd);
236 if (str->len > 2) g_string_truncate(str, str->len-2);
237 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANSETUP_LINE,
238 rec->name, rec->chatnet == NULL ? "" : rec->chatnet,
239 rec->password == NULL ? "" : rec->password, str->str);
241 g_string_free(str, TRUE);
242 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANSETUP_FOOTER);
245 static void cmd_channel(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
248 cmd_channel_list_joined();
250 command_runsub("channel", data, server, item);
253 /* SYNTAX: CHANNEL ADD [-auto | -noauto] [-bots <masks>] [-botcmd <command>]
254 <channel> <chatnet> [<password>] */
255 static void cmd_channel_add(const char *data)
258 CHATNET_REC *chatnetrec;
259 CHANNEL_SETUP_REC *rec;
260 char *botarg, *botcmdarg, *chatnet, *channel, *password;
263 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTIONS,
264 "channel add", &optlist, &channel, &chatnet, &password))
267 if (*chatnet == '\0' || *channel == '\0')
268 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
270 chatnetrec = chatnet_find(chatnet);
271 if (chatnetrec == NULL) {
272 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
273 TXT_UNKNOWN_CHATNET, chatnet);
274 cmd_params_free(free_arg);
278 botarg = g_hash_table_lookup(optlist, "bots");
279 botcmdarg = g_hash_table_lookup(optlist, "botcmd");
281 rec = channel_setup_find(channel, chatnet);
283 rec = CHAT_PROTOCOL(chatnetrec)->create_channel_setup();
284 rec->name = g_strdup(channel);
285 rec->chatnet = g_strdup(chatnet);
287 if (g_hash_table_lookup(optlist, "bots")) g_free_and_null(rec->botmasks);
288 if (g_hash_table_lookup(optlist, "botcmd")) g_free_and_null(rec->autosendcmd);
289 if (*password != '\0') g_free_and_null(rec->password);
291 if (g_hash_table_lookup(optlist, "auto")) rec->autojoin = TRUE;
292 if (g_hash_table_lookup(optlist, "noauto")) rec->autojoin = FALSE;
293 if (botarg != NULL && *botarg != '\0') rec->botmasks = g_strdup(botarg);
294 if (botcmdarg != NULL && *botcmdarg != '\0') rec->autosendcmd = g_strdup(botcmdarg);
295 if (*password != '\0' && strcmp(password, "-") != 0) rec->password = g_strdup(password);
297 signal_emit("channel add fill", 2, rec, optlist);
299 channel_setup_create(rec);
300 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
301 TXT_CHANSETUP_ADDED, channel, chatnet);
303 cmd_params_free(free_arg);
306 /* SYNTAX: CHANNEL REMOVE <channel> <chatnet> */
307 static void cmd_channel_remove(const char *data)
309 CHANNEL_SETUP_REC *rec;
310 char *chatnet, *channel;
313 if (!cmd_get_params(data, &free_arg, 2, &channel, &chatnet))
315 if (*chatnet == '\0' || *channel == '\0')
316 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
318 rec = channel_setup_find(channel, chatnet);
320 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CHANSETUP_NOT_FOUND, channel, chatnet);
322 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CHANSETUP_REMOVED, channel, chatnet);
323 channel_setup_remove(rec);
325 cmd_params_free(free_arg);
328 static int get_nick_length(void *data)
330 return strlen(((NICK_REC *) data)->nick);
333 static void display_sorted_nicks(CHANNEL_REC *channel, GSList *nicklist)
339 char *format, *stripped;
340 char *linebuf, nickmode[2] = { 0, 0 };
341 int *columns, cols, rows, last_col_rows, col, row, max_width;
342 int item_extra, linebuf_size;
344 window = window_find_closest(channel->server, channel->name,
345 MSGLEVEL_CLIENTCRAP);
346 max_width = window->width;
348 /* get the length of item extra stuff ("[ ] ") */
349 format = format_get_text(MODULE_NAME, NULL,
350 channel->server, channel->name,
351 TXT_NAMES_NICK, " ", "");
352 stripped = strip_codes(format);
353 item_extra = strlen(stripped);
357 if (settings_get_int("names_max_width") > 0 &&
358 max_width > settings_get_int("names_max_width"))
359 max_width = settings_get_int("names_max_width");
361 /* remove width of timestamp from max_width */
362 format_create_dest(&dest, channel->server, channel->name,
363 MSGLEVEL_CLIENTCRAP, NULL);
364 format = format_get_line_start(current_theme, &dest, time(NULL));
365 if (format != NULL) {
366 stripped = strip_codes(format);
367 max_width -= strlen(stripped);
372 /* calculate columns */
373 cols = get_max_column_count(nicklist, get_nick_length, max_width,
374 settings_get_int("names_max_columns"),
375 item_extra, 3, &columns, &rows);
376 nicklist = columns_sort_list(nicklist, rows);
378 /* rows in last column */
379 last_col_rows = rows-(cols*rows-g_slist_length(nicklist));
380 if (last_col_rows == 0)
381 last_col_rows = rows;
383 str = g_string_new(NULL);
384 linebuf_size = max_width+1; linebuf = g_malloc(linebuf_size);
387 for (tmp = nicklist; tmp != NULL; tmp = tmp->next) {
388 NICK_REC *rec = tmp->data;
390 nickmode[0] = rec->op ? '@' : rec->voice ? '+' : ' ';
392 if (linebuf_size < columns[col]-item_extra+1) {
393 linebuf_size = (columns[col]-item_extra+1)*2;
394 linebuf = g_realloc(linebuf, linebuf_size);
396 memset(linebuf, ' ', columns[col]-item_extra);
397 linebuf[columns[col]-item_extra] = '\0';
398 memcpy(linebuf, rec->nick, strlen(rec->nick));
400 format = format_get_text(MODULE_NAME, NULL,
401 channel->server, channel->name,
402 TXT_NAMES_NICK, nickmode, linebuf);
403 g_string_append(str, format);
407 printtext(channel->server, channel->name,
408 MSGLEVEL_CLIENTCRAP, "%s", str->str);
409 g_string_truncate(str, 0);
412 if (row == last_col_rows)
418 printtext(channel->server, channel->name,
419 MSGLEVEL_CLIENTCRAP, "%s", str->str);
422 g_slist_free(nicklist);
423 g_string_free(str, TRUE);
424 g_free_not_null(columns);
428 void fe_channels_nicklist(CHANNEL_REC *channel, int flags)
431 GSList *tmp, *nicklist, *sorted;
432 int nicks, normal, voices, ops;
434 nicks = normal = voices = ops = 0;
435 nicklist = nicklist_getnicks(channel);
438 /* sort the nicklist */
439 for (tmp = nicklist; tmp != NULL; tmp = tmp->next) {
445 if ((flags & CHANNEL_NICKLIST_FLAG_OPS) == 0)
447 } else if (nick->halfop) {
448 if ((flags & CHANNEL_NICKLIST_FLAG_HALFOPS) == 0)
450 } else if (nick->voice) {
452 if ((flags & CHANNEL_NICKLIST_FLAG_VOICES) == 0)
456 if ((flags & CHANNEL_NICKLIST_FLAG_NORMAL) == 0)
460 sorted = g_slist_insert_sorted(sorted, nick, (GCompareFunc)
463 g_slist_free(nicklist);
465 /* display the nicks */
466 printformat(channel->server, channel->name,
467 MSGLEVEL_CRAP, TXT_NAMES, channel->name, "");
468 display_sorted_nicks(channel, sorted);
469 g_slist_free(sorted);
471 printformat(channel->server, channel->name,
472 MSGLEVEL_CRAP, TXT_ENDOFNAMES,
473 channel->name, nicks, ops, voices, normal);
476 /* SYNTAX: NAMES [-ops -halfops -voices -normal] [<channels> | **] */
477 static void cmd_names(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
479 CHANNEL_REC *chanrec;
482 char *channel, **channels, **tmp;
486 g_return_if_fail(data != NULL);
487 if (!IS_SERVER(server) || !server->connected)
488 cmd_return_error(CMDERR_NOT_CONNECTED);
490 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
491 "names", &optlist, &channel))
494 if (strcmp(channel, "*") == 0 || *channel == '\0') {
495 if (!IS_CHANNEL(item))
496 cmd_param_error(CMDERR_NOT_JOINED);
498 channel = item->name;
502 if (g_hash_table_lookup(optlist, "ops") != NULL)
503 flags |= CHANNEL_NICKLIST_FLAG_OPS;
504 if (g_hash_table_lookup(optlist, "halfops") != NULL)
505 flags |= CHANNEL_NICKLIST_FLAG_HALFOPS;
506 if (g_hash_table_lookup(optlist, "voices") != NULL)
507 flags |= CHANNEL_NICKLIST_FLAG_VOICES;
508 if (g_hash_table_lookup(optlist, "normal") != NULL)
509 flags |= CHANNEL_NICKLIST_FLAG_NORMAL;
511 if (flags == 0) flags = CHANNEL_NICKLIST_FLAG_ALL;
513 unknowns = g_string_new(NULL);
515 channels = g_strsplit(channel, ",", -1);
516 for (tmp = channels; *tmp != NULL; tmp++) {
517 chanrec = channel_find(server, *tmp);
519 g_string_sprintfa(unknowns, "%s,", *tmp);
521 fe_channels_nicklist(chanrec, flags);
525 g_strfreev(channels);
527 if (unknowns->len > 1)
528 g_string_truncate(unknowns, unknowns->len-1);
530 if (unknowns->len > 0 && strcmp(channel, unknowns->str) != 0)
531 signal_emit("command names", 3, unknowns->str, server, item);
532 g_string_free(unknowns, TRUE);
534 cmd_params_free(free_arg);
537 /* SYNTAX: CYCLE [<channel>] [<message>] */
538 static void cmd_cycle(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
540 CHANNEL_REC *chanrec;
541 char *channame, *msg, *joindata;
544 g_return_if_fail(data != NULL);
545 if (!IS_SERVER(server) || !server->connected)
546 cmd_return_error(CMDERR_NOT_CONNECTED);
548 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTCHAN,
549 item, &channame, &msg))
551 if (*channame == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
553 chanrec = channel_find(server, channame);
554 if (chanrec == NULL) cmd_param_error(CMDERR_CHAN_NOT_FOUND);
556 joindata = chanrec->get_join_data(chanrec);
557 window_bind_add(window_item_window(chanrec),
558 chanrec->server->tag, chanrec->name);
559 channel_destroy(chanrec);
561 server->channels_join(server, joindata, FALSE);
564 cmd_params_free(free_arg);
567 void fe_channels_init(void)
569 settings_add_bool("lookandfeel", "autoclose_windows", TRUE);
570 settings_add_int("lookandfeel", "names_max_columns", 6);
571 settings_add_int("lookandfeel", "names_max_width", 0);
573 signal_add("channel created", (SIGNAL_FUNC) signal_channel_created);
574 signal_add("channel destroyed", (SIGNAL_FUNC) signal_channel_destroyed);
575 signal_add_last("window item destroy", (SIGNAL_FUNC) signal_window_item_destroy);
576 signal_add_last("window item changed", (SIGNAL_FUNC) signal_window_item_changed);
577 signal_add_last("server disconnected", (SIGNAL_FUNC) sig_disconnected);
579 command_bind_first("join", NULL, (SIGNAL_FUNC) cmd_wjoin_pre);
580 command_bind("join", NULL, (SIGNAL_FUNC) cmd_join);
581 command_bind_last("join", NULL, (SIGNAL_FUNC) cmd_wjoin_post);
582 command_bind("channel", NULL, (SIGNAL_FUNC) cmd_channel);
583 command_bind("channel add", NULL, (SIGNAL_FUNC) cmd_channel_add);
584 command_bind("channel remove", NULL, (SIGNAL_FUNC) cmd_channel_remove);
585 command_bind("channel list", NULL, (SIGNAL_FUNC) cmd_channel_list);
586 command_bind("names", NULL, (SIGNAL_FUNC) cmd_names);
587 command_bind("cycle", NULL, (SIGNAL_FUNC) cmd_cycle);
589 command_set_options("channel add", "auto noauto -bots -botcmd");
590 command_set_options("names", "ops halfops voices normal");
591 command_set_options("join", "window");
594 void fe_channels_deinit(void)
596 signal_remove("channel created", (SIGNAL_FUNC) signal_channel_created);
597 signal_remove("channel destroyed", (SIGNAL_FUNC) signal_channel_destroyed);
598 signal_remove("window item destroy", (SIGNAL_FUNC) signal_window_item_destroy);
599 signal_remove("window item changed", (SIGNAL_FUNC) signal_window_item_changed);
600 signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
602 command_unbind("join", (SIGNAL_FUNC) cmd_wjoin_pre);
603 command_unbind("join", (SIGNAL_FUNC) cmd_join);
604 command_unbind("join", (SIGNAL_FUNC) cmd_wjoin_post);
605 command_unbind("channel", (SIGNAL_FUNC) cmd_channel);
606 command_unbind("channel add", (SIGNAL_FUNC) cmd_channel_add);
607 command_unbind("channel remove", (SIGNAL_FUNC) cmd_channel_remove);
608 command_unbind("channel list", (SIGNAL_FUNC) cmd_channel_list);
609 command_unbind("names", (SIGNAL_FUNC) cmd_names);
610 command_unbind("cycle", (SIGNAL_FUNC) cmd_cycle);