source: root_commands.c @ 9b55485

Last change on this file since 9b55485 was 6738a67, checked in by Sven Moritz Hallberg <pesco@…>, at 2008-07-16T23:22:52Z

merge in latest trunk

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