source: root_commands.c @ 66b9e36a

Last change on this file since 66b9e36a was 2272cb3, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-20T23:11:32Z

Restored block/allow commands. Only "chat" is left now, which will need some
more rethinking anyway (and groupchat support is not back yet anyway).

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