source: root_commands.c @ 5a75d15

Last change on this file since 5a75d15 was 5a75d15, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-05T22:32:36Z

Chatroom improvements. Merged chatroom stub into normal chatroom stuff,
restored "chat add" behaviour a little bit better (don't clean up a
channel when its room disappears, just disconnect it from the groupchat).

  • Property mode set to 100644
File size: 26.2 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
[c133d4b8]488static set_t **cmd_channel_set_findhead( irc_t *irc, char *id )
489{
490        irc_channel_t *ic;
491       
492        if( ( ic = irc_channel_by_name( irc, id ) ) )
493                return &ic->set;
494        else
495                return NULL;
496}
497
498static void cmd_channel( irc_t *irc, char **cmd )
499{
500        if( g_strcasecmp( cmd[1], "set" ) == 0 )
501        {
502                MIN_ARGS( 2 );
503               
504                cmd_set_real( irc, cmd + 1, cmd_channel_set_findhead, NULL );
505        }
506        else
507        {
508                irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "channel", cmd[1] );
509        }
510}
511
[f73b969]512static void cmd_add( irc_t *irc, char **cmd )
[b7d3cc34]513{
514        account_t *a;
[f0cb961]515        int add_on_server = 1;
[f8de26f]516       
517        if( g_strcasecmp( cmd[1], "-tmp" ) == 0 )
518        {
[77fc000c]519                MIN_ARGS( 3 );
[f0cb961]520                add_on_server = 0;
[7adc657]521                cmd ++;
[f8de26f]522        }
[b7d3cc34]523       
[dbb0ce3]524        if( !( a = account_get( irc->b, cmd[1] ) ) )
[b7d3cc34]525        {
526                irc_usermsg( irc, "Invalid account" );
[f73b969]527                return;
[b7d3cc34]528        }
[0da65d5]529        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]530        {
531                irc_usermsg( irc, "That account is not on-line" );
[f73b969]532                return;
[b7d3cc34]533        }
534       
535        if( cmd[3] )
536        {
537                if( !nick_ok( cmd[3] ) )
538                {
539                        irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] );
[f73b969]540                        return;
[b7d3cc34]541                }
[dbb0ce3]542                else if( irc_user_by_name( irc, cmd[3] ) )
[b7d3cc34]543                {
544                        irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] );
[f73b969]545                        return;
[b7d3cc34]546                }
547                else
548                {
[5b52a48]549                        nick_set( a, cmd[2], cmd[3] );
[b7d3cc34]550                }
551        }
[f8de26f]552       
[f0cb961]553        if( add_on_server )
[0da65d5]554                a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL );
[7adc657]555        else
[dbb0ce3]556                /* Only for add -tmp. For regular adds, this callback will
557                   be called once the IM server confirms. */
[ad404ab]558                bee_user_new( irc->b, a->ic, cmd[2], BEE_USER_LOCAL );
[f0cb961]559       
560        irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2]  );
[b7d3cc34]561}
562
[dbb0ce3]563static void cmd_remove( irc_t *irc, char **cmd )
564{
565        irc_user_t *iu;
566        bee_user_t *bu;
567        char *s;
568       
569        if( !( iu = irc_user_by_name( irc, cmd[1] ) ) || !( bu = iu->bu ) )
570        {
571                irc_usermsg( irc, "Buddy `%s' not found", cmd[1] );
572                return;
573        }
574        s = g_strdup( bu->handle );
575       
576        bu->ic->acc->prpl->remove_buddy( bu->ic, bu->handle, NULL );
577        nick_del( bu->ic->acc, bu->handle );
[eabc9d2]578        //TODO(wilmer): bee_user_free() and/or let the IM mod do it? irc_user_free( irc, cmd[1] );
[dbb0ce3]579       
580        irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] );
581        g_free( s );
582       
583        return;
584}
585
[f73b969]586static void cmd_info( irc_t *irc, char **cmd )
[b7d3cc34]587{
[0da65d5]588        struct im_connection *ic;
[b7d3cc34]589        account_t *a;
590       
591        if( !cmd[2] )
592        {
[aa44bdd]593                irc_user_t *iu = irc_user_by_name( irc, cmd[1] );
594                if( !iu || !iu->bu )
[b7d3cc34]595                {
596                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]597                        return;
[b7d3cc34]598                }
[aa44bdd]599                ic = iu->bu->ic;
600                cmd[2] = iu->bu->handle;
[b7d3cc34]601        }
[aa44bdd]602        else if( !( a = account_get( irc->b, cmd[1] ) ) )
[b7d3cc34]603        {
604                irc_usermsg( irc, "Invalid account" );
[f73b969]605                return;
[b7d3cc34]606        }
[0da65d5]607        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]608        {
609                irc_usermsg( irc, "That account is not on-line" );
[f73b969]610                return;
[b7d3cc34]611        }
612       
[0da65d5]613        if( !ic->acc->prpl->get_info )
[b7d3cc34]614        {
615                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
616        }
[f73b969]617        else
618        {
[0da65d5]619                ic->acc->prpl->get_info( ic, cmd[2] );
[f73b969]620        }
[b7d3cc34]621}
622
[f73b969]623static void cmd_rename( irc_t *irc, char **cmd )
[b7d3cc34]624{
[57c96f7]625        irc_user_t *iu;
[b7d3cc34]626       
[57c96f7]627        iu = irc_user_by_name( irc, cmd[1] );
628       
629        if( iu == NULL )
[0baed0d]630        {
[57c96f7]631                irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[0baed0d]632        }
[57c96f7]633        else if( iu == irc->user )
[b7d3cc34]634        {
[57c96f7]635                irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] );
[b7d3cc34]636        }
[f73b969]637        else if( !nick_ok( cmd[2] ) )
[b7d3cc34]638        {
639                irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
640        }
[57c96f7]641        else if( irc_user_by_name( irc, cmd[2] ) )
[b7d3cc34]642        {
[57c96f7]643                irc_usermsg( irc, "Nick `%s' already exists", cmd[2] );
[b7d3cc34]644        }
[f73b969]645        else
[b7d3cc34]646        {
[57c96f7]647                if( !irc_user_set_nick( iu, cmd[2] ) )
648                {
649                        irc_usermsg( irc, "Error while changing nick" );
650                        return;
651                }
652               
653                if( iu == irc->root )
[f73b969]654                {
[7125cb3]655                        /* If we're called internally (user did "set root_nick"),
656                           let's not go O(INF). :-) */
[1195cec]657                        if( strcmp( cmd[0], "set_rename" ) != 0 )
[57c96f7]658                                set_setstr( &irc->b->set, "root_nick", cmd[2] );
[f73b969]659                }
[57c96f7]660                else if( iu->bu )
[f73b969]661                {
[57c96f7]662                        nick_set( iu->bu->ic->acc, iu->bu->handle, cmd[2] );
[f73b969]663                }
664               
665                irc_usermsg( irc, "Nick successfully changed" );
[b7d3cc34]666        }
667}
668
[57c96f7]669#if 0
[1195cec]670char *set_eval_root_nick( set_t *set, char *new_nick )
671{
672        irc_t *irc = set->data;
673       
674        if( strcmp( irc->mynick, new_nick ) != 0 )
675        {
676                char *cmd[] = { "set_rename", irc->mynick, new_nick, NULL };
677               
678                cmd_rename( irc, cmd );
679        }
680       
[7125cb3]681        return strcmp( irc->mynick, new_nick ) == 0 ? new_nick : SET_INVALID;
[1195cec]682}
683
[0baed0d]684char *set_eval_control_channel( set_t *set, char *new_name )
685{
686        irc_t *irc = set->data;
687       
688        if( strcmp( irc->channel, new_name ) != 0 )
689        {
690                char *cmd[] = { "set_rename", irc->channel, new_name, NULL };
691               
692                cmd_rename( irc, cmd );
693        }
694       
695        return strcmp( irc->channel, new_name ) == 0 ? new_name : SET_INVALID;
696}
[2272cb3]697#endif
[0baed0d]698
[f73b969]699static void cmd_block( irc_t *irc, char **cmd )
[b7d3cc34]700{
[0da65d5]701        struct im_connection *ic;
[b7d3cc34]702        account_t *a;
703       
[2272cb3]704        if( !cmd[2] && ( a = account_get( irc->b, cmd[1] ) ) && a->ic )
[87b6a3e]705        {
706                char *format;
707                GSList *l;
708               
709                if( strchr( irc->umode, 'b' ) != NULL )
710                        format = "%s\t%s";
711                else
[57ef864]712                        format = "%-32.32s  %-16.16s";
[87b6a3e]713               
714                irc_usermsg( irc, format, "Handle", "Nickname" );
[0da65d5]715                for( l = a->ic->deny; l; l = l->next )
[87b6a3e]716                {
[2272cb3]717                        bee_user_t *bu = bee_user_by_handle( irc->b, a->ic, l->data );
718                        irc_user_t *iu = bu ? bu->ui_data : NULL;
719                        irc_usermsg( irc, format, l->data, iu ? iu->nick : "(none)" );
[87b6a3e]720                }
721                irc_usermsg( irc, "End of list." );
722               
723                return;
724        }
725        else if( !cmd[2] )
[b7d3cc34]726        {
[2272cb3]727                irc_user_t *iu = irc_user_by_name( irc, cmd[1] );
728                if( !iu || !iu->bu )
[b7d3cc34]729                {
730                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]731                        return;
[b7d3cc34]732                }
[2272cb3]733                ic = iu->bu->ic;
734                cmd[2] = iu->bu->handle;
[b7d3cc34]735        }
[2272cb3]736        else if( !( a = account_get( irc->b, cmd[1] ) ) )
[b7d3cc34]737        {
738                irc_usermsg( irc, "Invalid account" );
[f73b969]739                return;
[b7d3cc34]740        }
[0da65d5]741        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]742        {
743                irc_usermsg( irc, "That account is not on-line" );
[f73b969]744                return;
[b7d3cc34]745        }
746       
[0da65d5]747        if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit )
[b7d3cc34]748        {
749                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
750        }
751        else
752        {
[84b045d]753                imc_rem_allow( ic, cmd[2] );
754                imc_add_block( ic, cmd[2] );
[da3b536]755                irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] );
[b7d3cc34]756        }
757}
758
[f73b969]759static void cmd_allow( irc_t *irc, char **cmd )
[b7d3cc34]760{
[0da65d5]761        struct im_connection *ic;
[b7d3cc34]762        account_t *a;
763       
[2272cb3]764        if( !cmd[2] && ( a = account_get( irc->b, cmd[1] ) ) && a->ic )
[87b6a3e]765        {
766                char *format;
767                GSList *l;
768               
769                if( strchr( irc->umode, 'b' ) != NULL )
770                        format = "%s\t%s";
771                else
[57ef864]772                        format = "%-32.32s  %-16.16s";
[87b6a3e]773               
774                irc_usermsg( irc, format, "Handle", "Nickname" );
[0da65d5]775                for( l = a->ic->permit; l; l = l->next )
[87b6a3e]776                {
[2272cb3]777                        bee_user_t *bu = bee_user_by_handle( irc->b, a->ic, l->data );
778                        irc_user_t *iu = bu ? bu->ui_data : NULL;
779                        irc_usermsg( irc, format, l->data, iu ? iu->nick : "(none)" );
[87b6a3e]780                }
781                irc_usermsg( irc, "End of list." );
782               
783                return;
784        }
785        else if( !cmd[2] )
[b7d3cc34]786        {
[2272cb3]787                irc_user_t *iu = irc_user_by_name( irc, cmd[1] );
788                if( !iu || !iu->bu )
[b7d3cc34]789                {
790                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]791                        return;
[b7d3cc34]792                }
[2272cb3]793                ic = iu->bu->ic;
794                cmd[2] = iu->bu->handle;
[b7d3cc34]795        }
[2272cb3]796        else if( !( a = account_get( irc->b, cmd[1] ) ) )
[b7d3cc34]797        {
798                irc_usermsg( irc, "Invalid account" );
[f73b969]799                return;
[b7d3cc34]800        }
[0da65d5]801        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]802        {
803                irc_usermsg( irc, "That account is not on-line" );
[f73b969]804                return;
[b7d3cc34]805        }
806       
[0da65d5]807        if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit )
[b7d3cc34]808        {
809                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
810        }
811        else
812        {
[84b045d]813                imc_rem_block( ic, cmd[2] );
814                imc_add_allow( ic, cmd[2] );
[b7d3cc34]815               
[da3b536]816                irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] );
[b7d3cc34]817        }
818}
819
[f73b969]820static void cmd_yesno( irc_t *irc, char **cmd )
[b7d3cc34]821{
822        query_t *q = NULL;
823        int numq = 0;
824       
825        if( irc->queries == NULL )
826        {
827                irc_usermsg( irc, "Did I ask you something?" );
[f73b969]828                return;
[b7d3cc34]829        }
830       
831        /* If there's an argument, the user seems to want to answer another question than the
832           first/last (depending on the query_order setting) one. */
833        if( cmd[1] )
834        {
835                if( sscanf( cmd[1], "%d", &numq ) != 1 )
836                {
837                        irc_usermsg( irc, "Invalid query number" );
[f73b969]838                        return;
[b7d3cc34]839                }
840               
841                for( q = irc->queries; q; q = q->next, numq -- )
842                        if( numq == 0 )
843                                break;
844               
845                if( !q )
846                {
847                        irc_usermsg( irc, "Uhm, I never asked you something like that..." );
[f73b969]848                        return;
[b7d3cc34]849                }
850        }
851       
852        if( g_strcasecmp( cmd[0], "yes" ) == 0 )
853                query_answer( irc, q, 1 );
854        else if( g_strcasecmp( cmd[0], "no" ) == 0 )
855                query_answer( irc, q, 0 );
856}
857
[f73b969]858static void cmd_set( irc_t *irc, char **cmd )
[b7d3cc34]859{
[c05eb5b]860        cmd_set_real( irc, cmd, NULL, NULL );
[b7d3cc34]861}
862
[f73b969]863static void cmd_blist( irc_t *irc, char **cmd )
[b7d3cc34]864{
865        int online = 0, away = 0, offline = 0;
[4c3519a]866        GSList *l;
[aefa533e]867        char s[256];
868        char *format;
[b7d3cc34]869        int n_online = 0, n_away = 0, n_offline = 0;
870       
871        if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 )
872                online = offline = away = 1;
873        else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 )
874                offline = 1;
875        else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 )
876                away = 1;
877        else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 )
878                online = 1;
879        else
[449a51d]880                online = away = 1;
[b7d3cc34]881       
[aefa533e]882        if( strchr( irc->umode, 'b' ) != NULL )
883                format = "%s\t%s\t%s";
884        else
885                format = "%-16.16s  %-40.40s  %s";
886       
[4c3519a]887        irc_usermsg( irc, format, "Nick", "Handle/Account", "Status" );
[b7d3cc34]888       
[4c3519a]889        for( l = irc->users; l; l = l->next )
[b7d3cc34]890        {
[4c3519a]891                irc_user_t *iu = l->data;
892                bee_user_t *bu = iu->bu;
893               
894                if( !bu || ( bu->flags & ( BEE_USER_ONLINE | BEE_USER_AWAY ) ) != BEE_USER_ONLINE )
895                        continue;
896               
[aefa533e]897                if( online == 1 )
898                {
[449a51d]899                        char st[256] = "Online";
900                       
[4c3519a]901                        if( bu->status_msg )
902                                g_snprintf( st, sizeof( st ) - 1, "Online (%s)", bu->status_msg );
[449a51d]903                       
[4c3519a]904                        g_snprintf( s, sizeof( s ) - 1, "%s %s(%s)", bu->handle, bu->ic->acc->prpl->name, bu->ic->acc->user );
905                        irc_usermsg( irc, format, iu->nick, s, st );
[aefa533e]906                }
907               
[b7d3cc34]908                n_online ++;
909        }
910
[4c3519a]911        for( l = irc->users; l; l = l->next )
[b7d3cc34]912        {
[4c3519a]913                irc_user_t *iu = l->data;
914                bee_user_t *bu = iu->bu;
915               
916                if( !bu || !( bu->flags & BEE_USER_ONLINE ) || !( bu->flags & BEE_USER_AWAY ) )
917                        continue;
918               
[aefa533e]919                if( away == 1 )
920                {
[4c3519a]921                        g_snprintf( s, sizeof( s ) - 1, "%s %s(%s)", bu->handle, bu->ic->acc->prpl->name, bu->ic->acc->user );
922                        irc_usermsg( irc, format, iu->nick, s, irc_user_get_away( iu ) );
[aefa533e]923                }
[b7d3cc34]924                n_away ++;
925        }
926       
[4c3519a]927        for( l = irc->users; l; l = l->next )
[b7d3cc34]928        {
[4c3519a]929                irc_user_t *iu = l->data;
930                bee_user_t *bu = iu->bu;
931               
932                if( !bu || bu->flags & BEE_USER_ONLINE )
933                        continue;
934               
[aefa533e]935                if( offline == 1 )
936                {
[4c3519a]937                        g_snprintf( s, sizeof( s ) - 1, "%s %s(%s)", bu->handle, bu->ic->acc->prpl->name, bu->ic->acc->user );
938                        irc_usermsg( irc, format, iu->nick, s, "Offline" );
[aefa533e]939                }
[b7d3cc34]940                n_offline ++;
941        }
942       
[aa5ac01]943        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
[b7d3cc34]944}
945
[f73b969]946static void cmd_qlist( irc_t *irc, char **cmd )
[b7d3cc34]947{
948        query_t *q = irc->queries;
949        int num;
950       
951        if( !q )
952        {
953                irc_usermsg( irc, "There are no pending questions." );
[f73b969]954                return;
[b7d3cc34]955        }
956       
957        irc_usermsg( irc, "Pending queries:" );
958       
959        for( num = 0; q; q = q->next, num ++ )
[0da65d5]960                if( q->ic ) /* Not necessary yet, but it might come later */
[c2fb3809]961                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question );
[5c09a59]962                else
963                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
[b7d3cc34]964}
965
[a9a7287]966static void cmd_chat( irc_t *irc, char **cmd )
967{
968        account_t *acc;
969       
970        if( g_strcasecmp( cmd[1], "add" ) == 0 )
971        {
[07054a5]972                char *channel, *s;
[7b71feb]973                struct irc_channel *ic;
[07054a5]974               
975                MIN_ARGS( 3 );
[a9a7287]976               
[7b71feb]977                if( !( acc = account_get( irc->b, cmd[2] ) ) )
[a9a7287]978                {
979                        irc_usermsg( irc, "Invalid account" );
980                        return;
981                }
[5a75d15]982                else if( !acc->prpl->chat_join )
983                {
984                        irc_usermsg( irc, "Named chatrooms not supported on that account." );
985                        return;
986                }
[a9a7287]987               
[07054a5]988                if( cmd[4] == NULL )
989                {
990                        channel = g_strdup( cmd[3] );
991                        if( ( s = strchr( channel, '@' ) ) )
992                                *s = 0;
993                }
994                else
995                {
996                        channel = g_strdup( cmd[4] );
997                }
998               
999                if( strchr( CTYPES, channel[0] ) == NULL )
1000                {
[7b71feb]1001                        s = g_strdup_printf( "#%s", channel );
[07054a5]1002                        g_free( channel );
1003                        channel = s;
1004                }
1005               
[5a75d15]1006                if( ( ic = irc_channel_new( irc, channel ) ) &&
1007                    set_setstr( &ic->set, "chat_type", "room" ) &&
1008                    set_setstr( &ic->set, "account", cmd[2] ) &&
1009                    set_setstr( &ic->set, "room", cmd[3] ) )
1010                {
1011                        irc_usermsg( irc, "Chatroom successfully added." );
1012                }
1013                else
[a9a7287]1014                {
[5a75d15]1015                        if( ic )
1016                                irc_channel_free( ic );
[a9a7287]1017                       
[5a75d15]1018                        irc_usermsg( irc, "Could not add chatroom." );
[d995c9b]1019                }
1020        }
[7b71feb]1021        /*
[39f93f0]1022        else if( g_strcasecmp( cmd[1], "with" ) == 0 )
1023        {
1024                user_t *u;
[3b99524]1025               
1026                MIN_ARGS( 2 );
[39f93f0]1027               
1028                if( ( u = user_find( irc, cmd[2] ) ) && u->ic && u->ic->acc->prpl->chat_with )
1029                {
1030                        if( !u->ic->acc->prpl->chat_with( u->ic, u->handle ) )
1031                        {
1032                                irc_usermsg( irc, "(Possible) failure while trying to open "
1033                                                  "a groupchat with %s.", u->nick );
1034                        }
1035                }
1036                else
1037                {
1038                        irc_usermsg( irc, "Can't open a groupchat with %s.", cmd[2] );
1039                }
1040        }
[7b71feb]1041        */
[a9a7287]1042        else
1043        {
1044                irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat", cmd[1] );
1045        }
[fa29d093]1046}
1047
[b8a491d]1048static void cmd_transfer( irc_t *irc, char **cmd )
[2c2df7d]1049{
1050        GSList *files = irc->file_transfers;
1051        enum { LIST, REJECT, CANCEL };
1052        int subcmd = LIST;
1053        int fid;
1054
1055        if( !files )
1056        {
1057                irc_usermsg( irc, "No pending transfers" );
1058                return;
1059        }
1060
[b8a491d]1061        if( cmd[1] && ( strcmp( cmd[1], "reject" ) == 0 ) )
[2c2df7d]1062        {
1063                subcmd = REJECT;
1064        }
[b8a491d]1065        else if( cmd[1] && ( strcmp( cmd[1], "cancel" ) == 0 ) && 
1066                 cmd[2] && ( sscanf( cmd[2], "%d", &fid ) == 1 ) )
[2c2df7d]1067        {
1068                subcmd = CANCEL;
1069        }
1070
1071        for( ; files; files = g_slist_next( files ) )
1072        {
1073                file_transfer_t *file = files->data;
1074               
1075                switch( subcmd ) {
1076                case LIST:
1077                        if ( file->status == FT_STATUS_LISTENING )
1078                                irc_usermsg( irc, 
1079                                        "Pending file(id %d): %s (Listening...)", file->local_id, file->file_name);
1080                        else 
1081                        {
1082                                int kb_per_s = 0;
[44961cb]1083                                time_t diff = time( NULL ) - file->started ? : 1;
[2c2df7d]1084                                if ( ( file->started > 0 ) && ( file->bytes_transferred > 0 ) )
1085                                        kb_per_s = file->bytes_transferred / 1024 / diff;
1086                                       
1087                                irc_usermsg( irc, 
1088                                        "Pending file(id %d): %s (%10zd/%zd kb, %d kb/s)", file->local_id, file->file_name, 
1089                                        file->bytes_transferred/1024, file->file_size/1024, kb_per_s);
1090                        }
1091                        break;
1092                case REJECT:
1093                        if( file->status == FT_STATUS_LISTENING )
1094                        {
1095                                irc_usermsg( irc, "Rejecting file transfer for %s", file->file_name );
[9d4352c]1096                                imcb_file_canceled( file->ic, file, "Denied by user" );
[2c2df7d]1097                        }
1098                        break;
1099                case CANCEL:
1100                        if( file->local_id == fid )
1101                        {
1102                                irc_usermsg( irc, "Canceling file transfer for %s", file->file_name );
[9d4352c]1103                                imcb_file_canceled( file->ic, file, "Canceled by user" );
[2c2df7d]1104                        }
1105                        break;
1106                }
1107        }
1108}
1109
[6c56f42]1110/* IMPORTANT: Keep this list sorted! The short command logic needs that. */
[0298d11]1111const command_t commands[] = {
[d860a8d]1112        { "account",        1, cmd_account,        0 },
[6c56f42]1113        { "add",            2, cmd_add,            0 },
[2272cb3]1114        { "allow",          1, cmd_allow,          0 },
[4c3519a]1115        { "blist",          0, cmd_blist,          0 },
[2272cb3]1116        { "block",          1, cmd_block,          0 },
[c133d4b8]1117        { "channel",        1, cmd_channel,        0 },
1118        { "chat",           1, cmd_chat,           0 },
[6c56f42]1119        { "drop",           1, cmd_drop,           0 },
[9d4352c]1120        { "ft",             0, cmd_transfer,       0 },
[6c56f42]1121        { "help",           0, cmd_help,           0 }, 
[0298d11]1122        { "identify",       1, cmd_identify,       0 },
[aa44bdd]1123        { "info",           1, cmd_info,           0 },
[6c56f42]1124        { "no",             0, cmd_yesno,          0 },
[9d4352c]1125        { "qlist",          0, cmd_qlist,          0 },
[0298d11]1126        { "register",       1, cmd_register,       0 },
[dbb0ce3]1127        { "remove",         1, cmd_remove,         0 },
[0298d11]1128        { "rename",         2, cmd_rename,         0 },
[6c56f42]1129        { "save",           0, cmd_save,           0 },
[0298d11]1130        { "set",            0, cmd_set,            0 },
[9d4352c]1131        { "transfer",       0, cmd_transfer,       0 },
[0298d11]1132        { "yes",            0, cmd_yesno,          0 },
1133        { NULL }
1134};
Note: See TracBrowser for help on using the repository browser.