source: root_commands.c @ 1195cec

Last change on this file since 1195cec was 1195cec, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-04-05T13:03:31Z

Changed root nicknames are now saved. (Bug #378)

  • Property mode set to 100644
File size: 24.1 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:
[30ce1ce]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] )
[30ce1ce]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] );
[30ce1ce]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                        {
[30ce1ce]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               
[5100caa]425                if( ( tmp = strchr( acc_handle, '/' ) ) )
426                {
427                        *tmp = 0;
428                        set_name = tmp + 1;
429                }
430               
[cd428e4]431                if( ( a = account_get( irc, acc_handle ) ) == NULL )
[5100caa]432                {
[75a4b85]433                        g_free( acc_handle );
[5100caa]434                        irc_usermsg( irc, "Invalid account" );
435                        return;
436                }
437               
[9334cc2]438                if( cmd[3] && set_name )
[5100caa]439                {
440                        set_t *s = set_find( &a->set, set_name );
441                       
[0da65d5]442                        if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY )
[5100caa]443                        {
[75a4b85]444                                g_free( acc_handle );
[911f2eb]445                                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" );
446                                return;
447                        }
[0da65d5]448                        else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY )
[911f2eb]449                        {
[75a4b85]450                                g_free( acc_handle );
[911f2eb]451                                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" );
[5100caa]452                                return;
453                        }
454                       
[71dc854]455                        if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
[cd428e4]456                                set_reset( &a->set, set_name );
457                        else
458                                set_setstr( &a->set, set_name, cmd[3] );
[5100caa]459                }
460                if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */
461                {
462                        char *s = set_getstr( &a->set, set_name );
463                        if( s )
464                                irc_usermsg( irc, "%s = `%s'", set_name, s );
465                        else
466                                irc_usermsg( irc, "%s is empty", set_name );
467                }
468                else
469                {
470                        set_t *s = a->set;
471                        while( s )
472                        {
473                                if( s->value || s->def )
[cd428e4]474                                        irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def );
[5100caa]475                                else
476                                        irc_usermsg( irc, "%s is empty", s->key );
477                                s = s->next;
478                        }
479                }
480               
481                g_free( acc_handle );
482        }
[b7d3cc34]483        else
484        {
[5c09a59]485                irc_usermsg( irc, "Unknown command: account %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[1] );
[b7d3cc34]486        }
487}
488
[f73b969]489static void cmd_add( irc_t *irc, char **cmd )
[b7d3cc34]490{
491        account_t *a;
[f0cb961]492        int add_on_server = 1;
[f8de26f]493       
494        if( g_strcasecmp( cmd[1], "-tmp" ) == 0 )
495        {
[f0cb961]496                add_on_server = 0;
[7adc657]497                cmd ++;
[f8de26f]498        }
[b7d3cc34]499       
500        if( !( a = account_get( irc, cmd[1] ) ) )
501        {
502                irc_usermsg( irc, "Invalid account" );
[f73b969]503                return;
[b7d3cc34]504        }
[0da65d5]505        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]506        {
507                irc_usermsg( irc, "That account is not on-line" );
[f73b969]508                return;
[b7d3cc34]509        }
510       
511        if( cmd[3] )
512        {
513                if( !nick_ok( cmd[3] ) )
514                {
515                        irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] );
[f73b969]516                        return;
[b7d3cc34]517                }
518                else if( user_find( irc, cmd[3] ) )
519                {
520                        irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] );
[f73b969]521                        return;
[b7d3cc34]522                }
523                else
524                {
[5b52a48]525                        nick_set( a, cmd[2], cmd[3] );
[b7d3cc34]526                }
527        }
[f8de26f]528       
[f0cb961]529        if( add_on_server )
[0da65d5]530                a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL );
[7adc657]531        else
532                /* Yeah, officially this is a call-*back*... So if we just
533                   called add_buddy, we'll wait for the IM server to respond
534                   before we do this. */
535                imcb_add_buddy( a->ic, cmd[2], NULL );
[f0cb961]536       
537        irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2]  );
[b7d3cc34]538}
539
[f73b969]540static void cmd_info( irc_t *irc, char **cmd )
[b7d3cc34]541{
[0da65d5]542        struct im_connection *ic;
[b7d3cc34]543        account_t *a;
544       
545        if( !cmd[2] )
546        {
547                user_t *u = user_find( irc, cmd[1] );
[0da65d5]548                if( !u || !u->ic )
[b7d3cc34]549                {
550                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]551                        return;
[b7d3cc34]552                }
[0da65d5]553                ic = u->ic;
[b7d3cc34]554                cmd[2] = u->handle;
555        }
556        else if( !( a = account_get( irc, cmd[1] ) ) )
557        {
558                irc_usermsg( irc, "Invalid account" );
[f73b969]559                return;
[b7d3cc34]560        }
[0da65d5]561        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]562        {
563                irc_usermsg( irc, "That account is not on-line" );
[f73b969]564                return;
[b7d3cc34]565        }
566       
[0da65d5]567        if( !ic->acc->prpl->get_info )
[b7d3cc34]568        {
569                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
570        }
[f73b969]571        else
572        {
[0da65d5]573                ic->acc->prpl->get_info( ic, cmd[2] );
[f73b969]574        }
[b7d3cc34]575}
576
[f73b969]577static void cmd_rename( irc_t *irc, char **cmd )
[b7d3cc34]578{
579        user_t *u;
580       
581        if( g_strcasecmp( cmd[1], irc->nick ) == 0 )
582        {
583                irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] );
584        }
[f73b969]585        else if( user_find( irc, cmd[2] ) && ( nick_cmp( cmd[1], cmd[2] ) != 0 ) )
[b7d3cc34]586        {
587                irc_usermsg( irc, "Nick `%s' already exists", cmd[2] );
588        }
[f73b969]589        else if( !nick_ok( cmd[2] ) )
[b7d3cc34]590        {
591                irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
592        }
[f73b969]593        else if( !( u = user_find( irc, cmd[1] ) ) )
[b7d3cc34]594        {
595                irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
596        }
[f73b969]597        else
[b7d3cc34]598        {
[f73b969]599                user_rename( irc, cmd[1], cmd[2] );
600                irc_write( irc, ":%s!%s@%s NICK %s", cmd[1], u->user, u->host, cmd[2] );
601                if( g_strcasecmp( cmd[1], irc->mynick ) == 0 )
602                {
603                        g_free( irc->mynick );
604                        irc->mynick = g_strdup( cmd[2] );
[1195cec]605                       
606                        if( strcmp( cmd[0], "set_rename" ) != 0 )
607                                set_setstr( &irc->set, "root_nick", cmd[2] );
[f73b969]608                }
609                else if( u->send_handler == buddy_send_handler )
610                {
[0da65d5]611                        nick_set( u->ic->acc, u->handle, cmd[2] );
[f73b969]612                }
613               
614                irc_usermsg( irc, "Nick successfully changed" );
[b7d3cc34]615        }
616}
617
[1195cec]618char *set_eval_root_nick( set_t *set, char *new_nick )
619{
620        irc_t *irc = set->data;
621       
622        if( strcmp( irc->mynick, new_nick ) != 0 )
623        {
624                char *cmd[] = { "set_rename", irc->mynick, new_nick, NULL };
625               
626                cmd_rename( irc, cmd );
627        }
628       
629        return strcmp( irc->mynick, new_nick ) == 0 ? new_nick : NULL;
630}
631
[f73b969]632static void cmd_remove( irc_t *irc, char **cmd )
[b7d3cc34]633{
634        user_t *u;
635        char *s;
636       
[0da65d5]637        if( !( u = user_find( irc, cmd[1] ) ) || !u->ic )
[b7d3cc34]638        {
639                irc_usermsg( irc, "Buddy `%s' not found", cmd[1] );
[f73b969]640                return;
[b7d3cc34]641        }
642        s = g_strdup( u->handle );
643       
[0da65d5]644        u->ic->acc->prpl->remove_buddy( u->ic, u->handle, NULL );
645        nick_del( u->ic->acc, u->handle );
[b7d3cc34]646        user_del( irc, cmd[1] );
647       
648        irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] );
649        g_free( s );
650       
[f73b969]651        return;
[b7d3cc34]652}
653
[f73b969]654static void cmd_block( irc_t *irc, char **cmd )
[b7d3cc34]655{
[0da65d5]656        struct im_connection *ic;
[b7d3cc34]657        account_t *a;
658       
[0da65d5]659        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
[87b6a3e]660        {
661                char *format;
662                GSList *l;
663               
664                if( strchr( irc->umode, 'b' ) != NULL )
665                        format = "%s\t%s";
666                else
[57ef864]667                        format = "%-32.32s  %-16.16s";
[87b6a3e]668               
669                irc_usermsg( irc, format, "Handle", "Nickname" );
[0da65d5]670                for( l = a->ic->deny; l; l = l->next )
[87b6a3e]671                {
[0da65d5]672                        user_t *u = user_findhandle( a->ic, l->data );
[87b6a3e]673                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
674                }
675                irc_usermsg( irc, "End of list." );
676               
677                return;
678        }
679        else if( !cmd[2] )
[b7d3cc34]680        {
681                user_t *u = user_find( irc, cmd[1] );
[0da65d5]682                if( !u || !u->ic )
[b7d3cc34]683                {
684                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]685                        return;
[b7d3cc34]686                }
[0da65d5]687                ic = u->ic;
[b7d3cc34]688                cmd[2] = u->handle;
689        }
690        else if( !( a = account_get( irc, cmd[1] ) ) )
691        {
692                irc_usermsg( irc, "Invalid account" );
[f73b969]693                return;
[b7d3cc34]694        }
[0da65d5]695        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]696        {
697                irc_usermsg( irc, "That account is not on-line" );
[f73b969]698                return;
[b7d3cc34]699        }
700       
[0da65d5]701        if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit )
[b7d3cc34]702        {
703                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
704        }
705        else
706        {
[84b045d]707                imc_rem_allow( ic, cmd[2] );
708                imc_add_block( ic, cmd[2] );
[da3b536]709                irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] );
[b7d3cc34]710        }
711}
712
[f73b969]713static void cmd_allow( irc_t *irc, char **cmd )
[b7d3cc34]714{
[0da65d5]715        struct im_connection *ic;
[b7d3cc34]716        account_t *a;
717       
[0da65d5]718        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
[87b6a3e]719        {
720                char *format;
721                GSList *l;
722               
723                if( strchr( irc->umode, 'b' ) != NULL )
724                        format = "%s\t%s";
725                else
[57ef864]726                        format = "%-32.32s  %-16.16s";
[87b6a3e]727               
728                irc_usermsg( irc, format, "Handle", "Nickname" );
[0da65d5]729                for( l = a->ic->permit; l; l = l->next )
[87b6a3e]730                {
[0da65d5]731                        user_t *u = user_findhandle( a->ic, l->data );
[87b6a3e]732                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
733                }
734                irc_usermsg( irc, "End of list." );
735               
736                return;
737        }
738        else if( !cmd[2] )
[b7d3cc34]739        {
740                user_t *u = user_find( irc, cmd[1] );
[0da65d5]741                if( !u || !u->ic )
[b7d3cc34]742                {
743                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]744                        return;
[b7d3cc34]745                }
[0da65d5]746                ic = u->ic;
[b7d3cc34]747                cmd[2] = u->handle;
748        }
749        else if( !( a = account_get( irc, cmd[1] ) ) )
750        {
751                irc_usermsg( irc, "Invalid account" );
[f73b969]752                return;
[b7d3cc34]753        }
[0da65d5]754        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]755        {
756                irc_usermsg( irc, "That account is not on-line" );
[f73b969]757                return;
[b7d3cc34]758        }
759       
[0da65d5]760        if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit )
[b7d3cc34]761        {
762                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
763        }
764        else
765        {
[84b045d]766                imc_rem_block( ic, cmd[2] );
767                imc_add_allow( ic, cmd[2] );
[b7d3cc34]768               
[da3b536]769                irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] );
[b7d3cc34]770        }
771}
772
[f73b969]773static void cmd_yesno( irc_t *irc, char **cmd )
[b7d3cc34]774{
775        query_t *q = NULL;
776        int numq = 0;
777       
778        if( irc->queries == NULL )
779        {
780                irc_usermsg( irc, "Did I ask you something?" );
[f73b969]781                return;
[b7d3cc34]782        }
783       
784        /* If there's an argument, the user seems to want to answer another question than the
785           first/last (depending on the query_order setting) one. */
786        if( cmd[1] )
787        {
788                if( sscanf( cmd[1], "%d", &numq ) != 1 )
789                {
790                        irc_usermsg( irc, "Invalid query number" );
[f73b969]791                        return;
[b7d3cc34]792                }
793               
794                for( q = irc->queries; q; q = q->next, numq -- )
795                        if( numq == 0 )
796                                break;
797               
798                if( !q )
799                {
800                        irc_usermsg( irc, "Uhm, I never asked you something like that..." );
[f73b969]801                        return;
[b7d3cc34]802                }
803        }
804       
805        if( g_strcasecmp( cmd[0], "yes" ) == 0 )
806                query_answer( irc, q, 1 );
807        else if( g_strcasecmp( cmd[0], "no" ) == 0 )
808                query_answer( irc, q, 0 );
809}
810
[f73b969]811static void cmd_set( irc_t *irc, char **cmd )
[b7d3cc34]812{
[71dc854]813        char *set_name = cmd[1];
[cd428e4]814       
[b7d3cc34]815        if( cmd[1] && cmd[2] )
816        {
[71dc854]817                if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
[cd428e4]818                {
819                        set_reset( &irc->set, cmd[2] );
820                        set_name = cmd[2];
821                }
822                else
823                {
824                        set_setstr( &irc->set, cmd[1], cmd[2] );
825                }
[b7d3cc34]826        }
[56f260a]827        if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */
[b7d3cc34]828        {
[cd428e4]829                char *s = set_getstr( &irc->set, set_name );
830                if( s )
831                        irc_usermsg( irc, "%s = `%s'", set_name, s );
[5100caa]832                else
[cd428e4]833                        irc_usermsg( irc, "%s is empty", set_name );
[d5bd9c0]834
835                if( strchr( set_name, '/' ) )
836                        irc_usermsg( irc, "Warning: / found in setting name, you're probably looking for the `account set' command." );
[b7d3cc34]837        }
838        else
839        {
840                set_t *s = irc->set;
841                while( s )
842                {
843                        if( s->value || s->def )
[cd428e4]844                                irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def );
[5100caa]845                        else
846                                irc_usermsg( irc, "%s is empty", s->key );
[b7d3cc34]847                        s = s->next;
848                }
849        }
850}
851
[f73b969]852static void cmd_save( irc_t *irc, char **cmd )
[b7d3cc34]853{
[ab49fdc]854        if( storage_save( irc, TRUE ) == STORAGE_OK )
[b7d3cc34]855                irc_usermsg( irc, "Configuration saved" );
856        else
857                irc_usermsg( irc, "Configuration could not be saved!" );
858}
859
[f73b969]860static void cmd_blist( irc_t *irc, char **cmd )
[b7d3cc34]861{
862        int online = 0, away = 0, offline = 0;
863        user_t *u;
[aefa533e]864        char s[256];
865        char *format;
[b7d3cc34]866        int n_online = 0, n_away = 0, n_offline = 0;
867       
868        if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 )
869                online = offline = away = 1;
870        else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 )
871                offline = 1;
872        else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 )
873                away = 1;
874        else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 )
875                online = 1;
876        else
877                online =  away = 1;
878       
[aefa533e]879        if( strchr( irc->umode, 'b' ) != NULL )
880                format = "%s\t%s\t%s";
881        else
882                format = "%-16.16s  %-40.40s  %s";
883       
884        irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" );
[b7d3cc34]885       
[0da65d5]886        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && !u->away )
[b7d3cc34]887        {
[aefa533e]888                if( online == 1 )
889                {
[e731120]890                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
[aefa533e]891                        irc_usermsg( irc, format, u->nick, s, "Online" );
892                }
893               
[b7d3cc34]894                n_online ++;
895        }
896
[0da65d5]897        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && u->away )
[b7d3cc34]898        {
[aefa533e]899                if( away == 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, u->away );
903                }
[b7d3cc34]904                n_away ++;
905        }
906       
[0da65d5]907        for( u = irc->users; u; u = u->next ) if( u->ic && !u->online )
[b7d3cc34]908        {
[aefa533e]909                if( offline == 1 )
910                {
[e731120]911                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
[aefa533e]912                        irc_usermsg( irc, format, u->nick, s, "Offline" );
913                }
[b7d3cc34]914                n_offline ++;
915        }
916       
[aa5ac01]917        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
[b7d3cc34]918}
919
[f73b969]920static void cmd_nick( irc_t *irc, char **cmd ) 
[b7d3cc34]921{
922        account_t *a;
923
924        if( !cmd[1] || !( a = account_get( irc, cmd[1] ) ) )
925        {
926                irc_usermsg( irc, "Invalid account");
927        }
[0da65d5]928        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]929        {
930                irc_usermsg( irc, "That account is not on-line" );
931        }
932        else if ( !cmd[2] ) 
933        {
[0da65d5]934                irc_usermsg( irc, "Your name is `%s'" , a->ic->displayname ? a->ic->displayname : "NULL" );
[b7d3cc34]935        }
[0da65d5]936        else if ( !a->prpl->set_my_name ) 
[b7d3cc34]937        {
938                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
939        }
940        else
941        {
942                irc_usermsg( irc, "Setting your name to `%s'", cmd[2] );
943               
[0da65d5]944                a->prpl->set_my_name( a->ic, cmd[2] );
[b7d3cc34]945        }
946}
947
[f73b969]948static void cmd_qlist( irc_t *irc, char **cmd )
[b7d3cc34]949{
950        query_t *q = irc->queries;
951        int num;
952       
953        if( !q )
954        {
955                irc_usermsg( irc, "There are no pending questions." );
[f73b969]956                return;
[b7d3cc34]957        }
958       
959        irc_usermsg( irc, "Pending queries:" );
960       
961        for( num = 0; q; q = q->next, num ++ )
[0da65d5]962                if( q->ic ) /* Not necessary yet, but it might come later */
[c2fb3809]963                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question );
[5c09a59]964                else
965                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
[b7d3cc34]966}
967
[fa29d093]968static void cmd_join_chat( irc_t *irc, char **cmd )
969{
970        account_t *a;
[0da65d5]971        struct im_connection *ic;
[fa29d093]972        char *chat, *channel, *nick = NULL, *password = NULL;
[0da65d5]973        struct groupchat *c;
[fa29d093]974       
975        if( !( a = account_get( irc, cmd[1] ) ) )
976        {
977                irc_usermsg( irc, "Invalid account" );
978                return;
979        }
[0da65d5]980        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[fa29d093]981        {
982                irc_usermsg( irc, "That account is not on-line" );
983                return;
984        }
985        else if( a->prpl->chat_join == NULL )
986        {
987                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
988                return;
989        }
[0da65d5]990        ic = a->ic;
[fa29d093]991       
992        chat = cmd[2];
993        if( cmd[3] )
994        {
[9da0bbf]995                if( cmd[3][0] != '#' && cmd[3][0] != '&' )
[7bf4326]996                        channel = g_strdup_printf( "&%s", cmd[3] );
997                else
998                        channel = g_strdup( cmd[3] );
[fa29d093]999        }
1000        else
1001        {
1002                char *s;
1003               
[0e7ab64]1004                channel = g_strdup_printf( "&%s", chat );
[fa29d093]1005                if( ( s = strchr( channel, '@' ) ) )
1006                        *s = 0;
1007        }
1008        if( cmd[3] && cmd[4] )
1009                nick = cmd[4];
[e35d1a1]1010        else
1011                nick = irc->nick;
[fa29d093]1012        if( cmd[3] && cmd[4] && cmd[5] )
1013                password = cmd[5];
1014       
[7bf4326]1015        if( !nick_ok( channel + 1 ) )
[0e7ab64]1016        {
1017                irc_usermsg( irc, "Invalid channel name: %s", channel );
1018                g_free( channel );
1019                return;
1020        }
1021        else if( g_strcasecmp( channel, irc->channel ) == 0 || irc_chat_by_channel( irc, channel ) )
1022        {
1023                irc_usermsg( irc, "Channel already exists: %s", channel );
1024                g_free( channel );
1025                return;
1026        }
[fa29d093]1027       
[e35d1a1]1028        if( ( c = a->prpl->chat_join( ic, chat, nick, password ) ) )
1029        {
1030                g_free( c->channel );
1031                c->channel = channel;
1032        }
1033        else
1034        {
1035                irc_usermsg( irc, "Tried to join chat, not sure if this was successful" );
1036                g_free( channel );
1037        }
[fa29d093]1038}
1039
[0298d11]1040const command_t commands[] = {
1041        { "help",           0, cmd_help,           0 }, 
1042        { "identify",       1, cmd_identify,       0 },
1043        { "register",       1, cmd_register,       0 },
1044        { "drop",           1, cmd_drop,           0 },
1045        { "account",        1, cmd_account,        0 },
1046        { "add",            2, cmd_add,            0 },
1047        { "info",           1, cmd_info,           0 },
1048        { "rename",         2, cmd_rename,         0 },
1049        { "remove",         1, cmd_remove,         0 },
1050        { "block",          1, cmd_block,          0 },
1051        { "allow",          1, cmd_allow,          0 },
1052        { "save",           0, cmd_save,           0 },
1053        { "set",            0, cmd_set,            0 },
1054        { "yes",            0, cmd_yesno,          0 },
1055        { "no",             0, cmd_yesno,          0 },
1056        { "blist",          0, cmd_blist,          0 },
1057        { "nick",           1, cmd_nick,           0 },
1058        { "qlist",          0, cmd_qlist,          0 },
[fa29d093]1059        { "join_chat",      2, cmd_join_chat,      0 },
[0298d11]1060        { NULL }
1061};
Note: See TracBrowser for help on using the repository browser.