source: root_commands.c @ 2288705

Last change on this file since 2288705 was 2288705, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-12-07T21:54:19Z

Merging head.

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