source: root_commands.c @ 7e83e8e4

Last change on this file since 7e83e8e4 was 7b71feb, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-05-13T23:34:38Z

Just enough code to join named chatrooms again. This UI is *not* final, the "chat"
command will probably mostly stick around for bw compatibility. Still thinking
about how this should work eventually.

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