source: root_commands.c @ 76b6521

Last change on this file since 76b6521 was 3183c21, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-09-06T22:59:32Z

Completely reviewed all uses of irc->password, irc_setpass() and
USTATUS_IDENTIFIED after another account overwriting vulnerability was
found by Tero Marttila.

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