source: root_commands.c @ 66b9e36a

Last change on this file since 66b9e36a was 2272cb3, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-20T23:11:32Z

Restored block/allow commands. Only "chat" is left now, which will need some
more rethinking anyway (and groupchat support is not back yet anyway).

  • Property mode set to 100644
File size: 27.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
[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}
[2272cb3]719#endif
[0baed0d]720
[f73b969]721static void cmd_block( irc_t *irc, char **cmd )
[b7d3cc34]722{
[0da65d5]723        struct im_connection *ic;
[b7d3cc34]724        account_t *a;
725       
[2272cb3]726        if( !cmd[2] && ( a = account_get( irc->b, cmd[1] ) ) && a->ic )
[87b6a3e]727        {
728                char *format;
729                GSList *l;
730               
731                if( strchr( irc->umode, 'b' ) != NULL )
732                        format = "%s\t%s";
733                else
[57ef864]734                        format = "%-32.32s  %-16.16s";
[87b6a3e]735               
736                irc_usermsg( irc, format, "Handle", "Nickname" );
[0da65d5]737                for( l = a->ic->deny; l; l = l->next )
[87b6a3e]738                {
[2272cb3]739                        bee_user_t *bu = bee_user_by_handle( irc->b, a->ic, l->data );
740                        irc_user_t *iu = bu ? bu->ui_data : NULL;
741                        irc_usermsg( irc, format, l->data, iu ? iu->nick : "(none)" );
[87b6a3e]742                }
743                irc_usermsg( irc, "End of list." );
744               
745                return;
746        }
747        else if( !cmd[2] )
[b7d3cc34]748        {
[2272cb3]749                irc_user_t *iu = irc_user_by_name( irc, cmd[1] );
750                if( !iu || !iu->bu )
[b7d3cc34]751                {
752                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]753                        return;
[b7d3cc34]754                }
[2272cb3]755                ic = iu->bu->ic;
756                cmd[2] = iu->bu->handle;
[b7d3cc34]757        }
[2272cb3]758        else if( !( a = account_get( irc->b, cmd[1] ) ) )
[b7d3cc34]759        {
760                irc_usermsg( irc, "Invalid account" );
[f73b969]761                return;
[b7d3cc34]762        }
[0da65d5]763        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]764        {
765                irc_usermsg( irc, "That account is not on-line" );
[f73b969]766                return;
[b7d3cc34]767        }
768       
[0da65d5]769        if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit )
[b7d3cc34]770        {
771                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
772        }
773        else
774        {
[84b045d]775                imc_rem_allow( ic, cmd[2] );
776                imc_add_block( ic, cmd[2] );
[da3b536]777                irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] );
[b7d3cc34]778        }
779}
780
[f73b969]781static void cmd_allow( irc_t *irc, char **cmd )
[b7d3cc34]782{
[0da65d5]783        struct im_connection *ic;
[b7d3cc34]784        account_t *a;
785       
[2272cb3]786        if( !cmd[2] && ( a = account_get( irc->b, cmd[1] ) ) && a->ic )
[87b6a3e]787        {
788                char *format;
789                GSList *l;
790               
791                if( strchr( irc->umode, 'b' ) != NULL )
792                        format = "%s\t%s";
793                else
[57ef864]794                        format = "%-32.32s  %-16.16s";
[87b6a3e]795               
796                irc_usermsg( irc, format, "Handle", "Nickname" );
[0da65d5]797                for( l = a->ic->permit; l; l = l->next )
[87b6a3e]798                {
[2272cb3]799                        bee_user_t *bu = bee_user_by_handle( irc->b, a->ic, l->data );
800                        irc_user_t *iu = bu ? bu->ui_data : NULL;
801                        irc_usermsg( irc, format, l->data, iu ? iu->nick : "(none)" );
[87b6a3e]802                }
803                irc_usermsg( irc, "End of list." );
804               
805                return;
806        }
807        else if( !cmd[2] )
[b7d3cc34]808        {
[2272cb3]809                irc_user_t *iu = irc_user_by_name( irc, cmd[1] );
810                if( !iu || !iu->bu )
[b7d3cc34]811                {
812                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]813                        return;
[b7d3cc34]814                }
[2272cb3]815                ic = iu->bu->ic;
816                cmd[2] = iu->bu->handle;
[b7d3cc34]817        }
[2272cb3]818        else if( !( a = account_get( irc->b, cmd[1] ) ) )
[b7d3cc34]819        {
820                irc_usermsg( irc, "Invalid account" );
[f73b969]821                return;
[b7d3cc34]822        }
[0da65d5]823        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]824        {
825                irc_usermsg( irc, "That account is not on-line" );
[f73b969]826                return;
[b7d3cc34]827        }
828       
[0da65d5]829        if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit )
[b7d3cc34]830        {
831                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
832        }
833        else
834        {
[84b045d]835                imc_rem_block( ic, cmd[2] );
836                imc_add_allow( ic, cmd[2] );
[b7d3cc34]837               
[da3b536]838                irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] );
[b7d3cc34]839        }
840}
841
[f73b969]842static void cmd_yesno( irc_t *irc, char **cmd )
[b7d3cc34]843{
844        query_t *q = NULL;
845        int numq = 0;
846       
847        if( irc->queries == NULL )
848        {
849                irc_usermsg( irc, "Did I ask you something?" );
[f73b969]850                return;
[b7d3cc34]851        }
852       
853        /* If there's an argument, the user seems to want to answer another question than the
854           first/last (depending on the query_order setting) one. */
855        if( cmd[1] )
856        {
857                if( sscanf( cmd[1], "%d", &numq ) != 1 )
858                {
859                        irc_usermsg( irc, "Invalid query number" );
[f73b969]860                        return;
[b7d3cc34]861                }
862               
863                for( q = irc->queries; q; q = q->next, numq -- )
864                        if( numq == 0 )
865                                break;
866               
867                if( !q )
868                {
869                        irc_usermsg( irc, "Uhm, I never asked you something like that..." );
[f73b969]870                        return;
[b7d3cc34]871                }
872        }
873       
874        if( g_strcasecmp( cmd[0], "yes" ) == 0 )
875                query_answer( irc, q, 1 );
876        else if( g_strcasecmp( cmd[0], "no" ) == 0 )
877                query_answer( irc, q, 0 );
878}
879
[f73b969]880static void cmd_set( irc_t *irc, char **cmd )
[b7d3cc34]881{
[c05eb5b]882        cmd_set_real( irc, cmd, NULL, NULL );
[b7d3cc34]883}
884
[f73b969]885static void cmd_blist( irc_t *irc, char **cmd )
[b7d3cc34]886{
887        int online = 0, away = 0, offline = 0;
[4c3519a]888        GSList *l;
[aefa533e]889        char s[256];
890        char *format;
[b7d3cc34]891        int n_online = 0, n_away = 0, n_offline = 0;
892       
893        if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 )
894                online = offline = away = 1;
895        else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 )
896                offline = 1;
897        else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 )
898                away = 1;
899        else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 )
900                online = 1;
901        else
[449a51d]902                online = away = 1;
[b7d3cc34]903       
[aefa533e]904        if( strchr( irc->umode, 'b' ) != NULL )
905                format = "%s\t%s\t%s";
906        else
907                format = "%-16.16s  %-40.40s  %s";
908       
[4c3519a]909        irc_usermsg( irc, format, "Nick", "Handle/Account", "Status" );
[b7d3cc34]910       
[4c3519a]911        for( l = irc->users; l; l = l->next )
[b7d3cc34]912        {
[4c3519a]913                irc_user_t *iu = l->data;
914                bee_user_t *bu = iu->bu;
915               
916                if( !bu || ( bu->flags & ( BEE_USER_ONLINE | BEE_USER_AWAY ) ) != BEE_USER_ONLINE )
917                        continue;
918               
[aefa533e]919                if( online == 1 )
920                {
[449a51d]921                        char st[256] = "Online";
922                       
[4c3519a]923                        if( bu->status_msg )
924                                g_snprintf( st, sizeof( st ) - 1, "Online (%s)", bu->status_msg );
[449a51d]925                       
[4c3519a]926                        g_snprintf( s, sizeof( s ) - 1, "%s %s(%s)", bu->handle, bu->ic->acc->prpl->name, bu->ic->acc->user );
927                        irc_usermsg( irc, format, iu->nick, s, st );
[aefa533e]928                }
929               
[b7d3cc34]930                n_online ++;
931        }
932
[4c3519a]933        for( l = irc->users; l; l = l->next )
[b7d3cc34]934        {
[4c3519a]935                irc_user_t *iu = l->data;
936                bee_user_t *bu = iu->bu;
937               
938                if( !bu || !( bu->flags & BEE_USER_ONLINE ) || !( bu->flags & BEE_USER_AWAY ) )
939                        continue;
940               
[aefa533e]941                if( away == 1 )
942                {
[4c3519a]943                        g_snprintf( s, sizeof( s ) - 1, "%s %s(%s)", bu->handle, bu->ic->acc->prpl->name, bu->ic->acc->user );
944                        irc_usermsg( irc, format, iu->nick, s, irc_user_get_away( iu ) );
[aefa533e]945                }
[b7d3cc34]946                n_away ++;
947        }
948       
[4c3519a]949        for( l = irc->users; l; l = l->next )
[b7d3cc34]950        {
[4c3519a]951                irc_user_t *iu = l->data;
952                bee_user_t *bu = iu->bu;
953               
954                if( !bu || bu->flags & BEE_USER_ONLINE )
955                        continue;
956               
[aefa533e]957                if( offline == 1 )
958                {
[4c3519a]959                        g_snprintf( s, sizeof( s ) - 1, "%s %s(%s)", bu->handle, bu->ic->acc->prpl->name, bu->ic->acc->user );
960                        irc_usermsg( irc, format, iu->nick, s, "Offline" );
[aefa533e]961                }
[b7d3cc34]962                n_offline ++;
963        }
964       
[aa5ac01]965        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
[b7d3cc34]966}
967
[f73b969]968static void cmd_qlist( irc_t *irc, char **cmd )
[b7d3cc34]969{
970        query_t *q = irc->queries;
971        int num;
972       
973        if( !q )
974        {
975                irc_usermsg( irc, "There are no pending questions." );
[f73b969]976                return;
[b7d3cc34]977        }
978       
979        irc_usermsg( irc, "Pending queries:" );
980       
981        for( num = 0; q; q = q->next, num ++ )
[0da65d5]982                if( q->ic ) /* Not necessary yet, but it might come later */
[c2fb3809]983                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question );
[5c09a59]984                else
985                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
[b7d3cc34]986}
987
[9d4352c]988#if 0
[e7bc722]989static set_t **cmd_chat_set_findhead( irc_t *irc, char *id )
990{
991        struct chat *c;
992       
993        if( ( c = chat_get( irc, id ) ) )
994                return &c->set;
995        else
996                return NULL;
997}
998
[a9a7287]999static void cmd_chat( irc_t *irc, char **cmd )
1000{
1001        account_t *acc;
1002        struct chat *c;
1003       
1004        if( g_strcasecmp( cmd[1], "add" ) == 0 )
1005        {
[07054a5]1006                char *channel, *s;
1007               
1008                MIN_ARGS( 3 );
[a9a7287]1009               
1010                if( !( acc = account_get( irc, cmd[2] ) ) )
1011                {
1012                        irc_usermsg( irc, "Invalid account" );
1013                        return;
1014                }
1015               
[07054a5]1016                if( cmd[4] == NULL )
1017                {
1018                        channel = g_strdup( cmd[3] );
1019                        if( ( s = strchr( channel, '@' ) ) )
1020                                *s = 0;
1021                }
1022                else
1023                {
1024                        channel = g_strdup( cmd[4] );
1025                }
1026               
1027                if( strchr( CTYPES, channel[0] ) == NULL )
1028                {
1029                        s = g_strdup_printf( "%c%s", CTYPES[0], channel );
1030                        g_free( channel );
1031                        channel = s;
1032                }
1033               
1034                if( ( c = chat_add( irc, acc, cmd[3], channel ) ) )
[a9a7287]1035                        irc_usermsg( irc, "Chatroom added successfully." );
1036                else
1037                        irc_usermsg( irc, "Could not add chatroom." );
[07054a5]1038               
1039                g_free( channel );
[a9a7287]1040        }
1041        else if( g_strcasecmp( cmd[1], "list" ) == 0 )
1042        {
1043                int i = 0;
1044               
1045                if( strchr( irc->umode, 'b' ) )
1046                        irc_usermsg( irc, "Chatroom list:" );
1047               
1048                for( c = irc->chatrooms; c; c = c->next )
1049                {
1050                        irc_usermsg( irc, "%2d. %s(%s) %s, %s", i, c->acc->prpl->name,
1051                                          c->acc->user, c->handle, c->channel );
1052                       
1053                        i ++;
1054                }
1055                irc_usermsg( irc, "End of chatroom list" );
1056        }
[e7bc722]1057        else if( g_strcasecmp( cmd[1], "set" ) == 0 )
1058        {
[2ea8736]1059                MIN_ARGS( 2 );
1060               
[c05eb5b]1061                cmd_set_real( irc, cmd + 1, cmd_chat_set_findhead, NULL );
[e7bc722]1062        }
[d995c9b]1063        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
1064        {
[3b99524]1065                MIN_ARGS( 2 );
[d995c9b]1066               
1067                if( ( c = chat_get( irc, cmd[2] ) ) )
1068                {
1069                        chat_del( irc, c );
1070                }
1071                else
1072                {
1073                        irc_usermsg( irc, "Could not remove chat." );
1074                }
1075        }
[39f93f0]1076        else if( g_strcasecmp( cmd[1], "with" ) == 0 )
1077        {
1078                user_t *u;
[3b99524]1079               
1080                MIN_ARGS( 2 );
[39f93f0]1081               
1082                if( ( u = user_find( irc, cmd[2] ) ) && u->ic && u->ic->acc->prpl->chat_with )
1083                {
1084                        if( !u->ic->acc->prpl->chat_with( u->ic, u->handle ) )
1085                        {
1086                                irc_usermsg( irc, "(Possible) failure while trying to open "
1087                                                  "a groupchat with %s.", u->nick );
1088                        }
1089                }
1090                else
1091                {
1092                        irc_usermsg( irc, "Can't open a groupchat with %s.", cmd[2] );
1093                }
1094        }
[a9a7287]1095        else
1096        {
1097                irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat", cmd[1] );
1098        }
[fa29d093]1099}
[9d4352c]1100#endif
[fa29d093]1101
[b8a491d]1102static void cmd_transfer( irc_t *irc, char **cmd )
[2c2df7d]1103{
1104        GSList *files = irc->file_transfers;
1105        enum { LIST, REJECT, CANCEL };
1106        int subcmd = LIST;
1107        int fid;
1108
1109        if( !files )
1110        {
1111                irc_usermsg( irc, "No pending transfers" );
1112                return;
1113        }
1114
[b8a491d]1115        if( cmd[1] && ( strcmp( cmd[1], "reject" ) == 0 ) )
[2c2df7d]1116        {
1117                subcmd = REJECT;
1118        }
[b8a491d]1119        else if( cmd[1] && ( strcmp( cmd[1], "cancel" ) == 0 ) && 
1120                 cmd[2] && ( sscanf( cmd[2], "%d", &fid ) == 1 ) )
[2c2df7d]1121        {
1122                subcmd = CANCEL;
1123        }
1124
1125        for( ; files; files = g_slist_next( files ) )
1126        {
1127                file_transfer_t *file = files->data;
1128               
1129                switch( subcmd ) {
1130                case LIST:
1131                        if ( file->status == FT_STATUS_LISTENING )
1132                                irc_usermsg( irc, 
1133                                        "Pending file(id %d): %s (Listening...)", file->local_id, file->file_name);
1134                        else 
1135                        {
1136                                int kb_per_s = 0;
[44961cb]1137                                time_t diff = time( NULL ) - file->started ? : 1;
[2c2df7d]1138                                if ( ( file->started > 0 ) && ( file->bytes_transferred > 0 ) )
1139                                        kb_per_s = file->bytes_transferred / 1024 / diff;
1140                                       
1141                                irc_usermsg( irc, 
1142                                        "Pending file(id %d): %s (%10zd/%zd kb, %d kb/s)", file->local_id, file->file_name, 
1143                                        file->bytes_transferred/1024, file->file_size/1024, kb_per_s);
1144                        }
1145                        break;
1146                case REJECT:
1147                        if( file->status == FT_STATUS_LISTENING )
1148                        {
1149                                irc_usermsg( irc, "Rejecting file transfer for %s", file->file_name );
[9d4352c]1150                                imcb_file_canceled( file->ic, file, "Denied by user" );
[2c2df7d]1151                        }
1152                        break;
1153                case CANCEL:
1154                        if( file->local_id == fid )
1155                        {
1156                                irc_usermsg( irc, "Canceling file transfer for %s", file->file_name );
[9d4352c]1157                                imcb_file_canceled( file->ic, file, "Canceled by user" );
[2c2df7d]1158                        }
1159                        break;
1160                }
1161        }
1162}
1163
[6c56f42]1164/* IMPORTANT: Keep this list sorted! The short command logic needs that. */
[0298d11]1165const command_t commands[] = {
[d860a8d]1166        { "account",        1, cmd_account,        0 },
[6c56f42]1167        { "add",            2, cmd_add,            0 },
[2272cb3]1168        { "allow",          1, cmd_allow,          0 },
[4c3519a]1169        { "blist",          0, cmd_blist,          0 },
[2272cb3]1170        { "block",          1, cmd_block,          0 },
[6c56f42]1171        { "drop",           1, cmd_drop,           0 },
[9d4352c]1172        { "ft",             0, cmd_transfer,       0 },
[6c56f42]1173        { "help",           0, cmd_help,           0 }, 
[0298d11]1174        { "identify",       1, cmd_identify,       0 },
[aa44bdd]1175        { "info",           1, cmd_info,           0 },
[6c56f42]1176        { "no",             0, cmd_yesno,          0 },
[9d4352c]1177        { "qlist",          0, cmd_qlist,          0 },
[0298d11]1178        { "register",       1, cmd_register,       0 },
[dbb0ce3]1179        { "remove",         1, cmd_remove,         0 },
[0298d11]1180        { "rename",         2, cmd_rename,         0 },
[6c56f42]1181        { "save",           0, cmd_save,           0 },
[0298d11]1182        { "set",            0, cmd_set,            0 },
[9d4352c]1183        { "transfer",       0, cmd_transfer,       0 },
[0298d11]1184        { "yes",            0, cmd_yesno,          0 },
[dbb0ce3]1185#if 0
[6c56f42]1186        { "chat",           1, cmd_chat,           0 },
[280c56a]1187#endif
[0298d11]1188        { NULL }
1189};
Note: See TracBrowser for help on using the repository browser.