source: root_commands.c @ dbb0ce3

Last change on this file since dbb0ce3 was dbb0ce3, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-11T19:38:45Z

Restored add/remove command and, oh, yes/no commands. Nice to be able to
answer the questions..

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