source: root_commands.c @ c821e8a

Last change on this file since c821e8a was c821e8a, checked in by ulim <a.sporto+bee@…>, at 2008-06-02T12:58:57Z

merged in upstream r384. No conflicts.

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