source: irc_commands.c @ dc96e6e

Last change on this file since dc96e6e was dc96e6e, checked in by dequis <dx@…>, at 2015-09-11T02:31:10Z

CAP REQ

  • Property mode set to 100644
File size: 24.0 KB
RevLine 
[5ebff60]1/********************************************************************\
[0298d11]2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[0e788f5]4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
[0298d11]5  \********************************************************************/
6
7/* IRC commands                                                         */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
[6f10697]22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
[0298d11]24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
[674a01d]28#include "help.h"
[0431ea1]29#include "ipc.h"
[0298d11]30
[dc96e6e]31typedef guint32 cap_flag;     /* 32 bits ought to be enough for anybody */
32
33typedef struct _cap_info {
34        char *name;
35        cap_flag flag;
36} cap_info_t;
37
38#define CAP_FOO (1 << 0)
39#define CAP_BAR (1 << 1)
40
41static const cap_info_t supported_caps[] = {
42        {"foo", CAP_FOO},
43        {"bar", CAP_BAR},
44        {NULL},
45};
46
47static cap_flag cap_flag_from_string(char *cap_name) {
48        int i;
49
50        if (!cap_name && !cap_name[0]) {
51                return 0;
52        }
53
54        if (cap_name[0] == '-') {
55                cap_name++;
56        }
57
58        for (i = 0; supported_caps[i].name; i++) {
59                if (strcmp(supported_caps[i].name, cap_name) == 0) {
60                        return supported_caps[i].flag;
61                }
62        }
63        return 0;
64}
65
66static gboolean irc_cmd_cap_req(irc_t *irc, char *caps)
67{
68        int i;
69        char *lower = NULL;
70        char **split = NULL;
71        cap_flag new_caps = irc->caps;
72
73        if (!caps || !caps[0]) {
74                return FALSE;
75        }
76
77        lower = g_ascii_strdown(caps, -1);
78        split = g_strsplit(lower, " ", -1);
79        g_free(lower);
80
81        for (i = 0; split[i]; i++) {
82                gboolean remove;
83                cap_flag flag;
84
85                if (!split[i][0]) {
86                        continue;   /* skip empty items (consecutive spaces) */
87                }
88
89                remove = (split[i][0] == '-');
90                flag = cap_flag_from_string(split[i]);
91               
92                if (!flag || (remove && !(irc->caps & flag))) {
93                        /* unsupported cap, or removing something that isn't there */
94                        g_strfreev(split);
95                        return FALSE;
96                }
97
98                if (remove) {
99                        new_caps &= ~flag;
100                } else {
101                        new_caps |= flag;
102                }
103        }
104
105        /* if we got here, set the new caps and ack */
106        irc->caps = new_caps;
107
108        g_strfreev(split);
109        return TRUE;
110}
111
[0ef1c92]112static void irc_cmd_cap(irc_t *irc, char **cmd)
113{
114        if (!(irc->status & USTATUS_LOGGED_IN)) {
115                /* Put registration on hold until CAP END */
116                irc->status |= USTATUS_CAP_PENDING;
117        }
118
119        if (g_strcasecmp(cmd[1], "LS") == 0) {
120                /* gboolean irc302 = (g_strcmp0(cmd[2], "302") == 0); */
[dc96e6e]121                //char *ls = g_strjoinv(" ", (char **) supported_caps);
122
123                irc_send_cap(irc, "LS", "foo bar");
124
125                //g_free(ls);
[0ef1c92]126
127        } else if (g_strcasecmp(cmd[1], "LIST") == 0) {
128                irc_send_cap(irc, "LIST", "");
129
130        } else if (g_strcasecmp(cmd[1], "REQ") == 0) {
[dc96e6e]131                gboolean ack = irc_cmd_cap_req(irc, cmd[2]);
132
133                irc_send_cap(irc, ack ? "ACK" : "NAK", cmd[2] ? : "");
[0ef1c92]134
135        } else if (g_strcasecmp(cmd[1], "END") == 0) {
136                irc->status &= ~USTATUS_CAP_PENDING;
137                irc_check_login(irc);
138
139        } else {
140                irc_send_num(irc, 410, "%s :Invalid CAP command", cmd[1]);
141        }
142
143}
144
[5ebff60]145static void irc_cmd_pass(irc_t *irc, char **cmd)
[0298d11]146{
[5ebff60]147        if (irc->status & USTATUS_LOGGED_IN) {
[a199d33]148                char *send_cmd[] = { "identify", cmd[1], NULL };
[5ebff60]149
[a199d33]150                /* We're already logged in, this client seems to send the PASS
151                   command last. (Possibly it won't send it at all if it turns
152                   out we don't require it, which will break this feature.)
153                   Try to identify using the given password. */
[5ebff60]154                root_command(irc, send_cmd);
[daae10f]155                return;
[a199d33]156        }
157        /* Handling in pre-logged-in state, first see if this server is
158           password-protected: */
[5ebff60]159        else if (global.conf->auth_pass &&
160                 (strncmp(global.conf->auth_pass, "md5:", 4) == 0 ?
161                  md5_verify_password(cmd[1], global.conf->auth_pass + 4) == 0 :
162                  strcmp(cmd[1], global.conf->auth_pass) == 0)) {
[79e826a]163                irc->status |= USTATUS_AUTHORIZED;
[5ebff60]164                irc_check_login(irc);
165        } else if (global.conf->auth_pass) {
166                irc_send_num(irc, 464, ":Incorrect password");
167        } else {
[a199d33]168                /* Remember the password and try to identify after USER/NICK. */
[5ebff60]169                irc_setpass(irc, cmd[1]);
170                irc_check_login(irc);
[a199d33]171        }
[0298d11]172}
173
[5ebff60]174static void irc_cmd_user(irc_t *irc, char **cmd)
[0298d11]175{
[5ebff60]176        irc->user->user = g_strdup(cmd[1]);
177        irc->user->fullname = g_strdup(cmd[4]);
178
179        irc_check_login(irc);
[0298d11]180}
181
[5ebff60]182static void irc_cmd_nick(irc_t *irc, char **cmd)
[0298d11]183{
[9a9b520]184        irc_user_t *iu;
[5ebff60]185
186        if ((iu = irc_user_by_name(irc, cmd[1])) && iu != irc->user) {
187                irc_send_num(irc, 433, "%s :This nick is already in use", cmd[1]);
188        } else if (!nick_ok(NULL, cmd[1])) {
[0298d11]189                /* [SH] Invalid characters. */
[5ebff60]190                irc_send_num(irc, 432, "%s :This nick contains invalid characters", cmd[1]);
191        } else if (irc->status & USTATUS_LOGGED_IN) {
[3558fea]192                /* WATCH OUT: iu from the first if reused here to check if the
193                   new nickname is the same (other than case, possibly). If it
194                   is, no need to reset identify-status. */
[5ebff60]195                if ((irc->status & USTATUS_IDENTIFIED) && iu != irc->user) {
196                        irc_setpass(irc, NULL);
[ffa1173]197                        irc->status &= ~USTATUS_IDENTIFIED;
[5ebff60]198                        irc_umode_set(irc, "-R", 1);
199                        irc_rootmsg(irc, "Changing nicks resets your identify status. "
200                                    "Re-identify or register a new account if you want "
201                                    "your configuration to be saved. See \x02help "
202                                    "nick_changes\x02.");
[ffa1173]203                }
[5ebff60]204
205                if (strcmp(cmd[1], irc->user->nick) != 0) {
206                        irc_user_set_nick(irc->user, cmd[1]);
207                }
208        } else {
209                g_free(irc->user->nick);
210                irc->user->nick = g_strdup(cmd[1]);
211
212                irc_check_login(irc);
[0298d11]213        }
214}
215
[5ebff60]216static void irc_cmd_quit(irc_t *irc, char **cmd)
[0298d11]217{
[5ebff60]218        if (cmd[1] && *cmd[1]) {
219                irc_abort(irc, 0, "Quit: %s", cmd[1]);
220        } else {
221                irc_abort(irc, 0, "Leaving...");
222        }
[0298d11]223}
224
[5ebff60]225static void irc_cmd_ping(irc_t *irc, char **cmd)
[0298d11]226{
[5ebff60]227        irc_write(irc, ":%s PONG %s :%s", irc->root->host,
228                  irc->root->host, cmd[1] ? cmd[1] : irc->root->host);
[0298d11]229}
230
[5ebff60]231static void irc_cmd_pong(irc_t *irc, char **cmd)
[3923003]232{
233        /* We could check the value we get back from the user, but in
234           fact we don't care, we're just happy s/he's still alive. */
235        irc->last_pong = gettime();
236        irc->pinging = 0;
237}
238
[5ebff60]239static void irc_cmd_join(irc_t *irc, char **cmd)
[b9e020a]240{
[58646d9]241        char *comma, *s = cmd[1];
[5ebff60]242
243        while (s) {
[58646d9]244                irc_channel_t *ic;
[5ebff60]245
246                if ((comma = strchr(s, ','))) {
[58646d9]247                        *comma = '\0';
[5ebff60]248                }
249
250                if ((ic = irc_channel_by_name(irc, s)) == NULL &&
251                    (ic = irc_channel_new(irc, s))) {
252                        if (strcmp(set_getstr(&ic->set, "type"), "control") != 0) {
[324c378]253                                /* Autoconfiguration is for control channels only ATM. */
[5ebff60]254                        } else if (bee_group_by_name(ic->irc->b, ic->name + 1, FALSE)) {
255                                set_setstr(&ic->set, "group", ic->name + 1);
256                                set_setstr(&ic->set, "fill_by", "group");
257                        } else if (set_setstr(&ic->set, "protocol", ic->name + 1)) {
258                                set_setstr(&ic->set, "fill_by", "protocol");
259                        } else if (set_setstr(&ic->set, "account", ic->name + 1)) {
260                                set_setstr(&ic->set, "fill_by", "account");
261                        } else {
[13fa2db]262                                /* The set commands above will run this already,
263                                   but if we didn't hit any, we have to fill the
264                                   channel with the default population. */
[5ebff60]265                                bee_irc_channel_update(ic->irc, ic, NULL);
[324c378]266                        }
[5ebff60]267                } else if (ic == NULL) {
268                        irc_send_num(irc, 479, "%s :Invalid channel name", s);
[58646d9]269                        goto next;
270                }
[5ebff60]271
272                if (ic->flags & IRC_CHANNEL_JOINED) {
[58646d9]273                        /* Dude, you're already there...
274                           RFC doesn't have any reply for that though? */
275                        goto next;
[5ebff60]276                }
277
278                if (ic->f->join && !ic->f->join(ic)) {
[58646d9]279                        /* The story is: FALSE either means the handler
280                           showed an error message, or is doing some work
281                           before the join should be confirmed. (In the
282                           latter case, the caller should take care of that
283                           confirmation.) TRUE means all's good, let the
284                           user join the channel right away. */
285                        goto next;
[5ebff60]286                }
287
288                irc_channel_add_user(ic, irc->user);
289
[58646d9]290next:
[5ebff60]291                if (comma) {
[58646d9]292                        s = comma + 1;
293                        *comma = ',';
[5ebff60]294                } else {
[58646d9]295                        break;
[5ebff60]296                }
[57119e8]297        }
[b9e020a]298}
299
[5ebff60]300static void irc_cmd_names(irc_t *irc, char **cmd)
[b9e020a]301{
302        irc_channel_t *ic;
[5ebff60]303
304        if (cmd[1] && (ic = irc_channel_by_name(irc, cmd[1]))) {
305                irc_send_names(ic);
306        }
[b9e020a]307        /* With no args, we should show /names of all chans. Make the code
308           below work well if necessary.
309        else
310        {
[5ebff60]311                GSList *l;
312
313                for( l = irc->channels; l; l = l->next )
314                        irc_send_names( l->data );
[b9e020a]315        }
316        */
317}
318
[5ebff60]319static void irc_cmd_part(irc_t *irc, char **cmd)
[b9e020a]320{
321        irc_channel_t *ic;
[5ebff60]322
323        if ((ic = irc_channel_by_name(irc, cmd[1])) == NULL) {
324                irc_send_num(irc, 403, "%s :No such channel", cmd[1]);
325        } else if (irc_channel_del_user(ic, irc->user, IRC_CDU_PART, cmd[2])) {
326                if (ic->f->part) {
327                        ic->f->part(ic, NULL);
328                }
329        } else {
330                irc_send_num(irc, 442, "%s :You're not on that channel", cmd[1]);
[b9e020a]331        }
332}
333
[5ebff60]334static void irc_cmd_whois(irc_t *irc, char **cmd)
[b95932e]335{
336        char *nick = cmd[1];
[5ebff60]337        irc_user_t *iu = irc_user_by_name(irc, nick);
338
339        if (iu) {
340                irc_send_whois(iu);
341        } else {
342                irc_send_num(irc, 401, "%s :Nick does not exist", nick);
343        }
[b95932e]344}
345
[5ebff60]346static void irc_cmd_whowas(irc_t *irc, char **cmd)
[b95932e]347{
348        /* For some reason irssi tries a whowas when whois fails. We can
349           ignore this, but then the user never gets a "user not found"
350           message from irssi which is a bit annoying. So just respond
351           with not-found and irssi users will get better error messages */
[5ebff60]352
353        irc_send_num(irc, 406, "%s :Nick does not exist", cmd[1]);
354        irc_send_num(irc, 369, "%s :End of WHOWAS", cmd[1]);
[b95932e]355}
356
[5ebff60]357static void irc_cmd_motd(irc_t *irc, char **cmd)
[9b69eb7]358{
[5ebff60]359        irc_send_motd(irc);
[9b69eb7]360}
361
[5ebff60]362static void irc_cmd_mode(irc_t *irc, char **cmd)
[0298d11]363{
[5ebff60]364        if (irc_channel_name_ok(cmd[1])) {
[b919363]365                irc_channel_t *ic;
[5ebff60]366
367                if ((ic = irc_channel_by_name(irc, cmd[1])) == NULL) {
368                        irc_send_num(irc, 403, "%s :No such channel", cmd[1]);
369                } else if (cmd[2]) {
370                        if (*cmd[2] == '+' || *cmd[2] == '-') {
371                                irc_send_num(irc, 477, "%s :Can't change channel modes", cmd[1]);
372                        } else if (*cmd[2] == 'b') {
373                                irc_send_num(irc, 368, "%s :No bans possible", cmd[1]);
374                        }
375                } else {
376                        irc_send_num(irc, 324, "%s +%s", cmd[1], ic->mode);
[0298d11]377                }
[5ebff60]378        } else {
379                if (nick_cmp(NULL, cmd[1], irc->user->nick) == 0) {
380                        if (cmd[2]) {
381                                irc_umode_set(irc, cmd[2], 0);
382                        } else {
383                                irc_send_num(irc, 221, "+%s", irc->umode);
384                        }
385                } else {
386                        irc_send_num(irc, 502, ":Don't touch their modes");
[0298d11]387                }
388        }
389}
390
[5ebff60]391static void irc_cmd_who(irc_t *irc, char **cmd)
[2f53ada]392{
393        char *channel = cmd[1];
394        irc_channel_t *ic;
[a72af0d]395        irc_user_t *iu;
[5ebff60]396
397        if (!channel || *channel == '0' || *channel == '*' || !*channel) {
398                irc_send_who(irc, irc->users, "**");
399        } else if ((ic = irc_channel_by_name(irc, channel))) {
400                irc_send_who(irc, ic->users, channel);
401        } else if ((iu = irc_user_by_name(irc, channel))) {
[a72af0d]402                /* Tiny hack! */
[5ebff60]403                GSList *l = g_slist_append(NULL, iu);
404                irc_send_who(irc, l, channel);
405                g_slist_free(l);
406        } else {
407                irc_send_num(irc, 403, "%s :No such channel", channel);
[a72af0d]408        }
[2f53ada]409}
410
[5ebff60]411static void irc_cmd_privmsg(irc_t *irc, char **cmd)
[b919363]412{
[280c56a]413        irc_channel_t *ic;
414        irc_user_t *iu;
[5ebff60]415
416        if (!cmd[2]) {
417                irc_send_num(irc, 412, ":No text to send");
[24b8bbb]418                return;
[280c56a]419        }
[5ebff60]420
[24b8bbb]421        /* Don't treat CTCP actions as real CTCPs, just convert them right now. */
[5ebff60]422        if (g_strncasecmp(cmd[2], "\001ACTION", 7) == 0) {
[24b8bbb]423                cmd[2] += 4;
[5ebff60]424                memcpy(cmd[2], "/me", 3);
425                if (cmd[2][strlen(cmd[2]) - 1] == '\001') {
426                        cmd[2][strlen(cmd[2]) - 1] = '\0';
427                }
[24b8bbb]428        }
[5ebff60]429
430        if (irc_channel_name_ok(cmd[1]) &&
431            (ic = irc_channel_by_name(irc, cmd[1]))) {
432                if (cmd[2][0] == '\001') {
[a7dbf45]433                        /* CTCPs to channels? Nah. Maybe later. */
[5ebff60]434                } else if (ic->f->privmsg) {
435                        ic->f->privmsg(ic, cmd[2]);
[a7dbf45]436                }
[5ebff60]437        } else if ((iu = irc_user_by_name(irc, cmd[1]))) {
438                if (cmd[2][0] == '\001') {
[24b8bbb]439                        char **ctcp;
[5ebff60]440
441                        if (iu->f->ctcp == NULL) {
[24b8bbb]442                                return;
[5ebff60]443                        }
444                        if (cmd[2][strlen(cmd[2]) - 1] == '\001') {
445                                cmd[2][strlen(cmd[2]) - 1] = '\0';
446                        }
447
448                        ctcp = split_command_parts(cmd[2] + 1, 0);
449                        iu->f->ctcp(iu, ctcp);
450                } else if (iu->f->privmsg) {
[92c8d41]451                        iu->last_channel = NULL;
[5ebff60]452                        iu->f->privmsg(iu, cmd[2]);
[bce78c8]453                }
[5ebff60]454        } else {
455                irc_send_num(irc, 401, "%s :No such nick/channel", cmd[1]);
[b919363]456        }
[280c56a]457}
458
[5ebff60]459static void irc_cmd_notice(irc_t *irc, char **cmd)
[d7f8500]460{
[bbc69f7]461        irc_user_t *iu;
[5ebff60]462
463        if (!cmd[2]) {
464                irc_send_num(irc, 412, ":No text to send");
[d7f8500]465                return;
466        }
[5ebff60]467
[d7f8500]468        /* At least for now just echo. IIRC some IRC clients use self-notices
469           for lag checks, so try to support that. */
[5ebff60]470        if (nick_cmp(NULL, cmd[1], irc->user->nick) == 0) {
471                irc_send_msg(irc->user, "NOTICE", irc->user->nick, cmd[2], NULL);
472        } else if ((iu = irc_user_by_name(irc, cmd[1]))) {
473                iu->f->privmsg(iu, cmd[2]);
474        }
[d7f8500]475}
476
[5ebff60]477static void irc_cmd_nickserv(irc_t *irc, char **cmd)
[d860a8d]478{
479        /* [SH] This aliases the NickServ command to PRIVMSG root */
480        /* [TV] This aliases the NS command to PRIVMSG root as well */
[5ebff60]481        root_command(irc, cmd + 1);
[d860a8d]482}
483
[5ebff60]484static void irc_cmd_oper_hack(irc_t *irc, char **cmd);
[280c56a]485
[5ebff60]486static void irc_cmd_oper(irc_t *irc, char **cmd)
[280c56a]487{
[060d066]488        /* Very non-standard evil but useful/secure hack, see below. */
[5ebff60]489        if (irc->status & OPER_HACK_ANY) {
490                return irc_cmd_oper_hack(irc, cmd);
[280c56a]491        }
[5ebff60]492
493        if (global.conf->oper_pass &&
494            (strncmp(global.conf->oper_pass, "md5:", 4) == 0 ?
495             md5_verify_password(cmd[2], global.conf->oper_pass + 4) == 0 :
496             strcmp(cmd[2], global.conf->oper_pass) == 0)) {
497                irc_umode_set(irc, "+o", 1);
498                irc_send_num(irc, 381, ":Password accepted");
499        } else {
500                irc_send_num(irc, 491, ":Incorrect password");
[280c56a]501        }
502}
503
[5ebff60]504static void irc_cmd_oper_hack(irc_t *irc, char **cmd)
[060d066]505{
[5ebff60]506        char *password = g_strjoinv(" ", cmd + 2);
507
[060d066]508        /* /OPER can now also be used to enter IM/identify passwords without
509           echoing. It's a hack but the extra password security is worth it. */
[5ebff60]510        if (irc->status & OPER_HACK_ACCOUNT_PASSWORD) {
[060d066]511                account_t *a;
[5ebff60]512
513                for (a = irc->b->accounts; a; a = a->next) {
514                        if (strcmp(a->pass, PASSWORD_PENDING) == 0) {
515                                set_setstr(&a->set, "password", password);
516                                irc_rootmsg(irc, "Password added to IM account "
517                                            "%s", a->tag);
[060d066]518                                /* The IRC client may expect this. 491 suggests the OPER
519                                   password was wrong, so the client won't expect a +o.
520                                   It may however repeat the password prompt. We'll see. */
[5ebff60]521                                irc_send_num(irc, 491, ":Password added to IM account "
522                                             "%s", a->tag);
[060d066]523                        }
[5ebff60]524                }
525        } else if (irc->status & OPER_HACK_IDENTIFY) {
[fda194f]526                char *send_cmd[] = { "identify", password, NULL, NULL };
527                irc->status &= ~OPER_HACK_IDENTIFY;
[5ebff60]528                if (irc->status & OPER_HACK_IDENTIFY_NOLOAD) {
[fda194f]529                        send_cmd[1] = "-noload";
530                        send_cmd[2] = password;
[5ebff60]531                } else if (irc->status & OPER_HACK_IDENTIFY_FORCE) {
[fda194f]532                        send_cmd[1] = "-force";
533                        send_cmd[2] = password;
534                }
[5ebff60]535                irc_send_num(irc, 491, ":Trying to identify");
536                root_command(irc, send_cmd);
537        } else if (irc->status & OPER_HACK_REGISTER) {
[060d066]538                char *send_cmd[] = { "register", password, NULL };
[5ebff60]539                irc_send_num(irc, 491, ":Trying to identify");
540                root_command(irc, send_cmd);
[060d066]541        }
[5ebff60]542
[060d066]543        irc->status &= ~OPER_HACK_ANY;
[5ebff60]544        g_free(password);
[060d066]545}
546
[5ebff60]547static void irc_cmd_invite(irc_t *irc, char **cmd)
[280c56a]548{
[66b9e36a]549        irc_channel_t *ic;
550        irc_user_t *iu;
[5ebff60]551
552        if ((iu = irc_user_by_name(irc, cmd[1])) == NULL) {
553                irc_send_num(irc, 401, "%s :No such nick", cmd[1]);
[66b9e36a]554                return;
[5ebff60]555        } else if ((ic = irc_channel_by_name(irc, cmd[2])) == NULL) {
556                irc_send_num(irc, 403, "%s :No such channel", cmd[2]);
[66b9e36a]557                return;
558        }
[5ebff60]559
560        if (!ic->f->invite) {
561                irc_send_num(irc, 482, "%s :Can't invite people here", cmd[2]);
562        } else if (ic->f->invite(ic, iu)) {
563                irc_send_num(irc, 341, "%s %s", iu->nick, ic->name);
564        }
[0298d11]565}
566
[5ebff60]567static void irc_cmd_kick(irc_t *irc, char **cmd)
[7821ee8]568{
569        irc_channel_t *ic;
570        irc_user_t *iu;
[5ebff60]571
572        if ((iu = irc_user_by_name(irc, cmd[2])) == NULL) {
573                irc_send_num(irc, 401, "%s :No such nick", cmd[2]);
[7821ee8]574                return;
[5ebff60]575        } else if ((ic = irc_channel_by_name(irc, cmd[1])) == NULL) {
576                irc_send_num(irc, 403, "%s :No such channel", cmd[1]);
[7821ee8]577                return;
[5ebff60]578        } else if (!ic->f->kick) {
579                irc_send_num(irc, 482, "%s :Can't kick people here", cmd[1]);
[7821ee8]580                return;
581        }
[5ebff60]582
583        ic->f->kick(ic, iu, cmd[3] ? cmd[3] : NULL);
[7821ee8]584}
585
[5ebff60]586static void irc_cmd_userhost(irc_t *irc, char **cmd)
[0298d11]587{
588        int i;
[5ebff60]589
[0298d11]590        /* [TV] Usable USERHOST-implementation according to
[5ebff60]591                RFC1459. Without this, mIRC shows an error
592                while connecting, and the used way of rejecting
593                breaks standards.
[0298d11]594        */
[5ebff60]595
596        for (i = 1; cmd[i]; i++) {
597                irc_user_t *iu = irc_user_by_name(irc, cmd[i]);
598
599                if (iu) {
600                        irc_send_num(irc, 302, ":%s=%c%s@%s", iu->nick,
601                                     irc_user_get_away(iu) ? '-' : '+',
602                                     iu->user, iu->host);
603                }
[003a12b]604        }
[0298d11]605}
606
[5ebff60]607static void irc_cmd_ison(irc_t *irc, char **cmd)
[0298d11]608{
[b4e4b95]609        char buff[IRC_MAX_LINE];
[0298d11]610        int lenleft, i;
[5ebff60]611
[0298d11]612        buff[0] = '\0';
[5ebff60]613
[0298d11]614        /* [SH] Leave room for : and \0 */
615        lenleft = IRC_MAX_LINE - 2;
[5ebff60]616
617        for (i = 1; cmd[i]; i++) {
[42616d1]618                char *this, *next;
[5ebff60]619
[42616d1]620                this = cmd[i];
[5ebff60]621                while (*this) {
[003a12b]622                        irc_user_t *iu;
[5ebff60]623
624                        if ((next = strchr(this, ' '))) {
[42616d1]625                                *next = 0;
[5ebff60]626                        }
627
628                        if ((iu = irc_user_by_name(irc, this)) &&
629                            iu->bu && iu->bu->flags & BEE_USER_ONLINE) {
630                                lenleft -= strlen(iu->nick) + 1;
631
632                                if (lenleft < 0) {
[42616d1]633                                        break;
[5ebff60]634                                }
635
636                                strcat(buff, iu->nick);
637                                strcat(buff, " ");
[0298d11]638                        }
[5ebff60]639
640                        if (next) {
[42616d1]641                                *next = ' ';
642                                this = next + 1;
[5ebff60]643                        } else {
[42616d1]644                                break;
[5ebff60]645                        }
[0298d11]646                }
[5ebff60]647
[42616d1]648                /* *sigh* */
[5ebff60]649                if (lenleft < 0) {
[42616d1]650                        break;
[5ebff60]651                }
652        }
653
654        if (strlen(buff) > 0) {
655                buff[strlen(buff) - 1] = '\0';
[0298d11]656        }
[5ebff60]657
658        irc_send_num(irc, 303, ":%s", buff);
[0298d11]659}
660
[5ebff60]661static void irc_cmd_watch(irc_t *irc, char **cmd)
[0298d11]662{
663        int i;
[5ebff60]664
[0298d11]665        /* Obviously we could also mark a user structure as being
666           watched, but what if the WATCH command is sent right
667           after connecting? The user won't exist yet then... */
[5ebff60]668        for (i = 1; cmd[i]; i++) {
[0298d11]669                char *nick;
[003a12b]670                irc_user_t *iu;
[5ebff60]671
672                if (!cmd[i][0] || !cmd[i][1]) {
[0298d11]673                        break;
674                }
[5ebff60]675
676                nick = g_strdup(cmd[i] + 1);
677                nick_lc(irc, nick);
678
679                iu = irc_user_by_name(irc, nick);
680
681                if (cmd[i][0] == '+') {
682                        if (!g_hash_table_lookup(irc->watches, nick)) {
683                                g_hash_table_insert(irc->watches, nick, nick);
684                        }
685
686                        if (iu && iu->bu && iu->bu->flags & BEE_USER_ONLINE) {
687                                irc_send_num(irc, 604, "%s %s %s %d :%s", iu->nick, iu->user,
688                                             iu->host, (int) time(NULL), "is online");
689                        } else {
690                                irc_send_num(irc, 605, "%s %s %s %d :%s", nick, "*", "*",
691                                             (int) time(NULL), "is offline");
692                        }
693                } else if (cmd[i][0] == '-') {
[0298d11]694                        gpointer okey, ovalue;
[5ebff60]695
696                        if (g_hash_table_lookup_extended(irc->watches, nick, &okey, &ovalue)) {
697                                g_hash_table_remove(irc->watches, okey);
698                                g_free(okey);
699
700                                irc_send_num(irc, 602, "%s %s %s %d :%s", nick, "*", "*", 0, "Stopped watching");
[0298d11]701                        }
702                }
703        }
704}
705
[5ebff60]706static void irc_cmd_topic(irc_t *irc, char **cmd)
[0298d11]707{
[5ebff60]708        irc_channel_t *ic = irc_channel_by_name(irc, cmd[1]);
[4469e7e]709        const char *new = cmd[2];
[5ebff60]710
711        if (ic == NULL) {
712                irc_send_num(irc, 403, "%s :No such channel", cmd[1]);
713        } else if (new) {
714                if (ic->f->topic == NULL) {
715                        irc_send_num(irc, 482, "%s :Can't change this channel's topic", ic->name);
716                } else if (ic->f->topic(ic, new)) {
717                        irc_send_topic(ic, TRUE);
718                }
719        } else {
720                irc_send_topic(ic, FALSE);
[50e1776]721        }
[0298d11]722}
723
[5ebff60]724static void irc_cmd_away(irc_t *irc, char **cmd)
[0298d11]725{
[5ebff60]726        if (cmd[1] && *cmd[1]) {
727                char away[strlen(cmd[1]) + 1];
[0298d11]728                int i, j;
[5ebff60]729
[0298d11]730                /* Copy away string, but skip control chars. Mainly because
731                   Jabber really doesn't like them. */
[5ebff60]732                for (i = j = 0; cmd[1][i]; i++) {
733                        if ((unsigned char) (away[j] = cmd[1][i]) >= ' ') {
734                                j++;
735                        }
736                }
[81186cab]737                away[j] = '\0';
[5ebff60]738
739                irc_send_num(irc, 306, ":You're now away: %s", away);
740                set_setstr(&irc->b->set, "away", away);
741        } else {
742                irc_send_num(irc, 305, ":Welcome back");
743                set_setstr(&irc->b->set, "away", NULL);
[0298d11]744        }
745}
746
[5ebff60]747static void irc_cmd_list(irc_t *irc, char **cmd)
[23d6165]748{
749        GSList *l;
[5ebff60]750
751        for (l = irc->channels; l; l = l->next) {
[23d6165]752                irc_channel_t *ic = l->data;
[5ebff60]753
754                irc_send_num(irc, 322, "%s %d :%s",
755                             ic->name, g_slist_length(ic->users), ic->topic ? : "");
[23d6165]756        }
[5ebff60]757        irc_send_num(irc, 323, ":%s", "End of /LIST");
[23d6165]758}
759
[5ebff60]760static void irc_cmd_version(irc_t *irc, char **cmd)
[82898af]761{
[5ebff60]762        irc_send_num(irc, 351, "%s-%s. %s :%s/%s ",
763                     PACKAGE, BITLBEE_VERSION, irc->root->host, ARCH, CPU);
[82898af]764}
765
[5ebff60]766static void irc_cmd_completions(irc_t *irc, char **cmd)
[0298d11]767{
768        help_t *h;
769        set_t *s;
770        int i;
[5ebff60]771
772        irc_send_msg_raw(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS OK");
773
774        for (i = 0; root_commands[i].command; i++) {
775                irc_send_msg_f(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS %s", root_commands[i].command);
776        }
777
778        for (h = global.help; h; h = h->next) {
779                irc_send_msg_f(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS help %s", h->title);
780        }
781
782        for (s = irc->b->set; s; s = s->next) {
783                irc_send_msg_f(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS set %s", s->key);
784        }
785
786        irc_send_msg_raw(irc->root, "NOTICE", irc->user->nick, "COMPLETIONS END");
[0298d11]787}
788
[5ebff60]789static void irc_cmd_rehash(irc_t *irc, char **cmd)
[f4a5940]790{
[5ebff60]791        if (global.conf->runmode == RUNMODE_INETD) {
792                ipc_master_cmd_rehash(NULL, NULL);
793        } else {
794                ipc_to_master(cmd);
795        }
796
797        irc_send_num(irc, 382, "%s :Rehashing", global.conf_file);
[f4a5940]798}
799
[0298d11]800static const command_t irc_commands[] = {
[0ef1c92]801        { "cap",         1, irc_cmd_cap,         0 },
[a199d33]802        { "pass",        1, irc_cmd_pass,        0 },
[0298d11]803        { "user",        4, irc_cmd_user,        IRC_CMD_PRE_LOGIN },
804        { "nick",        1, irc_cmd_nick,        0 },
805        { "quit",        0, irc_cmd_quit,        0 },
806        { "ping",        0, irc_cmd_ping,        0 },
[3923003]807        { "pong",        0, irc_cmd_pong,        IRC_CMD_LOGGED_IN },
[b9e020a]808        { "join",        1, irc_cmd_join,        IRC_CMD_LOGGED_IN },
809        { "names",       1, irc_cmd_names,       IRC_CMD_LOGGED_IN },
810        { "part",        1, irc_cmd_part,        IRC_CMD_LOGGED_IN },
[b95932e]811        { "whois",       1, irc_cmd_whois,       IRC_CMD_LOGGED_IN },
812        { "whowas",      1, irc_cmd_whowas,      IRC_CMD_LOGGED_IN },
[9b69eb7]813        { "motd",        0, irc_cmd_motd,        IRC_CMD_LOGGED_IN },
[b919363]814        { "mode",        1, irc_cmd_mode,        IRC_CMD_LOGGED_IN },
[2f53ada]815        { "who",         0, irc_cmd_who,         IRC_CMD_LOGGED_IN },
[280c56a]816        { "privmsg",     1, irc_cmd_privmsg,     IRC_CMD_LOGGED_IN },
[d7f8500]817        { "notice",      1, irc_cmd_notice,      IRC_CMD_LOGGED_IN },
[d860a8d]818        { "nickserv",    1, irc_cmd_nickserv,    IRC_CMD_LOGGED_IN },
819        { "ns",          1, irc_cmd_nickserv,    IRC_CMD_LOGGED_IN },
[81186cab]820        { "away",        0, irc_cmd_away,        IRC_CMD_LOGGED_IN },
[d7d677d]821        { "version",     0, irc_cmd_version,     IRC_CMD_LOGGED_IN },
822        { "completions", 0, irc_cmd_completions, IRC_CMD_LOGGED_IN },
[0298d11]823        { "userhost",    1, irc_cmd_userhost,    IRC_CMD_LOGGED_IN },
824        { "ison",        1, irc_cmd_ison,        IRC_CMD_LOGGED_IN },
825        { "watch",       1, irc_cmd_watch,       IRC_CMD_LOGGED_IN },
[003a12b]826        { "invite",      2, irc_cmd_invite,      IRC_CMD_LOGGED_IN },
[7821ee8]827        { "kick",        2, irc_cmd_kick,        IRC_CMD_LOGGED_IN },
[4469e7e]828        { "topic",       1, irc_cmd_topic,       IRC_CMD_LOGGED_IN },
[003a12b]829        { "oper",        2, irc_cmd_oper,        IRC_CMD_LOGGED_IN },
[23d6165]830        { "list",        0, irc_cmd_list,        IRC_CMD_LOGGED_IN },
[0431ea1]831        { "die",         0, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
[565a1ea]832        { "deaf",        0, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
[48721c3]833        { "wallops",     1, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
[dfc8a46]834        { "wall",        1, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
[f4a5940]835        { "rehash",      0, irc_cmd_rehash,      IRC_CMD_OPER_ONLY },
[54879ab]836        { "restart",     0, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
[48721c3]837        { "kill",        2, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
[0298d11]838        { NULL }
839};
840
[5ebff60]841void irc_exec(irc_t *irc, char *cmd[])
842{
[f1d38f2]843        int i, n_arg;
[5ebff60]844
845        if (!cmd[0]) {
[f73b969]846                return;
[5ebff60]847        }
848
849        for (i = 0; irc_commands[i].command; i++) {
850                if (g_strcasecmp(irc_commands[i].command, cmd[0]) == 0) {
[f1d38f2]851                        /* There should be no typo in the next line: */
[5ebff60]852                        for (n_arg = 0; cmd[n_arg]; n_arg++) {
[25c4c78]853                                ;
[edf9657]854                        }
[5ebff60]855                        n_arg--;
856
857                        if (irc_commands[i].flags & IRC_CMD_PRE_LOGIN && irc->status & USTATUS_LOGGED_IN) {
858                                irc_send_num(irc, 462, ":Only allowed before logging in");
859                        } else if (irc_commands[i].flags & IRC_CMD_LOGGED_IN && !(irc->status & USTATUS_LOGGED_IN)) {
860                                irc_send_num(irc, 451, ":Register first");
861                        } else if (irc_commands[i].flags & IRC_CMD_OPER_ONLY && !strchr(irc->umode, 'o')) {
862                                irc_send_num(irc, 481, ":Permission denied - You're not an IRC operator");
863                        } else if (n_arg < irc_commands[i].required_parameters) {
864                                irc_send_num(irc, 461, "%s :Need more parameters", cmd[0]);
865                        } else if (irc_commands[i].flags & IRC_CMD_TO_MASTER) {
[5424c76]866                                /* IPC doesn't make sense in inetd mode,
867                                    but the function will catch that. */
[5ebff60]868                                ipc_to_master(cmd);
869                        } else {
870                                irc_commands[i].execute(irc, cmd);
[f73b969]871                        }
[5ebff60]872
[2f13222]873                        return;
[0298d11]874                }
[5ebff60]875        }
876
877        if (irc->status & USTATUS_LOGGED_IN) {
878                irc_send_num(irc, 421, "%s :Unknown command", cmd[0]);
879        }
[0298d11]880}
Note: See TracBrowser for help on using the repository browser.