source: root_commands.c @ 89c11e7

Last change on this file since 89c11e7 was 24b8bbb, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-12T00:06:49Z

Start handling CTCPs, in a saner way than before.

  • Property mode set to 100644
File size: 26.9 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
720static void cmd_block( irc_t *irc, char **cmd )
721{
722        struct im_connection *ic;
723        account_t *a;
724       
725        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
726        {
727                char *format;
728                GSList *l;
729               
730                if( strchr( irc->umode, 'b' ) != NULL )
731                        format = "%s\t%s";
732                else
733                        format = "%-32.32s  %-16.16s";
734               
735                irc_usermsg( irc, format, "Handle", "Nickname" );
736                for( l = a->ic->deny; l; l = l->next )
737                {
738                        user_t *u = user_findhandle( a->ic, l->data );
739                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
740                }
741                irc_usermsg( irc, "End of list." );
742               
743                return;
744        }
745        else if( !cmd[2] )
746        {
747                user_t *u = user_find( irc, cmd[1] );
748                if( !u || !u->ic )
749                {
750                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
751                        return;
752                }
753                ic = u->ic;
754                cmd[2] = u->handle;
755        }
756        else if( !( a = account_get( irc, cmd[1] ) ) )
757        {
758                irc_usermsg( irc, "Invalid account" );
759                return;
760        }
761        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
762        {
763                irc_usermsg( irc, "That account is not on-line" );
764                return;
765        }
766       
767        if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit )
768        {
769                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
770        }
771        else
772        {
773                imc_rem_allow( ic, cmd[2] );
774                imc_add_block( ic, cmd[2] );
775                irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] );
776        }
777}
778
779static void cmd_allow( irc_t *irc, char **cmd )
780{
781        struct im_connection *ic;
782        account_t *a;
783       
784        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
785        {
786                char *format;
787                GSList *l;
788               
789                if( strchr( irc->umode, 'b' ) != NULL )
790                        format = "%s\t%s";
791                else
792                        format = "%-32.32s  %-16.16s";
793               
794                irc_usermsg( irc, format, "Handle", "Nickname" );
795                for( l = a->ic->permit; l; l = l->next )
796                {
797                        user_t *u = user_findhandle( a->ic, l->data );
798                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
799                }
800                irc_usermsg( irc, "End of list." );
801               
802                return;
803        }
804        else if( !cmd[2] )
805        {
806                user_t *u = user_find( irc, cmd[1] );
807                if( !u || !u->ic )
808                {
809                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
810                        return;
811                }
812                ic = u->ic;
813                cmd[2] = u->handle;
814        }
815        else if( !( a = account_get( irc, cmd[1] ) ) )
816        {
817                irc_usermsg( irc, "Invalid account" );
818                return;
819        }
820        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
821        {
822                irc_usermsg( irc, "That account is not on-line" );
823                return;
824        }
825       
826        if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit )
827        {
828                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
829        }
830        else
831        {
832                imc_rem_block( ic, cmd[2] );
833                imc_add_allow( ic, cmd[2] );
834               
835                irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] );
836        }
837}
838#endif
839
840static void cmd_yesno( irc_t *irc, char **cmd )
841{
842        query_t *q = NULL;
843        int numq = 0;
844       
845        if( irc->queries == NULL )
846        {
847                irc_usermsg( irc, "Did I ask you something?" );
848                return;
849        }
850       
851        /* If there's an argument, the user seems to want to answer another question than the
852           first/last (depending on the query_order setting) one. */
853        if( cmd[1] )
854        {
855                if( sscanf( cmd[1], "%d", &numq ) != 1 )
856                {
857                        irc_usermsg( irc, "Invalid query number" );
858                        return;
859                }
860               
861                for( q = irc->queries; q; q = q->next, numq -- )
862                        if( numq == 0 )
863                                break;
864               
865                if( !q )
866                {
867                        irc_usermsg( irc, "Uhm, I never asked you something like that..." );
868                        return;
869                }
870        }
871       
872        if( g_strcasecmp( cmd[0], "yes" ) == 0 )
873                query_answer( irc, q, 1 );
874        else if( g_strcasecmp( cmd[0], "no" ) == 0 )
875                query_answer( irc, q, 0 );
876}
877
878static void cmd_set( irc_t *irc, char **cmd )
879{
880        cmd_set_real( irc, cmd, NULL, NULL );
881}
882
883#if 0
884static void cmd_blist( irc_t *irc, char **cmd )
885{
886        int online = 0, away = 0, offline = 0;
887        user_t *u;
888        char s[256];
889        char *format;
890        int n_online = 0, n_away = 0, n_offline = 0;
891       
892        if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 )
893                online = offline = away = 1;
894        else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 )
895                offline = 1;
896        else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 )
897                away = 1;
898        else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 )
899                online = 1;
900        else
901                online = away = 1;
902       
903        if( strchr( irc->umode, 'b' ) != NULL )
904                format = "%s\t%s\t%s";
905        else
906                format = "%-16.16s  %-40.40s  %s";
907       
908        irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" );
909       
910        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && !u->away )
911        {
912                if( online == 1 )
913                {
914                        char st[256] = "Online";
915                       
916                        if( u->status_msg )
917                                g_snprintf( st, sizeof( st ) - 1, "Online (%s)", u->status_msg );
918                       
919                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
920                        irc_usermsg( irc, format, u->nick, s, st );
921                }
922               
923                n_online ++;
924        }
925
926        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && u->away )
927        {
928                if( away == 1 )
929                {
930                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
931                        irc_usermsg( irc, format, u->nick, s, u->away );
932                }
933                n_away ++;
934        }
935       
936        for( u = irc->users; u; u = u->next ) if( u->ic && !u->online )
937        {
938                if( offline == 1 )
939                {
940                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
941                        irc_usermsg( irc, format, u->nick, s, "Offline" );
942                }
943                n_offline ++;
944        }
945       
946        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
947}
948
949static void cmd_qlist( irc_t *irc, char **cmd )
950{
951        query_t *q = irc->queries;
952        int num;
953       
954        if( !q )
955        {
956                irc_usermsg( irc, "There are no pending questions." );
957                return;
958        }
959       
960        irc_usermsg( irc, "Pending queries:" );
961       
962        for( num = 0; q; q = q->next, num ++ )
963                if( q->ic ) /* Not necessary yet, but it might come later */
964                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question );
965                else
966                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
967}
968
969static set_t **cmd_chat_set_findhead( irc_t *irc, char *id )
970{
971        struct chat *c;
972       
973        if( ( c = chat_get( irc, id ) ) )
974                return &c->set;
975        else
976                return NULL;
977}
978
979static void cmd_chat( irc_t *irc, char **cmd )
980{
981        account_t *acc;
982        struct chat *c;
983       
984        if( g_strcasecmp( cmd[1], "add" ) == 0 )
985        {
986                char *channel, *s;
987               
988                MIN_ARGS( 3 );
989               
990                if( !( acc = account_get( irc, cmd[2] ) ) )
991                {
992                        irc_usermsg( irc, "Invalid account" );
993                        return;
994                }
995               
996                if( cmd[4] == NULL )
997                {
998                        channel = g_strdup( cmd[3] );
999                        if( ( s = strchr( channel, '@' ) ) )
1000                                *s = 0;
1001                }
1002                else
1003                {
1004                        channel = g_strdup( cmd[4] );
1005                }
1006               
1007                if( strchr( CTYPES, channel[0] ) == NULL )
1008                {
1009                        s = g_strdup_printf( "%c%s", CTYPES[0], channel );
1010                        g_free( channel );
1011                        channel = s;
1012                }
1013               
1014                if( ( c = chat_add( irc, acc, cmd[3], channel ) ) )
1015                        irc_usermsg( irc, "Chatroom added successfully." );
1016                else
1017                        irc_usermsg( irc, "Could not add chatroom." );
1018               
1019                g_free( channel );
1020        }
1021        else if( g_strcasecmp( cmd[1], "list" ) == 0 )
1022        {
1023                int i = 0;
1024               
1025                if( strchr( irc->umode, 'b' ) )
1026                        irc_usermsg( irc, "Chatroom list:" );
1027               
1028                for( c = irc->chatrooms; c; c = c->next )
1029                {
1030                        irc_usermsg( irc, "%2d. %s(%s) %s, %s", i, c->acc->prpl->name,
1031                                          c->acc->user, c->handle, c->channel );
1032                       
1033                        i ++;
1034                }
1035                irc_usermsg( irc, "End of chatroom list" );
1036        }
1037        else if( g_strcasecmp( cmd[1], "set" ) == 0 )
1038        {
1039                MIN_ARGS( 2 );
1040               
1041                cmd_set_real( irc, cmd + 1, cmd_chat_set_findhead, NULL );
1042        }
1043        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
1044        {
1045                MIN_ARGS( 2 );
1046               
1047                if( ( c = chat_get( irc, cmd[2] ) ) )
1048                {
1049                        chat_del( irc, c );
1050                }
1051                else
1052                {
1053                        irc_usermsg( irc, "Could not remove chat." );
1054                }
1055        }
1056        else if( g_strcasecmp( cmd[1], "with" ) == 0 )
1057        {
1058                user_t *u;
1059               
1060                MIN_ARGS( 2 );
1061               
1062                if( ( u = user_find( irc, cmd[2] ) ) && u->ic && u->ic->acc->prpl->chat_with )
1063                {
1064                        if( !u->ic->acc->prpl->chat_with( u->ic, u->handle ) )
1065                        {
1066                                irc_usermsg( irc, "(Possible) failure while trying to open "
1067                                                  "a groupchat with %s.", u->nick );
1068                        }
1069                }
1070                else
1071                {
1072                        irc_usermsg( irc, "Can't open a groupchat with %s.", cmd[2] );
1073                }
1074        }
1075        else
1076        {
1077                irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat", cmd[1] );
1078        }
1079}
1080
1081static void cmd_transfer( irc_t *irc, char **cmd )
1082{
1083        GSList *files = irc->file_transfers;
1084        enum { LIST, REJECT, CANCEL };
1085        int subcmd = LIST;
1086        int fid;
1087
1088        if( !files )
1089        {
1090                irc_usermsg( irc, "No pending transfers" );
1091                return;
1092        }
1093
1094        if( cmd[1] && ( strcmp( cmd[1], "reject" ) == 0 ) )
1095        {
1096                subcmd = REJECT;
1097        }
1098        else if( cmd[1] && ( strcmp( cmd[1], "cancel" ) == 0 ) &&
1099                 cmd[2] && ( sscanf( cmd[2], "%d", &fid ) == 1 ) )
1100        {
1101                subcmd = CANCEL;
1102        }
1103
1104        for( ; files; files = g_slist_next( files ) )
1105        {
1106                file_transfer_t *file = files->data;
1107               
1108                switch( subcmd ) {
1109                case LIST:
1110                        if ( file->status == FT_STATUS_LISTENING )
1111                                irc_usermsg( irc,
1112                                        "Pending file(id %d): %s (Listening...)", file->local_id, file->file_name);
1113                        else
1114                        {
1115                                int kb_per_s = 0;
1116                                time_t diff = time( NULL ) - file->started ? : 1;
1117                                if ( ( file->started > 0 ) && ( file->bytes_transferred > 0 ) )
1118                                        kb_per_s = file->bytes_transferred / 1024 / diff;
1119                                       
1120                                irc_usermsg( irc,
1121                                        "Pending file(id %d): %s (%10zd/%zd kb, %d kb/s)", file->local_id, file->file_name,
1122                                        file->bytes_transferred/1024, file->file_size/1024, kb_per_s);
1123                        }
1124                        break;
1125                case REJECT:
1126                        if( file->status == FT_STATUS_LISTENING )
1127                        {
1128                                irc_usermsg( irc, "Rejecting file transfer for %s", file->file_name );
1129                                imcb_file_canceled( file, "Denied by user" );
1130                        }
1131                        break;
1132                case CANCEL:
1133                        if( file->local_id == fid )
1134                        {
1135                                irc_usermsg( irc, "Canceling file transfer for %s", file->file_name );
1136                                imcb_file_canceled( file, "Canceled by user" );
1137                        }
1138                        break;
1139                }
1140        }
1141}
1142#endif
1143
1144/* IMPORTANT: Keep this list sorted! The short command logic needs that. */
1145const command_t commands[] = {
1146        { "account",        1, cmd_account,        0 },
1147        { "add",            2, cmd_add,            0 },
1148        { "drop",           1, cmd_drop,           0 },
1149        { "help",           0, cmd_help,           0 }, 
1150        { "identify",       1, cmd_identify,       0 },
1151        { "info",           1, cmd_info,           0 },
1152        { "no",             0, cmd_yesno,          0 },
1153        { "register",       1, cmd_register,       0 },
1154        { "remove",         1, cmd_remove,         0 },
1155        { "rename",         2, cmd_rename,         0 },
1156        { "save",           0, cmd_save,           0 },
1157        { "set",            0, cmd_set,            0 },
1158        { "yes",            0, cmd_yesno,          0 },
1159#if 0
1160        { "allow",          1, cmd_allow,          0 },
1161        { "blist",          0, cmd_blist,          0 },
1162        { "block",          1, cmd_block,          0 },
1163        { "chat",           1, cmd_chat,           0 },
1164        { "ft",             0, cmd_transfer,       0 },
1165        { "join_chat",      2, cmd_join_chat,      0 },
1166        { "qlist",          0, cmd_qlist,          0 },
1167        { "transfer",       0, cmd_transfer,       0 },
1168#endif
1169        { NULL }
1170};
Note: See TracBrowser for help on using the repository browser.