source: root_commands.c @ 71dc854

Last change on this file since 71dc854 was 71dc854, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-12-10T22:33:08Z

Fixed "set xxx" syntax (it showed all settings instead of just xxx).

  • Property mode set to 100644
File size: 22.3 KB
RevLine 
[b7d3cc34]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* User manager (root) commands                                         */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25
26#define BITLBEE_CORE
27#include "commands.h"
28#include "crypting.h"
29#include "bitlbee.h"
30#include "help.h"
31
32#include <string.h>
33
[f73b969]34void root_command_string( irc_t *irc, user_t *u, char *command, int flags )
[7e563ed]35{
36        char *cmd[IRC_MAX_ARGS];
37        char *s;
38        int k;
39        char q = 0;
40       
41        memset( cmd, 0, sizeof( cmd ) );
42        cmd[0] = command;
43        k = 1;
44        for( s = command; *s && k < ( IRC_MAX_ARGS - 1 ); s ++ )
45                if( *s == ' ' && !q )
46                {
47                        *s = 0;
48                        while( *++s == ' ' );
49                        if( *s == '"' || *s == '\'' )
50                        {
51                                q = *s;
52                                s ++;
53                        }
54                        if( *s )
55                        {
56                                cmd[k++] = s;
57                                s --;
58                        }
[619a681]59                        else
60                        {
61                                break;
62                        }
[7e563ed]63                }
[79c6f9f]64                else if( *s == '\\' && ( ( !q && s[1] ) || ( q && q == s[1] ) ) )
65                {
66                        char *cpy;
67                       
68                        for( cpy = s; *cpy; cpy ++ )
69                                cpy[0] = cpy[1];
70                }
[7e563ed]71                else if( *s == q )
72                {
73                        q = *s = 0;
74                }
75        cmd[k] = NULL;
76       
[f73b969]77        root_command( irc, cmd );
[7e563ed]78}
79
[f73b969]80void root_command( irc_t *irc, char *cmd[] )
[7e563ed]81{       
82        int i;
83       
84        if( !cmd[0] )
[f73b969]85                return;
[7e563ed]86       
87        for( i = 0; commands[i].command; i++ )
88                if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 )
89                {
90                        if( !cmd[commands[i].required_parameters] )
91                        {
92                                irc_usermsg( irc, "Not enough parameters given (need %d)", commands[i].required_parameters );
[f73b969]93                                return;
[7e563ed]94                        }
95                        commands[i].execute( irc, cmd );
[f73b969]96                        return;
[7e563ed]97                }
98       
99        irc_usermsg( irc, "Unknown command: %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[0] );
100}
101
[f73b969]102static void cmd_help( irc_t *irc, char **cmd )
[b7d3cc34]103{
104        char param[80];
105        int i;
106        char *s;
107       
108        memset( param, 0, sizeof(param) );
109        for ( i = 1; (cmd[i] != NULL && ( strlen(param) < (sizeof(param)-1) ) ); i++ ) {
110                if ( i != 1 )   // prepend space except for the first parameter
111                        strcat(param, " ");
112                strncat( param, cmd[i], sizeof(param) - strlen(param) - 1 );
113        }
114
115        s = help_get( &(global.help), param );
116        if( !s ) s = help_get( &(global.help), "" );
117       
118        if( s )
119        {
120                irc_usermsg( irc, "%s", s );
121                g_free( s );
122        }
123        else
124        {
125                irc_usermsg( irc, "Error opening helpfile." );
126        }
127}
128
[90bbb0e]129static void cmd_account( irc_t *irc, char **cmd );
130
[f73b969]131static void cmd_identify( irc_t *irc, char **cmd )
[b7d3cc34]132{
[ab49fdc]133        storage_status_t status = storage_load( irc->nick, cmd[1], irc );
[90bbb0e]134        char *account_on[] = { "account", "on", NULL };
[b7d3cc34]135       
[09adf08]136        switch (status) {
137        case STORAGE_INVALID_PASSWORD:
[b7d3cc34]138                irc_usermsg( irc, "Incorrect password" );
[09adf08]139                break;
140        case STORAGE_NO_SUCH_USER:
[b7d3cc34]141                irc_usermsg( irc, "The nick is (probably) not registered" );
[09adf08]142                break;
143        case STORAGE_OK:
[6ee9d2d]144                irc_usermsg( irc, "Password accepted, settings and accounts loaded" );
[238f828]145                irc_umode_set( irc, "+R", 1 );
[d5ccd83]146                if( set_getbool( &irc->set, "auto_connect" ) )
[90bbb0e]147                        cmd_account( irc, account_on );
[09adf08]148                break;
[c121f89]149        case STORAGE_OTHER_ERROR:
[09adf08]150        default:
[c121f89]151                irc_usermsg( irc, "Unknown error while loading configuration" );
[09adf08]152                break;
[b7d3cc34]153        }
154}
155
[f73b969]156static void cmd_register( irc_t *irc, char **cmd )
[b7d3cc34]157{
158        if( global.conf->authmode == AUTHMODE_REGISTERED )
159        {
160                irc_usermsg( irc, "This server does not allow registering new accounts" );
[f73b969]161                return;
[b7d3cc34]162        }
[1ee6c18]163
[7cad7b4]164        irc_setpass( irc, cmd[1] );
[ab49fdc]165        switch( storage_save( irc, FALSE )) {
[a1f17d4]166                case STORAGE_ALREADY_EXISTS:
167                        irc_usermsg( irc, "Nick is already registered" );
168                        break;
169                       
170                case STORAGE_OK:
[0383943]171                        irc_usermsg( irc, "Account successfully created" );
[79e826a]172                        irc->status |= USTATUS_IDENTIFIED;
[238f828]173                        irc_umode_set( irc, "+R", 1 );
[a1f17d4]174                        break;
175
176                default:
177                        irc_usermsg( irc, "Error registering" );
178                        break;
[b7d3cc34]179        }
180}
181
[f73b969]182static void cmd_drop( irc_t *irc, char **cmd )
[b7d3cc34]183{
[a1f17d4]184        storage_status_t status;
185       
[ab49fdc]186        status = storage_remove (irc->nick, cmd[1]);
[a1f17d4]187        switch (status) {
188        case STORAGE_NO_SUCH_USER:
[b7d3cc34]189                irc_usermsg( irc, "That account does not exist" );
[f73b969]190                break;
[a1f17d4]191        case STORAGE_INVALID_PASSWORD:
[1ee6c18]192                irc_usermsg( irc, "Password invalid" );
[f73b969]193                break;
[a1f17d4]194        case STORAGE_OK:
[7cad7b4]195                irc_setpass( irc, NULL );
[79e826a]196                irc->status &= ~USTATUS_IDENTIFIED;
[238f828]197                irc_umode_set( irc, "-R", 1 );
[a1f17d4]198                irc_usermsg( irc, "Account `%s' removed", irc->nick );
[f73b969]199                break;
[a1f17d4]200        default:
201                irc_usermsg( irc, "Error: '%d'", status );
[f73b969]202                break;
[b7d3cc34]203        }
204}
205
[f73b969]206static void cmd_account( irc_t *irc, char **cmd )
[b7d3cc34]207{
208        account_t *a;
209       
[3af70b0]210        if( global.conf->authmode == AUTHMODE_REGISTERED && !( irc->status & USTATUS_IDENTIFIED ) )
[b7d3cc34]211        {
212                irc_usermsg( irc, "This server only accepts registered users" );
[f73b969]213                return;
[b7d3cc34]214        }
215       
216        if( g_strcasecmp( cmd[1], "add" ) == 0 )
217        {
[7b23afd]218                struct prpl *prpl;
[b7d3cc34]219               
220                if( cmd[2] == NULL || cmd[3] == NULL || cmd[4] == NULL )
221                {
222                        irc_usermsg( irc, "Not enough parameters" );
[f73b969]223                        return;
[b7d3cc34]224                }
225               
[7b23afd]226                prpl = find_protocol(cmd[2]);
[b7d3cc34]227               
[7b23afd]228                if( prpl == NULL )
[b7d3cc34]229                {
230                        irc_usermsg( irc, "Unknown protocol" );
[f73b969]231                        return;
[b7d3cc34]232                }
233
[7b23afd]234                a = account_add( irc, prpl, cmd[3], cmd[4] );
[b7d3cc34]235                if( cmd[5] )
[5100caa]236                        set_setstr( &a->set, "server", cmd[5] );
[b7d3cc34]237               
238                irc_usermsg( irc, "Account successfully added" );
239        }
240        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
241        {
242                if( !cmd[2] )
243                {
244                        irc_usermsg( irc, "Not enough parameters given (need %d)", 2 );
245                }
246                else if( !( a = account_get( irc, cmd[2] ) ) )
247                {
248                        irc_usermsg( irc, "Invalid account" );
249                }
[0da65d5]250                else if( a->ic )
[b7d3cc34]251                {
252                        irc_usermsg( irc, "Account is still logged in, can't delete" );
253                }
254                else
255                {
256                        account_del( irc, a );
257                        irc_usermsg( irc, "Account deleted" );
258                }
259        }
260        else if( g_strcasecmp( cmd[1], "list" ) == 0 )
261        {
262                int i = 0;
263               
[e6e1f18]264                if( strchr( irc->umode, 'b' ) )
265                        irc_usermsg( irc, "Account list:" );
266               
[b7d3cc34]267                for( a = irc->accounts; a; a = a->next )
268                {
269                        char *con;
270                       
[0da65d5]271                        if( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) )
[b7d3cc34]272                                con = " (connected)";
[0da65d5]273                        else if( a->ic )
[b7d3cc34]274                                con = " (connecting)";
275                        else if( a->reconnect )
276                                con = " (awaiting reconnect)";
277                        else
278                                con = "";
279                       
[7b23afd]280                        irc_usermsg( irc, "%2d. %s, %s%s", i, a->prpl->name, a->user, con );
[b7d3cc34]281                       
282                        i ++;
283                }
284                irc_usermsg( irc, "End of account list" );
285        }
286        else if( g_strcasecmp( cmd[1], "on" ) == 0 )
287        {
288                if( cmd[2] )
289                {
290                        if( ( a = account_get( irc, cmd[2] ) ) )
291                        {
[0da65d5]292                                if( a->ic )
[b7d3cc34]293                                {
294                                        irc_usermsg( irc, "Account already online" );
[f73b969]295                                        return;
[b7d3cc34]296                                }
297                                else
298                                {
299                                        account_on( irc, a );
300                                }
301                        }
302                        else
303                        {
304                                irc_usermsg( irc, "Invalid account" );
[f73b969]305                                return;
[b7d3cc34]306                        }
307                }
308                else
309                {
310                        if ( irc->accounts ) {
311                                irc_usermsg( irc, "Trying to get all accounts connected..." );
312                       
313                                for( a = irc->accounts; a; a = a->next )
[0da65d5]314                                        if( !a->ic && a->auto_connect )
[b7d3cc34]315                                                account_on( irc, a );
316                        } 
317                        else
318                        {
319                                irc_usermsg( irc, "No accounts known. Use 'account add' to add one." );
320                        }
321                }
322        }
323        else if( g_strcasecmp( cmd[1], "off" ) == 0 )
324        {
325                if( !cmd[2] )
326                {
327                        irc_usermsg( irc, "Deactivating all active (re)connections..." );
328                       
329                        for( a = irc->accounts; a; a = a->next )
330                        {
[0da65d5]331                                if( a->ic )
[b7d3cc34]332                                        account_off( irc, a );
333                                else if( a->reconnect )
334                                        cancel_auto_reconnect( a );
335                        }
336                }
337                else if( ( a = account_get( irc, cmd[2] ) ) )
338                {
[0da65d5]339                        if( a->ic )
[b7d3cc34]340                        {
341                                account_off( irc, a );
342                        }
343                        else if( a->reconnect )
344                        {
345                                cancel_auto_reconnect( a );
346                                irc_usermsg( irc, "Reconnect cancelled" );
347                        }
348                        else
349                        {
350                                irc_usermsg( irc, "Account already offline" );
[f73b969]351                                return;
[b7d3cc34]352                        }
353                }
354                else
355                {
356                        irc_usermsg( irc, "Invalid account" );
[f73b969]357                        return;
[b7d3cc34]358                }
359        }
[5100caa]360        else if( g_strcasecmp( cmd[1], "set" ) == 0 )
361        {
362                char *acc_handle, *set_name = NULL, *tmp;
363               
364                if( !cmd[2] )
365                {
366                        irc_usermsg( irc, "Not enough parameters given (need %d)", 2 );
367                        return;
368                }
369               
[cd428e4]370                if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
371                        acc_handle = g_strdup( cmd[3] );
372                else
373                        acc_handle = g_strdup( cmd[2] );
374               
[5100caa]375                if( ( tmp = strchr( acc_handle, '/' ) ) )
376                {
377                        *tmp = 0;
378                        set_name = tmp + 1;
379                }
380               
[cd428e4]381                if( ( a = account_get( irc, acc_handle ) ) == NULL )
[5100caa]382                {
[75a4b85]383                        g_free( acc_handle );
[5100caa]384                        irc_usermsg( irc, "Invalid account" );
385                        return;
386                }
387               
[9334cc2]388                if( cmd[3] && set_name )
[5100caa]389                {
390                        set_t *s = set_find( &a->set, set_name );
391                       
[0da65d5]392                        if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY )
[5100caa]393                        {
[75a4b85]394                                g_free( acc_handle );
[911f2eb]395                                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" );
396                                return;
397                        }
[0da65d5]398                        else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY )
[911f2eb]399                        {
[75a4b85]400                                g_free( acc_handle );
[911f2eb]401                                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" );
[5100caa]402                                return;
403                        }
404                       
[71dc854]405                        if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
[cd428e4]406                                set_reset( &a->set, set_name );
407                        else
408                                set_setstr( &a->set, set_name, cmd[3] );
[5100caa]409                }
410                if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */
411                {
412                        char *s = set_getstr( &a->set, set_name );
413                        if( s )
414                                irc_usermsg( irc, "%s = `%s'", set_name, s );
415                        else
416                                irc_usermsg( irc, "%s is empty", set_name );
417                }
418                else
419                {
420                        set_t *s = a->set;
421                        while( s )
422                        {
423                                if( s->value || s->def )
[cd428e4]424                                        irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def );
[5100caa]425                                else
426                                        irc_usermsg( irc, "%s is empty", s->key );
427                                s = s->next;
428                        }
429                }
430               
431                g_free( acc_handle );
432        }
[b7d3cc34]433        else
434        {
[5c09a59]435                irc_usermsg( irc, "Unknown command: account %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[1] );
[b7d3cc34]436        }
437}
438
[f73b969]439static void cmd_add( irc_t *irc, char **cmd )
[b7d3cc34]440{
441        account_t *a;
[f0cb961]442        int add_on_server = 1;
[f8de26f]443       
444        if( g_strcasecmp( cmd[1], "-tmp" ) == 0 )
445        {
[f0cb961]446                add_on_server = 0;
[f8de26f]447                cmd ++;         /* So evil... :-D */
448        }
[b7d3cc34]449       
450        if( !( a = account_get( irc, cmd[1] ) ) )
451        {
452                irc_usermsg( irc, "Invalid account" );
[f73b969]453                return;
[b7d3cc34]454        }
[0da65d5]455        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]456        {
457                irc_usermsg( irc, "That account is not on-line" );
[f73b969]458                return;
[b7d3cc34]459        }
460       
461        if( cmd[3] )
462        {
463                if( !nick_ok( cmd[3] ) )
464                {
465                        irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] );
[f73b969]466                        return;
[b7d3cc34]467                }
468                else if( user_find( irc, cmd[3] ) )
469                {
470                        irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] );
[f73b969]471                        return;
[b7d3cc34]472                }
473                else
474                {
[5b52a48]475                        nick_set( a, cmd[2], cmd[3] );
[b7d3cc34]476                }
477        }
[f8de26f]478       
479        /* By making this optional, you can talk to people without having to
480           add them to your *real* (server-side) contact list. */
[f0cb961]481        if( add_on_server )
[0da65d5]482                a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL );
[b7d3cc34]483       
[f0cb961]484        /* add_buddy( a->ic, NULL, cmd[2], cmd[2] ); */
485       
486        irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2]  );
[b7d3cc34]487}
488
[f73b969]489static void cmd_info( irc_t *irc, char **cmd )
[b7d3cc34]490{
[0da65d5]491        struct im_connection *ic;
[b7d3cc34]492        account_t *a;
493       
494        if( !cmd[2] )
495        {
496                user_t *u = user_find( irc, cmd[1] );
[0da65d5]497                if( !u || !u->ic )
[b7d3cc34]498                {
499                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]500                        return;
[b7d3cc34]501                }
[0da65d5]502                ic = u->ic;
[b7d3cc34]503                cmd[2] = u->handle;
504        }
505        else if( !( a = account_get( irc, cmd[1] ) ) )
506        {
507                irc_usermsg( irc, "Invalid account" );
[f73b969]508                return;
[b7d3cc34]509        }
[0da65d5]510        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]511        {
512                irc_usermsg( irc, "That account is not on-line" );
[f73b969]513                return;
[b7d3cc34]514        }
515       
[0da65d5]516        if( !ic->acc->prpl->get_info )
[b7d3cc34]517        {
518                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
519        }
[f73b969]520        else
521        {
[0da65d5]522                ic->acc->prpl->get_info( ic, cmd[2] );
[f73b969]523        }
[b7d3cc34]524}
525
[f73b969]526static void cmd_rename( irc_t *irc, char **cmd )
[b7d3cc34]527{
528        user_t *u;
529       
530        if( g_strcasecmp( cmd[1], irc->nick ) == 0 )
531        {
532                irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] );
533        }
[f73b969]534        else if( user_find( irc, cmd[2] ) && ( nick_cmp( cmd[1], cmd[2] ) != 0 ) )
[b7d3cc34]535        {
536                irc_usermsg( irc, "Nick `%s' already exists", cmd[2] );
537        }
[f73b969]538        else if( !nick_ok( cmd[2] ) )
[b7d3cc34]539        {
540                irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
541        }
[f73b969]542        else if( !( u = user_find( irc, cmd[1] ) ) )
[b7d3cc34]543        {
544                irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
545        }
[f73b969]546        else
[b7d3cc34]547        {
[f73b969]548                user_rename( irc, cmd[1], cmd[2] );
549                irc_write( irc, ":%s!%s@%s NICK %s", cmd[1], u->user, u->host, cmd[2] );
550                if( g_strcasecmp( cmd[1], irc->mynick ) == 0 )
551                {
552                        g_free( irc->mynick );
553                        irc->mynick = g_strdup( cmd[2] );
554                }
555                else if( u->send_handler == buddy_send_handler )
556                {
[0da65d5]557                        nick_set( u->ic->acc, u->handle, cmd[2] );
[f73b969]558                }
559               
560                irc_usermsg( irc, "Nick successfully changed" );
[b7d3cc34]561        }
562}
563
[f73b969]564static void cmd_remove( irc_t *irc, char **cmd )
[b7d3cc34]565{
566        user_t *u;
567        char *s;
568       
[0da65d5]569        if( !( u = user_find( irc, cmd[1] ) ) || !u->ic )
[b7d3cc34]570        {
571                irc_usermsg( irc, "Buddy `%s' not found", cmd[1] );
[f73b969]572                return;
[b7d3cc34]573        }
574        s = g_strdup( u->handle );
575       
[0da65d5]576        u->ic->acc->prpl->remove_buddy( u->ic, u->handle, NULL );
577        nick_del( u->ic->acc, u->handle );
[b7d3cc34]578        user_del( irc, cmd[1] );
579       
580        irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] );
581        g_free( s );
582       
[f73b969]583        return;
[b7d3cc34]584}
585
[f73b969]586static void cmd_block( irc_t *irc, char **cmd )
[b7d3cc34]587{
[0da65d5]588        struct im_connection *ic;
[b7d3cc34]589        account_t *a;
590       
[0da65d5]591        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
[87b6a3e]592        {
593                char *format;
594                GSList *l;
595               
596                if( strchr( irc->umode, 'b' ) != NULL )
597                        format = "%s\t%s";
598                else
[57ef864]599                        format = "%-32.32s  %-16.16s";
[87b6a3e]600               
601                irc_usermsg( irc, format, "Handle", "Nickname" );
[0da65d5]602                for( l = a->ic->deny; l; l = l->next )
[87b6a3e]603                {
[0da65d5]604                        user_t *u = user_findhandle( a->ic, l->data );
[87b6a3e]605                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
606                }
607                irc_usermsg( irc, "End of list." );
608               
609                return;
610        }
611        else if( !cmd[2] )
[b7d3cc34]612        {
613                user_t *u = user_find( irc, cmd[1] );
[0da65d5]614                if( !u || !u->ic )
[b7d3cc34]615                {
616                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]617                        return;
[b7d3cc34]618                }
[0da65d5]619                ic = u->ic;
[b7d3cc34]620                cmd[2] = u->handle;
621        }
622        else if( !( a = account_get( irc, cmd[1] ) ) )
623        {
624                irc_usermsg( irc, "Invalid account" );
[f73b969]625                return;
[b7d3cc34]626        }
[0da65d5]627        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]628        {
629                irc_usermsg( irc, "That account is not on-line" );
[f73b969]630                return;
[b7d3cc34]631        }
632       
[0da65d5]633        if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit )
[b7d3cc34]634        {
635                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
636        }
637        else
638        {
[84b045d]639                imc_rem_allow( ic, cmd[2] );
640                imc_add_block( ic, cmd[2] );
[da3b536]641                irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] );
[b7d3cc34]642        }
643}
644
[f73b969]645static void cmd_allow( irc_t *irc, char **cmd )
[b7d3cc34]646{
[0da65d5]647        struct im_connection *ic;
[b7d3cc34]648        account_t *a;
649       
[0da65d5]650        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
[87b6a3e]651        {
652                char *format;
653                GSList *l;
654               
655                if( strchr( irc->umode, 'b' ) != NULL )
656                        format = "%s\t%s";
657                else
[57ef864]658                        format = "%-32.32s  %-16.16s";
[87b6a3e]659               
660                irc_usermsg( irc, format, "Handle", "Nickname" );
[0da65d5]661                for( l = a->ic->permit; l; l = l->next )
[87b6a3e]662                {
[0da65d5]663                        user_t *u = user_findhandle( a->ic, l->data );
[87b6a3e]664                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
665                }
666                irc_usermsg( irc, "End of list." );
667               
668                return;
669        }
670        else if( !cmd[2] )
[b7d3cc34]671        {
672                user_t *u = user_find( irc, cmd[1] );
[0da65d5]673                if( !u || !u->ic )
[b7d3cc34]674                {
675                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]676                        return;
[b7d3cc34]677                }
[0da65d5]678                ic = u->ic;
[b7d3cc34]679                cmd[2] = u->handle;
680        }
681        else if( !( a = account_get( irc, cmd[1] ) ) )
682        {
683                irc_usermsg( irc, "Invalid account" );
[f73b969]684                return;
[b7d3cc34]685        }
[0da65d5]686        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]687        {
688                irc_usermsg( irc, "That account is not on-line" );
[f73b969]689                return;
[b7d3cc34]690        }
691       
[0da65d5]692        if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit )
[b7d3cc34]693        {
694                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
695        }
696        else
697        {
[84b045d]698                imc_rem_block( ic, cmd[2] );
699                imc_add_allow( ic, cmd[2] );
[b7d3cc34]700               
[da3b536]701                irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] );
[b7d3cc34]702        }
703}
704
[f73b969]705static void cmd_yesno( irc_t *irc, char **cmd )
[b7d3cc34]706{
707        query_t *q = NULL;
708        int numq = 0;
709       
710        if( irc->queries == NULL )
711        {
712                irc_usermsg( irc, "Did I ask you something?" );
[f73b969]713                return;
[b7d3cc34]714        }
715       
716        /* If there's an argument, the user seems to want to answer another question than the
717           first/last (depending on the query_order setting) one. */
718        if( cmd[1] )
719        {
720                if( sscanf( cmd[1], "%d", &numq ) != 1 )
721                {
722                        irc_usermsg( irc, "Invalid query number" );
[f73b969]723                        return;
[b7d3cc34]724                }
725               
726                for( q = irc->queries; q; q = q->next, numq -- )
727                        if( numq == 0 )
728                                break;
729               
730                if( !q )
731                {
732                        irc_usermsg( irc, "Uhm, I never asked you something like that..." );
[f73b969]733                        return;
[b7d3cc34]734                }
735        }
736       
737        if( g_strcasecmp( cmd[0], "yes" ) == 0 )
738                query_answer( irc, q, 1 );
739        else if( g_strcasecmp( cmd[0], "no" ) == 0 )
740                query_answer( irc, q, 0 );
741}
742
[f73b969]743static void cmd_set( irc_t *irc, char **cmd )
[b7d3cc34]744{
[71dc854]745        char *set_name = cmd[1];
[cd428e4]746       
[b7d3cc34]747        if( cmd[1] && cmd[2] )
748        {
[71dc854]749                if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
[cd428e4]750                {
751                        set_reset( &irc->set, cmd[2] );
752                        set_name = cmd[2];
753                }
754                else
755                {
756                        set_setstr( &irc->set, cmd[1], cmd[2] );
757                }
[b7d3cc34]758        }
[56f260a]759        if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */
[b7d3cc34]760        {
[cd428e4]761                char *s = set_getstr( &irc->set, set_name );
762                if( s )
763                        irc_usermsg( irc, "%s = `%s'", set_name, s );
[5100caa]764                else
[cd428e4]765                        irc_usermsg( irc, "%s is empty", set_name );
[b7d3cc34]766        }
767        else
768        {
769                set_t *s = irc->set;
770                while( s )
771                {
772                        if( s->value || s->def )
[cd428e4]773                                irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def );
[5100caa]774                        else
775                                irc_usermsg( irc, "%s is empty", s->key );
[b7d3cc34]776                        s = s->next;
777                }
778        }
779}
780
[f73b969]781static void cmd_save( irc_t *irc, char **cmd )
[b7d3cc34]782{
[ab49fdc]783        if( storage_save( irc, TRUE ) == STORAGE_OK )
[b7d3cc34]784                irc_usermsg( irc, "Configuration saved" );
785        else
786                irc_usermsg( irc, "Configuration could not be saved!" );
787}
788
[f73b969]789static void cmd_blist( irc_t *irc, char **cmd )
[b7d3cc34]790{
791        int online = 0, away = 0, offline = 0;
792        user_t *u;
[aefa533e]793        char s[256];
794        char *format;
[b7d3cc34]795        int n_online = 0, n_away = 0, n_offline = 0;
796       
797        if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 )
798                online = offline = away = 1;
799        else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 )
800                offline = 1;
801        else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 )
802                away = 1;
803        else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 )
804                online = 1;
805        else
806                online =  away = 1;
807       
[aefa533e]808        if( strchr( irc->umode, 'b' ) != NULL )
809                format = "%s\t%s\t%s";
810        else
811                format = "%-16.16s  %-40.40s  %s";
812       
813        irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" );
[b7d3cc34]814       
[0da65d5]815        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && !u->away )
[b7d3cc34]816        {
[aefa533e]817                if( online == 1 )
818                {
[0da65d5]819                        g_snprintf( s, sizeof( s ) - 1, "%s@%s (%s)", u->user, u->host, u->ic->acc->prpl->name );
[aefa533e]820                        irc_usermsg( irc, format, u->nick, s, "Online" );
821                }
822               
[b7d3cc34]823                n_online ++;
824        }
825
[0da65d5]826        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && u->away )
[b7d3cc34]827        {
[aefa533e]828                if( away == 1 )
829                {
[0da65d5]830                        g_snprintf( s, sizeof( s ) - 1, "%s@%s (%s)", u->user, u->host, u->ic->acc->prpl->name );
[aefa533e]831                        irc_usermsg( irc, format, u->nick, s, u->away );
832                }
[b7d3cc34]833                n_away ++;
834        }
835       
[0da65d5]836        for( u = irc->users; u; u = u->next ) if( u->ic && !u->online )
[b7d3cc34]837        {
[aefa533e]838                if( offline == 1 )
839                {
[0da65d5]840                        g_snprintf( s, sizeof( s ) - 1, "%s@%s (%s)", u->user, u->host, u->ic->acc->prpl->name );
[aefa533e]841                        irc_usermsg( irc, format, u->nick, s, "Offline" );
842                }
[b7d3cc34]843                n_offline ++;
844        }
845       
[aa5ac01]846        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
[b7d3cc34]847}
848
[f73b969]849static void cmd_nick( irc_t *irc, char **cmd ) 
[b7d3cc34]850{
851        account_t *a;
852
853        if( !cmd[1] || !( a = account_get( irc, cmd[1] ) ) )
854        {
855                irc_usermsg( irc, "Invalid account");
856        }
[0da65d5]857        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]858        {
859                irc_usermsg( irc, "That account is not on-line" );
860        }
861        else if ( !cmd[2] ) 
862        {
[0da65d5]863                irc_usermsg( irc, "Your name is `%s'" , a->ic->displayname ? a->ic->displayname : "NULL" );
[b7d3cc34]864        }
[0da65d5]865        else if ( !a->prpl->set_my_name ) 
[b7d3cc34]866        {
867                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
868        }
869        else
870        {
871                irc_usermsg( irc, "Setting your name to `%s'", cmd[2] );
872               
[0da65d5]873                a->prpl->set_my_name( a->ic, cmd[2] );
[b7d3cc34]874        }
875}
876
[f73b969]877static void cmd_qlist( irc_t *irc, char **cmd )
[b7d3cc34]878{
879        query_t *q = irc->queries;
880        int num;
881       
882        if( !q )
883        {
884                irc_usermsg( irc, "There are no pending questions." );
[f73b969]885                return;
[b7d3cc34]886        }
887       
888        irc_usermsg( irc, "Pending queries:" );
889       
890        for( num = 0; q; q = q->next, num ++ )
[0da65d5]891                if( q->ic ) /* Not necessary yet, but it might come later */
[c2fb3809]892                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question );
[5c09a59]893                else
894                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
[b7d3cc34]895}
896
[fa29d093]897static void cmd_join_chat( irc_t *irc, char **cmd )
898{
899        account_t *a;
[0da65d5]900        struct im_connection *ic;
[fa29d093]901        char *chat, *channel, *nick = NULL, *password = NULL;
[0da65d5]902        struct groupchat *c;
[fa29d093]903       
904        if( !( a = account_get( irc, cmd[1] ) ) )
905        {
906                irc_usermsg( irc, "Invalid account" );
907                return;
908        }
[0da65d5]909        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[fa29d093]910        {
911                irc_usermsg( irc, "That account is not on-line" );
912                return;
913        }
914        else if( a->prpl->chat_join == NULL )
915        {
916                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
917                return;
918        }
[0da65d5]919        ic = a->ic;
[fa29d093]920       
921        chat = cmd[2];
922        if( cmd[3] )
923        {
[9da0bbf]924                if( cmd[3][0] != '#' && cmd[3][0] != '&' )
[7bf4326]925                        channel = g_strdup_printf( "&%s", cmd[3] );
926                else
927                        channel = g_strdup( cmd[3] );
[fa29d093]928        }
929        else
930        {
931                char *s;
932               
[0e7ab64]933                channel = g_strdup_printf( "&%s", chat );
[fa29d093]934                if( ( s = strchr( channel, '@' ) ) )
935                        *s = 0;
936        }
937        if( cmd[3] && cmd[4] )
938                nick = cmd[4];
[e35d1a1]939        else
940                nick = irc->nick;
[fa29d093]941        if( cmd[3] && cmd[4] && cmd[5] )
942                password = cmd[5];
943       
[7bf4326]944        if( !nick_ok( channel + 1 ) )
[0e7ab64]945        {
946                irc_usermsg( irc, "Invalid channel name: %s", channel );
947                g_free( channel );
948                return;
949        }
950        else if( g_strcasecmp( channel, irc->channel ) == 0 || irc_chat_by_channel( irc, channel ) )
951        {
952                irc_usermsg( irc, "Channel already exists: %s", channel );
953                g_free( channel );
954                return;
955        }
[fa29d093]956       
[e35d1a1]957        if( ( c = a->prpl->chat_join( ic, chat, nick, password ) ) )
958        {
959                g_free( c->channel );
960                c->channel = channel;
961        }
962        else
963        {
964                irc_usermsg( irc, "Tried to join chat, not sure if this was successful" );
965                g_free( channel );
966        }
[fa29d093]967}
968
[0298d11]969const command_t commands[] = {
970        { "help",           0, cmd_help,           0 }, 
971        { "identify",       1, cmd_identify,       0 },
972        { "register",       1, cmd_register,       0 },
973        { "drop",           1, cmd_drop,           0 },
974        { "account",        1, cmd_account,        0 },
975        { "add",            2, cmd_add,            0 },
976        { "info",           1, cmd_info,           0 },
977        { "rename",         2, cmd_rename,         0 },
978        { "remove",         1, cmd_remove,         0 },
979        { "block",          1, cmd_block,          0 },
980        { "allow",          1, cmd_allow,          0 },
981        { "save",           0, cmd_save,           0 },
982        { "set",            0, cmd_set,            0 },
983        { "yes",            0, cmd_yesno,          0 },
984        { "no",             0, cmd_yesno,          0 },
985        { "blist",          0, cmd_blist,          0 },
986        { "nick",           1, cmd_nick,           0 },
987        { "qlist",          0, cmd_qlist,          0 },
[fa29d093]988        { "join_chat",      2, cmd_join_chat,      0 },
[0298d11]989        { NULL }
990};
Note: See TracBrowser for help on using the repository browser.