source: root_commands.c @ fa29d093

Last change on this file since fa29d093 was fa29d093, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-03-28T05:53:11Z

Preparing for Jabber conference room support.

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