source: irc_commands.c @ 68c7c14

Last change on this file since 68c7c14 was a91ecee, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-01-23T14:20:18Z

/AWAY now only sets the IM-protocols away that belong to the current IRC
/connection (was an issue in single-process daemon mode).

  • Property mode set to 100644
File size: 16.9 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2006 Wilmer van der Gaast and others                *
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;
22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
28#include "ipc.h"
29
30static void irc_cmd_pass( irc_t *irc, char **cmd )
31{
32        if( global.conf->auth_pass && strcmp( cmd[1], global.conf->auth_pass ) == 0 )
33        {
34                irc->status = USTATUS_AUTHORIZED;
35                irc_check_login( irc );
36        }
37        else
38        {
39                irc_reply( irc, 464, ":Incorrect password" );
40        }
41}
42
43static void irc_cmd_user( irc_t *irc, char **cmd )
44{
45        irc->user = g_strdup( cmd[1] );
46        irc->realname = g_strdup( cmd[4] );
47       
48        irc_check_login( irc );
49}
50
51static void irc_cmd_nick( irc_t *irc, char **cmd )
52{
53        if( irc->nick )
54        {
55                irc_reply( irc, 438, ":The hand of the deity is upon thee, thy nick may not change" );
56        }
57        /* This is not clean, but for now it'll have to be like this... */
58        else if( ( nick_cmp( cmd[1], irc->mynick ) == 0 ) || ( nick_cmp( cmd[1], NS_NICK ) == 0 ) )
59        {
60                irc_reply( irc, 433, ":This nick is already in use" );
61        }
62        else if( !nick_ok( cmd[1] ) )
63        {
64                /* [SH] Invalid characters. */
65                irc_reply( irc, 432, ":This nick contains invalid characters" );
66        }
67        else
68        {
69                irc->nick = g_strdup( cmd[1] );
70               
71                irc_check_login( irc );
72        }
73}
74
75static void irc_cmd_quit( irc_t *irc, char **cmd )
76{
77        if( cmd[1] && *cmd[1] )
78                irc_abort( irc, 0, "Quit: %s", cmd[1] );
79        else
80                irc_abort( irc, 0, "Leaving..." );
81}
82
83static void irc_cmd_ping( irc_t *irc, char **cmd )
84{
85        irc_write( irc, ":%s PONG %s :%s", irc->myhost, irc->myhost, cmd[1]?cmd[1]:irc->myhost );
86}
87
88static void irc_cmd_oper( irc_t *irc, char **cmd )
89{
90        if( global.conf->oper_pass && strcmp( cmd[2], global.conf->oper_pass ) == 0 )
91        {
92                irc_umode_set( irc, "+o", 1 );
93                irc_reply( irc, 381, ":Password accepted" );
94        }
95        else
96        {
97                irc_reply( irc, 432, ":Incorrect password" );
98        }
99}
100
101static void irc_cmd_mode( irc_t *irc, char **cmd )
102{
103        if( *cmd[1] == '#' || *cmd[1] == '&' )
104        {
105                if( cmd[2] )
106                {
107                        if( *cmd[2] == '+' || *cmd[2] == '-' )
108                                irc_reply( irc, 477, "%s :Can't change channel modes", cmd[1] );
109                        else if( *cmd[2] == 'b' )
110                                irc_reply( irc, 368, "%s :No bans possible", cmd[1] );
111                }
112                else
113                        irc_reply( irc, 324, "%s +%s", cmd[1], CMODE );
114        }
115        else
116        {
117                if( nick_cmp( cmd[1], irc->nick ) == 0 )
118                {
119                        if( cmd[2] )
120                                irc_umode_set( irc, cmd[2], 0 );
121                }
122                else
123                        irc_reply( irc, 502, ":Don't touch their modes" );
124        }
125}
126
127static void irc_cmd_names( irc_t *irc, char **cmd )
128{
129        irc_names( irc, cmd[1]?cmd[1]:irc->channel );
130}
131
132static void irc_cmd_part( irc_t *irc, char **cmd )
133{
134        struct conversation *c;
135       
136        if( g_strcasecmp( cmd[1], irc->channel ) == 0 )
137        {
138                user_t *u = user_find( irc, irc->nick );
139               
140                /* Not allowed to leave control channel */
141                irc_part( irc, u, irc->channel );
142                irc_join( irc, u, irc->channel );
143        }
144        else if( ( c = conv_findchannel( cmd[1] ) ) )
145        {
146                user_t *u = user_find( irc, irc->nick );
147               
148                irc_part( irc, u, c->channel );
149               
150                if( c->gc && c->gc->prpl )
151                {
152                        c->joined = 0;
153                        c->gc->prpl->chat_leave( c->gc, c->id );
154                }
155        }
156        else
157        {
158                irc_reply( irc, 403, "%s :No such channel", cmd[1] );
159        }
160}
161
162static void irc_cmd_join( irc_t *irc, char **cmd )
163{
164        if( g_strcasecmp( cmd[1], irc->channel ) == 0 )
165                ; /* Dude, you're already there...
166                     RFC doesn't have any reply for that though? */
167        else if( cmd[1] )
168        {
169                if( ( cmd[1][0] == '#' || cmd[1][0] == '&' ) && cmd[1][1] )
170                {
171                        user_t *u = user_find( irc, cmd[1] + 1 );
172                       
173                        if( u && u->gc && u->gc->prpl && u->gc->prpl->chat_open )
174                        {
175                                irc_reply( irc, 403, "%s :Initializing groupchat in a different channel", cmd[1] );
176                               
177                                if( !u->gc->prpl->chat_open( u->gc, u->handle ) )
178                                {
179                                        irc_usermsg( irc, "Could not open a groupchat with %s, maybe you don't have a connection to him/her yet?", u->nick );
180                                }
181                        }
182                        else if( u )
183                        {
184                                irc_reply( irc, 403, "%s :Groupchats are not possible with %s", cmd[1], cmd[1]+1 );
185                        }
186                        else
187                        {
188                                irc_reply( irc, 403, "%s :No such nick", cmd[1] );
189                        }
190                }
191                else
192                {
193                        irc_reply( irc, 403, "%s :No such channel", cmd[1] );
194                }
195        }
196}
197
198static void irc_cmd_invite( irc_t *irc, char **cmd )
199{
200        char *nick = cmd[1], *channel = cmd[2];
201        struct conversation *c = conv_findchannel( channel );
202        user_t *u = user_find( irc, nick );
203       
204        if( u && c && ( u->gc == c->gc ) )
205                if( c->gc && c->gc->prpl && c->gc->prpl->chat_invite )
206                {
207                        c->gc->prpl->chat_invite( c->gc, c->id, "", u->handle );
208                        irc_reply( irc, 341, "%s %s", nick, channel );
209                        return;
210                }
211       
212        irc_reply( irc, 482, "%s :Invite impossible; User/Channel non-existent or incompatible", channel );
213}
214
215static void irc_cmd_privmsg( irc_t *irc, char **cmd )
216{
217        if ( !cmd[2] ) 
218        {
219                irc_reply( irc, 412, ":No text to send" );
220        }
221        else if ( irc->nick && g_strcasecmp( cmd[1], irc->nick ) == 0 ) 
222        {
223                irc_write( irc, ":%s!%s@%s %s %s :%s", irc->nick, irc->user, irc->host, cmd[0], cmd[1], cmd[2] ); 
224        }
225        else 
226        {
227                if( g_strcasecmp( cmd[1], irc->channel ) == 0 )
228                {
229                        unsigned int i;
230                        char *t = set_getstr( irc, "default_target" );
231                       
232                        if( g_strcasecmp( t, "last" ) == 0 && irc->last_target )
233                                cmd[1] = irc->last_target;
234                        else if( g_strcasecmp( t, "root" ) == 0 )
235                                cmd[1] = irc->mynick;
236                       
237                        for( i = 0; i < strlen( cmd[2] ); i ++ )
238                        {
239                                if( cmd[2][i] == ' ' ) break;
240                                if( cmd[2][i] == ':' || cmd[2][i] == ',' )
241                                {
242                                        cmd[1] = cmd[2];
243                                        cmd[2] += i;
244                                        *cmd[2] = 0;
245                                        while( *(++cmd[2]) == ' ' );
246                                        break;
247                                }
248                        }
249                       
250                        irc->is_private = 0;
251                       
252                        if( cmd[1] != irc->last_target )
253                        {
254                                if( irc->last_target )
255                                        g_free( irc->last_target );
256                                irc->last_target = g_strdup( cmd[1] );
257                        }
258                }
259                else
260                {
261                        irc->is_private = 1;
262                }
263                irc_send( irc, cmd[1], cmd[2], ( g_strcasecmp( cmd[0], "NOTICE" ) == 0 ) ? IM_FLAG_AWAY : 0 );
264        }
265}
266
267static void irc_cmd_who( irc_t *irc, char **cmd )
268{
269        char *channel = cmd[1];
270        user_t *u = irc->users;
271        struct conversation *c;
272        GList *l;
273       
274        if( !channel || *channel == '0' || *channel == '*' || !*channel )
275                while( u )
276                {
277                        irc_reply( irc, 352, "%s %s %s %s %s %c :0 %s", u->online ? irc->channel : "*", u->user, u->host, irc->myhost, u->nick, u->online ? ( u->away ? 'G' : 'H' ) : 'G', u->realname );
278                        u = u->next;
279                }
280        else if( g_strcasecmp( channel, irc->channel ) == 0 )
281                while( u )
282                {
283                        if( u->online )
284                                irc_reply( irc, 352, "%s %s %s %s %s %c :0 %s", channel, u->user, u->host, irc->myhost, u->nick, u->away ? 'G' : 'H', u->realname );
285                        u = u->next;
286                }
287        else if( ( c = conv_findchannel( channel ) ) )
288                for( l = c->in_room; l; l = l->next )
289                {
290                        if( ( u = user_findhandle( c->gc, l->data ) ) )
291                                irc_reply( irc, 352, "%s %s %s %s %s %c :0 %s", channel, u->user, u->host, irc->myhost, u->nick, u->away ? 'G' : 'H', u->realname );
292                }
293        else if( ( u = user_find( irc, channel ) ) )
294                irc_reply( irc, 352, "%s %s %s %s %s %c :0 %s", channel, u->user, u->host, irc->myhost, u->nick, u->online ? ( u->away ? 'G' : 'H' ) : 'G', u->realname );
295       
296        irc_reply( irc, 315, "%s :End of /WHO list", channel?channel:"**" );
297}
298
299static void irc_cmd_userhost( irc_t *irc, char **cmd )
300{
301        user_t *u;
302        int i;
303       
304        /* [TV] Usable USERHOST-implementation according to
305                RFC1459. Without this, mIRC shows an error
306                while connecting, and the used way of rejecting
307                breaks standards.
308        */
309       
310        for( i = 1; cmd[i]; i ++ )
311                if( ( u = user_find( irc, cmd[i] ) ) )
312                {
313                        if( u->online && u->away )
314                                irc_reply( irc, 302, ":%s=-%s@%s", u->nick, u->user, u->host );
315                        else
316                                irc_reply( irc, 302, ":%s=+%s@%s", u->nick, u->user, u->host );
317                }
318}
319
320static void irc_cmd_ison( irc_t *irc, char **cmd )
321{
322        user_t *u;
323        char buff[IRC_MAX_LINE];
324        int lenleft, i;
325       
326        buff[0] = '\0';
327       
328        /* [SH] Leave room for : and \0 */
329        lenleft = IRC_MAX_LINE - 2;
330       
331        for( i = 1; cmd[i]; i ++ )
332        {
333                if( ( u = user_find( irc, cmd[i] ) ) && u->online )
334                {
335                        /* [SH] Make sure we don't use too much buffer space. */
336                        lenleft -= strlen( u->nick ) + 1;
337                       
338                        if( lenleft < 0 )
339                        {
340                                break;
341                        }
342                       
343                        /* [SH] Add the nick to the buffer. Note
344                         * that an extra space is always added. Even
345                         * if it's the last nick in the list. Who
346                         * cares?
347                         */
348                       
349                        strcat( buff, u->nick );
350                        strcat( buff, " " );
351                }
352        }
353       
354        /* [WvG] Well, maybe someone cares, so why not remove it? */
355        if( strlen( buff ) > 0 )
356                buff[strlen(buff)-1] = '\0';
357       
358        irc_reply( irc, 303, ":%s", buff );
359}
360
361static void irc_cmd_watch( irc_t *irc, char **cmd )
362{
363        int i;
364       
365        /* Obviously we could also mark a user structure as being
366           watched, but what if the WATCH command is sent right
367           after connecting? The user won't exist yet then... */
368        for( i = 1; cmd[i]; i ++ )
369        {
370                char *nick;
371                user_t *u;
372               
373                if( !cmd[i][0] || !cmd[i][1] )
374                        break;
375               
376                nick = g_strdup( cmd[i] + 1 );
377                nick_lc( nick );
378               
379                u = user_find( irc, nick );
380               
381                if( cmd[i][0] == '+' )
382                {
383                        if( !g_hash_table_lookup( irc->watches, nick ) )
384                                g_hash_table_insert( irc->watches, nick, nick );
385                       
386                        if( u && u->online )
387                                irc_reply( irc, 604, "%s %s %s %d :%s", u->nick, u->user, u->host, time( NULL ), "is online" );
388                        else
389                                irc_reply( irc, 605, "%s %s %s %d :%s", nick, "*", "*", time( NULL ), "is offline" );
390                }
391                else if( cmd[i][0] == '-' )
392                {
393                        gpointer okey, ovalue;
394                       
395                        if( g_hash_table_lookup_extended( irc->watches, nick, &okey, &ovalue ) )
396                        {
397                                g_free( okey );
398                                g_hash_table_remove( irc->watches, okey );
399                               
400                                irc_reply( irc, 602, "%s %s %s %d :%s", nick, "*", "*", 0, "Stopped watching" );
401                        }
402                }
403        }
404}
405
406static void irc_cmd_topic( irc_t *irc, char **cmd )
407{
408        if( cmd[2] )
409                irc_reply( irc, 482, "%s :Cannot change topic", cmd[1] );
410        else
411                irc_topic( irc, cmd[1] );
412}
413
414static void irc_cmd_away( irc_t *irc, char **cmd )
415{
416        user_t *u = user_find( irc, irc->nick );
417        char *away = cmd[1];
418        account_t *a;
419       
420        if( !u ) return;
421       
422        if( away && *away )
423        {
424                int i, j;
425               
426                /* Copy away string, but skip control chars. Mainly because
427                   Jabber really doesn't like them. */
428                u->away = g_malloc( strlen( away ) + 1 );
429                for( i = j = 0; away[i]; i ++ )
430                        if( ( u->away[j] = away[i] ) >= ' ' )
431                                j ++;
432                u->away[j] = 0;
433               
434                irc_reply( irc, 306, ":You're now away: %s", u->away );
435                /* irc_umode_set( irc, irc->myhost, "+a" ); */
436        }
437        else
438        {
439                if( u->away ) g_free( u->away );
440                u->away = NULL;
441                /* irc_umode_set( irc, irc->myhost, "-a" ); */
442                irc_reply( irc, 305, ":Welcome back" );
443        }
444       
445        for( a = irc->accounts; a; a = a->next )
446        {
447                struct gaim_connection *gc = a->gc;
448               
449                if( gc && gc->flags & OPT_LOGGED_IN )
450                        proto_away( gc, u->away );
451        }
452}
453
454static void irc_cmd_whois( irc_t *irc, char **cmd )
455{
456        char *nick = cmd[1];
457        user_t *u = user_find( irc, nick );
458       
459        if( u )
460        {
461                irc_reply( irc, 311, "%s %s %s * :%s", u->nick, u->user, u->host, u->realname );
462               
463                if( u->gc )
464                        irc_reply( irc, 312, "%s %s.%s :%s network", u->nick, u->gc->user->username,
465                                   *u->gc->user->proto_opt[0] ? u->gc->user->proto_opt[0] : "", u->gc->prpl->name );
466                else
467                        irc_reply( irc, 312, "%s %s :%s", u->nick, irc->myhost, IRCD_INFO );
468               
469                if( !u->online )
470                        irc_reply( irc, 301, "%s :%s", u->nick, "User is offline" );
471                else if( u->away )
472                        irc_reply( irc, 301, "%s :%s", u->nick, u->away );
473               
474                irc_reply( irc, 318, "%s :End of /WHOIS list", nick );
475        }
476        else
477        {
478                irc_reply( irc, 401, "%s :Nick does not exist", nick );
479        }
480}
481
482static void irc_cmd_whowas( irc_t *irc, char **cmd )
483{
484        /* For some reason irssi tries a whowas when whois fails. We can
485           ignore this, but then the user never gets a "user not found"
486           message from irssi which is a bit annoying. So just respond
487           with not-found and irssi users will get better error messages */
488       
489        irc_reply( irc, 406, "%s :Nick does not exist", cmd[1] );
490        irc_reply( irc, 369, "%s :End of WHOWAS", cmd[1] );
491}
492
493static void irc_cmd_nickserv( irc_t *irc, char **cmd )
494{
495        /* [SH] This aliases the NickServ command to PRIVMSG root */
496        /* [TV] This aliases the NS command to PRIVMSG root as well */
497        root_command( irc, cmd + 1 );
498}
499
500static void irc_cmd_motd( irc_t *irc, char **cmd )
501{
502        irc_motd( irc );
503}
504
505static void irc_cmd_pong( irc_t *irc, char **cmd )
506{
507        /* We could check the value we get back from the user, but in
508           fact we don't care, we're just happy he's still alive. */
509        irc->last_pong = gettime();
510        irc->pinging = 0;
511}
512
513static void irc_cmd_completions( irc_t *irc, char **cmd )
514{
515        user_t *u = user_find( irc, irc->mynick );
516        help_t *h;
517        set_t *s;
518        int i;
519       
520        irc_privmsg( irc, u, "NOTICE", irc->nick, "COMPLETIONS ", "OK" );
521       
522        for( i = 0; commands[i].command; i ++ )
523                irc_privmsg( irc, u, "NOTICE", irc->nick, "COMPLETIONS ", commands[i].command );
524       
525        for( h = global.help; h; h = h->next )
526                irc_privmsg( irc, u, "NOTICE", irc->nick, "COMPLETIONS help ", h->string );
527       
528        for( s = irc->set; s; s = s->next )
529                irc_privmsg( irc, u, "NOTICE", irc->nick, "COMPLETIONS set ", s->key );
530       
531        irc_privmsg( irc, u, "NOTICE", irc->nick, "COMPLETIONS ", "END" );
532}
533
534static void irc_cmd_rehash( irc_t *irc, char **cmd )
535{
536        if( global.conf->runmode == RUNMODE_INETD )
537                ipc_master_cmd_rehash( NULL, NULL );
538        else
539                ipc_to_master( cmd );
540       
541        irc_reply( irc, 382, "%s :Rehashing", CONF_FILE );
542}
543
544static const command_t irc_commands[] = {
545        { "pass",        1, irc_cmd_pass,        IRC_CMD_PRE_LOGIN },
546        { "user",        4, irc_cmd_user,        IRC_CMD_PRE_LOGIN },
547        { "nick",        1, irc_cmd_nick,        0 },
548        { "quit",        0, irc_cmd_quit,        0 },
549        { "ping",        0, irc_cmd_ping,        0 },
550        { "oper",        2, irc_cmd_oper,        IRC_CMD_LOGGED_IN },
551        { "mode",        1, irc_cmd_mode,        IRC_CMD_LOGGED_IN },
552        { "names",       0, irc_cmd_names,       IRC_CMD_LOGGED_IN },
553        { "part",        1, irc_cmd_part,        IRC_CMD_LOGGED_IN },
554        { "join",        1, irc_cmd_join,        IRC_CMD_LOGGED_IN },
555        { "invite",      2, irc_cmd_invite,      IRC_CMD_LOGGED_IN },
556        { "privmsg",     1, irc_cmd_privmsg,     IRC_CMD_LOGGED_IN },
557        { "notice",      1, irc_cmd_privmsg,     IRC_CMD_LOGGED_IN },
558        { "who",         0, irc_cmd_who,         IRC_CMD_LOGGED_IN },
559        { "userhost",    1, irc_cmd_userhost,    IRC_CMD_LOGGED_IN },
560        { "ison",        1, irc_cmd_ison,        IRC_CMD_LOGGED_IN },
561        { "watch",       1, irc_cmd_watch,       IRC_CMD_LOGGED_IN },
562        { "topic",       1, irc_cmd_topic,       IRC_CMD_LOGGED_IN },
563        { "away",        0, irc_cmd_away,        IRC_CMD_LOGGED_IN },
564        { "whois",       1, irc_cmd_whois,       IRC_CMD_LOGGED_IN },
565        { "whowas",      1, irc_cmd_whowas,      IRC_CMD_LOGGED_IN },
566        { "nickserv",    1, irc_cmd_nickserv,    IRC_CMD_LOGGED_IN },
567        { "ns",          1, irc_cmd_nickserv,    IRC_CMD_LOGGED_IN },
568        { "motd",        0, irc_cmd_motd,        IRC_CMD_LOGGED_IN },
569        { "pong",        0, irc_cmd_pong,        IRC_CMD_LOGGED_IN },
570        { "completions", 0, irc_cmd_completions, IRC_CMD_LOGGED_IN },
571        { "die",         0, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
572        { "wallops",     1, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
573        { "lilo",        1, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
574        { "rehash",      0, irc_cmd_rehash,      IRC_CMD_OPER_ONLY },
575        { "kill",        2, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
576        { NULL }
577};
578
579void irc_exec( irc_t *irc, char *cmd[] )
580{       
581        int i, n_arg;
582       
583        if( !cmd[0] )
584                return;
585       
586        for( i = 0; irc_commands[i].command; i++ )
587                if( g_strcasecmp( irc_commands[i].command, cmd[0] ) == 0 )
588                {
589                        /* There should be no typo in the next line: */
590                        for( n_arg = 0; cmd[n_arg]; n_arg ++ ); n_arg --;
591                       
592                        if( irc_commands[i].flags & IRC_CMD_PRE_LOGIN && irc->status >= USTATUS_LOGGED_IN )
593                        {
594                                irc_reply( irc, 462, ":Only allowed before logging in" );
595                        }
596                        else if( irc_commands[i].flags & IRC_CMD_LOGGED_IN && irc->status < USTATUS_LOGGED_IN )
597                        {
598                                irc_reply( irc, 451, ":Register first" );
599                        }
600                        else if( irc_commands[i].flags & IRC_CMD_OPER_ONLY && !strchr( irc->umode, 'o' ) )
601                        {
602                                irc_reply( irc, 481, ":Permission denied - You're not an IRC operator" );
603                        }
604                        else if( n_arg < irc_commands[i].required_parameters )
605                        {
606                                irc_reply( irc, 461, "%s :Need more parameters", cmd[0] );
607                        }
608                        else if( irc_commands[i].flags & IRC_CMD_TO_MASTER )
609                        {
610                                /* IPC doesn't make sense in inetd mode,
611                                    but the function will catch that. */
612                                ipc_to_master( cmd );
613                        }
614                        else
615                        {
616                                irc_commands[i].execute( irc, cmd );
617                        }
618                       
619                        break;
620                }
621}
Note: See TracBrowser for help on using the repository browser.