source: root_commands.c @ 8a08d92

Last change on this file since 8a08d92 was 8a08d92, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-06-24T21:44:39Z

Fixed crash on setting an unknown setting.

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