source: irc_commands.c @ c22c210

Last change on this file since c22c210 was c22c210, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-01-14T17:48:29Z

Checks if there's an OPER password set before checking it, to prevent crashes.

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