source: root_commands.c @ 6c56f42

Last change on this file since 6c56f42 was 6c56f42, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-11T23:04:24Z

Allow short versions of root commands (just give enough to match exactly
one command), for the lazy users.

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