More robust CAP management and CAP multi-prefix support
[rofl0r-ixchat.git] / src / common / outbound.c
blobb1861fb853b5f3dcf9fd557704475812da848929
1 /* X-Chat
2 * Copyright (C) 1998 Peter Zelezny.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 #define _GNU_SOURCE /* for memrchr */
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <ctype.h>
24 #include <limits.h>
25 #include <errno.h>
27 #define WANTSOCKET
28 #define WANTARPA
29 #include "inet.h"
31 #include <sys/wait.h>
33 #include <unistd.h>
34 #include <time.h>
35 #include <signal.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
39 #include "xchat.h"
40 #include "plugin.h"
41 #include "ignore.h"
42 #include "util.h"
43 #include "fe.h"
44 #include "cfgfiles.h" /* xchat_fopen_file() */
45 #include "network.h" /* net_ip() */
46 #include "modes.h"
47 #include "notify.h"
48 #include "inbound.h"
49 #include "text.h"
50 #include "xchatc.h"
51 #include "servlist.h"
52 #include "server.h"
53 #include "tree.h"
54 #include "outbound.h"
57 #ifdef USE_DEBUG
58 extern int current_mem_usage;
59 #endif
60 #define TBUFSIZE 4096
62 static void help (session *sess, char *tbuf, char *helpcmd, int quiet);
63 static int cmd_server (session *sess, char *tbuf, char *word[], char *word_eol[]);
64 static void handle_say (session *sess, char *text, int check_spch);
67 static void
68 notj_msg (struct session *sess)
70 PrintText (sess, _("No channel joined. Try /join #<channel>\n"));
73 void
74 notc_msg (struct session *sess)
76 PrintText (sess, _("Not connected. Try /server <host> [<port>]\n"));
79 static char *
80 random_line (char *file_name)
82 FILE *fh;
83 char buf[512];
84 int lines, ran;
86 if (!file_name[0])
87 goto nofile;
89 fh = xchat_fopen_file (file_name, "r", 0);
90 if (!fh)
92 nofile:
93 /* reason is not a file, an actual reason! */
94 return strdup (file_name);
97 /* count number of lines in file */
98 lines = 0;
99 while (fgets (buf, sizeof (buf), fh))
100 lines++;
102 if (lines < 1)
103 goto nofile;
105 /* go down a random number */
106 rewind (fh);
107 ran = RAND_INT (lines);
110 fgets (buf, sizeof (buf), fh);
111 lines--;
113 while (lines > ran);
114 fclose (fh);
115 buf[strlen (buf) - 1] = 0; /* remove the trailing '\n' */
116 return strdup (buf);
119 void
120 server_sendpart (server * serv, char *channel, char *reason)
122 if (!reason)
124 reason = random_line (prefs.partreason);
125 serv->p_part (serv, channel, reason);
126 free (reason);
127 } else
129 /* reason set by /quit, /close argument */
130 serv->p_part (serv, channel, reason);
134 void
135 server_sendquit (session * sess)
137 char *rea, *colrea;
139 if (!sess->quitreason)
141 colrea = strdup (prefs.quitreason);
142 check_special_chars (colrea, FALSE);
143 rea = random_line (colrea);
144 free (colrea);
145 sess->server->p_quit (sess->server, rea);
146 free (rea);
147 } else
149 /* reason set by /quit, /close argument */
150 sess->server->p_quit (sess->server, sess->quitreason);
154 void
155 process_data_init (char *buf, char *cmd, char *word[],
156 char *word_eol[], gboolean handle_quotes,
157 gboolean allow_escape_quotes)
159 int wordcount = 2;
160 int space = FALSE;
161 int quote = FALSE;
162 int j = 0;
163 int len;
165 word[0] = "\000\000";
166 word_eol[0] = "\000\000";
167 word[1] = (char *)buf;
168 word_eol[1] = (char *)cmd;
170 while (1)
172 switch (*cmd)
174 case 0:
175 buf[j] = 0;
176 for (j = wordcount; j < PDIWORDS; j++)
178 word[j] = "\000\000";
179 word_eol[j] = "\000\000";
181 return;
182 case '\042':
183 if (!handle_quotes)
184 goto def;
185 /* two quotes turn into 1 */
186 if (allow_escape_quotes && cmd[1] == '\042')
188 cmd++;
189 goto def;
191 if (quote)
193 quote = FALSE;
194 space = FALSE;
195 } else
196 quote = TRUE;
197 cmd++;
198 break;
199 case ' ':
200 if (!quote)
202 if (!space)
204 buf[j] = 0;
205 j++;
207 if (wordcount < PDIWORDS)
209 word[wordcount] = &buf[j];
210 word_eol[wordcount] = cmd + 1;
211 wordcount++;
214 space = TRUE;
216 cmd++;
217 break;
219 default:
220 def:
221 space = FALSE;
222 len = g_utf8_skip[((unsigned char *)cmd)[0]];
223 if (len == 1)
225 buf[j] = *cmd;
226 j++;
227 cmd++;
228 } else
230 /* skip past a multi-byte utf8 char */
231 memcpy (buf + j, cmd, len);
232 j += len;
233 cmd += len;
239 static int
240 cmd_addbutton (struct session *sess, char *tbuf, char *word[],
241 char *word_eol[])
243 if (*word[2] && *word_eol[3])
245 if (sess->type == SESS_DIALOG)
247 list_addentry (&dlgbutton_list, word_eol[3], word[2]);
248 fe_dlgbuttons_update (sess);
249 } else
251 list_addentry (&button_list, word_eol[3], word[2]);
252 fe_buttons_update (sess);
254 return TRUE;
256 return FALSE;
259 static int
260 cmd_allchannels (session *sess, char *tbuf, char *word[], char *word_eol[])
262 GSList *list = sess_list;
264 if (!*word_eol[2])
265 return FALSE;
267 while (list)
269 sess = list->data;
270 if (sess->type == SESS_CHANNEL && sess->channel[0] && sess->server->connected)
272 handle_command (sess, word_eol[2], FALSE);
274 list = list->next;
277 return TRUE;
280 static int
281 cmd_allchannelslocal (session *sess, char *tbuf, char *word[], char *word_eol[])
283 GSList *list = sess_list;
284 server *serv = sess->server;
286 if (!*word_eol[2])
287 return FALSE;
289 while (list)
291 sess = list->data;
292 if (sess->type == SESS_CHANNEL && sess->channel[0] &&
293 sess->server->connected && sess->server == serv)
295 handle_command (sess, word_eol[2], FALSE);
297 list = list->next;
300 return TRUE;
303 static int
304 cmd_allservers (struct session *sess, char *tbuf, char *word[],
305 char *word_eol[])
307 GSList *list;
308 server *serv;
310 if (!*word_eol[2])
311 return FALSE;
313 list = serv_list;
314 while (list)
316 serv = list->data;
317 if (serv->connected)
318 handle_command (serv->front_session, word_eol[2], FALSE);
319 list = list->next;
322 return TRUE;
325 static int
326 cmd_away (struct session *sess, char *tbuf, char *word[], char *word_eol[])
328 GSList *list;
329 char *reason = word_eol[2];
331 if (!(*reason))
333 if (sess->server->is_away)
335 if (sess->server->last_away_reason)
336 PrintTextf (sess, _("Already marked away: %s\n"), sess->server->last_away_reason);
337 return FALSE;
340 if (sess->server->reconnect_away)
341 reason = sess->server->last_away_reason;
342 else
343 /* must manage memory pointed to by random_line() */
344 reason = random_line (prefs.awayreason);
346 sess->server->p_set_away (sess->server, reason);
348 if (prefs.show_away_message)
350 snprintf (tbuf, TBUFSIZE, "me is away: %s", reason);
351 for (list = sess_list; list; list = list->next)
353 /* am I the right server and not a dialog box */
354 if (((struct session *) list->data)->server == sess->server
355 && ((struct session *) list->data)->type == SESS_CHANNEL
356 && ((struct session *) list->data)->channel[0])
358 handle_command ((session *) list->data, tbuf, TRUE);
363 if (sess->server->last_away_reason != reason)
365 if (sess->server->last_away_reason)
366 free (sess->server->last_away_reason);
368 if (reason == word_eol[2])
369 sess->server->last_away_reason = strdup (reason);
370 else
371 sess->server->last_away_reason = reason;
374 if (!sess->server->connected)
375 sess->server->reconnect_away = 1;
377 return TRUE;
380 static int
381 cmd_back (struct session *sess, char *tbuf, char *word[], char *word_eol[])
383 GSList *list;
384 unsigned int gone;
386 if (sess->server->is_away)
388 sess->server->p_set_back (sess->server);
390 if (prefs.show_away_message)
392 gone = time (NULL) - sess->server->away_time;
393 sprintf (tbuf, "me is back (gone %.2d:%.2d:%.2d)", gone / 3600,
394 (gone / 60) % 60, gone % 60);
395 for (list = sess_list; list; list = list->next)
397 /* am I the right server and not a dialog box */
398 if (((struct session *) list->data)->server == sess->server
399 && ((struct session *) list->data)->type == SESS_CHANNEL
400 && ((struct session *) list->data)->channel[0])
402 handle_command ((session *) list->data, tbuf, TRUE);
407 else
409 PrintText (sess, _("Already marked back.\n"));
412 if (sess->server->last_away_reason)
413 free (sess->server->last_away_reason);
414 sess->server->last_away_reason = NULL;
416 return TRUE;
419 static void
420 ban (session * sess, char *tbuf, char *mask, char *bantypestr, int deop)
422 int bantype;
423 struct User *user;
424 char *at, *dot, *lastdot;
425 char username[64], fullhost[128], domain[128], *mode, *p2;
426 server *serv = sess->server;
428 user = userlist_find (sess, mask);
429 if (user && user->hostname) /* it's a nickname, let's find a proper ban mask */
431 if (deop)
433 mode = "-o+b ";
434 p2 = user->nick;
435 } else
437 mode = "+b";
438 p2 = "";
441 mask = user->hostname;
443 at = strchr (mask, '@'); /* FIXME: utf8 */
444 if (!at)
445 return; /* can't happen? */
446 *at = 0;
448 if (mask[0] == '~' || mask[0] == '+' ||
449 mask[0] == '=' || mask[0] == '^' || mask[0] == '-')
451 /* the ident is prefixed with something, we replace that sign with an * */
452 safe_strcpy (username+1, mask+1, sizeof (username)-1);
453 username[0] = '*';
454 } else if (at - mask < USERNAMELEN)
456 /* we just add an * in the begining of the ident */
457 safe_strcpy (username+1, mask, sizeof (username)-1);
458 username[0] = '*';
459 } else
461 /* ident might be too long, we just ban what it gives and add an * in the end */
462 safe_strcpy (username, mask, sizeof (username));
464 *at = '@';
465 safe_strcpy (fullhost, at + 1, sizeof (fullhost));
467 dot = strchr (fullhost, '.');
468 if (dot)
470 safe_strcpy (domain, dot, sizeof (domain));
471 } else
473 safe_strcpy (domain, fullhost, sizeof (domain));
476 if (*bantypestr)
477 bantype = atoi (bantypestr);
478 else
479 bantype = prefs.bantype;
481 tbuf[0] = 0;
482 if (inet_addr (fullhost) != -1) /* "fullhost" is really a IP number */
484 lastdot = strrchr (fullhost, '.');
485 if (!lastdot)
486 return; /* can't happen? */
488 *lastdot = 0;
489 strcpy (domain, fullhost);
490 *lastdot = '.';
492 switch (bantype)
494 case 0:
495 snprintf (tbuf, TBUFSIZE, "%s%s *!*@%s.*", mode, p2, domain);
496 break;
498 case 1:
499 snprintf (tbuf, TBUFSIZE, "%s%s *!*@%s", mode, p2, fullhost);
500 break;
502 case 2:
503 snprintf (tbuf, TBUFSIZE, "%s%s *!%s@%s.*", mode, p2, username, domain);
504 break;
506 case 3:
507 snprintf (tbuf, TBUFSIZE, "%s%s *!%s@%s", mode, p2, username, fullhost);
508 break;
510 } else
512 switch (bantype)
514 case 0:
515 snprintf (tbuf, TBUFSIZE, "%s%s *!*@*%s", mode, p2, domain);
516 break;
518 case 1:
519 snprintf (tbuf, TBUFSIZE, "%s%s *!*@%s", mode, p2, fullhost);
520 break;
522 case 2:
523 snprintf (tbuf, TBUFSIZE, "%s%s *!%s@*%s", mode, p2, username, domain);
524 break;
526 case 3:
527 snprintf (tbuf, TBUFSIZE, "%s%s *!%s@%s", mode, p2, username, fullhost);
528 break;
532 } else
534 snprintf (tbuf, TBUFSIZE, "+b %s", mask);
536 serv->p_mode (serv, sess->channel, tbuf);
539 static int
540 cmd_ban (struct session *sess, char *tbuf, char *word[], char *word_eol[])
542 char *mask = word[2];
544 if (*mask)
546 ban (sess, tbuf, mask, word[3], 0);
547 } else
549 sess->server->p_mode (sess->server, sess->channel, "+b"); /* banlist */
552 return TRUE;
555 static int
556 cmd_unban (struct session *sess, char *tbuf, char *word[], char *word_eol[])
558 /* Allow more than one mask in /unban -- tvk */
559 int i = 2;
561 while (1)
563 if (!*word[i])
565 if (i == 2)
566 return FALSE;
567 send_channel_modes (sess, tbuf, word, 2, i, '-', 'b', 0);
568 return TRUE;
570 i++;
574 static int
575 cmd_chanopt (struct session *sess, char *tbuf, char *word[], char *word_eol[])
577 /* chanopt.c */
578 return chanopt_command (sess, tbuf, word, word_eol);
581 static int
582 cmd_charset (struct session *sess, char *tbuf, char *word[], char *word_eol[])
584 server *serv = sess->server;
585 const char *locale = NULL;
586 int offset = 0;
588 if (strcmp (word[2], "-quiet") == 0)
589 offset++;
591 if (!word[2 + offset][0])
593 g_get_charset (&locale);
594 PrintTextf (sess, "Current charset: %s\n",
595 serv->encoding ? serv->encoding : locale);
596 return TRUE;
599 if (servlist_check_encoding (word[2 + offset]))
601 server_set_encoding (serv, word[2 + offset]);
602 if (offset < 1)
603 PrintTextf (sess, "Charset changed to: %s\n", word[2 + offset]);
604 } else
606 PrintTextf (sess, "\0034Unknown charset:\017 %s\n", word[2 + offset]);
609 return TRUE;
612 static int
613 cmd_clear (struct session *sess, char *tbuf, char *word[], char *word_eol[])
615 GSList *list = sess_list;
616 char *reason = word_eol[2];
618 if (strcasecmp (reason, "HISTORY") == 0)
620 history_free (&sess->history);
621 return TRUE;
624 if (strncasecmp (reason, "all", 3) == 0)
626 while (list)
628 sess = list->data;
629 if (!sess->nick_said)
630 fe_text_clear (list->data, 0);
631 list = list->next;
633 return TRUE;
636 if (reason[0] != '-' && !isdigit (reason[0]) && reason[0] != 0)
637 return FALSE;
639 fe_text_clear (sess, atoi (reason));
640 return TRUE;
643 static int
644 cmd_close (struct session *sess, char *tbuf, char *word[], char *word_eol[])
646 GSList *list;
648 if (strcmp (word[2], "-m") == 0)
650 list = sess_list;
651 while (list)
653 sess = list->data;
654 list = list->next;
655 if (sess->type == SESS_DIALOG)
656 fe_close_window (sess);
658 } else
660 if (*word_eol[2])
661 sess->quitreason = word_eol[2];
662 fe_close_window (sess);
665 return TRUE;
668 static int
669 cmd_ctcp (struct session *sess, char *tbuf, char *word[], char *word_eol[])
671 int mbl;
672 char *to = word[2];
673 if (*to)
675 char *msg = word_eol[3];
676 if (*msg)
678 unsigned char *cmd = (unsigned char *)msg;
680 /* make the first word upper case (as per RFC) */
681 while (1)
683 if (*cmd == ' ' || *cmd == 0)
684 break;
685 mbl = g_utf8_skip[*cmd];
686 if (mbl == 1)
687 *cmd = toupper (*cmd);
688 cmd += mbl;
691 sess->server->p_ctcp (sess->server, to, msg);
693 EMIT_SIGNAL (XP_TE_CTCPSEND, sess, to, msg, NULL, NULL, 0);
695 return TRUE;
698 return FALSE;
701 static int
702 cmd_country (struct session *sess, char *tbuf, char *word[], char *word_eol[])
704 char *code = word[2];
705 if (*code)
707 /* search? */
708 if (strcmp (code, "-s") == 0)
710 country_search (word[3], sess, (void *)PrintTextf);
711 return TRUE;
714 /* search, but forgot the -s */
715 if (strchr (code, '*'))
717 country_search (code, sess, (void *)PrintTextf);
718 return TRUE;
721 sprintf (tbuf, "%s = %s\n", code, country (code));
722 PrintText (sess, tbuf);
723 return TRUE;
725 return FALSE;
728 static int
729 cmd_cycle (struct session *sess, char *tbuf, char *word[], char *word_eol[])
731 char *key = sess->channelkey;
732 char *chan = word[2];
733 if (!*chan)
734 chan = sess->channel;
735 if (*chan && sess->type == SESS_CHANNEL)
737 sess->server->p_cycle (sess->server, chan, key);
738 return TRUE;
740 return FALSE;
743 static int
744 cmd_dcc (struct session *sess, char *tbuf, char *word[], char *word_eol[])
746 int goodtype;
747 struct DCC *dcc = 0;
748 char *type = word[2];
749 if (*type)
751 if (!strcasecmp (type, "HELP"))
752 return FALSE;
753 if (!strcasecmp (type, "CLOSE"))
755 if (*word[3] && *word[4])
757 goodtype = 0;
758 if (!strcasecmp (word[3], "SEND"))
760 dcc = find_dcc (word[4], word[5], TYPE_SEND);
761 dcc_abort (sess, dcc);
762 goodtype = TRUE;
764 if (!strcasecmp (word[3], "GET"))
766 dcc = find_dcc (word[4], word[5], TYPE_RECV);
767 dcc_abort (sess, dcc);
768 goodtype = TRUE;
770 if (!strcasecmp (word[3], "CHAT"))
772 dcc = find_dcc (word[4], "", TYPE_CHATRECV);
773 if (!dcc)
774 dcc = find_dcc (word[4], "", TYPE_CHATSEND);
775 dcc_abort (sess, dcc);
776 goodtype = TRUE;
779 if (!goodtype)
780 return FALSE;
782 if (!dcc)
783 EMIT_SIGNAL (XP_TE_NODCC, sess, NULL, NULL, NULL, NULL, 0);
785 return TRUE;
788 return FALSE;
790 if ((!strcasecmp (type, "CHAT")) || (!strcasecmp (type, "PCHAT")))
792 char *nick = word[3];
793 int passive = (!strcasecmp(type, "PCHAT")) ? 1 : 0;
794 if (*nick)
795 dcc_chat (sess, nick, passive);
796 return TRUE;
798 if (!strcasecmp (type, "LIST"))
800 dcc_show_list (sess);
801 return TRUE;
803 if (!strcasecmp (type, "GET"))
805 char *nick = word[3];
806 char *file = word[4];
807 if (!*file)
809 if (*nick)
810 dcc_get_nick (sess, nick);
811 } else
813 dcc = find_dcc (nick, file, TYPE_RECV);
814 if (dcc)
815 dcc_get (dcc);
816 else
817 EMIT_SIGNAL (XP_TE_NODCC, sess, NULL, NULL, NULL, NULL, 0);
819 return TRUE;
821 if ((!strcasecmp (type, "SEND")) || (!strcasecmp (type, "PSEND")))
823 int i = 3, maxcps;
824 char *nick, *file;
825 int passive = (!strcasecmp(type, "PSEND")) ? 1 : 0;
827 nick = word[i];
828 if (!*nick)
829 return FALSE;
831 maxcps = prefs.dcc_max_send_cps;
832 if (!strncasecmp(nick, "-maxcps=", 8))
834 maxcps = atoi(nick + 8);
835 i++;
836 nick = word[i];
837 if (!*nick)
838 return FALSE;
841 i++;
843 file = word[i];
844 if (!*file)
846 fe_dcc_send_filereq (sess, nick, maxcps, passive);
847 return TRUE;
852 dcc_send (sess, nick, file, maxcps, passive);
853 i++;
854 file = word[i];
856 while (*file);
858 return TRUE;
861 return FALSE;
864 dcc_show_list (sess);
865 return TRUE;
868 static int
869 cmd_debug (struct session *sess, char *tbuf, char *word[], char *word_eol[])
871 struct session *s;
872 struct server *v;
873 GSList *list = sess_list;
875 PrintText (sess, "Session T Channel WaitChan WillChan Server\n");
876 while (list)
878 s = (struct session *) list->data;
879 sprintf (tbuf, "%p %1x %-10.10s %-10.10s %-10.10s %p\n",
880 s, s->type, s->channel, s->waitchannel,
881 s->willjoinchannel, s->server);
882 PrintText (sess, tbuf);
883 list = list->next;
886 list = serv_list;
887 PrintText (sess, "Server Sock Name\n");
888 while (list)
890 v = (struct server *) list->data;
891 sprintf (tbuf, "%p %-5d %s\n",
892 v, v->sok, v->servername);
893 PrintText (sess, tbuf);
894 list = list->next;
897 sprintf (tbuf,
898 "\nfront_session: %p\n"
899 "current_tab: %p\n\n",
900 sess->server->front_session, current_tab);
901 PrintText (sess, tbuf);
902 #ifdef USE_DEBUG
903 sprintf (tbuf, "current mem: %d\n\n", current_mem_usage);
904 PrintText (sess, tbuf);
905 #endif /* !MEMORY_DEBUG */
907 return TRUE;
910 static int
911 cmd_delbutton (struct session *sess, char *tbuf, char *word[],
912 char *word_eol[])
914 if (*word[2])
916 if (sess->type == SESS_DIALOG)
918 if (list_delentry (&dlgbutton_list, word[2]))
919 fe_dlgbuttons_update (sess);
920 } else
922 if (list_delentry (&button_list, word[2]))
923 fe_buttons_update (sess);
925 return TRUE;
927 return FALSE;
930 static int
931 cmd_dehop (struct session *sess, char *tbuf, char *word[], char *word_eol[])
933 int i = 2;
935 while (1)
937 if (!*word[i])
939 if (i == 2)
940 return FALSE;
941 send_channel_modes (sess, tbuf, word, 2, i, '-', 'h', 0);
942 return TRUE;
944 i++;
948 static int
949 cmd_deop (struct session *sess, char *tbuf, char *word[], char *word_eol[])
951 int i = 2;
953 while (1)
955 if (!*word[i])
957 if (i == 2)
958 return FALSE;
959 send_channel_modes (sess, tbuf, word, 2, i, '-', 'o', 0);
960 return TRUE;
962 i++;
966 typedef struct
968 char **nicks;
969 int i;
970 session *sess;
971 char *reason;
972 char *tbuf;
973 } multidata;
975 static int
976 mdehop_cb (struct User *user, multidata *data)
978 if (user->hop && !user->me)
980 data->nicks[data->i] = user->nick;
981 data->i++;
983 return TRUE;
986 static int
987 cmd_mdehop (struct session *sess, char *tbuf, char *word[], char *word_eol[])
989 char **nicks = malloc (sizeof (char *) * sess->hops);
990 multidata data;
992 data.nicks = nicks;
993 data.i = 0;
994 tree_foreach (sess->usertree, (tree_traverse_func *)mdehop_cb, &data);
995 send_channel_modes (sess, tbuf, nicks, 0, data.i, '-', 'h', 0);
996 free (nicks);
998 return TRUE;
1001 static int
1002 mdeop_cb (struct User *user, multidata *data)
1004 if (user->op && !user->me)
1006 data->nicks[data->i] = user->nick;
1007 data->i++;
1009 return TRUE;
1012 static int
1013 cmd_mdeop (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1015 char **nicks = malloc (sizeof (char *) * sess->ops);
1016 multidata data;
1018 data.nicks = nicks;
1019 data.i = 0;
1020 tree_foreach (sess->usertree, (tree_traverse_func *)mdeop_cb, &data);
1021 send_channel_modes (sess, tbuf, nicks, 0, data.i, '-', 'o', 0);
1022 free (nicks);
1024 return TRUE;
1027 GSList *menu_list = NULL;
1029 static void
1030 menu_free (menu_entry *me)
1032 free (me->path);
1033 if (me->label)
1034 free (me->label);
1035 if (me->cmd)
1036 free (me->cmd);
1037 if (me->ucmd)
1038 free (me->ucmd);
1039 if (me->group)
1040 free (me->group);
1041 if (me->icon)
1042 free (me->icon);
1043 free (me);
1046 /* strings equal? but ignore underscores */
1049 menu_streq (const char *s1, const char *s2, int def)
1051 /* for separators */
1052 if (s1 == NULL && s2 == NULL)
1053 return 0;
1054 if (s1 == NULL || s2 == NULL)
1055 return 1;
1056 while (*s1)
1058 if (*s1 == '_')
1059 s1++;
1060 if (*s2 == '_')
1061 s2++;
1062 if (*s1 != *s2)
1063 return 1;
1064 s1++;
1065 s2++;
1067 if (!*s2)
1068 return 0;
1069 return def;
1072 static menu_entry *
1073 menu_entry_find (char *path, char *label)
1075 GSList *list;
1076 menu_entry *me;
1078 list = menu_list;
1079 while (list)
1081 me = list->data;
1082 if (!strcmp (path, me->path))
1084 if (me->label && label && !strcmp (label, me->label))
1085 return me;
1087 list = list->next;
1089 return NULL;
1092 static void
1093 menu_del_children (char *path, char *label)
1095 GSList *list, *next;
1096 menu_entry *me;
1097 char buf[512];
1099 if (!label)
1100 label = "";
1101 if (path[0])
1102 snprintf (buf, sizeof (buf), "%s/%s", path, label);
1103 else
1104 snprintf (buf, sizeof (buf), "%s", label);
1106 list = menu_list;
1107 while (list)
1109 me = list->data;
1110 next = list->next;
1111 if (!menu_streq (buf, me->path, 0))
1113 menu_list = g_slist_remove (menu_list, me);
1114 menu_free (me);
1116 list = next;
1120 static int
1121 menu_del (char *path, char *label)
1123 GSList *list;
1124 menu_entry *me;
1126 list = menu_list;
1127 while (list)
1129 me = list->data;
1130 if (!menu_streq (me->label, label, 1) && !menu_streq (me->path, path, 1))
1132 menu_list = g_slist_remove (menu_list, me);
1133 fe_menu_del (me);
1134 menu_free (me);
1135 /* delete this item's children, if any */
1136 menu_del_children (path, label);
1137 return 1;
1139 list = list->next;
1142 return 0;
1145 static char
1146 menu_is_mainmenu_root (char *path, gint16 *offset)
1148 static const char *menus[] = {"\x4$TAB","\x5$TRAY","\x4$URL","\x5$NICK","\x5$CHAN"};
1149 int i;
1151 for (i = 0; i < 5; i++)
1153 if (!strncmp (path, menus[i] + 1, menus[i][0]))
1155 *offset = menus[i][0] + 1; /* number of bytes to offset the root */
1156 return 0; /* is not main menu */
1160 *offset = 0;
1161 return 1; /* is main menu */
1164 static void
1165 menu_add (char *path, char *label, char *cmd, char *ucmd, int pos, int state, int markup, int enable, int mod, int key, char *group, char *icon)
1167 menu_entry *me;
1169 /* already exists? */
1170 me = menu_entry_find (path, label);
1171 if (me)
1173 /* update only */
1174 me->state = state;
1175 me->enable = enable;
1176 fe_menu_update (me);
1177 return;
1180 me = malloc (sizeof (menu_entry));
1181 me->pos = pos;
1182 me->modifier = mod;
1183 me->is_main = menu_is_mainmenu_root (path, &me->root_offset);
1184 me->state = state;
1185 me->markup = markup;
1186 me->enable = enable;
1187 me->key = key;
1188 me->path = strdup (path);
1189 me->label = NULL;
1190 me->cmd = NULL;
1191 me->ucmd = NULL;
1192 me->group = NULL;
1193 me->icon = NULL;
1195 if (label)
1196 me->label = strdup (label);
1197 if (cmd)
1198 me->cmd = strdup (cmd);
1199 if (ucmd)
1200 me->ucmd = strdup (ucmd);
1201 if (group)
1202 me->group = strdup (group);
1203 if (icon)
1204 me->icon = strdup (icon);
1206 menu_list = g_slist_append (menu_list, me);
1207 label = fe_menu_add (me);
1208 if (label)
1210 /* FE has given us a stripped label */
1211 free (me->label);
1212 me->label = strdup (label);
1213 g_free (label); /* this is from pango */
1217 static int
1218 cmd_menu (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1220 int idx = 2;
1221 int len;
1222 int pos = 0xffff;
1223 int state;
1224 int toggle = FALSE;
1225 int enable = TRUE;
1226 int markup = FALSE;
1227 int key = 0;
1228 int mod = 0;
1229 char *label;
1230 char *group = NULL;
1231 char *icon = NULL;
1233 if (!word[2][0] || !word[3][0])
1234 return FALSE;
1236 /* -eX enabled or not? */
1237 if (word[idx][0] == '-' && word[idx][1] == 'e')
1239 enable = atoi (word[idx] + 2);
1240 idx++;
1243 /* -i<ICONFILE> */
1244 if (word[idx][0] == '-' && word[idx][1] == 'i')
1246 icon = word[idx] + 2;
1247 idx++;
1250 /* -k<mod>,<key> key binding */
1251 if (word[idx][0] == '-' && word[idx][1] == 'k')
1253 char *comma = strchr (word[idx], ',');
1254 if (!comma)
1255 return FALSE;
1256 mod = atoi (word[idx] + 2);
1257 key = atoi (comma + 1);
1258 idx++;
1261 /* -m to specify PangoMarkup language */
1262 if (word[idx][0] == '-' && word[idx][1] == 'm')
1264 markup = TRUE;
1265 idx++;
1268 /* -pX to specify menu position */
1269 if (word[idx][0] == '-' && word[idx][1] == 'p')
1271 pos = atoi (word[idx] + 2);
1272 idx++;
1275 /* -rSTATE,GROUP to specify a radio item */
1276 if (word[idx][0] == '-' && word[idx][1] == 'r')
1278 state = atoi (word[idx] + 2);
1279 group = word[idx] + 4;
1280 idx++;
1283 /* -tX to specify toggle item with default state */
1284 if (word[idx][0] == '-' && word[idx][1] == 't')
1286 state = atoi (word[idx] + 2);
1287 idx++;
1288 toggle = TRUE;
1291 if (word[idx+1][0] == 0)
1292 return FALSE;
1294 /* the path */
1295 path_part (word[idx+1], tbuf, 512);
1296 len = strlen (tbuf);
1297 if (len)
1298 tbuf[len - 1] = 0;
1300 /* the name of the item */
1301 label = file_part (word[idx + 1]);
1302 if (label[0] == '-' && label[1] == 0)
1303 label = NULL; /* separator */
1305 if (markup)
1307 char *p; /* to force pango closing tags through */
1308 for (p = label; *p; p++)
1309 if (*p == 3)
1310 *p = '/';
1313 if (!strcasecmp (word[idx], "ADD"))
1315 if (toggle)
1317 menu_add (tbuf, label, word[idx + 2], word[idx + 3], pos, state, markup, enable, mod, key, NULL, NULL);
1318 } else
1320 if (word[idx + 2][0])
1321 menu_add (tbuf, label, word[idx + 2], NULL, pos, state, markup, enable, mod, key, group, icon);
1322 else
1323 menu_add (tbuf, label, NULL, NULL, pos, state, markup, enable, mod, key, group, icon);
1325 return TRUE;
1328 if (!strcasecmp (word[idx], "DEL"))
1330 menu_del (tbuf, label);
1331 return TRUE;
1334 return FALSE;
1337 static int
1338 mkick_cb (struct User *user, multidata *data)
1340 if (!user->op && !user->me)
1341 data->sess->server->p_kick (data->sess->server, data->sess->channel, user->nick, data->reason);
1342 return TRUE;
1345 static int
1346 mkickops_cb (struct User *user, multidata *data)
1348 if (user->op && !user->me)
1349 data->sess->server->p_kick (data->sess->server, data->sess->channel, user->nick, data->reason);
1350 return TRUE;
1353 static int
1354 cmd_mkick (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1356 multidata data;
1358 data.sess = sess;
1359 data.reason = word_eol[2];
1360 tree_foreach (sess->usertree, (tree_traverse_func *)mkickops_cb, &data);
1361 tree_foreach (sess->usertree, (tree_traverse_func *)mkick_cb, &data);
1363 return TRUE;
1366 static int
1367 cmd_devoice (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1369 int i = 2;
1371 while (1)
1373 if (!*word[i])
1375 if (i == 2)
1376 return FALSE;
1377 send_channel_modes (sess, tbuf, word, 2, i, '-', 'v', 0);
1378 return TRUE;
1380 i++;
1384 static int
1385 cmd_discon (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1387 sess->server->disconnect (sess, TRUE, -1);
1388 return TRUE;
1391 static int
1392 cmd_dns (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1394 char *nick = word[2];
1395 struct User *user;
1397 if (*nick)
1399 if (strchr (nick, '.') == NULL)
1401 user = userlist_find (sess, nick);
1402 if (user && user->hostname)
1404 do_dns (sess, user->nick, user->hostname);
1405 } else
1407 sess->server->p_get_ip (sess->server, nick);
1408 sess->server->doing_dns = TRUE;
1410 } else
1412 snprintf (tbuf, TBUFSIZE, "exec -d %s %s", prefs.dnsprogram, nick);
1413 handle_command (sess, tbuf, FALSE);
1415 return TRUE;
1417 return FALSE;
1420 static int
1421 cmd_echo (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1423 PrintText (sess, word_eol[2]);
1424 return TRUE;
1427 static void
1428 exec_check_process (struct session *sess)
1430 int val;
1432 if (sess->running_exec == NULL)
1433 return;
1434 val = waitpid (sess->running_exec->childpid, NULL, WNOHANG);
1435 if (val == -1 || val > 0)
1437 close (sess->running_exec->myfd);
1438 fe_input_remove (sess->running_exec->iotag);
1439 free (sess->running_exec);
1440 sess->running_exec = NULL;
1444 static int
1445 cmd_execs (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1447 int r;
1449 exec_check_process (sess);
1450 if (sess->running_exec == NULL)
1452 EMIT_SIGNAL (XP_TE_NOCHILD, sess, NULL, NULL, NULL, NULL, 0);
1453 return FALSE;
1455 r = kill (sess->running_exec->childpid, SIGSTOP);
1456 if (r == -1)
1457 PrintText (sess, "Error in kill(2)\n");
1459 return TRUE;
1462 static int
1463 cmd_execc (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1465 int r;
1467 exec_check_process (sess);
1468 if (sess->running_exec == NULL)
1470 EMIT_SIGNAL (XP_TE_NOCHILD, sess, NULL, NULL, NULL, NULL, 0);
1471 return FALSE;
1473 r = kill (sess->running_exec->childpid, SIGCONT);
1474 if (r == -1)
1475 PrintText (sess, "Error in kill(2)\n");
1477 return TRUE;
1480 static int
1481 cmd_execk (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1483 int r;
1485 exec_check_process (sess);
1486 if (sess->running_exec == NULL)
1488 EMIT_SIGNAL (XP_TE_NOCHILD, sess, NULL, NULL, NULL, NULL, 0);
1489 return FALSE;
1491 if (strcmp (word[2], "-9") == 0)
1492 r = kill (sess->running_exec->childpid, SIGKILL);
1493 else
1494 r = kill (sess->running_exec->childpid, SIGTERM);
1495 if (r == -1)
1496 PrintText (sess, "Error in kill(2)\n");
1498 return TRUE;
1501 /* OS/2 Can't have the /EXECW command because it uses pipe(2) not socketpair
1502 and thus it is simplex --AGL */
1503 static int
1504 cmd_execw (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1506 int len;
1507 char *temp;
1508 exec_check_process (sess);
1509 if (sess->running_exec == NULL)
1511 EMIT_SIGNAL (XP_TE_NOCHILD, sess, NULL, NULL, NULL, NULL, 0);
1512 return FALSE;
1514 len = strlen(word_eol[2]);
1515 temp = malloc(len + 2);
1516 sprintf(temp, "%s\n", word_eol[2]);
1517 PrintText(sess, temp);
1518 write(sess->running_exec->myfd, temp, len + 1);
1519 free(temp);
1521 return TRUE;
1524 /* convert ANSI escape color codes to mIRC codes */
1526 static short escconv[] =
1527 /* 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 */
1528 { 1,4,3,5,2,10,6,1, 1,7,9,8,12,11,13,1 };
1530 static void
1531 exec_handle_colors (char *buf, int len)
1533 char numb[16];
1534 char *nbuf;
1535 int i = 0, j = 0, k = 0, firstn = 0, col, colf = 0, colb = 0;
1536 int esc = FALSE, backc = FALSE, bold = FALSE;
1538 /* any escape codes in this text? */
1539 if (strchr (buf, 27) == 0)
1540 return;
1542 nbuf = malloc (len + 1);
1544 while (i < len)
1546 switch (buf[i])
1548 case '\r':
1549 break;
1550 case 27:
1551 esc = TRUE;
1552 break;
1553 case ';':
1554 if (!esc)
1555 goto norm;
1556 backc = TRUE;
1557 numb[k] = 0;
1558 firstn = atoi (numb);
1559 k = 0;
1560 break;
1561 case '[':
1562 if (!esc)
1563 goto norm;
1564 break;
1565 default:
1566 if (esc)
1568 if (buf[i] >= 'A' && buf[i] <= 'z')
1570 if (buf[i] == 'm')
1572 /* ^[[0m */
1573 if (k == 0 || (numb[0] == '0' && k == 1))
1575 nbuf[j] = '\017';
1576 j++;
1577 bold = FALSE;
1578 goto cont;
1581 numb[k] = 0;
1582 col = atoi (numb);
1583 backc = FALSE;
1585 if (firstn == 1)
1586 bold = TRUE;
1588 if (firstn >= 30 && firstn <= 37)
1589 colf = firstn - 30;
1591 if (col >= 40)
1593 colb = col - 40;
1594 backc = TRUE;
1597 if (col >= 30 && col <= 37)
1598 colf = col - 30;
1600 if (bold)
1601 colf += 8;
1603 if (backc)
1605 colb = escconv[colb % 14];
1606 colf = escconv[colf % 14];
1607 j += sprintf (&nbuf[j], "\003%d,%02d", colf, colb);
1608 } else
1610 colf = escconv[colf % 14];
1611 j += sprintf (&nbuf[j], "\003%02d", colf);
1614 cont: esc = FALSE;
1615 backc = FALSE;
1616 k = 0;
1617 } else
1619 if (isdigit ((unsigned char) buf[i]) && k < (sizeof (numb) - 1))
1621 numb[k] = buf[i];
1622 k++;
1625 } else
1627 norm: nbuf[j] = buf[i];
1628 j++;
1631 i++;
1634 nbuf[j] = 0;
1635 memcpy (buf, nbuf, j + 1);
1636 free (nbuf);
1639 static gboolean
1640 exec_data (GIOChannel *source, GIOCondition condition, struct nbexec *s)
1642 char *buf, *readpos, *rest;
1643 int rd, len;
1644 int sok = s->myfd;
1646 len = s->buffill;
1647 if (len) {
1648 /* append new data to buffered incomplete line */
1649 buf = malloc(len + 2050);
1650 memcpy(buf, s->linebuf, len);
1651 readpos = buf + len;
1652 free(s->linebuf);
1653 s->linebuf = NULL;
1655 else
1656 readpos = buf = malloc(2050);
1658 rd = read (sok, readpos, 2048);
1659 if (rd < 1)
1661 /* The process has died */
1662 kill(s->childpid, SIGKILL);
1663 if (len) {
1664 buf[len] = '\0';
1665 exec_handle_colors(buf, len);
1666 if (s->tochannel)
1668 /* must turn off auto-completion temporarily */
1669 unsigned int old = prefs.nickcompletion;
1670 prefs.nickcompletion = 0;
1671 handle_multiline (s->sess, buf, FALSE, TRUE);
1672 prefs.nickcompletion = old;
1674 else
1675 PrintText (s->sess, buf);
1677 free(buf);
1678 waitpid (s->childpid, NULL, 0);
1679 s->sess->running_exec = NULL;
1680 fe_input_remove (s->iotag);
1681 close (sok);
1682 free (s);
1683 return TRUE;
1685 len += rd;
1686 buf[len] = '\0';
1688 rest = memrchr(buf, '\n', len);
1689 if (rest)
1690 rest++;
1691 else
1692 rest = buf;
1693 if (*rest) {
1694 s->buffill = len - (rest - buf); /* = strlen(rest) */
1695 s->linebuf = malloc(s->buffill + 1);
1696 memcpy(s->linebuf, rest, s->buffill);
1697 *rest = '\0';
1698 len -= s->buffill; /* possibly 0 */
1700 else
1701 s->buffill = 0;
1703 if (len) {
1704 exec_handle_colors (buf, len);
1705 if (s->tochannel)
1706 handle_multiline (s->sess, buf, FALSE, TRUE);
1707 else
1708 PrintText (s->sess, buf);
1711 free(buf);
1712 return TRUE;
1715 static int
1716 cmd_exec (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1718 int tochannel = FALSE;
1719 char *cmd = word_eol[2];
1720 int fds[2], pid = 0;
1721 struct nbexec *s;
1722 int shell = TRUE;
1723 int fd;
1725 if (*cmd)
1727 exec_check_process (sess);
1728 if (sess->running_exec != NULL)
1730 EMIT_SIGNAL (XP_TE_ALREADYPROCESS, sess, NULL, NULL, NULL, NULL, 0);
1731 return TRUE;
1734 if (!strcmp (word[2], "-d"))
1736 if (!*word[3])
1737 return FALSE;
1738 cmd = word_eol[3];
1739 shell = FALSE;
1741 else if (!strcmp (word[2], "-o"))
1743 if (!*word[3])
1744 return FALSE;
1745 cmd = word_eol[3];
1746 tochannel = TRUE;
1749 if (shell)
1751 if (access ("/bin/sh", X_OK) != 0)
1753 fe_message (_("I need /bin/sh to run!\n"), FE_MSG_ERROR);
1754 return TRUE;
1758 if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
1760 PrintText (sess, "socketpair(2) failed\n");
1761 return FALSE;
1763 s = (struct nbexec *) malloc (sizeof (struct nbexec));
1764 memset(s, 0, sizeof(*s));
1765 s->myfd = fds[0];
1766 s->tochannel = tochannel;
1767 s->sess = sess;
1769 pid = fork ();
1770 if (pid == 0)
1772 /* This is the child's context */
1773 close (0);
1774 close (1);
1775 close (2);
1776 /* Close parent's end of pipe */
1777 close(s->myfd);
1778 /* Copy the child end of the pipe to stdout and stderr */
1779 dup2 (fds[1], 1);
1780 dup2 (fds[1], 2);
1781 /* Also copy it to stdin so we can write to it */
1782 dup2 (fds[1], 0);
1783 /* Now close all open file descriptors except stdin, stdout and stderr */
1784 for (fd = 3; fd < 1024; fd++) close(fd);
1785 /* Now we call /bin/sh to run our cmd ; made it more friendly -DC1 */
1786 if (shell)
1788 execl ("/bin/sh", "sh", "-c", cmd, NULL);
1789 } else
1791 char **argv;
1792 int argc;
1794 my_poptParseArgvString (cmd, &argc, &argv);
1795 execvp (argv[0], argv);
1797 /* not reached unless error */
1798 /*printf("exec error\n");*/
1799 fflush (stdout);
1800 fflush (stdin);
1801 _exit (0);
1803 if (pid == -1)
1805 /* Parent context, fork() failed */
1807 PrintText (sess, "Error in fork(2)\n");
1808 close(fds[0]);
1809 close(fds[1]);
1810 } else
1812 /* Parent path */
1813 close(fds[1]);
1814 s->childpid = pid;
1815 s->iotag = fe_input_add (s->myfd, FIA_READ|FIA_EX, exec_data, s);
1816 sess->running_exec = s;
1817 return TRUE;
1820 return FALSE;
1823 static int
1824 cmd_flushq (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1826 sprintf (tbuf, "Flushing server send queue, %d bytes.\n", sess->server->sendq_len);
1827 PrintText (sess, tbuf);
1828 sess->server->flush_queue (sess->server);
1829 return TRUE;
1832 static int
1833 cmd_quit (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1835 if (*word_eol[2])
1836 sess->quitreason = word_eol[2];
1837 sess->server->disconnect (sess, TRUE, -1);
1838 sess->quitreason = NULL;
1839 return 2;
1842 static int
1843 cmd_gate (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1845 char *server_name = word[2];
1846 server *serv = sess->server;
1847 if (*server_name)
1849 char *port = word[3];
1850 #ifdef USE_OPENSSL
1851 serv->use_ssl = FALSE;
1852 #endif
1853 server_fill_her_up (serv);
1854 if (*port)
1855 serv->connect (serv, server_name, atoi (port), TRUE);
1856 else
1857 serv->connect (serv, server_name, 23, TRUE);
1858 return TRUE;
1860 return FALSE;
1863 typedef struct
1865 char *cmd;
1866 session *sess;
1867 } getvalinfo;
1869 static void
1870 get_int_cb (int cancel, int val, getvalinfo *info)
1872 char buf[512];
1874 if (!cancel)
1876 snprintf (buf, sizeof (buf), "%s %d", info->cmd, val);
1877 if (is_session (info->sess))
1878 handle_command (info->sess, buf, FALSE);
1881 free (info->cmd);
1882 free (info);
1885 static int
1886 cmd_getint (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1888 getvalinfo *info;
1890 if (!word[4][0])
1891 return FALSE;
1893 info = malloc (sizeof (*info));
1894 info->cmd = strdup (word[3]);
1895 info->sess = sess;
1897 fe_get_int (word[4], atoi (word[2]), get_int_cb, info);
1899 return TRUE;
1902 static void
1903 get_file_cb (char *cmd, char *file)
1905 char buf[1024 + 128];
1907 /* execute the command once per file, then once more with
1908 no args */
1909 if (file)
1911 snprintf (buf, sizeof (buf), "%s %s", cmd, file);
1912 handle_command (current_sess, buf, FALSE);
1914 else
1916 handle_command (current_sess, cmd, FALSE);
1917 free (cmd);
1921 static int
1922 cmd_getfile (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1924 int idx = 2;
1925 int flags = 0;
1927 if (!word[3][0])
1928 return FALSE;
1930 if (!strcmp (word[2], "-folder"))
1932 flags |= FRF_CHOOSEFOLDER;
1933 idx++;
1936 if (!strcmp (word[idx], "-multi"))
1938 flags |= FRF_MULTIPLE;
1939 idx++;
1942 if (!strcmp (word[idx], "-save"))
1944 flags |= FRF_WRITE;
1945 idx++;
1948 fe_get_file (word[idx+1], word[idx+2], (void *)get_file_cb, strdup (word[idx]), flags);
1950 return TRUE;
1953 static void
1954 get_str_cb (int cancel, char *val, getvalinfo *info)
1956 char buf[512];
1958 if (!cancel)
1960 snprintf (buf, sizeof (buf), "%s %s", info->cmd, val);
1961 if (is_session (info->sess))
1962 handle_command (info->sess, buf, FALSE);
1965 free (info->cmd);
1966 free (info);
1969 static int
1970 cmd_getstr (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1972 getvalinfo *info;
1974 if (!word[4][0])
1975 return FALSE;
1977 info = malloc (sizeof (*info));
1978 info->cmd = strdup (word[3]);
1979 info->sess = sess;
1981 fe_get_str (word[4], word[2], get_str_cb, info);
1983 return TRUE;
1986 static int
1987 cmd_ghost (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1989 if (!word[2][0])
1990 return FALSE;
1992 sess->server->p_ns_ghost (sess->server, word[2], word[3]);
1993 return TRUE;
1996 static int
1997 cmd_gui (struct session *sess, char *tbuf, char *word[], char *word_eol[])
1999 switch (str_ihash (word[2]))
2001 case 0x058b836e: fe_ctrl_gui (sess, 8, 0); break; /* APPLY */
2002 case 0xac1eee45: fe_ctrl_gui (sess, 7, 2); break; /* ATTACH */
2003 case 0x05a72f63: fe_ctrl_gui (sess, 4, atoi (word[3])); break; /* COLOR */
2004 case 0xb06a1793: fe_ctrl_gui (sess, 7, 1); break; /* DETACH */
2005 case 0x05cfeff0: fe_ctrl_gui (sess, 3, 0); break; /* FLASH */
2006 case 0x05d154d8: fe_ctrl_gui (sess, 2, 0); break; /* FOCUS */
2007 case 0x0030dd42: fe_ctrl_gui (sess, 0, 0); break; /* HIDE */
2008 case 0x61addbe3: fe_ctrl_gui (sess, 5, 0); break; /* ICONIFY */
2009 case 0xc0851aaa: fe_message (word[3], FE_MSG_INFO|FE_MSG_MARKUP); break; /* MSGBOX */
2010 case 0x0035dafd: fe_ctrl_gui (sess, 1, 0); break; /* SHOW */
2011 case 0x0033155f: /* MENU */
2012 if (!strcasecmp (word[3], "TOGGLE"))
2013 fe_ctrl_gui (sess, 6, 0);
2014 else
2015 return FALSE;
2016 break;
2017 default:
2018 return FALSE;
2021 return TRUE;
2024 typedef struct
2026 int longfmt;
2027 int i, t;
2028 char *buf;
2029 } help_list;
2031 static void
2032 show_help_line (session *sess, help_list *hl, char *name, char *usage)
2034 int j, len, max;
2035 char *p;
2037 if (name[0] == '.') /* hidden command? */
2038 return;
2040 if (hl->longfmt) /* long format for /HELP -l */
2042 if (!usage || usage[0] == 0)
2043 PrintTextf (sess, " \0034%s\003 :\n", name);
2044 else
2045 PrintTextf (sess, " \0034%s\003 : %s\n", name, _(usage));
2046 return;
2049 /* append the name into buffer, but convert to uppercase */
2050 len = strlen (hl->buf);
2051 p = name;
2052 while (*p)
2054 hl->buf[len] = toupper ((unsigned char) *p);
2055 len++;
2056 p++;
2058 hl->buf[len] = 0;
2060 hl->t++;
2061 if (hl->t == 5)
2063 hl->t = 0;
2064 strcat (hl->buf, "\n");
2065 PrintText (sess, hl->buf);
2066 hl->buf[0] = ' ';
2067 hl->buf[1] = ' ';
2068 hl->buf[2] = 0;
2069 } else
2071 /* append some spaces after the command name */
2072 max = strlen (name);
2073 if (max < 10)
2075 max = 10 - max;
2076 for (j = 0; j < max; j++)
2078 hl->buf[len] = ' ';
2079 len++;
2080 hl->buf[len] = 0;
2086 static int
2087 cmd_help (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2089 int i = 0, longfmt = 0;
2090 char *helpcmd = "";
2091 GSList *list;
2093 if (tbuf)
2094 helpcmd = word[2];
2095 if (*helpcmd && strcmp (helpcmd, "-l") == 0)
2096 longfmt = 1;
2098 if (*helpcmd && !longfmt)
2100 help (sess, tbuf, helpcmd, FALSE);
2101 } else
2103 struct popup *pop;
2104 char *buf = malloc (4096);
2105 help_list hl;
2107 hl.longfmt = longfmt;
2108 hl.buf = buf;
2110 PrintTextf (sess, "\n%s\n\n", _("Commands Available:"));
2111 buf[0] = ' ';
2112 buf[1] = ' ';
2113 buf[2] = 0;
2114 hl.t = 0;
2115 hl.i = 0;
2116 while (xc_cmds[i].name)
2118 show_help_line (sess, &hl, xc_cmds[i].name, xc_cmds[i].help);
2119 i++;
2121 strcat (buf, "\n");
2122 PrintText (sess, buf);
2124 PrintTextf (sess, "\n%s\n\n", _("User defined commands:"));
2125 buf[0] = ' ';
2126 buf[1] = ' ';
2127 buf[2] = 0;
2128 hl.t = 0;
2129 hl.i = 0;
2130 list = command_list;
2131 while (list)
2133 pop = list->data;
2134 show_help_line (sess, &hl, pop->name, pop->cmd);
2135 list = list->next;
2137 strcat (buf, "\n");
2138 PrintText (sess, buf);
2140 PrintTextf (sess, "\n%s\n\n", _("Plugin defined commands:"));
2141 buf[0] = ' ';
2142 buf[1] = ' ';
2143 buf[2] = 0;
2144 hl.t = 0;
2145 hl.i = 0;
2146 plugin_command_foreach (sess, &hl, (void *)show_help_line);
2147 strcat (buf, "\n");
2148 PrintText (sess, buf);
2149 free (buf);
2151 PrintTextf (sess, "\n%s\n\n", _("Type /HELP <command> for more information, or /HELP -l"));
2153 return TRUE;
2156 static int
2157 cmd_id (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2159 if (word[2][0])
2161 sess->server->p_ns_identify (sess->server, word[2]);
2162 return TRUE;
2165 return FALSE;
2168 static int
2169 cmd_ignore (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2171 int i;
2172 int type = 0;
2173 int quiet = 0;
2174 char *mask;
2176 if (!*word[2])
2178 ignore_showlist (sess);
2179 return TRUE;
2181 if (!*word[3])
2182 return FALSE;
2184 i = 3;
2185 while (1)
2187 if (!*word[i])
2189 if (type == 0)
2190 return FALSE;
2192 mask = word[2];
2193 if (strchr (mask, '?') == NULL &&
2194 strchr (mask, '*') == NULL)
2196 mask = tbuf;
2197 snprintf (tbuf, TBUFSIZE, "%s!*@*", word[2]);
2200 i = ignore_add (mask, type);
2201 if (quiet)
2202 return TRUE;
2203 switch (i)
2205 case 1:
2206 EMIT_SIGNAL (XP_TE_IGNOREADD, sess, mask, NULL, NULL, NULL, 0);
2207 break;
2208 case 2: /* old ignore changed */
2209 EMIT_SIGNAL (XP_TE_IGNORECHANGE, sess, mask, NULL, NULL, NULL, 0);
2211 return TRUE;
2213 if (!strcasecmp (word[i], "UNIGNORE"))
2214 type |= IG_UNIG;
2215 else if (!strcasecmp (word[i], "ALL"))
2216 type |= IG_PRIV | IG_NOTI | IG_CHAN | IG_CTCP | IG_INVI | IG_DCC;
2217 else if (!strcasecmp (word[i], "PRIV"))
2218 type |= IG_PRIV;
2219 else if (!strcasecmp (word[i], "NOTI"))
2220 type |= IG_NOTI;
2221 else if (!strcasecmp (word[i], "CHAN"))
2222 type |= IG_CHAN;
2223 else if (!strcasecmp (word[i], "CTCP"))
2224 type |= IG_CTCP;
2225 else if (!strcasecmp (word[i], "INVI"))
2226 type |= IG_INVI;
2227 else if (!strcasecmp (word[i], "QUIET"))
2228 quiet = 1;
2229 else if (!strcasecmp (word[i], "NOSAVE"))
2230 type |= IG_NOSAVE;
2231 else if (!strcasecmp (word[i], "DCC"))
2232 type |= IG_DCC;
2233 else
2235 sprintf (tbuf, _("Unknown arg '%s' ignored."), word[i]);
2236 PrintText (sess, tbuf);
2238 i++;
2242 static int
2243 cmd_invite (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2245 if (!*word[2])
2246 return FALSE;
2247 if (*word[3])
2248 sess->server->p_invite (sess->server, word[3], word[2]);
2249 else
2250 sess->server->p_invite (sess->server, sess->channel, word[2]);
2251 return TRUE;
2254 static int
2255 cmd_join (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2257 char *chan = word[2];
2258 if (*chan)
2260 char *po, *pass = word[3];
2261 sess->server->p_join (sess->server, chan, pass);
2262 if (sess->channel[0] == 0 && sess->waitchannel[0])
2264 po = strchr (chan, ',');
2265 if (po)
2266 *po = 0;
2267 safe_strcpy (sess->waitchannel, chan, CHANLEN);
2269 return TRUE;
2271 return FALSE;
2274 static int
2275 cmd_kick (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2277 char *nick = word[2];
2278 char *reason = word_eol[3];
2279 if (*nick)
2281 sess->server->p_kick (sess->server, sess->channel, nick, reason);
2282 return TRUE;
2284 return FALSE;
2287 static int
2288 cmd_kickban (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2290 char *nick = word[2];
2291 char *reason = word_eol[3];
2292 struct User *user;
2294 if (*nick)
2296 /* if the reason is a 1 digit number, treat it as a bantype */
2298 user = userlist_find (sess, nick);
2300 if (isdigit ((unsigned char) reason[0]) && reason[1] == 0)
2302 ban (sess, tbuf, nick, reason, (user && user->op));
2303 reason[0] = 0;
2304 } else
2305 ban (sess, tbuf, nick, "", (user && user->op));
2307 sess->server->p_kick (sess->server, sess->channel, nick, reason);
2309 return TRUE;
2311 return FALSE;
2314 static int
2315 cmd_killall (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2317 xchat_exit();
2318 return 2;
2321 static int
2322 cmd_lagcheck (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2324 lag_check ();
2325 return TRUE;
2328 static void
2329 lastlog (session *sess, char *search, gboolean regexp)
2331 session *lastlog_sess;
2333 if (!is_session (sess))
2334 return;
2336 lastlog_sess = find_dialog (sess->server, "(lastlog)");
2337 if (!lastlog_sess)
2338 lastlog_sess = new_ircwindow (sess->server, "(lastlog)", SESS_DIALOG, 0);
2340 lastlog_sess->lastlog_sess = sess;
2341 lastlog_sess->lastlog_regexp = regexp; /* remember the search type */
2343 fe_text_clear (lastlog_sess, 0);
2344 fe_lastlog (sess, lastlog_sess, search, regexp);
2347 static int
2348 cmd_lastlog (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2350 if (*word_eol[2])
2352 if (!strcmp (word[2], "-r"))
2353 lastlog (sess, word_eol[3], TRUE);
2354 else
2355 lastlog (sess, word_eol[2], FALSE);
2356 return TRUE;
2359 return FALSE;
2362 static int
2363 cmd_list (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2365 sess->server->p_list_channels (sess->server, word_eol[2], 1);
2367 return TRUE;
2370 gboolean
2371 load_perform_file (session *sess, char *file)
2373 char tbuf[1024 + 4];
2374 char *nl;
2375 FILE *fp;
2377 fp = xchat_fopen_file (file, "r", XOF_FULLPATH);
2378 if (!fp)
2379 return FALSE;
2381 tbuf[1024] = 0;
2382 while (fgets (tbuf, 1024, fp))
2384 nl = strchr (tbuf, '\n');
2385 if (nl == tbuf) /* skip empty commands */
2386 continue;
2387 if (nl)
2388 *nl = 0;
2389 if (tbuf[0] == prefs.cmdchar[0])
2390 handle_command (sess, tbuf + 1, TRUE);
2391 else
2392 handle_command (sess, tbuf, TRUE);
2394 fclose (fp);
2395 return TRUE;
2398 static int
2399 cmd_load (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2401 char *error, *arg, *file;
2402 int len;
2404 if (!word[2][0])
2405 return FALSE;
2407 if (strcmp (word[2], "-e") == 0)
2409 file = expand_homedir (word[3]);
2410 if (!load_perform_file (sess, file))
2412 PrintTextf (sess, _("Cannot access %s\n"), file);
2413 PrintText (sess, errorstring (errno));
2415 free (file);
2416 return TRUE;
2419 #ifdef USE_PLUGIN
2420 len = strlen (word[2]);
2421 if (len > 3 && strcasecmp (".so", word[2] + len - 3) == 0)
2423 arg = NULL;
2424 if (word_eol[3][0])
2425 arg = word_eol[3];
2427 file = expand_homedir (word[2]);
2428 error = plugin_load (sess, file, arg);
2429 free (file);
2431 if (error)
2432 PrintText (sess, error);
2434 return TRUE;
2436 #endif
2438 sprintf (tbuf, "Unknown file type %s. Maybe you need to install the Perl or Python plugin?\n", word[2]);
2439 PrintText (sess, tbuf);
2441 return FALSE;
2444 static int
2445 cmd_me (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2447 char *act = word_eol[2];
2449 if (!(*act))
2450 return FALSE;
2452 if (sess->type == SESS_SERVER)
2454 notj_msg (sess);
2455 return TRUE;
2458 snprintf (tbuf, TBUFSIZE, "\001ACTION %s\001\r", act);
2459 /* first try through DCC CHAT */
2460 if (dcc_write_chat (sess->channel, tbuf))
2462 /* print it to screen */
2463 inbound_action (sess, sess->channel, sess->server->nick, "", act, TRUE, FALSE);
2464 } else
2466 /* DCC CHAT failed, try through server */
2467 if (sess->server->connected)
2469 sess->server->p_action (sess->server, sess->channel, act);
2470 /* print it to screen */
2471 inbound_action (sess, sess->channel, sess->server->nick, "", act, TRUE, FALSE);
2472 } else
2474 notc_msg (sess);
2478 return TRUE;
2481 static int
2482 cmd_mode (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2484 /* +channel channels are dying, let those servers whine about modes.
2485 * return info about current channel if available and no info is given */
2486 if ((*word[2] == '+') || (*word[2] == 0) || (!is_channel(sess->server, word[2]) &&
2487 !(rfc_casecmp(sess->server->nick, word[2]) == 0)))
2489 if(sess->channel[0] == 0)
2490 return FALSE;
2491 sess->server->p_mode (sess->server, sess->channel, word_eol[2]);
2493 else
2494 sess->server->p_mode (sess->server, word[2], word_eol[3]);
2495 return TRUE;
2498 static int
2499 mop_cb (struct User *user, multidata *data)
2501 if (!user->op)
2503 data->nicks[data->i] = user->nick;
2504 data->i++;
2506 return TRUE;
2509 static int
2510 cmd_mop (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2512 char **nicks = malloc (sizeof (char *) * (sess->total - sess->ops));
2513 multidata data;
2515 data.nicks = nicks;
2516 data.i = 0;
2517 tree_foreach (sess->usertree, (tree_traverse_func *)mop_cb, &data);
2518 send_channel_modes (sess, tbuf, nicks, 0, data.i, '+', 'o', 0);
2520 free (nicks);
2522 return TRUE;
2525 static int
2526 cmd_msg (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2528 char *nick = word[2];
2529 char *msg = word_eol[3];
2530 struct session *newsess;
2532 if (*nick)
2534 if (*msg)
2536 if (strcmp (nick, ".") == 0)
2537 { /* /msg the last nick /msg'ed */
2538 if (sess->lastnick[0])
2539 nick = sess->lastnick;
2540 } else
2542 safe_strcpy (sess->lastnick, nick, NICKLEN); /* prime the last nick memory */
2545 if (*nick == '=')
2547 nick++;
2548 if (!dcc_write_chat (nick, msg))
2550 EMIT_SIGNAL (XP_TE_NODCC, sess, NULL, NULL, NULL, NULL, 0);
2551 return TRUE;
2553 } else
2555 if (!sess->server->connected)
2557 notc_msg (sess);
2558 return TRUE;
2560 sess->server->p_message (sess->server, nick, msg);
2562 newsess = find_dialog (sess->server, nick);
2563 if (!newsess)
2564 newsess = find_channel (sess->server, nick);
2565 if (newsess)
2566 inbound_chanmsg (newsess->server, NULL, newsess->channel,
2567 newsess->server->nick, msg, TRUE, FALSE);
2568 else
2570 /* mask out passwords */
2571 if (strcasecmp (nick, "nickserv") == 0 &&
2572 strncasecmp (msg, "identify ", 9) == 0)
2573 msg = "identify ****";
2574 EMIT_SIGNAL (XP_TE_MSGSEND, sess, nick, msg, NULL, NULL, 0);
2577 return TRUE;
2580 return FALSE;
2583 static int
2584 cmd_names (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2586 if (*word[2])
2587 sess->server->p_names (sess->server, word[2]);
2588 else
2589 sess->server->p_names (sess->server, sess->channel);
2590 return TRUE;
2593 static int
2594 cmd_nctcp (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2596 if (*word_eol[3])
2598 sess->server->p_nctcp (sess->server, word[2], word_eol[3]);
2599 return TRUE;
2601 return FALSE;
2604 static int
2605 cmd_newserver (struct session *sess, char *tbuf, char *word[],
2606 char *word_eol[])
2608 if (strcmp (word[2], "-noconnect") == 0)
2610 new_ircwindow (NULL, word[3], SESS_SERVER, 0);
2611 return TRUE;
2614 sess = new_ircwindow (NULL, NULL, SESS_SERVER, 0);
2615 cmd_server (sess, tbuf, word, word_eol);
2616 return TRUE;
2619 static int
2620 cmd_nick (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2622 char *nick = word[2];
2623 if (*nick)
2625 if (sess->server->connected)
2626 sess->server->p_change_nick (sess->server, nick);
2627 else
2628 inbound_newnick (sess->server, sess->server->nick, nick, TRUE);
2629 return TRUE;
2631 return FALSE;
2634 static int
2635 cmd_notice (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2637 if (*word[2] && *word_eol[3])
2639 sess->server->p_notice (sess->server, word[2], word_eol[3]);
2640 EMIT_SIGNAL (XP_TE_NOTICESEND, sess, word[2], word_eol[3], NULL, NULL, 0);
2641 return TRUE;
2643 return FALSE;
2646 static int
2647 cmd_notify (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2649 int i = 1;
2650 char *net = NULL;
2652 if (*word[2])
2654 if (strcmp (word[2], "-n") == 0) /* comma sep network list */
2656 net = word[3];
2657 i += 2;
2660 while (1)
2662 i++;
2663 if (!*word[i])
2664 break;
2665 if (notify_deluser (word[i]))
2667 EMIT_SIGNAL (XP_TE_DELNOTIFY, sess, word[i], NULL, NULL, NULL, 0);
2668 return TRUE;
2671 if (net && strcmp (net, "ASK") == 0)
2672 fe_notify_ask (word[i], NULL);
2673 else
2675 notify_adduser (word[i], net);
2676 EMIT_SIGNAL (XP_TE_ADDNOTIFY, sess, word[i], NULL, NULL, NULL, 0);
2679 } else
2680 notify_showlist (sess);
2681 return TRUE;
2684 static int
2685 cmd_op (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2687 int i = 2;
2689 while (1)
2691 if (!*word[i])
2693 if (i == 2)
2694 return FALSE;
2695 send_channel_modes (sess, tbuf, word, 2, i, '+', 'o', 0);
2696 return TRUE;
2698 i++;
2702 static int
2703 cmd_part (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2705 char *chan = word[2];
2706 char *reason = word_eol[3];
2707 if (!*chan)
2708 chan = sess->channel;
2709 if ((*chan) && is_channel (sess->server, chan))
2711 if (reason[0] == 0)
2712 reason = NULL;
2713 server_sendpart (sess->server, chan, reason);
2714 return TRUE;
2716 return FALSE;
2719 static int
2720 cmd_ping (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2722 char timestring[64];
2723 unsigned long tim;
2724 char *to = word[2];
2726 tim = make_ping_time ();
2728 snprintf (timestring, sizeof (timestring), "%lu", tim);
2729 sess->server->p_ping (sess->server, to, timestring);
2731 return TRUE;
2734 void
2735 open_query (server *serv, char *nick, gboolean focus_existing)
2737 session *sess;
2739 sess = find_dialog (serv, nick);
2740 if (!sess)
2741 new_ircwindow (serv, nick, SESS_DIALOG, 1);
2742 else if (focus_existing)
2743 fe_ctrl_gui (sess, 2, 0); /* bring-to-front */
2746 static int
2747 cmd_query (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2749 char *nick = word[2];
2750 gboolean focus = TRUE;
2752 if (strcmp (word[2], "-nofocus") == 0)
2754 nick = word[3];
2755 focus = FALSE;
2758 if (*nick && !is_channel (sess->server, nick))
2760 open_query (sess->server, nick, focus);
2761 return TRUE;
2763 return FALSE;
2766 static int
2767 cmd_quote (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2769 char *raw = word_eol[2];
2771 return sess->server->p_raw (sess->server, raw);
2774 static int
2775 cmd_reconnect (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2777 int tmp = prefs.recon_delay;
2778 GSList *list;
2779 server *serv = sess->server;
2781 prefs.recon_delay = 0;
2783 if (!strcasecmp (word[2], "ALL"))
2785 list = serv_list;
2786 while (list)
2788 serv = list->data;
2789 if (serv->connected)
2790 serv->auto_reconnect (serv, TRUE, -1);
2791 list = list->next;
2794 /* If it isn't "ALL" and there is something
2795 there it *should* be a server they are trying to connect to*/
2796 else if (*word[2])
2798 int offset = 0;
2799 #ifdef USE_OPENSSL
2800 int use_ssl = FALSE;
2802 if (strcmp (word[2], "-ssl") == 0)
2804 use_ssl = TRUE;
2805 offset++; /* args move up by 1 word */
2807 serv->use_ssl = use_ssl;
2808 serv->accept_invalid_cert = TRUE;
2809 #endif
2811 if (*word[4+offset])
2812 safe_strcpy (serv->password, word[4+offset], sizeof (serv->password));
2813 if (*word[3+offset])
2814 serv->port = atoi (word[3+offset]);
2815 safe_strcpy (serv->hostname, word[2+offset], sizeof (serv->hostname));
2816 serv->auto_reconnect (serv, TRUE, -1);
2818 else
2820 serv->auto_reconnect (serv, TRUE, -1);
2822 prefs.recon_delay = tmp;
2824 return TRUE;
2827 static int
2828 cmd_recv (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2830 if (*word_eol[2])
2832 sess->server->p_inline (sess->server, word_eol[2], strlen (word_eol[2]));
2833 return TRUE;
2836 return FALSE;
2839 static int
2840 cmd_say (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2842 char *speech = word_eol[2];
2843 if (*speech)
2845 handle_say (sess, speech, FALSE);
2846 return TRUE;
2848 return FALSE;
2851 static int
2852 cmd_send (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2854 guint32 addr;
2855 socklen_t len;
2856 struct sockaddr_in SAddr;
2858 if (!word[2][0])
2859 return FALSE;
2861 addr = dcc_get_my_address ();
2862 if (addr == 0)
2864 /* use the one from our connected server socket */
2865 memset (&SAddr, 0, sizeof (struct sockaddr_in));
2866 len = sizeof (SAddr);
2867 getsockname (sess->server->sok, (struct sockaddr *) &SAddr, &len);
2868 addr = SAddr.sin_addr.s_addr;
2870 addr = ntohl (addr);
2872 if ((addr & 0xffff0000) == 0xc0a80000 || /* 192.168.x.x */
2873 (addr & 0xff000000) == 0x0a000000) /* 10.x.x.x */
2874 /* we got a private net address, let's PSEND or it'll fail */
2875 snprintf (tbuf, 512, "DCC PSEND %s", word_eol[2]);
2876 else
2877 snprintf (tbuf, 512, "DCC SEND %s", word_eol[2]);
2879 handle_command (sess, tbuf, FALSE);
2881 return TRUE;
2884 static int
2885 cmd_setcursor (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2887 int delta = FALSE;
2889 if (*word[2])
2891 if (word[2][0] == '-' || word[2][0] == '+')
2892 delta = TRUE;
2893 fe_set_inputbox_cursor (sess, delta, atoi (word[2]));
2894 return TRUE;
2897 return FALSE;
2900 static int
2901 cmd_settab (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2903 if (*word_eol[2])
2905 strcpy (tbuf, sess->channel);
2906 safe_strcpy (sess->channel, word_eol[2], CHANLEN);
2907 fe_set_channel (sess);
2908 strcpy (sess->channel, tbuf);
2911 return TRUE;
2914 static int
2915 cmd_settext (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2917 fe_set_inputbox_contents (sess, word_eol[2]);
2918 return TRUE;
2921 static int
2922 cmd_splay (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2924 if (*word[2])
2926 sound_play (word[2], FALSE);
2927 return TRUE;
2930 return FALSE;
2933 static int
2934 parse_irc_url (char *url, char *server_name[], char *port[], char *channel[], int *use_ssl)
2936 char *co;
2937 #ifdef USE_OPENSSL
2938 if (strncasecmp ("ircs://", url, 7) == 0)
2940 *use_ssl = TRUE;
2941 *server_name = url + 7;
2942 goto urlserv;
2944 #endif
2946 if (strncasecmp ("irc://", url, 6) == 0)
2948 *server_name = url + 6;
2949 #ifdef USE_OPENSSL
2950 urlserv:
2951 #endif
2952 /* check for port */
2953 co = strchr (*server_name, ':');
2954 if (co)
2956 *port = co + 1;
2957 *co = 0;
2958 } else
2959 co = *server_name;
2960 /* check for channel - mirc style */
2961 co = strchr (co + 1, '/');
2962 if (co)
2964 *co = 0;
2965 co++;
2966 if (*co == '#')
2967 *channel = co+1;
2968 else
2969 *channel = co;
2972 return TRUE;
2974 return FALSE;
2977 static int
2978 cmd_server (struct session *sess, char *tbuf, char *word[], char *word_eol[])
2980 int offset = 0;
2981 char *server_name = NULL;
2982 char *port = NULL;
2983 char *pass = NULL;
2984 char *channel = NULL;
2985 int use_ssl = FALSE;
2986 int is_url = TRUE;
2987 server *serv = sess->server;
2989 #ifdef USE_OPENSSL
2990 /* BitchX uses -ssl, mIRC uses -e, let's support both */
2991 if (strcmp (word[2], "-ssl") == 0 || strcmp (word[2], "-e") == 0)
2993 use_ssl = TRUE;
2994 offset++; /* args move up by 1 word */
2996 #endif
2998 if (!parse_irc_url (word[2 + offset], &server_name, &port, &channel, &use_ssl))
3000 is_url = FALSE;
3001 server_name = word[2 + offset];
3003 if (port)
3004 pass = word[3 + offset];
3005 else
3007 port = word[3 + offset];
3008 pass = word[4 + offset];
3011 if (!(*server_name))
3012 return FALSE;
3014 sess->server->network = NULL;
3016 /* dont clear it for /servchan */
3017 if (strncasecmp (word_eol[1], "SERVCHAN ", 9))
3018 sess->willjoinchannel[0] = 0;
3020 if (channel)
3022 sess->willjoinchannel[0] = '#';
3023 safe_strcpy ((sess->willjoinchannel + 1), channel, (CHANLEN - 1));
3026 /* support +7000 style ports like mIRC */
3027 if (port[0] == '+')
3029 port++;
3030 #ifdef USE_OPENSSL
3031 use_ssl = TRUE;
3032 #endif
3035 if (*pass)
3037 safe_strcpy (serv->password, pass, sizeof (serv->password));
3039 #ifdef USE_OPENSSL
3040 serv->use_ssl = use_ssl;
3041 serv->accept_invalid_cert = TRUE;
3042 #endif
3044 /* try to connect by Network name */
3045 if (servlist_connect_by_netname (sess, server_name, !is_url))
3046 return TRUE;
3048 if (*port)
3050 serv->connect (serv, server_name, atoi (port), FALSE);
3051 } else
3053 /* -1 for default port */
3054 serv->connect (serv, server_name, -1, FALSE);
3057 /* try to associate this connection with a listed network */
3058 if (!serv->network)
3059 /* search for this hostname in the entire server list */
3060 serv->network = servlist_net_find_from_server (server_name);
3061 /* may return NULL, but that's OK */
3063 return TRUE;
3066 static int
3067 cmd_servchan (struct session *sess, char *tbuf, char *word[],
3068 char *word_eol[])
3070 int offset = 0;
3072 #ifdef USE_OPENSSL
3073 if (strcmp (word[2], "-ssl") == 0)
3074 offset++;
3075 #endif
3077 if (*word[4 + offset])
3079 safe_strcpy (sess->willjoinchannel, word[4 + offset], CHANLEN);
3080 return cmd_server (sess, tbuf, word, word_eol);
3083 return FALSE;
3086 static int
3087 cmd_topic (struct session *sess, char *tbuf, char *word[], char *word_eol[])
3089 if (word[2][0] && is_channel (sess->server, word[2]))
3090 sess->server->p_topic (sess->server, word[2], word_eol[3]);
3091 else
3092 sess->server->p_topic (sess->server, sess->channel, word_eol[2]);
3093 return TRUE;
3096 static int
3097 cmd_tray (struct session *sess, char *tbuf, char *word[], char *word_eol[])
3099 if (strcmp (word[2], "-b") == 0)
3101 fe_tray_set_balloon (word[3], word[4][0] ? word[4] : NULL);
3102 return TRUE;
3105 if (strcmp (word[2], "-t") == 0)
3107 fe_tray_set_tooltip (word[3][0] ? word[3] : NULL);
3108 return TRUE;
3111 if (strcmp (word[2], "-i") == 0)
3113 fe_tray_set_icon (atoi (word[3]));
3114 return TRUE;
3117 if (strcmp (word[2], "-f") != 0)
3118 return FALSE;
3120 if (!word[3][0])
3122 fe_tray_set_file (NULL); /* default xchat icon */
3123 return TRUE;
3126 if (!word[4][0])
3128 fe_tray_set_file (word[3]); /* fixed custom icon */
3129 return TRUE;
3132 /* flash between 2 icons */
3133 fe_tray_set_flash (word[4], word[5][0] ? word[5] : NULL, atoi (word[3]));
3134 return TRUE;
3137 static int
3138 cmd_unignore (struct session *sess, char *tbuf, char *word[],
3139 char *word_eol[])
3141 char *mask = word[2];
3142 char *arg = word[3];
3143 if (*mask)
3145 if (ignore_del (mask, NULL))
3147 if (strcasecmp (arg, "QUIET"))
3148 EMIT_SIGNAL (XP_TE_IGNOREREMOVE, sess, mask, NULL, NULL, NULL, 0);
3150 return TRUE;
3152 return FALSE;
3155 static int
3156 cmd_unload (struct session *sess, char *tbuf, char *word[], char *word_eol[])
3158 #ifdef USE_PLUGIN
3159 int len, by_file = FALSE;
3161 len = strlen (word[2]);
3162 if (len > 3 && strcasecmp (word[2] + len - 3, ".so") == 0)
3163 by_file = TRUE;
3165 switch (plugin_kill (word[2], by_file))
3167 case 0:
3168 PrintText (sess, _("No such plugin found.\n"));
3169 break;
3170 case 1:
3171 return TRUE;
3172 case 2:
3173 PrintText (sess, _("That plugin is refusing to unload.\n"));
3174 break;
3176 #endif
3178 return FALSE;
3181 static server *
3182 find_server_from_hostname (char *hostname)
3184 GSList *list = serv_list;
3185 server *serv;
3187 while (list)
3189 serv = list->data;
3190 if (!strcasecmp (hostname, serv->hostname) && serv->connected)
3191 return serv;
3192 list = list->next;
3195 return NULL;
3198 static server *
3199 find_server_from_net (void *net)
3201 GSList *list = serv_list;
3202 server *serv;
3204 while (list)
3206 serv = list->data;
3207 if (serv->network == net && serv->connected)
3208 return serv;
3209 list = list->next;
3212 return NULL;
3215 static void
3216 url_join_only (server *serv, char *tbuf, char *channel)
3218 /* already connected, JOIN only. FIXME: support keys? */
3219 tbuf[0] = '#';
3220 /* tbuf is 4kb */
3221 safe_strcpy ((tbuf + 1), channel, 256);
3222 serv->p_join (serv, tbuf, "");
3225 static int
3226 cmd_url (struct session *sess, char *tbuf, char *word[], char *word_eol[])
3228 if (word[2][0])
3230 char *server_name = NULL;
3231 char *port = NULL;
3232 char *channel = NULL;
3233 char *url = g_strdup (word[2]);
3234 int use_ssl = FALSE;
3235 void *net;
3236 server *serv;
3238 if (parse_irc_url (url, &server_name, &port, &channel, &use_ssl))
3240 /* maybe we're already connected to this net */
3242 /* check for "FreeNode" */
3243 net = servlist_net_find (server_name, NULL, strcasecmp);
3244 /* check for "irc.eu.freenode.net" */
3245 if (!net)
3246 net = servlist_net_find_from_server (server_name);
3248 if (net)
3250 /* found the network, but are we connected? */
3251 serv = find_server_from_net (net);
3252 if (serv)
3254 url_join_only (serv, tbuf, channel);
3255 g_free (url);
3256 return TRUE;
3259 else
3261 /* an un-listed connection */
3262 serv = find_server_from_hostname (server_name);
3263 if (serv)
3265 url_join_only (serv, tbuf, channel);
3266 g_free (url);
3267 return TRUE;
3271 /* not connected to this net, open new window */
3272 cmd_newserver (sess, tbuf, word, word_eol);
3274 } else
3275 fe_open_url (word[2]);
3276 g_free (url);
3277 return TRUE;
3280 return FALSE;
3283 static int
3284 userlist_cb (struct User *user, session *sess)
3286 time_t lt;
3288 if (!user->lasttalk)
3289 lt = 0;
3290 else
3291 lt = time (0) - user->lasttalk;
3292 PrintTextf (sess,
3293 "\00306%s\t\00314[\00310%-38s\00314] \017ov\0033=\017%d%d away=%u lt\0033=\017%d\n",
3294 user->nick, user->hostname, user->op, user->voice, user->away, lt);
3296 return TRUE;
3299 static int
3300 cmd_uselect (struct session *sess, char *tbuf, char *word[], char *word_eol[])
3302 int idx = 2;
3303 int clear = TRUE;
3304 int scroll = FALSE;
3306 if (strcmp (word[2], "-a") == 0) /* ADD (don't clear selections) */
3308 clear = FALSE;
3309 idx++;
3311 if (strcmp (word[idx], "-s") == 0) /* SCROLL TO */
3313 scroll = TRUE;
3314 idx++;
3316 /* always valid, no args means clear the selection list */
3317 fe_uselect (sess, word + idx, clear, scroll);
3318 return TRUE;
3321 static int
3322 cmd_userlist (struct session *sess, char *tbuf, char *word[],
3323 char *word_eol[])
3325 tree_foreach (sess->usertree, (tree_traverse_func *)userlist_cb, sess);
3326 return TRUE;
3329 static int
3330 wallchop_cb (struct User *user, multidata *data)
3332 if (user->op)
3334 if (data->i)
3335 strcat (data->tbuf, ",");
3336 strcat (data->tbuf, user->nick);
3337 data->i++;
3339 if (data->i == 5)
3341 data->i = 0;
3342 sprintf (data->tbuf + strlen (data->tbuf),
3343 " :[@%s] %s", data->sess->channel, data->reason);
3344 data->sess->server->p_raw (data->sess->server, data->tbuf);
3345 strcpy (data->tbuf, "NOTICE ");
3348 return TRUE;
3351 static int
3352 cmd_wallchop (struct session *sess, char *tbuf, char *word[],
3353 char *word_eol[])
3355 multidata data;
3357 if (!(*word_eol[2]))
3358 return FALSE;
3360 strcpy (tbuf, "NOTICE ");
3362 data.reason = word_eol[2];
3363 data.tbuf = tbuf;
3364 data.i = 0;
3365 data.sess = sess;
3366 tree_foreach (sess->usertree, (tree_traverse_func*)wallchop_cb, &data);
3368 if (data.i)
3370 sprintf (tbuf + strlen (tbuf),
3371 " :[@%s] %s", sess->channel, word_eol[2]);
3372 sess->server->p_raw (sess->server, tbuf);
3375 return TRUE;
3378 static int
3379 cmd_wallchan (struct session *sess, char *tbuf, char *word[],
3380 char *word_eol[])
3382 GSList *list;
3384 if (*word_eol[2])
3386 list = sess_list;
3387 while (list)
3389 sess = list->data;
3390 if (sess->type == SESS_CHANNEL)
3392 inbound_chanmsg (sess->server, NULL, sess->channel,
3393 sess->server->nick, word_eol[2], TRUE, FALSE);
3394 sess->server->p_message (sess->server, sess->channel, word_eol[2]);
3396 list = list->next;
3398 return TRUE;
3400 return FALSE;
3403 static int
3404 cmd_hop (struct session *sess, char *tbuf, char *word[], char *word_eol[])
3406 int i = 2;
3408 while (1)
3410 if (!*word[i])
3412 if (i == 2)
3413 return FALSE;
3414 send_channel_modes (sess, tbuf, word, 2, i, '+', 'h', 0);
3415 return TRUE;
3417 i++;
3421 static int
3422 cmd_voice (struct session *sess, char *tbuf, char *word[], char *word_eol[])
3424 int i = 2;
3426 while (1)
3428 if (!*word[i])
3430 if (i == 2)
3431 return FALSE;
3432 send_channel_modes (sess, tbuf, word, 2, i, '+', 'v', 0);
3433 return TRUE;
3435 i++;
3439 /* *MUST* be kept perfectly sorted for the bsearch to work */
3440 const struct commands xc_cmds[] = {
3441 {"ADDBUTTON", cmd_addbutton, 0, 0, 1,
3442 N_("ADDBUTTON <name> <action>, adds a button under the user-list")},
3443 {"ALLCHAN", cmd_allchannels, 0, 0, 1,
3444 N_("ALLCHAN <cmd>, sends a command to all channels you're in")},
3445 {"ALLCHANL", cmd_allchannelslocal, 0, 0, 1,
3446 N_("ALLCHANL <cmd>, sends a command to all channels on the current server")},
3447 {"ALLSERV", cmd_allservers, 0, 0, 1,
3448 N_("ALLSERV <cmd>, sends a command to all servers you're in")},
3449 {"AWAY", cmd_away, 1, 0, 1, N_("AWAY [<reason>], sets you away")},
3450 {"BACK", cmd_back, 1, 0, 1, N_("BACK, sets you back (not away)")},
3451 {"BAN", cmd_ban, 1, 1, 1,
3452 N_("BAN <mask> [<bantype>], bans everyone matching the mask from the current channel. If they are already on the channel this doesn't kick them (needs chanop)")},
3453 {"CHANOPT", cmd_chanopt, 0, 0, 1, N_("CHANOPT [-quiet] <variable> [<value>]")},
3454 {"CHARSET", cmd_charset, 0, 0, 1, N_("CHARSET [<encoding>], get or set the encoding used for the current connection")},
3455 {"CLEAR", cmd_clear, 0, 0, 1, N_("CLEAR [ALL|HISTORY], Clears the current text window or command history")},
3456 {"CLOSE", cmd_close, 0, 0, 1, N_("CLOSE, Closes the current window/tab")},
3458 {"COUNTRY", cmd_country, 0, 0, 1,
3459 N_("COUNTRY [-s] <code|wildcard>, finds a country code, eg: au = australia")},
3460 {"CTCP", cmd_ctcp, 1, 0, 1,
3461 N_("CTCP <nick> <message>, send the CTCP message to nick, common messages are VERSION and USERINFO")},
3462 {"CYCLE", cmd_cycle, 1, 1, 1,
3463 N_("CYCLE [<channel>], parts the current or given channel and immediately rejoins")},
3464 {"DCC", cmd_dcc, 0, 0, 1,
3465 N_("\n"
3466 "DCC GET <nick> - accept an offered file\n"
3467 "DCC SEND [-maxcps=#] <nick> [file] - send a file to someone\n"
3468 "DCC PSEND [-maxcps=#] <nick> [file] - send a file using passive mode\n"
3469 "DCC LIST - show DCC list\n"
3470 "DCC CHAT <nick> - offer DCC CHAT to someone\n"
3471 "DCC PCHAT <nick> - offer DCC CHAT using passive mode\n"
3472 "DCC CLOSE <type> <nick> <file> example:\n"
3473 " /dcc close send johnsmith file.tar.gz")},
3474 {"DEBUG", cmd_debug, 0, 0, 1, 0},
3476 {"DEHOP", cmd_dehop, 1, 1, 1,
3477 N_("DEHOP <nick>, removes chanhalf-op status from the nick on the current channel (needs chanop)")},
3478 {"DELBUTTON", cmd_delbutton, 0, 0, 1,
3479 N_("DELBUTTON <name>, deletes a button from under the user-list")},
3480 {"DEOP", cmd_deop, 1, 1, 1,
3481 N_("DEOP <nick>, removes chanop status from the nick on the current channel (needs chanop)")},
3482 {"DEVOICE", cmd_devoice, 1, 1, 1,
3483 N_("DEVOICE <nick>, removes voice status from the nick on the current channel (needs chanop)")},
3484 {"DISCON", cmd_discon, 0, 0, 1, N_("DISCON, Disconnects from server")},
3485 {"DNS", cmd_dns, 0, 0, 1, N_("DNS <nick|host|ip>, Finds a users IP number")},
3486 {"ECHO", cmd_echo, 0, 0, 1, N_("ECHO <text>, Prints text locally")},
3487 {"EXEC", cmd_exec, 0, 0, 1,
3488 N_("EXEC [-o] <command>, runs the command. If -o flag is used then output is sent to current channel, else is printed to current text box")},
3489 {"EXECCONT", cmd_execc, 0, 0, 1, N_("EXECCONT, sends the process SIGCONT")},
3490 {"EXECKILL", cmd_execk, 0, 0, 1,
3491 N_("EXECKILL [-9], kills a running exec in the current session. If -9 is given the process is SIGKILL'ed")},
3492 {"EXECSTOP", cmd_execs, 0, 0, 1, N_("EXECSTOP, sends the process SIGSTOP")},
3493 {"EXECWRITE", cmd_execw, 0, 0, 1, N_("EXECWRITE, sends data to the processes stdin")},
3494 {"FLUSHQ", cmd_flushq, 0, 0, 1,
3495 N_("FLUSHQ, flushes the current server's send queue")},
3496 {"GATE", cmd_gate, 0, 0, 1,
3497 N_("GATE <host> [<port>], proxies through a host, port defaults to 23")},
3498 {"GETFILE", cmd_getfile, 0, 0, 1, "GETFILE [-folder] [-multi] [-save] <command> <title> [<initial>]"},
3499 {"GETINT", cmd_getint, 0, 0, 1, "GETINT <default> <command> <prompt>"},
3500 {"GETSTR", cmd_getstr, 0, 0, 1, "GETSTR <default> <command> <prompt>"},
3501 {"GHOST", cmd_ghost, 1, 0, 1, N_("GHOST <nick> [password], Kills a ghosted nickname")},
3502 {"GUI", cmd_gui, 0, 0, 1, "GUI [APPLY|ATTACH|DETACH|SHOW|HIDE|FOCUS|FLASH|ICONIFY|COLOR <n>]\n"
3503 " GUI [MSGBOX <text>|MENU TOGGLE]"},
3504 {"HELP", cmd_help, 0, 0, 1, 0},
3505 {"HOP", cmd_hop, 1, 1, 1,
3506 N_("HOP <nick>, gives chanhalf-op status to the nick (needs chanop)")},
3507 {"ID", cmd_id, 1, 0, 1, N_("ID <password>, identifies yourself to nickserv")},
3508 {"IGNORE", cmd_ignore, 0, 0, 1,
3509 N_("IGNORE <mask> <types..> <options..>\n"
3510 " mask - host mask to ignore, eg: *!*@*.aol.com\n"
3511 " types - types of data to ignore, one or all of:\n"
3512 " PRIV, CHAN, NOTI, CTCP, DCC, INVI, ALL\n"
3513 " options - NOSAVE, QUIET")},
3515 {"INVITE", cmd_invite, 1, 0, 1,
3516 N_("INVITE <nick> [<channel>], invites someone to a channel, by default the current channel (needs chanop)")},
3517 {"JOIN", cmd_join, 1, 0, 0, N_("JOIN <channel>, joins the channel")},
3518 {"KICK", cmd_kick, 1, 1, 1,
3519 N_("KICK <nick>, kicks the nick from the current channel (needs chanop)")},
3520 {"KICKBAN", cmd_kickban, 1, 1, 1,
3521 N_("KICKBAN <nick>, bans then kicks the nick from the current channel (needs chanop)")},
3522 {"KILLALL", cmd_killall, 0, 0, 1, "KILLALL, immediately exit"},
3523 {"LAGCHECK", cmd_lagcheck, 0, 0, 1,
3524 N_("LAGCHECK, forces a new lag check")},
3525 {"LASTLOG", cmd_lastlog, 0, 0, 1,
3526 N_("LASTLOG <string>, searches for a string in the buffer")},
3527 {"LIST", cmd_list, 1, 0, 1, 0},
3528 {"LOAD", cmd_load, 0, 0, 1, N_("LOAD [-e] <file>, loads a plugin or script")},
3530 {"MDEHOP", cmd_mdehop, 1, 1, 1,
3531 N_("MDEHOP, Mass deop's all chanhalf-ops in the current channel (needs chanop)")},
3532 {"MDEOP", cmd_mdeop, 1, 1, 1,
3533 N_("MDEOP, Mass deop's all chanops in the current channel (needs chanop)")},
3534 {"ME", cmd_me, 0, 0, 1,
3535 N_("ME <action>, sends the action to the current channel (actions are written in the 3rd person, like /me jumps)")},
3536 {"MENU", cmd_menu, 0, 0, 1, "MENU [-eX] [-i<ICONFILE>] [-k<mod>,<key>] [-m] [-pX] [-r<X,group>] [-tX] {ADD|DEL} <path> [command] [unselect command]\n"
3537 " See http://xchat.org/docs/menu/ for more details."},
3538 {"MKICK", cmd_mkick, 1, 1, 1,
3539 N_("MKICK, Mass kicks everyone except you in the current channel (needs chanop)")},
3540 {"MODE", cmd_mode, 1, 0, 1, 0},
3541 {"MOP", cmd_mop, 1, 1, 1,
3542 N_("MOP, Mass op's all users in the current channel (needs chanop)")},
3543 {"MSG", cmd_msg, 0, 0, 1, N_("MSG <nick> <message>, sends a private message")},
3545 {"NAMES", cmd_names, 1, 0, 1,
3546 N_("NAMES, Lists the nicks on the current channel")},
3547 {"NCTCP", cmd_nctcp, 1, 0, 1,
3548 N_("NCTCP <nick> <message>, Sends a CTCP notice")},
3549 {"NEWSERVER", cmd_newserver, 0, 0, 1, N_("NEWSERVER [-noconnect] <hostname> [<port>]")},
3550 {"NICK", cmd_nick, 0, 0, 1, N_("NICK <nickname>, sets your nick")},
3552 {"NOTICE", cmd_notice, 1, 0, 1,
3553 N_("NOTICE <nick/channel> <message>, sends a notice. Notices are a type of message that should be auto reacted to")},
3554 {"NOTIFY", cmd_notify, 0, 0, 1,
3555 N_("NOTIFY [-n network1[,network2,...]] [<nick>], displays your notify list or adds someone to it")},
3556 {"OP", cmd_op, 1, 1, 1,
3557 N_("OP <nick>, gives chanop status to the nick (needs chanop)")},
3558 {"PART", cmd_part, 1, 1, 0,
3559 N_("PART [<channel>] [<reason>], leaves the channel, by default the current one")},
3560 {"PING", cmd_ping, 1, 0, 1,
3561 N_("PING <nick | channel>, CTCP pings nick or channel")},
3562 {"QUERY", cmd_query, 0, 0, 1,
3563 N_("QUERY [-nofocus] <nick>, opens up a new privmsg window to someone")},
3564 {"QUIT", cmd_quit, 0, 0, 1,
3565 N_("QUIT [<reason>], disconnects from the current server")},
3566 {"QUOTE", cmd_quote, 1, 0, 1,
3567 N_("QUOTE <text>, sends the text in raw form to the server")},
3568 #ifdef USE_OPENSSL
3569 {"RECONNECT", cmd_reconnect, 0, 0, 1,
3570 N_("RECONNECT [-ssl] [<host>] [<port>] [<password>], Can be called just as /RECONNECT to reconnect to the current server or with /RECONNECT ALL to reconnect to all the open servers")},
3571 #else
3572 {"RECONNECT", cmd_reconnect, 0, 0, 1,
3573 N_("RECONNECT [<host>] [<port>] [<password>], Can be called just as /RECONNECT to reconnect to the current server or with /RECONNECT ALL to reconnect to all the open servers")},
3574 #endif
3575 {"RECV", cmd_recv, 1, 0, 1, N_("RECV <text>, send raw data to xchat, as if it was received from the irc server")},
3577 {"SAY", cmd_say, 0, 0, 1,
3578 N_("SAY <text>, sends the text to the object in the current window")},
3579 {"SEND", cmd_send, 0, 0, 1, N_("SEND <nick> [<file>]")},
3580 #ifdef USE_OPENSSL
3581 {"SERVCHAN", cmd_servchan, 0, 0, 1,
3582 N_("SERVCHAN [-ssl] <host> <port> <channel>, connects and joins a channel")},
3583 #else
3584 {"SERVCHAN", cmd_servchan, 0, 0, 1,
3585 N_("SERVCHAN <host> <port> <channel>, connects and joins a channel")},
3586 #endif
3587 #ifdef USE_OPENSSL
3588 {"SERVER", cmd_server, 0, 0, 1,
3589 N_("SERVER [-ssl] <host> [<port>] [<password>], connects to a server, the default port is 6667 for normal connections, and 9999 for ssl connections")},
3590 #else
3591 {"SERVER", cmd_server, 0, 0, 1,
3592 N_("SERVER <host> [<port>] [<password>], connects to a server, the default port is 6667")},
3593 #endif
3594 {"SET", cmd_set, 0, 0, 1, N_("SET [-e] [-off|-on] [-quiet] <variable> [<value>]")},
3595 {"SETCURSOR", cmd_setcursor, 0, 0, 1, N_("SETCURSOR [-|+]<position>, reposition the cursor in the inputbox")},
3596 {"SETTAB", cmd_settab, 0, 0, 1, N_("SETTAB <new name>, change a tab's name, tab_trunc limit still applies")},
3597 {"SETTEXT", cmd_settext, 0, 0, 1, N_("SETTEXT <new text>, replace the text in the input box")},
3598 {"SPLAY", cmd_splay, 0, 0, 1, "SPLAY <soundfile>"},
3599 {"TOPIC", cmd_topic, 1, 1, 1,
3600 N_("TOPIC [<topic>], sets the topic if one is given, else shows the current topic")},
3601 {"TRAY", cmd_tray, 0, 0, 1,
3602 N_("\nTRAY -f <timeout> <file1> [<file2>] Blink tray between two icons.\n"
3603 "TRAY -f <filename> Set tray to a fixed icon.\n"
3604 "TRAY -i <number> Blink tray with an internal icon.\n"
3605 "TRAY -t <text> Set the tray tooltip.\n"
3606 "TRAY -b <title> <text> Set the tray balloon."
3608 {"UNBAN", cmd_unban, 1, 1, 1,
3609 N_("UNBAN <mask> [<mask>...], unbans the specified masks.")},
3610 {"UNIGNORE", cmd_unignore, 0, 0, 1, N_("UNIGNORE <mask> [QUIET]")},
3611 {"UNLOAD", cmd_unload, 0, 0, 1, N_("UNLOAD <name>, unloads a plugin or script")},
3612 {"URL", cmd_url, 0, 0, 1, N_("URL <url>, opens a URL in your browser")},
3613 {"USELECT", cmd_uselect, 0, 1, 0,
3614 N_("USELECT [-a] [-s] <nick1> <nick2> etc, highlights nick(s) in channel userlist")},
3615 {"USERLIST", cmd_userlist, 1, 1, 1, 0},
3616 {"VOICE", cmd_voice, 1, 1, 1,
3617 N_("VOICE <nick>, gives voice status to someone (needs chanop)")},
3618 {"WALLCHAN", cmd_wallchan, 1, 1, 1,
3619 N_("WALLCHAN <message>, writes the message to all channels")},
3620 {"WALLCHOP", cmd_wallchop, 1, 1, 1,
3621 N_("WALLCHOP <message>, sends the message to all chanops on the current channel")},
3622 {0, 0, 0, 0, 0, 0}
3626 static int
3627 command_compare (const void *a, const void *b)
3629 return strcasecmp (a, ((struct commands *)b)->name);
3632 static struct commands *
3633 find_internal_command (char *name)
3635 /* the "-1" is to skip the NULL terminator */
3636 return bsearch (name, xc_cmds, (sizeof (xc_cmds) /
3637 sizeof (xc_cmds[0])) - 1, sizeof (xc_cmds[0]), command_compare);
3640 static void
3641 help (session *sess, char *tbuf, char *helpcmd, int quiet)
3643 struct commands *cmd;
3645 if (plugin_show_help (sess, helpcmd))
3646 return;
3648 cmd = find_internal_command (helpcmd);
3650 if (cmd)
3652 if (cmd->help)
3654 snprintf (tbuf, TBUFSIZE, _("Usage: %s\n"), _(cmd->help));
3655 PrintText (sess, tbuf);
3656 } else
3658 if (!quiet)
3659 PrintText (sess, _("\nNo help available on that command.\n"));
3661 return;
3664 if (!quiet)
3665 PrintText (sess, _("No such command.\n"));
3668 /* inserts %a, %c, %d etc into buffer. Also handles &x %x for word/word_eol. *
3669 * returns 2 on buffer overflow
3670 * returns 1 on success *
3671 * returns 0 on bad-args-for-user-command *
3672 * - word/word_eol args might be NULL *
3673 * - this beast is used for UserCommands, UserlistButtons and CTCP replies */
3676 auto_insert (char *dest, int destlen, unsigned char *src, char *word[],
3677 char *word_eol[], char *a, char *c, char *d, char *e, char *h,
3678 char *n, char *s)
3680 int num;
3681 char buf[32];
3682 time_t now;
3683 struct tm *tm_ptr;
3684 char *utf;
3685 gsize utf_len;
3686 char *orig = dest;
3688 destlen--;
3690 while (src[0])
3692 if (src[0] == '%' || src[0] == '&')
3694 if (isdigit ((unsigned char) src[1]))
3696 if (isdigit ((unsigned char) src[2]) && isdigit ((unsigned char) src[3]))
3698 buf[0] = src[1];
3699 buf[1] = src[2];
3700 buf[2] = src[3];
3701 buf[3] = 0;
3702 dest[0] = atoi (buf);
3703 utf = g_locale_to_utf8 (dest, 1, 0, &utf_len, 0);
3704 if (utf)
3706 if ((dest - orig) + utf_len >= destlen)
3708 g_free (utf);
3709 return 2;
3712 memcpy (dest, utf, utf_len);
3713 g_free (utf);
3714 dest += utf_len;
3716 src += 3;
3717 } else
3719 if (word)
3721 src++;
3722 num = src[0] - '0'; /* ascii to decimal */
3723 if (*word[num] == 0)
3724 return 0;
3726 if (src[-1] == '%')
3727 utf = word[num];
3728 else
3729 utf = word_eol[num];
3731 /* avoid recusive usercommand overflow */
3732 if ((dest - orig) + strlen (utf) >= destlen)
3733 return 2;
3735 strcpy (dest, utf);
3736 dest += strlen (dest);
3739 } else
3741 if (src[0] == '&')
3742 goto lamecode;
3743 src++;
3744 utf = NULL;
3745 switch (src[0])
3747 case '%':
3748 if ((dest - orig) + 2 >= destlen)
3749 return 2;
3750 dest[0] = '%';
3751 dest[1] = 0;
3752 dest++;
3753 break;
3754 case 'a':
3755 utf = a; break;
3756 case 'c':
3757 utf = c; break;
3758 case 'd':
3759 utf = d; break;
3760 case 'e':
3761 utf = e; break;
3762 case 'h':
3763 utf = h; break;
3764 case 'm':
3765 utf = get_cpu_str (); break;
3766 case 'n':
3767 utf = n; break;
3768 case 's':
3769 utf = s; break;
3770 case 't':
3771 now = time (0);
3772 utf = ctime (&now);
3773 utf[19] = 0;
3774 break;
3775 case 'v':
3776 utf = PACKAGE_VERSION; break;
3777 break;
3778 case 'y':
3779 now = time (0);
3780 tm_ptr = localtime (&now);
3781 snprintf (buf, sizeof (buf), "%4d%02d%02d", 1900 +
3782 tm_ptr->tm_year, 1 + tm_ptr->tm_mon, tm_ptr->tm_mday);
3783 utf = buf;
3784 break;
3785 default:
3786 src--;
3787 goto lamecode;
3790 if (utf)
3792 if ((dest - orig) + strlen (utf) >= destlen)
3793 return 2;
3794 strcpy (dest, utf);
3795 dest += strlen (dest);
3799 src++;
3800 } else
3802 utf_len = g_utf8_skip[src[0]];
3804 if ((dest - orig) + utf_len >= destlen)
3805 return 2;
3807 if (utf_len == 1)
3809 lamecode:
3810 dest[0] = src[0];
3811 dest++;
3812 src++;
3813 } else
3815 memcpy (dest, src, utf_len);
3816 dest += utf_len;
3817 src += utf_len;
3822 dest[0] = 0;
3824 return 1;
3827 void
3828 check_special_chars (char *cmd, int do_ascii) /* check for %X */
3830 int occur = 0;
3831 int len = strlen (cmd);
3832 char *buf, *utf;
3833 char tbuf[4];
3834 int i = 0, j = 0;
3835 gsize utf_len;
3837 if (!len)
3838 return;
3840 buf = malloc (len + 1);
3842 if (buf)
3844 while (cmd[j])
3846 switch (cmd[j])
3848 case '%':
3849 occur++;
3850 if ( do_ascii &&
3851 j + 3 < len &&
3852 (isdigit ((unsigned char) cmd[j + 1]) && isdigit ((unsigned char) cmd[j + 2]) &&
3853 isdigit ((unsigned char) cmd[j + 3])))
3855 tbuf[0] = cmd[j + 1];
3856 tbuf[1] = cmd[j + 2];
3857 tbuf[2] = cmd[j + 3];
3858 tbuf[3] = 0;
3859 buf[i] = atoi (tbuf);
3860 utf = g_locale_to_utf8 (buf + i, 1, 0, &utf_len, 0);
3861 if (utf)
3863 memcpy (buf + i, utf, utf_len);
3864 g_free (utf);
3865 i += (utf_len - 1);
3867 j += 3;
3868 } else
3870 switch (cmd[j + 1])
3872 case 'R':
3873 buf[i] = '\026';
3874 break;
3875 case 'U':
3876 buf[i] = '\037';
3877 break;
3878 case 'B':
3879 buf[i] = '\002';
3880 break;
3881 case 'C':
3882 buf[i] = '\003';
3883 break;
3884 case 'O':
3885 buf[i] = '\017';
3886 break;
3887 case 'H': /* CL: invisible text code */
3888 buf[i] = HIDDEN_CHAR;
3889 break;
3890 case '%':
3891 buf[i] = '%';
3892 break;
3893 default:
3894 buf[i] = '%';
3895 j--;
3896 break;
3898 j++;
3899 break;
3900 default:
3901 buf[i] = cmd[j];
3904 j++;
3905 i++;
3907 buf[i] = 0;
3908 if (occur)
3909 strcpy (cmd, buf);
3910 free (buf);
3914 typedef struct
3916 char *nick;
3917 int len;
3918 struct User *best;
3919 int bestlen;
3920 char *space;
3921 char *tbuf;
3922 } nickdata;
3924 static int
3925 nick_comp_cb (struct User *user, nickdata *data)
3927 int lenu;
3929 if (!rfc_ncasecmp (user->nick, data->nick, data->len))
3931 lenu = strlen (user->nick);
3932 if (lenu == data->len)
3934 snprintf (data->tbuf, TBUFSIZE, "%s%s", user->nick, data->space);
3935 data->len = -1;
3936 return FALSE;
3937 } else if (lenu < data->bestlen)
3939 data->bestlen = lenu;
3940 data->best = user;
3944 return TRUE;
3947 static void
3948 perform_nick_completion (struct session *sess, char *cmd, char *tbuf)
3950 int len;
3951 char *space = strchr (cmd, ' ');
3952 if (space && space != cmd)
3954 if (space[-1] == prefs.nick_suffix[0] && space - 1 != cmd)
3956 len = space - cmd - 1;
3957 if (len < NICKLEN)
3959 char nick[NICKLEN];
3960 nickdata data;
3962 memcpy (nick, cmd, len);
3963 nick[len] = 0;
3965 data.nick = nick;
3966 data.len = len;
3967 data.bestlen = INT_MAX;
3968 data.best = NULL;
3969 data.tbuf = tbuf;
3970 data.space = space - 1;
3971 tree_foreach (sess->usertree, (tree_traverse_func *)nick_comp_cb, &data);
3973 if (data.len == -1)
3974 return;
3976 if (data.best)
3978 snprintf (tbuf, TBUFSIZE, "%s%s", data.best->nick, space - 1);
3979 return;
3985 strcpy (tbuf, cmd);
3988 static void
3989 user_command (session * sess, char *tbuf, char *cmd, char *word[],
3990 char *word_eol[])
3992 if (!auto_insert (tbuf, 2048, cmd, word, word_eol, "", sess->channel, "",
3993 server_get_network (sess->server, TRUE), "",
3994 sess->server->nick, ""))
3996 PrintText (sess, _("Bad arguments for user command.\n"));
3997 return;
4000 handle_command (sess, tbuf, TRUE);
4003 /* handle text entered without a CMDchar prefix */
4005 static void
4006 handle_say (session *sess, char *text, int check_spch)
4008 struct DCC *dcc;
4009 char *word[PDIWORDS+1];
4010 char *word_eol[PDIWORDS+1];
4011 char pdibuf_static[1024];
4012 char newcmd_static[1024];
4013 char *pdibuf = pdibuf_static;
4014 char *newcmd = newcmd_static;
4015 int len;
4016 int newcmdlen = sizeof newcmd_static;
4018 if (strcmp (sess->channel, "(lastlog)") == 0)
4020 lastlog (sess->lastlog_sess, text, sess->lastlog_regexp);
4021 return;
4024 len = strlen (text);
4025 if (len >= sizeof pdibuf_static)
4026 pdibuf = malloc (len + 1);
4028 if (len + NICKLEN >= newcmdlen)
4029 newcmd = malloc (newcmdlen = len + NICKLEN + 1);
4031 if (check_spch && prefs.perc_color)
4032 check_special_chars (text, prefs.perc_ascii);
4034 /* Python relies on this */
4035 word[PDIWORDS] = NULL;
4036 word_eol[PDIWORDS] = NULL;
4038 /* split the text into words and word_eol */
4039 process_data_init (pdibuf, text, word, word_eol, TRUE, FALSE);
4041 /* a command of "" can be hooked for non-commands */
4042 if (plugin_emit_command (sess, "", word, word_eol))
4043 goto xit;
4045 /* incase a plugin did /close */
4046 if (!is_session (sess))
4047 goto xit;
4049 if (!sess->channel[0] || sess->type == SESS_SERVER || sess->type == SESS_NOTICES || sess->type == SESS_SNOTICES)
4051 notj_msg (sess);
4052 goto xit;
4055 if (prefs.nickcompletion)
4056 perform_nick_completion (sess, text, newcmd);
4057 else
4058 safe_strcpy (newcmd, text, newcmdlen);
4060 text = newcmd;
4062 if (sess->type == SESS_DIALOG)
4064 /* try it via dcc, if possible */
4065 dcc = dcc_write_chat (sess->channel, text);
4066 if (dcc)
4068 inbound_chanmsg (sess->server, NULL, sess->channel,
4069 sess->server->nick, text, TRUE, FALSE);
4070 set_topic (sess, net_ip (dcc->addr), net_ip (dcc->addr));
4071 goto xit;
4075 if (sess->server->connected)
4077 unsigned int max;
4078 unsigned char t = 0;
4080 /* maximum allowed message text */
4081 /* :nickname!username@host.com PRIVMSG #channel :text\r\n */
4082 max = 512;
4083 max -= 16; /* :, !, @, " PRIVMSG ", " ", :, \r, \n */
4084 max -= strlen (sess->server->nick);
4085 max -= strlen (sess->channel);
4086 if (sess->me && sess->me->hostname)
4087 max -= strlen (sess->me->hostname);
4088 else
4090 max -= 9; /* username */
4091 max -= 65; /* max possible hostname and '@' */
4094 if (strlen (text) > max)
4096 int i = 0, size;
4098 /* traverse the utf8 string and find the nearest cut point that
4099 doesn't split 1 char in half */
4100 while (1)
4102 size = g_utf8_skip[((unsigned char *)text)[i]];
4103 if ((i + size) >= max)
4104 break;
4105 i += size;
4107 max = i;
4108 t = text[max];
4109 text[max] = 0; /* insert a NULL terminator to shorten it */
4112 inbound_chanmsg (sess->server, sess, sess->channel, sess->server->nick,
4113 text, TRUE, FALSE);
4114 sess->server->p_message (sess->server, sess->channel, text);
4116 if (t)
4118 text[max] = t;
4119 handle_say (sess, text + max, FALSE);
4122 } else
4124 notc_msg (sess);
4127 xit:
4128 if (pdibuf != pdibuf_static)
4129 free (pdibuf);
4131 if (newcmd != newcmd_static)
4132 free (newcmd);
4135 /* handle a command, without the '/' prefix */
4138 handle_command (session *sess, char *cmd, int check_spch)
4140 struct popup *pop;
4141 int user_cmd = FALSE;
4142 GSList *list;
4143 char *word[PDIWORDS+1];
4144 char *word_eol[PDIWORDS+1];
4145 static int command_level = 0;
4146 struct commands *int_cmd;
4147 char pdibuf_static[1024];
4148 char tbuf_static[TBUFSIZE];
4149 char *pdibuf;
4150 char *tbuf;
4151 int len;
4152 int ret = TRUE;
4154 if (command_level > 99)
4156 fe_message (_("Too many recursive usercommands, aborting."), FE_MSG_ERROR);
4157 return TRUE;
4159 command_level++;
4160 /* anything below MUST DEC command_level before returning */
4162 len = strlen (cmd);
4163 if (len >= sizeof (pdibuf_static))
4164 pdibuf = malloc (len + 1);
4165 else
4166 pdibuf = pdibuf_static;
4168 if ((len * 2) >= sizeof (tbuf_static))
4169 tbuf = malloc ((len * 2) + 1);
4170 else
4171 tbuf = tbuf_static;
4173 /* split the text into words and word_eol */
4174 process_data_init (pdibuf, cmd, word, word_eol, TRUE, TRUE);
4176 /* ensure an empty string at index 32 for cmd_deop etc */
4177 /* (internal use only, plugins can still only read 1-31). */
4178 word[PDIWORDS] = "\000\000";
4179 word_eol[PDIWORDS] = "\000\000";
4181 int_cmd = find_internal_command (word[1]);
4182 /* redo it without quotes processing, for some commands like /JOIN */
4183 if (int_cmd && !int_cmd->handle_quotes)
4184 process_data_init (pdibuf, cmd, word, word_eol, FALSE, FALSE);
4186 if (check_spch && prefs.perc_color)
4187 check_special_chars (cmd, prefs.perc_ascii);
4189 if (plugin_emit_command (sess, word[1], word, word_eol))
4190 goto xit;
4192 /* incase a plugin did /close */
4193 if (!is_session (sess))
4194 goto xit;
4196 /* first see if it's a userCommand */
4197 list = command_list;
4198 while (list)
4200 pop = (struct popup *) list->data;
4201 if (!strcasecmp (pop->name, word[1]))
4203 user_command (sess, tbuf, pop->cmd, word, word_eol);
4204 user_cmd = TRUE;
4206 list = list->next;
4209 if (user_cmd)
4210 goto xit;
4212 /* now check internal commands */
4213 int_cmd = find_internal_command (word[1]);
4215 if (int_cmd)
4217 if (int_cmd->needserver && !sess->server->connected)
4219 notc_msg (sess);
4220 } else if (int_cmd->needchannel && !sess->channel[0])
4222 notj_msg (sess);
4223 } else
4225 switch (int_cmd->callback (sess, tbuf, word, word_eol))
4227 case FALSE:
4228 help (sess, tbuf, int_cmd->name, TRUE);
4229 break;
4230 case 2:
4231 ret = FALSE;
4232 goto xit;
4235 } else
4237 /* unknown command, just send it to the server and hope */
4238 if (!sess->server->connected)
4239 PrintText (sess, _("Unknown Command. Try /help\n"));
4240 else
4241 sess->server->p_raw (sess->server, cmd);
4244 xit:
4245 command_level--;
4247 if (pdibuf != pdibuf_static)
4248 free (pdibuf);
4250 if (tbuf != tbuf_static)
4251 free (tbuf);
4253 return ret;
4256 /* handle one line entered into the input box */
4258 static int
4259 handle_user_input (session *sess, char *text, int history, int nocommand)
4261 if (*text == '\0')
4262 return 1;
4264 if (history)
4265 history_add (&sess->history, text);
4267 /* is it NOT a command, just text? */
4268 if (nocommand || text[0] != prefs.cmdchar[0])
4270 handle_say (sess, text, TRUE);
4271 return 1;
4274 /* check for // */
4275 if (text[0] == prefs.cmdchar[0] && text[1] == prefs.cmdchar[0])
4277 handle_say (sess, text + 1, TRUE);
4278 return 1;
4281 if (prefs.cmdchar[0] == '/')
4283 int i;
4284 const char *unix_dirs [] = {
4285 "/bin/", "/boot/", "/dev/",
4286 "/etc/", "/home/", "/lib/",
4287 "/lost+found/", "/mnt/", "/opt/",
4288 "/proc/", "/root/", "/sbin/",
4289 "/tmp/", "/usr/", "/var/",
4290 "/gnome/", NULL};
4291 for (i = 0; unix_dirs[i] != NULL; i++)
4292 if (strncmp (text, unix_dirs[i], strlen (unix_dirs[i]))==0)
4294 handle_say (sess, text, TRUE);
4295 return 1;
4299 return handle_command (sess, text + 1, TRUE);
4302 /* changed by Steve Green. Macs sometimes paste with imbedded \r */
4303 void
4304 handle_multiline (session *sess, char *cmd, int history, int nocommand)
4306 while (*cmd)
4308 char *cr = cmd + strcspn (cmd, "\n\r");
4309 int end_of_string = *cr == 0;
4310 *cr = 0;
4311 if (!handle_user_input (sess, cmd, history, nocommand))
4312 return;
4313 if (end_of_string)
4314 break;
4315 cmd = cr + 1;
4319 /*void
4320 handle_multiline (session *sess, char *cmd, int history, int nocommand)
4322 char *cr;
4324 cr = strchr (cmd, '\n');
4325 if (cr)
4327 while (1)
4329 if (cr)
4330 *cr = 0;
4331 if (!handle_user_input (sess, cmd, history, nocommand))
4332 return;
4333 if (!cr)
4334 break;
4335 cmd = cr + 1;
4336 if (*cmd == 0)
4337 break;
4338 cr = strchr (cmd, '\n');
4340 } else
4342 handle_user_input (sess, cmd, history, nocommand);