source: root_commands.c @ 07054a5

Last change on this file since 07054a5 was 07054a5, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-08-31T22:49:32Z

"chat add" can generate a channel name by itself if necessary. Also fixed
MIN_ARGS, using a variable named i showed why precompiler macros really
are evil. :-)

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