source: root_commands.c @ de49316

Last change on this file since de49316 was c05eb5b, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-09-29T21:53:05Z

Checking account setting's flags again before changing them.

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