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->disconnected) {
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 /* already joined to channel, set it active */
136 window = window_item_window(channel);
137 if (window != active_win)
138 window_set_active(window);
140 window_item_set_active(active_win, (WI_ITEM_REC *) channel);
143 static void cmd_wjoin_post(const char *data)
149 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
150 PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
151 "join", &optlist, &nick))
154 if (g_hash_table_lookup(optlist, "window") != NULL) {
155 signal_remove("channel created",
156 (SIGNAL_FUNC) signal_channel_created_curwin);
158 cmd_params_free(free_arg);
161 static void cmd_channel_list_joined(void)
163 CHANNEL_REC *channel;
165 GSList *nicklist, *tmp, *ntmp;
167 if (channels == NULL) {
168 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_NOT_IN_CHANNELS);
172 /* print active channel */
173 channel = CHANNEL(active_win->active);
175 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CURRENT_CHANNEL, channel->name);
177 /* print list of all channels, their modes, server tags and nicks */
178 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANLIST_HEADER);
179 for (tmp = channels; tmp != NULL; tmp = tmp->next) {
182 nicklist = nicklist_getnicks(channel);
183 nicks = g_string_new(NULL);
184 for (ntmp = nicklist; ntmp != NULL; ntmp = ntmp->next) {
185 NICK_REC *rec = ntmp->data;
187 g_string_sprintfa(nicks, "%s ", rec->nick);
190 if (nicks->len > 1) g_string_truncate(nicks, nicks->len-1);
191 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANLIST_LINE,
192 channel->name, channel->mode, channel->server->tag, nicks->str);
194 g_slist_free(nicklist);
195 g_string_free(nicks, TRUE);
199 /* SYNTAX: CHANNEL LIST */
200 static void cmd_channel_list(void)
205 str = g_string_new(NULL);
206 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANSETUP_HEADER);
207 for (tmp = setupchannels; tmp != NULL; tmp = tmp->next) {
208 CHANNEL_SETUP_REC *rec = tmp->data;
210 g_string_truncate(str, 0);
212 g_string_append(str, "autojoin, ");
213 if (rec->botmasks != NULL && *rec->botmasks != '\0')
214 g_string_sprintfa(str, "bots: %s, ", rec->botmasks);
215 if (rec->autosendcmd != NULL && *rec->autosendcmd != '\0')
216 g_string_sprintfa(str, "botcmd: %s, ", rec->autosendcmd);
218 if (str->len > 2) g_string_truncate(str, str->len-2);
219 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANSETUP_LINE,
220 rec->name, rec->chatnet == NULL ? "" : rec->chatnet,
221 rec->password == NULL ? "" : rec->password, str->str);
223 g_string_free(str, TRUE);
224 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_CHANSETUP_FOOTER);
227 static void cmd_channel(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
230 cmd_channel_list_joined();
231 else if (server != NULL && server_ischannel(server, data)) {
232 signal_emit("command join", 3, data, server, item);
234 command_runsub("channel", data, server, item);
238 /* SYNTAX: CHANNEL ADD [-auto | -noauto] [-bots <masks>] [-botcmd <command>]
239 <channel> <chatnet> [<password>] */
240 static void cmd_channel_add(const char *data)
243 CHATNET_REC *chatnetrec;
244 CHANNEL_SETUP_REC *rec;
245 char *botarg, *botcmdarg, *chatnet, *channel, *password;
248 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTIONS,
249 "channel add", &optlist, &channel, &chatnet, &password))
252 if (*chatnet == '\0' || *channel == '\0')
253 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
255 chatnetrec = chatnet_find(chatnet);
256 if (chatnetrec == NULL) {
257 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
258 TXT_UNKNOWN_CHATNET, chatnet);
259 cmd_params_free(free_arg);
263 botarg = g_hash_table_lookup(optlist, "bots");
264 botcmdarg = g_hash_table_lookup(optlist, "botcmd");
266 rec = channel_setup_find(channel, chatnet);
268 rec = CHAT_PROTOCOL(chatnetrec)->create_channel_setup();
269 rec->name = g_strdup(channel);
270 rec->chatnet = g_strdup(chatnet);
272 if (g_hash_table_lookup(optlist, "bots")) g_free_and_null(rec->botmasks);
273 if (g_hash_table_lookup(optlist, "botcmd")) g_free_and_null(rec->autosendcmd);
274 if (*password != '\0') g_free_and_null(rec->password);
276 if (g_hash_table_lookup(optlist, "auto")) rec->autojoin = TRUE;
277 if (g_hash_table_lookup(optlist, "noauto")) rec->autojoin = FALSE;
278 if (botarg != NULL && *botarg != '\0') rec->botmasks = g_strdup(botarg);
279 if (botcmdarg != NULL && *botcmdarg != '\0') rec->autosendcmd = g_strdup(botcmdarg);
280 if (*password != '\0' && strcmp(password, "-") != 0) rec->password = g_strdup(password);
282 signal_emit("channel add fill", 2, rec, optlist);
284 channel_setup_create(rec);
285 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
286 TXT_CHANSETUP_ADDED, channel, chatnet);
288 cmd_params_free(free_arg);
291 /* SYNTAX: CHANNEL REMOVE <channel> <chatnet> */
292 static void cmd_channel_remove(const char *data)
294 CHANNEL_SETUP_REC *rec;
295 char *chatnet, *channel;
298 if (!cmd_get_params(data, &free_arg, 2, &channel, &chatnet))
300 if (*chatnet == '\0' || *channel == '\0')
301 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
303 rec = channel_setup_find(channel, chatnet);
305 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CHANSETUP_NOT_FOUND, channel, chatnet);
307 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CHANSETUP_REMOVED, channel, chatnet);
308 channel_setup_remove(rec);
310 cmd_params_free(free_arg);
313 static int get_nick_length(void *data)
315 return strlen(((NICK_REC *) data)->nick);
318 static void display_sorted_nicks(CHANNEL_REC *channel, GSList *nicklist)
324 char *format, *stripped, *prefix_format;
325 char *linebuf, nickmode[2] = { 0, 0 };
326 int *columns, cols, rows, last_col_rows, col, row, max_width;
327 int item_extra, linebuf_size, formatnum;
329 window = window_find_closest(channel->server, channel->name,
330 MSGLEVEL_CLIENTCRAP);
331 max_width = window->width;
333 /* get the length of item extra stuff ("[ ] ") */
334 format = format_get_text(MODULE_NAME, NULL,
335 channel->server, channel->name,
336 TXT_NAMES_NICK, " ", "");
337 stripped = strip_codes(format);
338 item_extra = strlen(stripped);
342 if (settings_get_int("names_max_width") > 0 &&
343 settings_get_int("names_max_width") < max_width)
344 max_width = settings_get_int("names_max_width");
346 /* remove width of the timestamp from max_width */
347 format_create_dest(&dest, channel->server, channel->name,
348 MSGLEVEL_CLIENTCRAP, NULL);
349 format = format_get_line_start(current_theme, &dest, time(NULL));
350 if (format != NULL) {
351 stripped = strip_codes(format);
352 max_width -= strlen(stripped);
357 /* remove width of the prefix from max_width */
358 prefix_format = format_get_text(MODULE_NAME, NULL,
359 channel->server, channel->name,
360 TXT_NAMES_PREFIX, channel->name);
361 if (prefix_format != NULL) {
362 stripped = strip_codes(prefix_format);
363 max_width -= strlen(stripped);
367 if (max_width <= 0) {
368 /* we should always have at least some space .. if we
369 really don't, it won't show properly anyway. */
373 /* calculate columns */
374 cols = get_max_column_count(nicklist, get_nick_length, max_width,
375 settings_get_int("names_max_columns"),
376 item_extra, 3, &columns, &rows);
377 nicklist = columns_sort_list(nicklist, rows);
379 /* rows in last column */
380 last_col_rows = rows-(cols*rows-g_slist_length(nicklist));
381 if (last_col_rows == 0)
382 last_col_rows = rows;
384 str = g_string_new(prefix_format);
385 linebuf_size = max_width+1; linebuf = g_malloc(linebuf_size);
388 for (tmp = nicklist; tmp != NULL; tmp = tmp->next) {
389 NICK_REC *rec = tmp->data;
393 else if (rec->halfop)
400 if (linebuf_size < columns[col]-item_extra+1) {
401 linebuf_size = (columns[col]-item_extra+1)*2;
402 linebuf = g_realloc(linebuf, linebuf_size);
404 memset(linebuf, ' ', columns[col]-item_extra);
405 linebuf[columns[col]-item_extra] = '\0';
406 memcpy(linebuf, rec->nick, strlen(rec->nick));
408 formatnum = rec->op ? TXT_NAMES_NICK_OP :
409 rec->halfop ? TXT_NAMES_NICK_HALFOP :
410 rec->voice ? TXT_NAMES_NICK_VOICE :
412 format = format_get_text(MODULE_NAME, NULL,
413 channel->server, channel->name,
414 formatnum, nickmode, linebuf);
415 g_string_append(str, format);
419 printtext(channel->server, channel->name,
420 MSGLEVEL_CLIENTCRAP, "%s", str->str);
421 g_string_truncate(str, 0);
422 if (prefix_format != NULL)
423 g_string_assign(str, prefix_format);
426 if (row == last_col_rows)
431 if (str->len > strlen(prefix_format)) {
432 printtext(channel->server, channel->name,
433 MSGLEVEL_CLIENTCRAP, "%s", str->str);
436 g_slist_free(nicklist);
437 g_string_free(str, TRUE);
438 g_free_not_null(columns);
439 g_free_not_null(prefix_format);
443 void fe_channels_nicklist(CHANNEL_REC *channel, int flags)
446 GSList *tmp, *nicklist, *sorted;
447 int nicks, normal, voices, halfops, ops;
449 nicks = normal = voices = halfops = ops = 0;
450 nicklist = nicklist_getnicks(channel);
453 /* sort the nicklist */
454 for (tmp = nicklist; tmp != NULL; tmp = tmp->next) {
460 if ((flags & CHANNEL_NICKLIST_FLAG_OPS) == 0)
462 } else if (nick->halfop) {
464 if ((flags & CHANNEL_NICKLIST_FLAG_HALFOPS) == 0)
466 } else if (nick->voice) {
468 if ((flags & CHANNEL_NICKLIST_FLAG_VOICES) == 0)
472 if ((flags & CHANNEL_NICKLIST_FLAG_NORMAL) == 0)
476 sorted = g_slist_insert_sorted(sorted, nick, (GCompareFunc)
479 g_slist_free(nicklist);
481 /* display the nicks */
482 if ((flags & CHANNEL_NICKLIST_FLAG_COUNT) == 0) {
483 printformat(channel->server, channel->name,
484 MSGLEVEL_CLIENTCRAP, TXT_NAMES, channel->name, nicks, ops, halfops, voices, normal);
485 display_sorted_nicks(channel, sorted);
487 g_slist_free(sorted);
489 printformat(channel->server, channel->name,
490 MSGLEVEL_CLIENTNOTICE, TXT_ENDOFNAMES,
491 channel->name, nicks, ops, halfops, voices, normal);
494 /* SYNTAX: NAMES [-count | -ops -halfops -voices -normal] [<channels> | **] */
495 static void cmd_names(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
497 CHANNEL_REC *chanrec;
500 char *channel, **channels, **tmp;
504 g_return_if_fail(data != NULL);
505 if (!IS_SERVER(server) || !server->connected)
506 cmd_return_error(CMDERR_NOT_CONNECTED);
508 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
509 "names", &optlist, &channel))
512 if (strcmp(channel, "*") == 0 || *channel == '\0') {
513 if (!IS_CHANNEL(item))
514 cmd_param_error(CMDERR_NOT_JOINED);
516 channel = item->name;
520 if (g_hash_table_lookup(optlist, "ops") != NULL)
521 flags |= CHANNEL_NICKLIST_FLAG_OPS;
522 if (g_hash_table_lookup(optlist, "halfops") != NULL)
523 flags |= CHANNEL_NICKLIST_FLAG_HALFOPS;
524 if (g_hash_table_lookup(optlist, "voices") != NULL)
525 flags |= CHANNEL_NICKLIST_FLAG_VOICES;
526 if (g_hash_table_lookup(optlist, "normal") != NULL)
527 flags |= CHANNEL_NICKLIST_FLAG_NORMAL;
528 if (g_hash_table_lookup(optlist, "count") != NULL)
529 flags |= CHANNEL_NICKLIST_FLAG_COUNT;
531 if (flags == 0) flags = CHANNEL_NICKLIST_FLAG_ALL;
533 unknowns = g_string_new(NULL);
535 channels = g_strsplit(channel, ",", -1);
536 for (tmp = channels; *tmp != NULL; tmp++) {
537 chanrec = channel_find(server, *tmp);
539 g_string_sprintfa(unknowns, "%s,", *tmp);
541 fe_channels_nicklist(chanrec, flags);
545 g_strfreev(channels);
547 if (unknowns->len > 1)
548 g_string_truncate(unknowns, unknowns->len-1);
550 if (unknowns->len > 0 && strcmp(channel, unknowns->str) != 0)
551 signal_emit("command names", 3, unknowns->str, server, item);
552 g_string_free(unknowns, TRUE);
554 cmd_params_free(free_arg);
557 /* SYNTAX: CYCLE [<channel>] [<message>] */
558 static void cmd_cycle(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
560 CHANNEL_REC *chanrec;
561 char *channame, *msg, *joindata;
564 g_return_if_fail(data != NULL);
565 if (!IS_SERVER(server) || !server->connected)
566 cmd_return_error(CMDERR_NOT_CONNECTED);
568 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTCHAN,
569 item, &channame, &msg))
571 if (*channame == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
573 chanrec = channel_find(server, channame);
574 if (chanrec == NULL) cmd_param_error(CMDERR_CHAN_NOT_FOUND);
576 joindata = chanrec->get_join_data(chanrec);
577 window_bind_add(window_item_window(chanrec),
578 chanrec->server->tag, chanrec->name);
580 /* FIXME: kludgy kludgy... */
581 signal_emit("command part", 3, data, server, item);
583 if (g_slist_find(channels, chanrec) != NULL) {
584 chanrec->left = TRUE;
585 channel_destroy(chanrec);
588 server->channels_join(server, joindata, FALSE);
591 cmd_params_free(free_arg);
594 void fe_channels_init(void)
596 settings_add_bool("lookandfeel", "autoclose_windows", TRUE);
597 settings_add_int("lookandfeel", "names_max_columns", 6);
598 settings_add_int("lookandfeel", "names_max_width", 0);
600 signal_add("channel created", (SIGNAL_FUNC) signal_channel_created);
601 signal_add("channel destroyed", (SIGNAL_FUNC) signal_channel_destroyed);
602 signal_add_last("window item changed", (SIGNAL_FUNC) signal_window_item_changed);
603 signal_add_last("server disconnected", (SIGNAL_FUNC) sig_disconnected);
605 command_bind_first("join", NULL, (SIGNAL_FUNC) cmd_wjoin_pre);
606 command_bind("join", NULL, (SIGNAL_FUNC) cmd_join);
607 command_bind_last("join", NULL, (SIGNAL_FUNC) cmd_wjoin_post);
608 command_bind("channel", NULL, (SIGNAL_FUNC) cmd_channel);
609 command_bind("channel add", NULL, (SIGNAL_FUNC) cmd_channel_add);
610 command_bind("channel remove", NULL, (SIGNAL_FUNC) cmd_channel_remove);
611 command_bind("channel list", NULL, (SIGNAL_FUNC) cmd_channel_list);
612 command_bind("names", NULL, (SIGNAL_FUNC) cmd_names);
613 command_bind("cycle", NULL, (SIGNAL_FUNC) cmd_cycle);
615 command_set_options("channel add", "auto noauto -bots -botcmd");
616 command_set_options("names", "count ops halfops voices normal");
617 command_set_options("join", "window");
620 void fe_channels_deinit(void)
622 signal_remove("channel created", (SIGNAL_FUNC) signal_channel_created);
623 signal_remove("channel destroyed", (SIGNAL_FUNC) signal_channel_destroyed);
624 signal_remove("window item changed", (SIGNAL_FUNC) signal_window_item_changed);
625 signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
627 command_unbind("join", (SIGNAL_FUNC) cmd_wjoin_pre);
628 command_unbind("join", (SIGNAL_FUNC) cmd_join);
629 command_unbind("join", (SIGNAL_FUNC) cmd_wjoin_post);
630 command_unbind("channel", (SIGNAL_FUNC) cmd_channel);
631 command_unbind("channel add", (SIGNAL_FUNC) cmd_channel_add);
632 command_unbind("channel remove", (SIGNAL_FUNC) cmd_channel_remove);
633 command_unbind("channel list", (SIGNAL_FUNC) cmd_channel_list);
634 command_unbind("names", (SIGNAL_FUNC) cmd_names);
635 command_unbind("cycle", (SIGNAL_FUNC) cmd_cycle);