source: root_commands.c @ 9d4352c

Last change on this file since 9d4352c was 9d4352c, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-20T23:03:01Z

Restored a few more root commands.

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