source: root_commands.c @ 8dbe021f

Last change on this file since 8dbe021f was 8dbe021f, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-04-04T23:13:07Z

Don't pass an account_t pointer directly to query_add() since query_del()
wants to free it! Passing an indirect pointer instead.

  • Property mode set to 100644
File size: 23.5 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:
[30ce1ce]201                irc_usermsg( irc, "Error: `%d'", status );
[f73b969]202                break;
[b7d3cc34]203        }
204}
205
[7f421d6]206void cmd_account_del_yes( gpointer w, void *data )
207{
[8dbe021f]208        account_t **aptr = data;
209        irc_t *irc = (*aptr)->irc;
[7f421d6]210       
[8dbe021f]211        if( (*aptr)->ic )
[7f421d6]212        {
213                irc_usermsg( irc, "Account is still logged in, can't delete" );
214        }
215        else
216        {
[8dbe021f]217                account_del( irc, (*aptr) );
[7f421d6]218                irc_usermsg( irc, "Account deleted" );
219        }
[8dbe021f]220        g_free( aptr );
[7f421d6]221}
222
223void cmd_account_del_no( gpointer w, void *data )
224{
[8dbe021f]225        g_free( data );
[7f421d6]226}
227
[f73b969]228static void cmd_account( irc_t *irc, char **cmd )
[b7d3cc34]229{
230        account_t *a;
231       
[3af70b0]232        if( global.conf->authmode == AUTHMODE_REGISTERED && !( irc->status & USTATUS_IDENTIFIED ) )
[b7d3cc34]233        {
234                irc_usermsg( irc, "This server only accepts registered users" );
[f73b969]235                return;
[b7d3cc34]236        }
237       
238        if( g_strcasecmp( cmd[1], "add" ) == 0 )
239        {
[7b23afd]240                struct prpl *prpl;
[b7d3cc34]241               
242                if( cmd[2] == NULL || cmd[3] == NULL || cmd[4] == NULL )
243                {
244                        irc_usermsg( irc, "Not enough parameters" );
[f73b969]245                        return;
[b7d3cc34]246                }
247               
[7b23afd]248                prpl = find_protocol(cmd[2]);
[b7d3cc34]249               
[7b23afd]250                if( prpl == NULL )
[b7d3cc34]251                {
252                        irc_usermsg( irc, "Unknown protocol" );
[f73b969]253                        return;
[b7d3cc34]254                }
255
[7b23afd]256                a = account_add( irc, prpl, cmd[3], cmd[4] );
[b7d3cc34]257                if( cmd[5] )
[30ce1ce]258                {
259                        irc_usermsg( irc, "Warning: Passing a servername/other flags to `account add' "
260                                          "is now deprecated. Use `account set' instead." );
[5100caa]261                        set_setstr( &a->set, "server", cmd[5] );
[30ce1ce]262                }
[b7d3cc34]263               
264                irc_usermsg( irc, "Account successfully added" );
265        }
266        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
267        {
268                if( !cmd[2] )
269                {
270                        irc_usermsg( irc, "Not enough parameters given (need %d)", 2 );
271                }
272                else if( !( a = account_get( irc, cmd[2] ) ) )
273                {
274                        irc_usermsg( irc, "Invalid account" );
275                }
[0da65d5]276                else if( a->ic )
[b7d3cc34]277                {
278                        irc_usermsg( irc, "Account is still logged in, can't delete" );
279                }
280                else
281                {
[8dbe021f]282                        account_t **aptr;
[7f421d6]283                        char *msg;
284                       
[8dbe021f]285                        aptr = g_malloc( sizeof( aptr ) );
286                        *aptr = a;
287                       
[7f421d6]288                        msg = g_strdup_printf( "If you remove this account (%s(%s)), BitlBee will "
289                                               "also forget all your saved nicknames. If you want "
290                                               "to change your username/password, use the `account "
291                                               "set' command. Are you sure you want to delete this "
292                                               "account?", a->prpl->name, a->user );
[8dbe021f]293                        query_add( irc, NULL, msg, cmd_account_del_yes, cmd_account_del_no, aptr );
[7f421d6]294                        g_free( msg );
[b7d3cc34]295                }
296        }
297        else if( g_strcasecmp( cmd[1], "list" ) == 0 )
298        {
299                int i = 0;
300               
[e6e1f18]301                if( strchr( irc->umode, 'b' ) )
302                        irc_usermsg( irc, "Account list:" );
303               
[b7d3cc34]304                for( a = irc->accounts; a; a = a->next )
305                {
306                        char *con;
307                       
[0da65d5]308                        if( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) )
[b7d3cc34]309                                con = " (connected)";
[0da65d5]310                        else if( a->ic )
[b7d3cc34]311                                con = " (connecting)";
312                        else if( a->reconnect )
313                                con = " (awaiting reconnect)";
314                        else
315                                con = "";
316                       
[7b23afd]317                        irc_usermsg( irc, "%2d. %s, %s%s", i, a->prpl->name, a->user, con );
[b7d3cc34]318                       
319                        i ++;
320                }
321                irc_usermsg( irc, "End of account list" );
322        }
323        else if( g_strcasecmp( cmd[1], "on" ) == 0 )
324        {
325                if( cmd[2] )
326                {
327                        if( ( a = account_get( irc, cmd[2] ) ) )
328                        {
[0da65d5]329                                if( a->ic )
[b7d3cc34]330                                {
331                                        irc_usermsg( irc, "Account already online" );
[f73b969]332                                        return;
[b7d3cc34]333                                }
334                                else
335                                {
336                                        account_on( irc, a );
337                                }
338                        }
339                        else
340                        {
341                                irc_usermsg( irc, "Invalid account" );
[f73b969]342                                return;
[b7d3cc34]343                        }
344                }
345                else
346                {
347                        if ( irc->accounts ) {
348                                irc_usermsg( irc, "Trying to get all accounts connected..." );
349                       
350                                for( a = irc->accounts; a; a = a->next )
[0da65d5]351                                        if( !a->ic && a->auto_connect )
[b7d3cc34]352                                                account_on( irc, a );
353                        } 
354                        else
355                        {
[30ce1ce]356                                irc_usermsg( irc, "No accounts known. Use `account add' to add one." );
[b7d3cc34]357                        }
358                }
359        }
360        else if( g_strcasecmp( cmd[1], "off" ) == 0 )
361        {
362                if( !cmd[2] )
363                {
364                        irc_usermsg( irc, "Deactivating all active (re)connections..." );
365                       
366                        for( a = irc->accounts; a; a = a->next )
367                        {
[0da65d5]368                                if( a->ic )
[b7d3cc34]369                                        account_off( irc, a );
370                                else if( a->reconnect )
371                                        cancel_auto_reconnect( a );
372                        }
373                }
374                else if( ( a = account_get( irc, cmd[2] ) ) )
375                {
[0da65d5]376                        if( a->ic )
[b7d3cc34]377                        {
378                                account_off( irc, a );
379                        }
380                        else if( a->reconnect )
381                        {
382                                cancel_auto_reconnect( a );
383                                irc_usermsg( irc, "Reconnect cancelled" );
384                        }
385                        else
386                        {
387                                irc_usermsg( irc, "Account already offline" );
[f73b969]388                                return;
[b7d3cc34]389                        }
390                }
391                else
392                {
393                        irc_usermsg( irc, "Invalid account" );
[f73b969]394                        return;
[b7d3cc34]395                }
396        }
[5100caa]397        else if( g_strcasecmp( cmd[1], "set" ) == 0 )
398        {
399                char *acc_handle, *set_name = NULL, *tmp;
400               
401                if( !cmd[2] )
402                {
403                        irc_usermsg( irc, "Not enough parameters given (need %d)", 2 );
404                        return;
405                }
406               
[cd428e4]407                if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
408                        acc_handle = g_strdup( cmd[3] );
409                else
410                        acc_handle = g_strdup( cmd[2] );
411               
[5100caa]412                if( ( tmp = strchr( acc_handle, '/' ) ) )
413                {
414                        *tmp = 0;
415                        set_name = tmp + 1;
416                }
417               
[cd428e4]418                if( ( a = account_get( irc, acc_handle ) ) == NULL )
[5100caa]419                {
[75a4b85]420                        g_free( acc_handle );
[5100caa]421                        irc_usermsg( irc, "Invalid account" );
422                        return;
423                }
424               
[9334cc2]425                if( cmd[3] && set_name )
[5100caa]426                {
427                        set_t *s = set_find( &a->set, set_name );
428                       
[0da65d5]429                        if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY )
[5100caa]430                        {
[75a4b85]431                                g_free( acc_handle );
[911f2eb]432                                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" );
433                                return;
434                        }
[0da65d5]435                        else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY )
[911f2eb]436                        {
[75a4b85]437                                g_free( acc_handle );
[911f2eb]438                                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" );
[5100caa]439                                return;
440                        }
441                       
[71dc854]442                        if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
[cd428e4]443                                set_reset( &a->set, set_name );
444                        else
445                                set_setstr( &a->set, set_name, cmd[3] );
[5100caa]446                }
447                if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */
448                {
449                        char *s = set_getstr( &a->set, set_name );
450                        if( s )
451                                irc_usermsg( irc, "%s = `%s'", set_name, s );
452                        else
453                                irc_usermsg( irc, "%s is empty", set_name );
454                }
455                else
456                {
457                        set_t *s = a->set;
458                        while( s )
459                        {
460                                if( s->value || s->def )
[cd428e4]461                                        irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def );
[5100caa]462                                else
463                                        irc_usermsg( irc, "%s is empty", s->key );
464                                s = s->next;
465                        }
466                }
467               
468                g_free( acc_handle );
469        }
[b7d3cc34]470        else
471        {
[5c09a59]472                irc_usermsg( irc, "Unknown command: account %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[1] );
[b7d3cc34]473        }
474}
475
[f73b969]476static void cmd_add( irc_t *irc, char **cmd )
[b7d3cc34]477{
478        account_t *a;
[f0cb961]479        int add_on_server = 1;
[f8de26f]480       
481        if( g_strcasecmp( cmd[1], "-tmp" ) == 0 )
482        {
[f0cb961]483                add_on_server = 0;
[7adc657]484                cmd ++;
[f8de26f]485        }
[b7d3cc34]486       
487        if( !( a = account_get( irc, cmd[1] ) ) )
488        {
489                irc_usermsg( irc, "Invalid account" );
[f73b969]490                return;
[b7d3cc34]491        }
[0da65d5]492        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]493        {
494                irc_usermsg( irc, "That account is not on-line" );
[f73b969]495                return;
[b7d3cc34]496        }
497       
498        if( cmd[3] )
499        {
500                if( !nick_ok( cmd[3] ) )
501                {
502                        irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] );
[f73b969]503                        return;
[b7d3cc34]504                }
505                else if( user_find( irc, cmd[3] ) )
506                {
507                        irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] );
[f73b969]508                        return;
[b7d3cc34]509                }
510                else
511                {
[5b52a48]512                        nick_set( a, cmd[2], cmd[3] );
[b7d3cc34]513                }
514        }
[f8de26f]515       
[f0cb961]516        if( add_on_server )
[0da65d5]517                a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL );
[7adc657]518        else
519                /* Yeah, officially this is a call-*back*... So if we just
520                   called add_buddy, we'll wait for the IM server to respond
521                   before we do this. */
522                imcb_add_buddy( a->ic, cmd[2], NULL );
[f0cb961]523       
524        irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2]  );
[b7d3cc34]525}
526
[f73b969]527static void cmd_info( irc_t *irc, char **cmd )
[b7d3cc34]528{
[0da65d5]529        struct im_connection *ic;
[b7d3cc34]530        account_t *a;
531       
532        if( !cmd[2] )
533        {
534                user_t *u = user_find( irc, cmd[1] );
[0da65d5]535                if( !u || !u->ic )
[b7d3cc34]536                {
537                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]538                        return;
[b7d3cc34]539                }
[0da65d5]540                ic = u->ic;
[b7d3cc34]541                cmd[2] = u->handle;
542        }
543        else if( !( a = account_get( irc, cmd[1] ) ) )
544        {
545                irc_usermsg( irc, "Invalid account" );
[f73b969]546                return;
[b7d3cc34]547        }
[0da65d5]548        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]549        {
550                irc_usermsg( irc, "That account is not on-line" );
[f73b969]551                return;
[b7d3cc34]552        }
553       
[0da65d5]554        if( !ic->acc->prpl->get_info )
[b7d3cc34]555        {
556                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
557        }
[f73b969]558        else
559        {
[0da65d5]560                ic->acc->prpl->get_info( ic, cmd[2] );
[f73b969]561        }
[b7d3cc34]562}
563
[f73b969]564static void cmd_rename( irc_t *irc, char **cmd )
[b7d3cc34]565{
566        user_t *u;
567       
568        if( g_strcasecmp( cmd[1], irc->nick ) == 0 )
569        {
570                irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] );
571        }
[f73b969]572        else if( user_find( irc, cmd[2] ) && ( nick_cmp( cmd[1], cmd[2] ) != 0 ) )
[b7d3cc34]573        {
574                irc_usermsg( irc, "Nick `%s' already exists", cmd[2] );
575        }
[f73b969]576        else if( !nick_ok( cmd[2] ) )
[b7d3cc34]577        {
578                irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
579        }
[f73b969]580        else if( !( u = user_find( irc, cmd[1] ) ) )
[b7d3cc34]581        {
582                irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
583        }
[f73b969]584        else
[b7d3cc34]585        {
[f73b969]586                user_rename( irc, cmd[1], cmd[2] );
587                irc_write( irc, ":%s!%s@%s NICK %s", cmd[1], u->user, u->host, cmd[2] );
588                if( g_strcasecmp( cmd[1], irc->mynick ) == 0 )
589                {
590                        g_free( irc->mynick );
591                        irc->mynick = g_strdup( cmd[2] );
592                }
593                else if( u->send_handler == buddy_send_handler )
594                {
[0da65d5]595                        nick_set( u->ic->acc, u->handle, cmd[2] );
[f73b969]596                }
597               
598                irc_usermsg( irc, "Nick successfully changed" );
[b7d3cc34]599        }
600}
601
[f73b969]602static void cmd_remove( irc_t *irc, char **cmd )
[b7d3cc34]603{
604        user_t *u;
605        char *s;
606       
[0da65d5]607        if( !( u = user_find( irc, cmd[1] ) ) || !u->ic )
[b7d3cc34]608        {
609                irc_usermsg( irc, "Buddy `%s' not found", cmd[1] );
[f73b969]610                return;
[b7d3cc34]611        }
612        s = g_strdup( u->handle );
613       
[0da65d5]614        u->ic->acc->prpl->remove_buddy( u->ic, u->handle, NULL );
615        nick_del( u->ic->acc, u->handle );
[b7d3cc34]616        user_del( irc, cmd[1] );
617       
618        irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] );
619        g_free( s );
620       
[f73b969]621        return;
[b7d3cc34]622}
623
[f73b969]624static void cmd_block( irc_t *irc, char **cmd )
[b7d3cc34]625{
[0da65d5]626        struct im_connection *ic;
[b7d3cc34]627        account_t *a;
628       
[0da65d5]629        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
[87b6a3e]630        {
631                char *format;
632                GSList *l;
633               
634                if( strchr( irc->umode, 'b' ) != NULL )
635                        format = "%s\t%s";
636                else
[57ef864]637                        format = "%-32.32s  %-16.16s";
[87b6a3e]638               
639                irc_usermsg( irc, format, "Handle", "Nickname" );
[0da65d5]640                for( l = a->ic->deny; l; l = l->next )
[87b6a3e]641                {
[0da65d5]642                        user_t *u = user_findhandle( a->ic, l->data );
[87b6a3e]643                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
644                }
645                irc_usermsg( irc, "End of list." );
646               
647                return;
648        }
649        else if( !cmd[2] )
[b7d3cc34]650        {
651                user_t *u = user_find( irc, cmd[1] );
[0da65d5]652                if( !u || !u->ic )
[b7d3cc34]653                {
654                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]655                        return;
[b7d3cc34]656                }
[0da65d5]657                ic = u->ic;
[b7d3cc34]658                cmd[2] = u->handle;
659        }
660        else if( !( a = account_get( irc, cmd[1] ) ) )
661        {
662                irc_usermsg( irc, "Invalid account" );
[f73b969]663                return;
[b7d3cc34]664        }
[0da65d5]665        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]666        {
667                irc_usermsg( irc, "That account is not on-line" );
[f73b969]668                return;
[b7d3cc34]669        }
670       
[0da65d5]671        if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit )
[b7d3cc34]672        {
673                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
674        }
675        else
676        {
[84b045d]677                imc_rem_allow( ic, cmd[2] );
678                imc_add_block( ic, cmd[2] );
[da3b536]679                irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] );
[b7d3cc34]680        }
681}
682
[f73b969]683static void cmd_allow( irc_t *irc, char **cmd )
[b7d3cc34]684{
[0da65d5]685        struct im_connection *ic;
[b7d3cc34]686        account_t *a;
687       
[0da65d5]688        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
[87b6a3e]689        {
690                char *format;
691                GSList *l;
692               
693                if( strchr( irc->umode, 'b' ) != NULL )
694                        format = "%s\t%s";
695                else
[57ef864]696                        format = "%-32.32s  %-16.16s";
[87b6a3e]697               
698                irc_usermsg( irc, format, "Handle", "Nickname" );
[0da65d5]699                for( l = a->ic->permit; l; l = l->next )
[87b6a3e]700                {
[0da65d5]701                        user_t *u = user_findhandle( a->ic, l->data );
[87b6a3e]702                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
703                }
704                irc_usermsg( irc, "End of list." );
705               
706                return;
707        }
708        else if( !cmd[2] )
[b7d3cc34]709        {
710                user_t *u = user_find( irc, cmd[1] );
[0da65d5]711                if( !u || !u->ic )
[b7d3cc34]712                {
713                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]714                        return;
[b7d3cc34]715                }
[0da65d5]716                ic = u->ic;
[b7d3cc34]717                cmd[2] = u->handle;
718        }
719        else if( !( a = account_get( irc, cmd[1] ) ) )
720        {
721                irc_usermsg( irc, "Invalid account" );
[f73b969]722                return;
[b7d3cc34]723        }
[0da65d5]724        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]725        {
726                irc_usermsg( irc, "That account is not on-line" );
[f73b969]727                return;
[b7d3cc34]728        }
729       
[0da65d5]730        if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit )
[b7d3cc34]731        {
732                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
733        }
734        else
735        {
[84b045d]736                imc_rem_block( ic, cmd[2] );
737                imc_add_allow( ic, cmd[2] );
[b7d3cc34]738               
[da3b536]739                irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] );
[b7d3cc34]740        }
741}
742
[f73b969]743static void cmd_yesno( irc_t *irc, char **cmd )
[b7d3cc34]744{
745        query_t *q = NULL;
746        int numq = 0;
747       
748        if( irc->queries == NULL )
749        {
750                irc_usermsg( irc, "Did I ask you something?" );
[f73b969]751                return;
[b7d3cc34]752        }
753       
754        /* If there's an argument, the user seems to want to answer another question than the
755           first/last (depending on the query_order setting) one. */
756        if( cmd[1] )
757        {
758                if( sscanf( cmd[1], "%d", &numq ) != 1 )
759                {
760                        irc_usermsg( irc, "Invalid query number" );
[f73b969]761                        return;
[b7d3cc34]762                }
763               
764                for( q = irc->queries; q; q = q->next, numq -- )
765                        if( numq == 0 )
766                                break;
767               
768                if( !q )
769                {
770                        irc_usermsg( irc, "Uhm, I never asked you something like that..." );
[f73b969]771                        return;
[b7d3cc34]772                }
773        }
774       
775        if( g_strcasecmp( cmd[0], "yes" ) == 0 )
776                query_answer( irc, q, 1 );
777        else if( g_strcasecmp( cmd[0], "no" ) == 0 )
778                query_answer( irc, q, 0 );
779}
780
[f73b969]781static void cmd_set( irc_t *irc, char **cmd )
[b7d3cc34]782{
[71dc854]783        char *set_name = cmd[1];
[cd428e4]784       
[b7d3cc34]785        if( cmd[1] && cmd[2] )
786        {
[71dc854]787                if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
[cd428e4]788                {
789                        set_reset( &irc->set, cmd[2] );
790                        set_name = cmd[2];
791                }
792                else
793                {
794                        set_setstr( &irc->set, cmd[1], cmd[2] );
795                }
[b7d3cc34]796        }
[56f260a]797        if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */
[b7d3cc34]798        {
[cd428e4]799                char *s = set_getstr( &irc->set, set_name );
800                if( s )
801                        irc_usermsg( irc, "%s = `%s'", set_name, s );
[5100caa]802                else
[cd428e4]803                        irc_usermsg( irc, "%s is empty", set_name );
[d5bd9c0]804
805                if( strchr( set_name, '/' ) )
806                        irc_usermsg( irc, "Warning: / found in setting name, you're probably looking for the `account set' command." );
[b7d3cc34]807        }
808        else
809        {
810                set_t *s = irc->set;
811                while( s )
812                {
813                        if( s->value || s->def )
[cd428e4]814                                irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def );
[5100caa]815                        else
816                                irc_usermsg( irc, "%s is empty", s->key );
[b7d3cc34]817                        s = s->next;
818                }
819        }
820}
821
[f73b969]822static void cmd_save( irc_t *irc, char **cmd )
[b7d3cc34]823{
[ab49fdc]824        if( storage_save( irc, TRUE ) == STORAGE_OK )
[b7d3cc34]825                irc_usermsg( irc, "Configuration saved" );
826        else
827                irc_usermsg( irc, "Configuration could not be saved!" );
828}
829
[f73b969]830static void cmd_blist( irc_t *irc, char **cmd )
[b7d3cc34]831{
832        int online = 0, away = 0, offline = 0;
833        user_t *u;
[aefa533e]834        char s[256];
835        char *format;
[b7d3cc34]836        int n_online = 0, n_away = 0, n_offline = 0;
837       
838        if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 )
839                online = offline = away = 1;
840        else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 )
841                offline = 1;
842        else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 )
843                away = 1;
844        else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 )
845                online = 1;
846        else
847                online =  away = 1;
848       
[aefa533e]849        if( strchr( irc->umode, 'b' ) != NULL )
850                format = "%s\t%s\t%s";
851        else
852                format = "%-16.16s  %-40.40s  %s";
853       
854        irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" );
[b7d3cc34]855       
[0da65d5]856        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && !u->away )
[b7d3cc34]857        {
[aefa533e]858                if( online == 1 )
859                {
[e731120]860                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
[aefa533e]861                        irc_usermsg( irc, format, u->nick, s, "Online" );
862                }
863               
[b7d3cc34]864                n_online ++;
865        }
866
[0da65d5]867        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && u->away )
[b7d3cc34]868        {
[aefa533e]869                if( away == 1 )
870                {
[e731120]871                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
[aefa533e]872                        irc_usermsg( irc, format, u->nick, s, u->away );
873                }
[b7d3cc34]874                n_away ++;
875        }
876       
[0da65d5]877        for( u = irc->users; u; u = u->next ) if( u->ic && !u->online )
[b7d3cc34]878        {
[aefa533e]879                if( offline == 1 )
880                {
[e731120]881                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
[aefa533e]882                        irc_usermsg( irc, format, u->nick, s, "Offline" );
883                }
[b7d3cc34]884                n_offline ++;
885        }
886       
[aa5ac01]887        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
[b7d3cc34]888}
889
[f73b969]890static void cmd_nick( irc_t *irc, char **cmd ) 
[b7d3cc34]891{
892        account_t *a;
893
894        if( !cmd[1] || !( a = account_get( irc, cmd[1] ) ) )
895        {
896                irc_usermsg( irc, "Invalid account");
897        }
[0da65d5]898        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]899        {
900                irc_usermsg( irc, "That account is not on-line" );
901        }
902        else if ( !cmd[2] ) 
903        {
[0da65d5]904                irc_usermsg( irc, "Your name is `%s'" , a->ic->displayname ? a->ic->displayname : "NULL" );
[b7d3cc34]905        }
[0da65d5]906        else if ( !a->prpl->set_my_name ) 
[b7d3cc34]907        {
908                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
909        }
910        else
911        {
912                irc_usermsg( irc, "Setting your name to `%s'", cmd[2] );
913               
[0da65d5]914                a->prpl->set_my_name( a->ic, cmd[2] );
[b7d3cc34]915        }
916}
917
[f73b969]918static void cmd_qlist( irc_t *irc, char **cmd )
[b7d3cc34]919{
920        query_t *q = irc->queries;
921        int num;
922       
923        if( !q )
924        {
925                irc_usermsg( irc, "There are no pending questions." );
[f73b969]926                return;
[b7d3cc34]927        }
928       
929        irc_usermsg( irc, "Pending queries:" );
930       
931        for( num = 0; q; q = q->next, num ++ )
[0da65d5]932                if( q->ic ) /* Not necessary yet, but it might come later */
[c2fb3809]933                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question );
[5c09a59]934                else
935                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
[b7d3cc34]936}
937
[fa29d093]938static void cmd_join_chat( irc_t *irc, char **cmd )
939{
940        account_t *a;
[0da65d5]941        struct im_connection *ic;
[fa29d093]942        char *chat, *channel, *nick = NULL, *password = NULL;
[0da65d5]943        struct groupchat *c;
[fa29d093]944       
945        if( !( a = account_get( irc, cmd[1] ) ) )
946        {
947                irc_usermsg( irc, "Invalid account" );
948                return;
949        }
[0da65d5]950        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[fa29d093]951        {
952                irc_usermsg( irc, "That account is not on-line" );
953                return;
954        }
955        else if( a->prpl->chat_join == NULL )
956        {
957                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
958                return;
959        }
[0da65d5]960        ic = a->ic;
[fa29d093]961       
962        chat = cmd[2];
963        if( cmd[3] )
964        {
[9da0bbf]965                if( cmd[3][0] != '#' && cmd[3][0] != '&' )
[7bf4326]966                        channel = g_strdup_printf( "&%s", cmd[3] );
967                else
968                        channel = g_strdup( cmd[3] );
[fa29d093]969        }
970        else
971        {
972                char *s;
973               
[0e7ab64]974                channel = g_strdup_printf( "&%s", chat );
[fa29d093]975                if( ( s = strchr( channel, '@' ) ) )
976                        *s = 0;
977        }
978        if( cmd[3] && cmd[4] )
979                nick = cmd[4];
[e35d1a1]980        else
981                nick = irc->nick;
[fa29d093]982        if( cmd[3] && cmd[4] && cmd[5] )
983                password = cmd[5];
984       
[7bf4326]985        if( !nick_ok( channel + 1 ) )
[0e7ab64]986        {
987                irc_usermsg( irc, "Invalid channel name: %s", channel );
988                g_free( channel );
989                return;
990        }
991        else if( g_strcasecmp( channel, irc->channel ) == 0 || irc_chat_by_channel( irc, channel ) )
992        {
993                irc_usermsg( irc, "Channel already exists: %s", channel );
994                g_free( channel );
995                return;
996        }
[fa29d093]997       
[e35d1a1]998        if( ( c = a->prpl->chat_join( ic, chat, nick, password ) ) )
999        {
1000                g_free( c->channel );
1001                c->channel = channel;
1002        }
1003        else
1004        {
1005                irc_usermsg( irc, "Tried to join chat, not sure if this was successful" );
1006                g_free( channel );
1007        }
[fa29d093]1008}
1009
[0298d11]1010const command_t commands[] = {
1011        { "help",           0, cmd_help,           0 }, 
1012        { "identify",       1, cmd_identify,       0 },
1013        { "register",       1, cmd_register,       0 },
1014        { "drop",           1, cmd_drop,           0 },
1015        { "account",        1, cmd_account,        0 },
1016        { "add",            2, cmd_add,            0 },
1017        { "info",           1, cmd_info,           0 },
1018        { "rename",         2, cmd_rename,         0 },
1019        { "remove",         1, cmd_remove,         0 },
1020        { "block",          1, cmd_block,          0 },
1021        { "allow",          1, cmd_allow,          0 },
1022        { "save",           0, cmd_save,           0 },
1023        { "set",            0, cmd_set,            0 },
1024        { "yes",            0, cmd_yesno,          0 },
1025        { "no",             0, cmd_yesno,          0 },
1026        { "blist",          0, cmd_blist,          0 },
1027        { "nick",           1, cmd_nick,           0 },
1028        { "qlist",          0, cmd_qlist,          0 },
[fa29d093]1029        { "join_chat",      2, cmd_join_chat,      0 },
[0298d11]1030        { NULL }
1031};
Note: See TracBrowser for help on using the repository browser.