source: root_commands.c @ 7e83e8e4

Last change on this file since 7e83e8e4 was 7b71feb, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-05-13T23:34:38Z

Just enough code to join named chatrooms again. This UI is *not* final, the "chat"
command will probably mostly stick around for bw compatibility. Still thinking
about how this should work eventually.

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