source: root_commands.c @ d4810df

Last change on this file since d4810df was d4810df, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-09-02T07:48:56Z

Fixed check for set -del arguments.

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