source: root_commands.c @ 6c56f42

Last change on this file since 6c56f42 was 6c56f42, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-11T23:04:24Z

Allow short versions of root commands (just give enough to match exactly
one command), for the lazy users.

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