source: root_commands.c @ f536a99

Last change on this file since f536a99 was f536a99, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-08-25T20:48:48Z

Fixed NULL pointer dereference on trying to read non-existent settings.

  • Property mode set to 100644
File size: 24.7 KB
RevLine 
[b7d3cc34]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* User manager (root) 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 "commands.h"
28#include "crypting.h"
29#include "bitlbee.h"
30#include "help.h"
31
32#include <string.h>
33
[f73b969]34void root_command_string( irc_t *irc, user_t *u, char *command, int flags )
[7e563ed]35{
36        char *cmd[IRC_MAX_ARGS];
37        char *s;
38        int k;
39        char q = 0;
40       
41        memset( cmd, 0, sizeof( cmd ) );
42        cmd[0] = command;
43        k = 1;
44        for( s = command; *s && k < ( IRC_MAX_ARGS - 1 ); s ++ )
45                if( *s == ' ' && !q )
46                {
47                        *s = 0;
48                        while( *++s == ' ' );
49                        if( *s == '"' || *s == '\'' )
50                        {
51                                q = *s;
52                                s ++;
53                        }
54                        if( *s )
55                        {
56                                cmd[k++] = s;
57                                s --;
58                        }
[619a681]59                        else
60                        {
61                                break;
62                        }
[7e563ed]63                }
[79c6f9f]64                else if( *s == '\\' && ( ( !q && s[1] ) || ( q && q == s[1] ) ) )
65                {
66                        char *cpy;
67                       
68                        for( cpy = s; *cpy; cpy ++ )
69                                cpy[0] = cpy[1];
70                }
[7e563ed]71                else if( *s == q )
72                {
73                        q = *s = 0;
74                }
75        cmd[k] = NULL;
76       
[f73b969]77        root_command( irc, cmd );
[7e563ed]78}
79
[f73b969]80void root_command( irc_t *irc, char *cmd[] )
[7e563ed]81{       
82        int i;
83       
84        if( !cmd[0] )
[f73b969]85                return;
[7e563ed]86       
87        for( i = 0; commands[i].command; i++ )
88                if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 )
89                {
90                        if( !cmd[commands[i].required_parameters] )
91                        {
92                                irc_usermsg( irc, "Not enough parameters given (need %d)", commands[i].required_parameters );
[f73b969]93                                return;
[7e563ed]94                        }
95                        commands[i].execute( irc, cmd );
[f73b969]96                        return;
[7e563ed]97                }
98       
99        irc_usermsg( irc, "Unknown command: %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[0] );
100}
101
[f73b969]102static void cmd_help( irc_t *irc, char **cmd )
[b7d3cc34]103{
104        char param[80];
105        int i;
106        char *s;
107       
108        memset( param, 0, sizeof(param) );
109        for ( i = 1; (cmd[i] != NULL && ( strlen(param) < (sizeof(param)-1) ) ); i++ ) {
110                if ( i != 1 )   // prepend space except for the first parameter
111                        strcat(param, " ");
112                strncat( param, cmd[i], sizeof(param) - strlen(param) - 1 );
113        }
114
115        s = help_get( &(global.help), param );
116        if( !s ) s = help_get( &(global.help), "" );
117       
118        if( s )
119        {
120                irc_usermsg( irc, "%s", s );
121                g_free( s );
122        }
123        else
124        {
125                irc_usermsg( irc, "Error opening helpfile." );
126        }
127}
128
[90bbb0e]129static void cmd_account( irc_t *irc, char **cmd );
130
[f73b969]131static void cmd_identify( irc_t *irc, char **cmd )
[b7d3cc34]132{
[ab49fdc]133        storage_status_t status = storage_load( irc->nick, cmd[1], irc );
[90bbb0e]134        char *account_on[] = { "account", "on", NULL };
[b7d3cc34]135       
[09adf08]136        switch (status) {
137        case STORAGE_INVALID_PASSWORD:
[b7d3cc34]138                irc_usermsg( irc, "Incorrect password" );
[09adf08]139                break;
140        case STORAGE_NO_SUCH_USER:
[b7d3cc34]141                irc_usermsg( irc, "The nick is (probably) not registered" );
[09adf08]142                break;
143        case STORAGE_OK:
[6ee9d2d]144                irc_usermsg( irc, "Password accepted, settings and accounts loaded" );
[238f828]145                irc_umode_set( irc, "+R", 1 );
[d5ccd83]146                if( set_getbool( &irc->set, "auto_connect" ) )
[90bbb0e]147                        cmd_account( irc, account_on );
[09adf08]148                break;
[c121f89]149        case STORAGE_OTHER_ERROR:
[09adf08]150        default:
[c121f89]151                irc_usermsg( irc, "Unknown error while loading configuration" );
[09adf08]152                break;
[b7d3cc34]153        }
154}
155
[f73b969]156static void cmd_register( irc_t *irc, char **cmd )
[b7d3cc34]157{
158        if( global.conf->authmode == AUTHMODE_REGISTERED )
159        {
160                irc_usermsg( irc, "This server does not allow registering new accounts" );
[f73b969]161                return;
[b7d3cc34]162        }
[1ee6c18]163
[7cad7b4]164        irc_setpass( irc, cmd[1] );
[ab49fdc]165        switch( storage_save( irc, FALSE )) {
[a1f17d4]166                case STORAGE_ALREADY_EXISTS:
167                        irc_usermsg( irc, "Nick is already registered" );
168                        break;
169                       
170                case STORAGE_OK:
[0383943]171                        irc_usermsg( irc, "Account successfully created" );
[79e826a]172                        irc->status |= USTATUS_IDENTIFIED;
[238f828]173                        irc_umode_set( irc, "+R", 1 );
[a1f17d4]174                        break;
175
176                default:
177                        irc_usermsg( irc, "Error registering" );
178                        break;
[b7d3cc34]179        }
180}
181
[f73b969]182static void cmd_drop( irc_t *irc, char **cmd )
[b7d3cc34]183{
[a1f17d4]184        storage_status_t status;
185       
[ab49fdc]186        status = storage_remove (irc->nick, cmd[1]);
[a1f17d4]187        switch (status) {
188        case STORAGE_NO_SUCH_USER:
[b7d3cc34]189                irc_usermsg( irc, "That account does not exist" );
[f73b969]190                break;
[a1f17d4]191        case STORAGE_INVALID_PASSWORD:
[1ee6c18]192                irc_usermsg( irc, "Password invalid" );
[f73b969]193                break;
[a1f17d4]194        case STORAGE_OK:
[7cad7b4]195                irc_setpass( irc, NULL );
[79e826a]196                irc->status &= ~USTATUS_IDENTIFIED;
[238f828]197                irc_umode_set( irc, "-R", 1 );
[a1f17d4]198                irc_usermsg( irc, "Account `%s' removed", irc->nick );
[f73b969]199                break;
[a1f17d4]200        default:
[30ce1ce]201                irc_usermsg( irc, "Error: `%d'", status );
[f73b969]202                break;
[b7d3cc34]203        }
204}
205
[f35aee7]206struct cmd_account_del_data
[7f421d6]207{
[f35aee7]208        account_t *a;
209        irc_t *irc;
210};
211
212void cmd_account_del_yes( void *data )
213{
214        struct cmd_account_del_data *cad = data;
215        account_t *a;
[7f421d6]216       
[f35aee7]217        for( a = cad->irc->accounts; a && a != cad->a; a = a->next );
218       
219        if( a == NULL )
220        {
221                irc_usermsg( cad->irc, "Account already deleted" );
222        }
223        else if( a->ic )
[7f421d6]224        {
[f35aee7]225                irc_usermsg( cad->irc, "Account is still logged in, can't delete" );
[7f421d6]226        }
227        else
228        {
[f35aee7]229                account_del( cad->irc, a );
230                irc_usermsg( cad->irc, "Account deleted" );
[7f421d6]231        }
[f35aee7]232        g_free( data );
[7f421d6]233}
234
[f35aee7]235void cmd_account_del_no( void *data )
[7f421d6]236{
[8dbe021f]237        g_free( data );
[7f421d6]238}
239
[f536a99]240static void cmd_showset( irc_t *irc, set_t **head, char *key )
[f3579fd]241{
[f536a99]242        char *val;
[f3579fd]243       
[f536a99]244        if( ( val = set_getstr( head, key ) ) )
245                irc_usermsg( irc, "%s = `%s'", key, val );
[f3579fd]246        else
[f536a99]247                irc_usermsg( irc, "%s is empty", key );
[f3579fd]248}
249
[f73b969]250static void cmd_account( irc_t *irc, char **cmd )
[b7d3cc34]251{
252        account_t *a;
253       
[3af70b0]254        if( global.conf->authmode == AUTHMODE_REGISTERED && !( irc->status & USTATUS_IDENTIFIED ) )
[b7d3cc34]255        {
256                irc_usermsg( irc, "This server only accepts registered users" );
[f73b969]257                return;
[b7d3cc34]258        }
259       
260        if( g_strcasecmp( cmd[1], "add" ) == 0 )
261        {
[7b23afd]262                struct prpl *prpl;
[b7d3cc34]263               
264                if( cmd[2] == NULL || cmd[3] == NULL || cmd[4] == NULL )
265                {
266                        irc_usermsg( irc, "Not enough parameters" );
[f73b969]267                        return;
[b7d3cc34]268                }
269               
[7b23afd]270                prpl = find_protocol(cmd[2]);
[b7d3cc34]271               
[7b23afd]272                if( prpl == NULL )
[b7d3cc34]273                {
274                        irc_usermsg( irc, "Unknown protocol" );
[f73b969]275                        return;
[b7d3cc34]276                }
277
[7b23afd]278                a = account_add( irc, prpl, cmd[3], cmd[4] );
[b7d3cc34]279                if( cmd[5] )
[30ce1ce]280                {
281                        irc_usermsg( irc, "Warning: Passing a servername/other flags to `account add' "
282                                          "is now deprecated. Use `account set' instead." );
[5100caa]283                        set_setstr( &a->set, "server", cmd[5] );
[30ce1ce]284                }
[b7d3cc34]285               
286                irc_usermsg( irc, "Account successfully added" );
287        }
288        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
289        {
290                if( !cmd[2] )
291                {
292                        irc_usermsg( irc, "Not enough parameters given (need %d)", 2 );
293                }
294                else if( !( a = account_get( irc, cmd[2] ) ) )
295                {
296                        irc_usermsg( irc, "Invalid account" );
297                }
[0da65d5]298                else if( a->ic )
[b7d3cc34]299                {
300                        irc_usermsg( irc, "Account is still logged in, can't delete" );
301                }
302                else
303                {
[f35aee7]304                        struct cmd_account_del_data *cad;
[7f421d6]305                        char *msg;
306                       
[f35aee7]307                        cad = g_malloc( sizeof( struct cmd_account_del_data ) );
308                        cad->a = a;
309                        cad->irc = irc;
[8dbe021f]310                       
[7f421d6]311                        msg = g_strdup_printf( "If you remove this account (%s(%s)), BitlBee will "
312                                               "also forget all your saved nicknames. If you want "
313                                               "to change your username/password, use the `account "
314                                               "set' command. Are you sure you want to delete this "
315                                               "account?", a->prpl->name, a->user );
[f35aee7]316                        query_add( irc, NULL, msg, cmd_account_del_yes, cmd_account_del_no, cad );
[7f421d6]317                        g_free( msg );
[b7d3cc34]318                }
319        }
320        else if( g_strcasecmp( cmd[1], "list" ) == 0 )
321        {
322                int i = 0;
323               
[e6e1f18]324                if( strchr( irc->umode, 'b' ) )
325                        irc_usermsg( irc, "Account list:" );
326               
[b7d3cc34]327                for( a = irc->accounts; a; a = a->next )
328                {
329                        char *con;
330                       
[0da65d5]331                        if( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) )
[b7d3cc34]332                                con = " (connected)";
[0da65d5]333                        else if( a->ic )
[b7d3cc34]334                                con = " (connecting)";
335                        else if( a->reconnect )
336                                con = " (awaiting reconnect)";
337                        else
338                                con = "";
339                       
[7b23afd]340                        irc_usermsg( irc, "%2d. %s, %s%s", i, a->prpl->name, a->user, con );
[b7d3cc34]341                       
342                        i ++;
343                }
344                irc_usermsg( irc, "End of account list" );
345        }
346        else if( g_strcasecmp( cmd[1], "on" ) == 0 )
347        {
348                if( cmd[2] )
349                {
350                        if( ( a = account_get( irc, cmd[2] ) ) )
351                        {
[0da65d5]352                                if( a->ic )
[b7d3cc34]353                                {
354                                        irc_usermsg( irc, "Account already online" );
[f73b969]355                                        return;
[b7d3cc34]356                                }
357                                else
358                                {
359                                        account_on( irc, a );
360                                }
361                        }
362                        else
363                        {
364                                irc_usermsg( irc, "Invalid account" );
[f73b969]365                                return;
[b7d3cc34]366                        }
367                }
368                else
369                {
370                        if ( irc->accounts ) {
371                                irc_usermsg( irc, "Trying to get all accounts connected..." );
372                       
373                                for( a = irc->accounts; a; a = a->next )
[0da65d5]374                                        if( !a->ic && a->auto_connect )
[b7d3cc34]375                                                account_on( irc, a );
376                        } 
377                        else
378                        {
[30ce1ce]379                                irc_usermsg( irc, "No accounts known. Use `account add' to add one." );
[b7d3cc34]380                        }
381                }
382        }
383        else if( g_strcasecmp( cmd[1], "off" ) == 0 )
384        {
385                if( !cmd[2] )
386                {
387                        irc_usermsg( irc, "Deactivating all active (re)connections..." );
388                       
389                        for( a = irc->accounts; a; a = a->next )
390                        {
[0da65d5]391                                if( a->ic )
[b7d3cc34]392                                        account_off( irc, a );
393                                else if( a->reconnect )
394                                        cancel_auto_reconnect( a );
395                        }
396                }
397                else if( ( a = account_get( irc, cmd[2] ) ) )
398                {
[0da65d5]399                        if( a->ic )
[b7d3cc34]400                        {
401                                account_off( irc, a );
402                        }
403                        else if( a->reconnect )
404                        {
405                                cancel_auto_reconnect( a );
406                                irc_usermsg( irc, "Reconnect cancelled" );
407                        }
408                        else
409                        {
410                                irc_usermsg( irc, "Account already offline" );
[f73b969]411                                return;
[b7d3cc34]412                        }
413                }
414                else
415                {
416                        irc_usermsg( irc, "Invalid account" );
[f73b969]417                        return;
[b7d3cc34]418                }
419        }
[5100caa]420        else if( g_strcasecmp( cmd[1], "set" ) == 0 )
421        {
422                char *acc_handle, *set_name = NULL, *tmp;
423               
424                if( !cmd[2] )
425                {
426                        irc_usermsg( irc, "Not enough parameters given (need %d)", 2 );
427                        return;
428                }
429               
[cd428e4]430                if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
431                        acc_handle = g_strdup( cmd[3] );
432                else
433                        acc_handle = g_strdup( cmd[2] );
434               
[23c4e64]435                if( !acc_handle )
436                {
437                        irc_usermsg( irc, "Not enough parameters given (need %d)", 3 );
438                        return;
439                }
440               
[5100caa]441                if( ( tmp = strchr( acc_handle, '/' ) ) )
442                {
443                        *tmp = 0;
444                        set_name = tmp + 1;
445                }
446               
[cd428e4]447                if( ( a = account_get( irc, acc_handle ) ) == NULL )
[5100caa]448                {
[75a4b85]449                        g_free( acc_handle );
[5100caa]450                        irc_usermsg( irc, "Invalid account" );
451                        return;
452                }
453               
[9334cc2]454                if( cmd[3] && set_name )
[5100caa]455                {
456                        set_t *s = set_find( &a->set, set_name );
[f3579fd]457                        int st;
[5100caa]458                       
[0da65d5]459                        if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY )
[5100caa]460                        {
[75a4b85]461                                g_free( acc_handle );
[911f2eb]462                                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" );
463                                return;
464                        }
[0da65d5]465                        else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY )
[911f2eb]466                        {
[75a4b85]467                                g_free( acc_handle );
[911f2eb]468                                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" );
[5100caa]469                                return;
470                        }
471                       
[71dc854]472                        if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
[f3579fd]473                                st = set_reset( &a->set, set_name );
[cd428e4]474                        else
[f3579fd]475                                st = set_setstr( &a->set, set_name, cmd[3] );
476                       
477                        if( set_getstr( &a->set, set_name ) == NULL )
478                        {
479                                if( st )
480                                        irc_usermsg( irc, "Setting changed successfully" );
481                                else
482                                        irc_usermsg( irc, "Failed to change setting" );
483                        }
484                        else
485                        {
[f536a99]486                                cmd_showset( irc, &a->set, set_name );
[f3579fd]487                        }
[5100caa]488                }
[f3579fd]489                else if( set_name )
[5100caa]490                {
[f536a99]491                        cmd_showset( irc, &a->set, set_name );
[5100caa]492                }
493                else
494                {
495                        set_t *s = a->set;
496                        while( s )
497                        {
[f536a99]498                                cmd_showset( irc, &s, s->key );
[5100caa]499                                s = s->next;
500                        }
501                }
502               
503                g_free( acc_handle );
504        }
[b7d3cc34]505        else
506        {
[5c09a59]507                irc_usermsg( irc, "Unknown command: account %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[1] );
[b7d3cc34]508        }
509}
510
[f73b969]511static void cmd_add( irc_t *irc, char **cmd )
[b7d3cc34]512{
513        account_t *a;
[f0cb961]514        int add_on_server = 1;
[f8de26f]515       
516        if( g_strcasecmp( cmd[1], "-tmp" ) == 0 )
517        {
[f0cb961]518                add_on_server = 0;
[7adc657]519                cmd ++;
[f8de26f]520        }
[b7d3cc34]521       
522        if( !( a = account_get( irc, cmd[1] ) ) )
523        {
524                irc_usermsg( irc, "Invalid account" );
[f73b969]525                return;
[b7d3cc34]526        }
[0da65d5]527        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]528        {
529                irc_usermsg( irc, "That account is not on-line" );
[f73b969]530                return;
[b7d3cc34]531        }
532       
533        if( cmd[3] )
534        {
535                if( !nick_ok( cmd[3] ) )
536                {
537                        irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] );
[f73b969]538                        return;
[b7d3cc34]539                }
540                else if( user_find( irc, cmd[3] ) )
541                {
542                        irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] );
[f73b969]543                        return;
[b7d3cc34]544                }
545                else
546                {
[5b52a48]547                        nick_set( a, cmd[2], cmd[3] );
[b7d3cc34]548                }
549        }
[f8de26f]550       
[f0cb961]551        if( add_on_server )
[0da65d5]552                a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL );
[7adc657]553        else
554                /* Yeah, officially this is a call-*back*... So if we just
555                   called add_buddy, we'll wait for the IM server to respond
556                   before we do this. */
557                imcb_add_buddy( a->ic, cmd[2], NULL );
[f0cb961]558       
559        irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2]  );
[b7d3cc34]560}
561
[f73b969]562static void cmd_info( irc_t *irc, char **cmd )
[b7d3cc34]563{
[0da65d5]564        struct im_connection *ic;
[b7d3cc34]565        account_t *a;
566       
567        if( !cmd[2] )
568        {
569                user_t *u = user_find( irc, cmd[1] );
[0da65d5]570                if( !u || !u->ic )
[b7d3cc34]571                {
572                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]573                        return;
[b7d3cc34]574                }
[0da65d5]575                ic = u->ic;
[b7d3cc34]576                cmd[2] = u->handle;
577        }
578        else if( !( a = account_get( irc, cmd[1] ) ) )
579        {
580                irc_usermsg( irc, "Invalid account" );
[f73b969]581                return;
[b7d3cc34]582        }
[0da65d5]583        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]584        {
585                irc_usermsg( irc, "That account is not on-line" );
[f73b969]586                return;
[b7d3cc34]587        }
588       
[0da65d5]589        if( !ic->acc->prpl->get_info )
[b7d3cc34]590        {
591                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
592        }
[f73b969]593        else
594        {
[0da65d5]595                ic->acc->prpl->get_info( ic, cmd[2] );
[f73b969]596        }
[b7d3cc34]597}
598
[f73b969]599static void cmd_rename( irc_t *irc, char **cmd )
[b7d3cc34]600{
601        user_t *u;
602       
603        if( g_strcasecmp( cmd[1], irc->nick ) == 0 )
604        {
605                irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] );
606        }
[f73b969]607        else if( user_find( irc, cmd[2] ) && ( nick_cmp( cmd[1], cmd[2] ) != 0 ) )
[b7d3cc34]608        {
609                irc_usermsg( irc, "Nick `%s' already exists", cmd[2] );
610        }
[f73b969]611        else if( !nick_ok( cmd[2] ) )
[b7d3cc34]612        {
613                irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
614        }
[f73b969]615        else if( !( u = user_find( irc, cmd[1] ) ) )
[b7d3cc34]616        {
617                irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
618        }
[f73b969]619        else
[b7d3cc34]620        {
[f73b969]621                user_rename( irc, cmd[1], cmd[2] );
622                irc_write( irc, ":%s!%s@%s NICK %s", cmd[1], u->user, u->host, cmd[2] );
623                if( g_strcasecmp( cmd[1], irc->mynick ) == 0 )
624                {
625                        g_free( irc->mynick );
626                        irc->mynick = g_strdup( cmd[2] );
[1195cec]627                       
[7125cb3]628                        /* If we're called internally (user did "set root_nick"),
629                           let's not go O(INF). :-) */
[1195cec]630                        if( strcmp( cmd[0], "set_rename" ) != 0 )
631                                set_setstr( &irc->set, "root_nick", cmd[2] );
[f73b969]632                }
633                else if( u->send_handler == buddy_send_handler )
634                {
[0da65d5]635                        nick_set( u->ic->acc, u->handle, cmd[2] );
[f73b969]636                }
637               
638                irc_usermsg( irc, "Nick successfully changed" );
[b7d3cc34]639        }
640}
641
[1195cec]642char *set_eval_root_nick( set_t *set, char *new_nick )
643{
644        irc_t *irc = set->data;
645       
646        if( strcmp( irc->mynick, new_nick ) != 0 )
647        {
648                char *cmd[] = { "set_rename", irc->mynick, new_nick, NULL };
649               
650                cmd_rename( irc, cmd );
651        }
652       
[7125cb3]653        return strcmp( irc->mynick, new_nick ) == 0 ? new_nick : SET_INVALID;
[1195cec]654}
655
[f73b969]656static void cmd_remove( irc_t *irc, char **cmd )
[b7d3cc34]657{
658        user_t *u;
659        char *s;
660       
[0da65d5]661        if( !( u = user_find( irc, cmd[1] ) ) || !u->ic )
[b7d3cc34]662        {
663                irc_usermsg( irc, "Buddy `%s' not found", cmd[1] );
[f73b969]664                return;
[b7d3cc34]665        }
666        s = g_strdup( u->handle );
667       
[0da65d5]668        u->ic->acc->prpl->remove_buddy( u->ic, u->handle, NULL );
669        nick_del( u->ic->acc, u->handle );
[b7d3cc34]670        user_del( irc, cmd[1] );
671       
672        irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] );
673        g_free( s );
674       
[f73b969]675        return;
[b7d3cc34]676}
677
[f73b969]678static void cmd_block( irc_t *irc, char **cmd )
[b7d3cc34]679{
[0da65d5]680        struct im_connection *ic;
[b7d3cc34]681        account_t *a;
682       
[0da65d5]683        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
[87b6a3e]684        {
685                char *format;
686                GSList *l;
687               
688                if( strchr( irc->umode, 'b' ) != NULL )
689                        format = "%s\t%s";
690                else
[57ef864]691                        format = "%-32.32s  %-16.16s";
[87b6a3e]692               
693                irc_usermsg( irc, format, "Handle", "Nickname" );
[0da65d5]694                for( l = a->ic->deny; l; l = l->next )
[87b6a3e]695                {
[0da65d5]696                        user_t *u = user_findhandle( a->ic, l->data );
[87b6a3e]697                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
698                }
699                irc_usermsg( irc, "End of list." );
700               
701                return;
702        }
703        else if( !cmd[2] )
[b7d3cc34]704        {
705                user_t *u = user_find( irc, cmd[1] );
[0da65d5]706                if( !u || !u->ic )
[b7d3cc34]707                {
708                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]709                        return;
[b7d3cc34]710                }
[0da65d5]711                ic = u->ic;
[b7d3cc34]712                cmd[2] = u->handle;
713        }
714        else if( !( a = account_get( irc, cmd[1] ) ) )
715        {
716                irc_usermsg( irc, "Invalid account" );
[f73b969]717                return;
[b7d3cc34]718        }
[0da65d5]719        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]720        {
721                irc_usermsg( irc, "That account is not on-line" );
[f73b969]722                return;
[b7d3cc34]723        }
724       
[0da65d5]725        if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit )
[b7d3cc34]726        {
727                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
728        }
729        else
730        {
[84b045d]731                imc_rem_allow( ic, cmd[2] );
732                imc_add_block( ic, cmd[2] );
[da3b536]733                irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] );
[b7d3cc34]734        }
735}
736
[f73b969]737static void cmd_allow( irc_t *irc, char **cmd )
[b7d3cc34]738{
[0da65d5]739        struct im_connection *ic;
[b7d3cc34]740        account_t *a;
741       
[0da65d5]742        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
[87b6a3e]743        {
744                char *format;
745                GSList *l;
746               
747                if( strchr( irc->umode, 'b' ) != NULL )
748                        format = "%s\t%s";
749                else
[57ef864]750                        format = "%-32.32s  %-16.16s";
[87b6a3e]751               
752                irc_usermsg( irc, format, "Handle", "Nickname" );
[0da65d5]753                for( l = a->ic->permit; l; l = l->next )
[87b6a3e]754                {
[0da65d5]755                        user_t *u = user_findhandle( a->ic, l->data );
[87b6a3e]756                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
757                }
758                irc_usermsg( irc, "End of list." );
759               
760                return;
761        }
762        else if( !cmd[2] )
[b7d3cc34]763        {
764                user_t *u = user_find( irc, cmd[1] );
[0da65d5]765                if( !u || !u->ic )
[b7d3cc34]766                {
767                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]768                        return;
[b7d3cc34]769                }
[0da65d5]770                ic = u->ic;
[b7d3cc34]771                cmd[2] = u->handle;
772        }
773        else if( !( a = account_get( irc, cmd[1] ) ) )
774        {
775                irc_usermsg( irc, "Invalid account" );
[f73b969]776                return;
[b7d3cc34]777        }
[0da65d5]778        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]779        {
780                irc_usermsg( irc, "That account is not on-line" );
[f73b969]781                return;
[b7d3cc34]782        }
783       
[0da65d5]784        if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit )
[b7d3cc34]785        {
786                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
787        }
788        else
789        {
[84b045d]790                imc_rem_block( ic, cmd[2] );
791                imc_add_allow( ic, cmd[2] );
[b7d3cc34]792               
[da3b536]793                irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] );
[b7d3cc34]794        }
795}
796
[f73b969]797static void cmd_yesno( irc_t *irc, char **cmd )
[b7d3cc34]798{
799        query_t *q = NULL;
800        int numq = 0;
801       
802        if( irc->queries == NULL )
803        {
804                irc_usermsg( irc, "Did I ask you something?" );
[f73b969]805                return;
[b7d3cc34]806        }
807       
808        /* If there's an argument, the user seems to want to answer another question than the
809           first/last (depending on the query_order setting) one. */
810        if( cmd[1] )
811        {
812                if( sscanf( cmd[1], "%d", &numq ) != 1 )
813                {
814                        irc_usermsg( irc, "Invalid query number" );
[f73b969]815                        return;
[b7d3cc34]816                }
817               
818                for( q = irc->queries; q; q = q->next, numq -- )
819                        if( numq == 0 )
820                                break;
821               
822                if( !q )
823                {
824                        irc_usermsg( irc, "Uhm, I never asked you something like that..." );
[f73b969]825                        return;
[b7d3cc34]826                }
827        }
828       
829        if( g_strcasecmp( cmd[0], "yes" ) == 0 )
830                query_answer( irc, q, 1 );
831        else if( g_strcasecmp( cmd[0], "no" ) == 0 )
832                query_answer( irc, q, 0 );
833}
834
[f73b969]835static void cmd_set( irc_t *irc, char **cmd )
[b7d3cc34]836{
[71dc854]837        char *set_name = cmd[1];
[cd428e4]838       
[b7d3cc34]839        if( cmd[1] && cmd[2] )
840        {
[f3579fd]841                int st;
842               
[71dc854]843                if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
[cd428e4]844                {
[f3579fd]845                        st = set_reset( &irc->set, cmd[2] );
[cd428e4]846                        set_name = cmd[2];
847                }
848                else
849                {
[f3579fd]850                        st = set_setstr( &irc->set, cmd[1], cmd[2] );
851                }
852               
853                /* Normally we just show the variable's new/unchanged
854                   value as feedback to the user, but this has always
855                   caused confusion when changing the password. Give
856                   other feedback instead: */
857                if( set_getstr( &irc->set, set_name ) == NULL )
858                {
859                        if( st )
860                                irc_usermsg( irc, "Setting changed successfully" );
861                        else
862                                irc_usermsg( irc, "Failed to change setting" );
863                }
864                else
865                {
[f536a99]866                        cmd_showset( irc, &irc->set, set_name );
[cd428e4]867                }
[b7d3cc34]868        }
[f3579fd]869        else if( set_name )
[b7d3cc34]870        {
[f536a99]871                cmd_showset( irc, &irc->set, set_name );
[d5bd9c0]872
873                if( strchr( set_name, '/' ) )
874                        irc_usermsg( irc, "Warning: / found in setting name, you're probably looking for the `account set' command." );
[b7d3cc34]875        }
876        else
877        {
878                set_t *s = irc->set;
879                while( s )
880                {
[f536a99]881                        cmd_showset( irc, &s, s->key );
[b7d3cc34]882                        s = s->next;
883                }
884        }
885}
886
[f73b969]887static void cmd_save( irc_t *irc, char **cmd )
[b7d3cc34]888{
[ab49fdc]889        if( storage_save( irc, TRUE ) == STORAGE_OK )
[b7d3cc34]890                irc_usermsg( irc, "Configuration saved" );
891        else
892                irc_usermsg( irc, "Configuration could not be saved!" );
893}
894
[f73b969]895static void cmd_blist( irc_t *irc, char **cmd )
[b7d3cc34]896{
897        int online = 0, away = 0, offline = 0;
898        user_t *u;
[aefa533e]899        char s[256];
900        char *format;
[b7d3cc34]901        int n_online = 0, n_away = 0, n_offline = 0;
902       
903        if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 )
904                online = offline = away = 1;
905        else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 )
906                offline = 1;
907        else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 )
908                away = 1;
909        else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 )
910                online = 1;
911        else
912                online =  away = 1;
913       
[aefa533e]914        if( strchr( irc->umode, 'b' ) != NULL )
915                format = "%s\t%s\t%s";
916        else
917                format = "%-16.16s  %-40.40s  %s";
918       
919        irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" );
[b7d3cc34]920       
[0da65d5]921        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && !u->away )
[b7d3cc34]922        {
[aefa533e]923                if( online == 1 )
924                {
[e731120]925                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
[aefa533e]926                        irc_usermsg( irc, format, u->nick, s, "Online" );
927                }
928               
[b7d3cc34]929                n_online ++;
930        }
931
[0da65d5]932        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && u->away )
[b7d3cc34]933        {
[aefa533e]934                if( away == 1 )
935                {
[e731120]936                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
[aefa533e]937                        irc_usermsg( irc, format, u->nick, s, u->away );
938                }
[b7d3cc34]939                n_away ++;
940        }
941       
[0da65d5]942        for( u = irc->users; u; u = u->next ) if( u->ic && !u->online )
[b7d3cc34]943        {
[aefa533e]944                if( offline == 1 )
945                {
[e731120]946                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
[aefa533e]947                        irc_usermsg( irc, format, u->nick, s, "Offline" );
948                }
[b7d3cc34]949                n_offline ++;
950        }
951       
[aa5ac01]952        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
[b7d3cc34]953}
954
[f73b969]955static void cmd_nick( irc_t *irc, char **cmd ) 
[b7d3cc34]956{
957        account_t *a;
958
959        if( !cmd[1] || !( a = account_get( irc, cmd[1] ) ) )
960        {
961                irc_usermsg( irc, "Invalid account");
962        }
[0da65d5]963        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]964        {
965                irc_usermsg( irc, "That account is not on-line" );
966        }
967        else if ( !cmd[2] ) 
968        {
[0da65d5]969                irc_usermsg( irc, "Your name is `%s'" , a->ic->displayname ? a->ic->displayname : "NULL" );
[b7d3cc34]970        }
[0da65d5]971        else if ( !a->prpl->set_my_name ) 
[b7d3cc34]972        {
973                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
974        }
975        else
976        {
977                irc_usermsg( irc, "Setting your name to `%s'", cmd[2] );
978               
[0da65d5]979                a->prpl->set_my_name( a->ic, cmd[2] );
[b7d3cc34]980        }
981}
982
[f73b969]983static void cmd_qlist( irc_t *irc, char **cmd )
[b7d3cc34]984{
985        query_t *q = irc->queries;
986        int num;
987       
988        if( !q )
989        {
990                irc_usermsg( irc, "There are no pending questions." );
[f73b969]991                return;
[b7d3cc34]992        }
993       
994        irc_usermsg( irc, "Pending queries:" );
995       
996        for( num = 0; q; q = q->next, num ++ )
[0da65d5]997                if( q->ic ) /* Not necessary yet, but it might come later */
[c2fb3809]998                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question );
[5c09a59]999                else
1000                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
[b7d3cc34]1001}
1002
[fa29d093]1003static void cmd_join_chat( irc_t *irc, char **cmd )
1004{
1005        account_t *a;
[0da65d5]1006        struct im_connection *ic;
[fa29d093]1007        char *chat, *channel, *nick = NULL, *password = NULL;
[0da65d5]1008        struct groupchat *c;
[fa29d093]1009       
1010        if( !( a = account_get( irc, cmd[1] ) ) )
1011        {
1012                irc_usermsg( irc, "Invalid account" );
1013                return;
1014        }
[0da65d5]1015        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[fa29d093]1016        {
1017                irc_usermsg( irc, "That account is not on-line" );
1018                return;
1019        }
1020        else if( a->prpl->chat_join == NULL )
1021        {
1022                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
1023                return;
1024        }
[0da65d5]1025        ic = a->ic;
[fa29d093]1026       
1027        chat = cmd[2];
1028        if( cmd[3] )
1029        {
[9da0bbf]1030                if( cmd[3][0] != '#' && cmd[3][0] != '&' )
[7bf4326]1031                        channel = g_strdup_printf( "&%s", cmd[3] );
1032                else
1033                        channel = g_strdup( cmd[3] );
[fa29d093]1034        }
1035        else
1036        {
1037                char *s;
1038               
[0e7ab64]1039                channel = g_strdup_printf( "&%s", chat );
[fa29d093]1040                if( ( s = strchr( channel, '@' ) ) )
1041                        *s = 0;
1042        }
1043        if( cmd[3] && cmd[4] )
1044                nick = cmd[4];
[e35d1a1]1045        else
1046                nick = irc->nick;
[fa29d093]1047        if( cmd[3] && cmd[4] && cmd[5] )
1048                password = cmd[5];
1049       
[7bf4326]1050        if( !nick_ok( channel + 1 ) )
[0e7ab64]1051        {
1052                irc_usermsg( irc, "Invalid channel name: %s", channel );
1053                g_free( channel );
1054                return;
1055        }
1056        else if( g_strcasecmp( channel, irc->channel ) == 0 || irc_chat_by_channel( irc, channel ) )
1057        {
1058                irc_usermsg( irc, "Channel already exists: %s", channel );
1059                g_free( channel );
1060                return;
1061        }
[fa29d093]1062       
[e35d1a1]1063        if( ( c = a->prpl->chat_join( ic, chat, nick, password ) ) )
1064        {
1065                g_free( c->channel );
1066                c->channel = channel;
1067        }
1068        else
1069        {
1070                irc_usermsg( irc, "Tried to join chat, not sure if this was successful" );
1071                g_free( channel );
1072        }
[fa29d093]1073}
1074
[0298d11]1075const command_t commands[] = {
1076        { "help",           0, cmd_help,           0 }, 
1077        { "identify",       1, cmd_identify,       0 },
1078        { "register",       1, cmd_register,       0 },
1079        { "drop",           1, cmd_drop,           0 },
1080        { "account",        1, cmd_account,        0 },
1081        { "add",            2, cmd_add,            0 },
1082        { "info",           1, cmd_info,           0 },
1083        { "rename",         2, cmd_rename,         0 },
1084        { "remove",         1, cmd_remove,         0 },
1085        { "block",          1, cmd_block,          0 },
1086        { "allow",          1, cmd_allow,          0 },
1087        { "save",           0, cmd_save,           0 },
1088        { "set",            0, cmd_set,            0 },
1089        { "yes",            0, cmd_yesno,          0 },
1090        { "no",             0, cmd_yesno,          0 },
1091        { "blist",          0, cmd_blist,          0 },
1092        { "nick",           1, cmd_nick,           0 },
1093        { "qlist",          0, cmd_qlist,          0 },
[fa29d093]1094        { "join_chat",      2, cmd_join_chat,      0 },
[0298d11]1095        { NULL }
1096};
Note: See TracBrowser for help on using the repository browser.