source: root_commands.c @ ba7d16f

Last change on this file since ba7d16f was ba7d16f, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-24T17:12:53Z

Now seems like a pretty good time to finally kill crypting.c and storage_text.
This means people won't be able to upgrade from BitlBee 1.0 to this version
anymore but only via 1.2.

1.0 is old enough that I don't really expect this to be a problem.

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