source: root_commands.c @ 57c96f7

Last change on this file since 57c96f7 was 57c96f7, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-05T01:00:02Z

Restored the rename command.

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