source: root_commands.c @ cf1a979

Last change on this file since cf1a979 was c133d4b8, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-05T01:06:26Z

Added "channel set" command, mostly so I can test all that stuff. Needs
more work.

  • Property mode set to 100644
File size: 25.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
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 set_t **cmd_channel_set_findhead( irc_t *irc, char *id )
489{
490        irc_channel_t *ic;
491       
492        if( ( ic = irc_channel_by_name( irc, id ) ) )
493                return &ic->set;
494        else
495                return NULL;
496}
497
498static void cmd_channel( irc_t *irc, char **cmd )
499{
500        if( g_strcasecmp( cmd[1], "set" ) == 0 )
501        {
502                MIN_ARGS( 2 );
503               
504                cmd_set_real( irc, cmd + 1, cmd_channel_set_findhead, NULL );
505        }
506        else
507        {
508                irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "channel", cmd[1] );
509        }
510}
511
512static void cmd_add( irc_t *irc, char **cmd )
513{
514        account_t *a;
515        int add_on_server = 1;
516       
517        if( g_strcasecmp( cmd[1], "-tmp" ) == 0 )
518        {
519                MIN_ARGS( 3 );
520                add_on_server = 0;
521                cmd ++;
522        }
523       
524        if( !( a = account_get( irc->b, cmd[1] ) ) )
525        {
526                irc_usermsg( irc, "Invalid account" );
527                return;
528        }
529        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
530        {
531                irc_usermsg( irc, "That account is not on-line" );
532                return;
533        }
534       
535        if( cmd[3] )
536        {
537                if( !nick_ok( cmd[3] ) )
538                {
539                        irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] );
540                        return;
541                }
542                else if( irc_user_by_name( irc, cmd[3] ) )
543                {
544                        irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] );
545                        return;
546                }
547                else
548                {
549                        nick_set( a, cmd[2], cmd[3] );
550                }
551        }
552       
553        if( add_on_server )
554                a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL );
555        else
556                /* Only for add -tmp. For regular adds, this callback will
557                   be called once the IM server confirms. */
558                bee_user_new( irc->b, a->ic, cmd[2], BEE_USER_LOCAL );
559       
560        irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2]  );
561}
562
563static void cmd_remove( irc_t *irc, char **cmd )
564{
565        irc_user_t *iu;
566        bee_user_t *bu;
567        char *s;
568       
569        if( !( iu = irc_user_by_name( irc, cmd[1] ) ) || !( bu = iu->bu ) )
570        {
571                irc_usermsg( irc, "Buddy `%s' not found", cmd[1] );
572                return;
573        }
574        s = g_strdup( bu->handle );
575       
576        bu->ic->acc->prpl->remove_buddy( bu->ic, bu->handle, NULL );
577        nick_del( bu->ic->acc, bu->handle );
578        //TODO(wilmer): bee_user_free() and/or let the IM mod do it? irc_user_free( irc, cmd[1] );
579       
580        irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] );
581        g_free( s );
582       
583        return;
584}
585
586static void cmd_info( irc_t *irc, char **cmd )
587{
588        struct im_connection *ic;
589        account_t *a;
590       
591        if( !cmd[2] )
592        {
593                irc_user_t *iu = irc_user_by_name( irc, cmd[1] );
594                if( !iu || !iu->bu )
595                {
596                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
597                        return;
598                }
599                ic = iu->bu->ic;
600                cmd[2] = iu->bu->handle;
601        }
602        else if( !( a = account_get( irc->b, cmd[1] ) ) )
603        {
604                irc_usermsg( irc, "Invalid account" );
605                return;
606        }
607        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
608        {
609                irc_usermsg( irc, "That account is not on-line" );
610                return;
611        }
612       
613        if( !ic->acc->prpl->get_info )
614        {
615                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
616        }
617        else
618        {
619                ic->acc->prpl->get_info( ic, cmd[2] );
620        }
621}
622
623static void cmd_rename( irc_t *irc, char **cmd )
624{
625        irc_user_t *iu;
626       
627        iu = irc_user_by_name( irc, cmd[1] );
628       
629        if( iu == NULL )
630        {
631                irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
632        }
633        else if( iu == irc->user )
634        {
635                irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] );
636        }
637        else if( !nick_ok( cmd[2] ) )
638        {
639                irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
640        }
641        else if( irc_user_by_name( irc, cmd[2] ) )
642        {
643                irc_usermsg( irc, "Nick `%s' already exists", cmd[2] );
644        }
645        else
646        {
647                if( !irc_user_set_nick( iu, cmd[2] ) )
648                {
649                        irc_usermsg( irc, "Error while changing nick" );
650                        return;
651                }
652               
653                if( iu == irc->root )
654                {
655                        /* If we're called internally (user did "set root_nick"),
656                           let's not go O(INF). :-) */
657                        if( strcmp( cmd[0], "set_rename" ) != 0 )
658                                set_setstr( &irc->b->set, "root_nick", cmd[2] );
659                }
660                else if( iu->bu )
661                {
662                        nick_set( iu->bu->ic->acc, iu->bu->handle, cmd[2] );
663                }
664               
665                irc_usermsg( irc, "Nick successfully changed" );
666        }
667}
668
669#if 0
670char *set_eval_root_nick( set_t *set, char *new_nick )
671{
672        irc_t *irc = set->data;
673       
674        if( strcmp( irc->mynick, new_nick ) != 0 )
675        {
676                char *cmd[] = { "set_rename", irc->mynick, new_nick, NULL };
677               
678                cmd_rename( irc, cmd );
679        }
680       
681        return strcmp( irc->mynick, new_nick ) == 0 ? new_nick : SET_INVALID;
682}
683
684char *set_eval_control_channel( set_t *set, char *new_name )
685{
686        irc_t *irc = set->data;
687       
688        if( strcmp( irc->channel, new_name ) != 0 )
689        {
690                char *cmd[] = { "set_rename", irc->channel, new_name, NULL };
691               
692                cmd_rename( irc, cmd );
693        }
694       
695        return strcmp( irc->channel, new_name ) == 0 ? new_name : SET_INVALID;
696}
697#endif
698
699static void cmd_block( irc_t *irc, char **cmd )
700{
701        struct im_connection *ic;
702        account_t *a;
703       
704        if( !cmd[2] && ( a = account_get( irc->b, cmd[1] ) ) && a->ic )
705        {
706                char *format;
707                GSList *l;
708               
709                if( strchr( irc->umode, 'b' ) != NULL )
710                        format = "%s\t%s";
711                else
712                        format = "%-32.32s  %-16.16s";
713               
714                irc_usermsg( irc, format, "Handle", "Nickname" );
715                for( l = a->ic->deny; l; l = l->next )
716                {
717                        bee_user_t *bu = bee_user_by_handle( irc->b, a->ic, l->data );
718                        irc_user_t *iu = bu ? bu->ui_data : NULL;
719                        irc_usermsg( irc, format, l->data, iu ? iu->nick : "(none)" );
720                }
721                irc_usermsg( irc, "End of list." );
722               
723                return;
724        }
725        else if( !cmd[2] )
726        {
727                irc_user_t *iu = irc_user_by_name( irc, cmd[1] );
728                if( !iu || !iu->bu )
729                {
730                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
731                        return;
732                }
733                ic = iu->bu->ic;
734                cmd[2] = iu->bu->handle;
735        }
736        else if( !( a = account_get( irc->b, cmd[1] ) ) )
737        {
738                irc_usermsg( irc, "Invalid account" );
739                return;
740        }
741        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
742        {
743                irc_usermsg( irc, "That account is not on-line" );
744                return;
745        }
746       
747        if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit )
748        {
749                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
750        }
751        else
752        {
753                imc_rem_allow( ic, cmd[2] );
754                imc_add_block( ic, cmd[2] );
755                irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] );
756        }
757}
758
759static void cmd_allow( irc_t *irc, char **cmd )
760{
761        struct im_connection *ic;
762        account_t *a;
763       
764        if( !cmd[2] && ( a = account_get( irc->b, cmd[1] ) ) && a->ic )
765        {
766                char *format;
767                GSList *l;
768               
769                if( strchr( irc->umode, 'b' ) != NULL )
770                        format = "%s\t%s";
771                else
772                        format = "%-32.32s  %-16.16s";
773               
774                irc_usermsg( irc, format, "Handle", "Nickname" );
775                for( l = a->ic->permit; l; l = l->next )
776                {
777                        bee_user_t *bu = bee_user_by_handle( irc->b, a->ic, l->data );
778                        irc_user_t *iu = bu ? bu->ui_data : NULL;
779                        irc_usermsg( irc, format, l->data, iu ? iu->nick : "(none)" );
780                }
781                irc_usermsg( irc, "End of list." );
782               
783                return;
784        }
785        else if( !cmd[2] )
786        {
787                irc_user_t *iu = irc_user_by_name( irc, cmd[1] );
788                if( !iu || !iu->bu )
789                {
790                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
791                        return;
792                }
793                ic = iu->bu->ic;
794                cmd[2] = iu->bu->handle;
795        }
796        else if( !( a = account_get( irc->b, cmd[1] ) ) )
797        {
798                irc_usermsg( irc, "Invalid account" );
799                return;
800        }
801        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
802        {
803                irc_usermsg( irc, "That account is not on-line" );
804                return;
805        }
806       
807        if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit )
808        {
809                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
810        }
811        else
812        {
813                imc_rem_block( ic, cmd[2] );
814                imc_add_allow( ic, cmd[2] );
815               
816                irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] );
817        }
818}
819
820static void cmd_yesno( irc_t *irc, char **cmd )
821{
822        query_t *q = NULL;
823        int numq = 0;
824       
825        if( irc->queries == NULL )
826        {
827                irc_usermsg( irc, "Did I ask you something?" );
828                return;
829        }
830       
831        /* If there's an argument, the user seems to want to answer another question than the
832           first/last (depending on the query_order setting) one. */
833        if( cmd[1] )
834        {
835                if( sscanf( cmd[1], "%d", &numq ) != 1 )
836                {
837                        irc_usermsg( irc, "Invalid query number" );
838                        return;
839                }
840               
841                for( q = irc->queries; q; q = q->next, numq -- )
842                        if( numq == 0 )
843                                break;
844               
845                if( !q )
846                {
847                        irc_usermsg( irc, "Uhm, I never asked you something like that..." );
848                        return;
849                }
850        }
851       
852        if( g_strcasecmp( cmd[0], "yes" ) == 0 )
853                query_answer( irc, q, 1 );
854        else if( g_strcasecmp( cmd[0], "no" ) == 0 )
855                query_answer( irc, q, 0 );
856}
857
858static void cmd_set( irc_t *irc, char **cmd )
859{
860        cmd_set_real( irc, cmd, NULL, NULL );
861}
862
863static void cmd_blist( irc_t *irc, char **cmd )
864{
865        int online = 0, away = 0, offline = 0;
866        GSList *l;
867        char s[256];
868        char *format;
869        int n_online = 0, n_away = 0, n_offline = 0;
870       
871        if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 )
872                online = offline = away = 1;
873        else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 )
874                offline = 1;
875        else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 )
876                away = 1;
877        else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 )
878                online = 1;
879        else
880                online = away = 1;
881       
882        if( strchr( irc->umode, 'b' ) != NULL )
883                format = "%s\t%s\t%s";
884        else
885                format = "%-16.16s  %-40.40s  %s";
886       
887        irc_usermsg( irc, format, "Nick", "Handle/Account", "Status" );
888       
889        for( l = irc->users; l; l = l->next )
890        {
891                irc_user_t *iu = l->data;
892                bee_user_t *bu = iu->bu;
893               
894                if( !bu || ( bu->flags & ( BEE_USER_ONLINE | BEE_USER_AWAY ) ) != BEE_USER_ONLINE )
895                        continue;
896               
897                if( online == 1 )
898                {
899                        char st[256] = "Online";
900                       
901                        if( bu->status_msg )
902                                g_snprintf( st, sizeof( st ) - 1, "Online (%s)", bu->status_msg );
903                       
904                        g_snprintf( s, sizeof( s ) - 1, "%s %s(%s)", bu->handle, bu->ic->acc->prpl->name, bu->ic->acc->user );
905                        irc_usermsg( irc, format, iu->nick, s, st );
906                }
907               
908                n_online ++;
909        }
910
911        for( l = irc->users; l; l = l->next )
912        {
913                irc_user_t *iu = l->data;
914                bee_user_t *bu = iu->bu;
915               
916                if( !bu || !( bu->flags & BEE_USER_ONLINE ) || !( bu->flags & BEE_USER_AWAY ) )
917                        continue;
918               
919                if( away == 1 )
920                {
921                        g_snprintf( s, sizeof( s ) - 1, "%s %s(%s)", bu->handle, bu->ic->acc->prpl->name, bu->ic->acc->user );
922                        irc_usermsg( irc, format, iu->nick, s, irc_user_get_away( iu ) );
923                }
924                n_away ++;
925        }
926       
927        for( l = irc->users; l; l = l->next )
928        {
929                irc_user_t *iu = l->data;
930                bee_user_t *bu = iu->bu;
931               
932                if( !bu || bu->flags & BEE_USER_ONLINE )
933                        continue;
934               
935                if( offline == 1 )
936                {
937                        g_snprintf( s, sizeof( s ) - 1, "%s %s(%s)", bu->handle, bu->ic->acc->prpl->name, bu->ic->acc->user );
938                        irc_usermsg( irc, format, iu->nick, s, "Offline" );
939                }
940                n_offline ++;
941        }
942       
943        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
944}
945
946static void cmd_qlist( irc_t *irc, char **cmd )
947{
948        query_t *q = irc->queries;
949        int num;
950       
951        if( !q )
952        {
953                irc_usermsg( irc, "There are no pending questions." );
954                return;
955        }
956       
957        irc_usermsg( irc, "Pending queries:" );
958       
959        for( num = 0; q; q = q->next, num ++ )
960                if( q->ic ) /* Not necessary yet, but it might come later */
961                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question );
962                else
963                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
964}
965
966static void cmd_chat( irc_t *irc, char **cmd )
967{
968        account_t *acc;
969       
970        if( g_strcasecmp( cmd[1], "add" ) == 0 )
971        {
972                char *channel, *s;
973                struct irc_channel *ic;
974               
975                MIN_ARGS( 3 );
976               
977                if( !( acc = account_get( irc->b, cmd[2] ) ) )
978                {
979                        irc_usermsg( irc, "Invalid account" );
980                        return;
981                }
982               
983                if( cmd[4] == NULL )
984                {
985                        channel = g_strdup( cmd[3] );
986                        if( ( s = strchr( channel, '@' ) ) )
987                                *s = 0;
988                }
989                else
990                {
991                        channel = g_strdup( cmd[4] );
992                }
993               
994                if( strchr( CTYPES, channel[0] ) == NULL )
995                {
996                        s = g_strdup_printf( "#%s", channel );
997                        g_free( channel );
998                        channel = s;
999                }
1000               
1001                if( ( ic = irc_channel_new( irc, channel ) ) )
1002                {
1003                        struct irc_groupchat_stub *igs;
1004                       
1005                        ic->data = igs = g_new0( struct irc_groupchat_stub, 1 );
1006                        igs->acc = acc;
1007                        igs->room = g_strdup( cmd[3] );
1008                }
1009        }
1010        /*
1011        else if( g_strcasecmp( cmd[1], "with" ) == 0 )
1012        {
1013                user_t *u;
1014               
1015                MIN_ARGS( 2 );
1016               
1017                if( ( u = user_find( irc, cmd[2] ) ) && u->ic && u->ic->acc->prpl->chat_with )
1018                {
1019                        if( !u->ic->acc->prpl->chat_with( u->ic, u->handle ) )
1020                        {
1021                                irc_usermsg( irc, "(Possible) failure while trying to open "
1022                                                  "a groupchat with %s.", u->nick );
1023                        }
1024                }
1025                else
1026                {
1027                        irc_usermsg( irc, "Can't open a groupchat with %s.", cmd[2] );
1028                }
1029        }
1030        */
1031        else
1032        {
1033                irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat", cmd[1] );
1034        }
1035}
1036
1037static void cmd_transfer( irc_t *irc, char **cmd )
1038{
1039        GSList *files = irc->file_transfers;
1040        enum { LIST, REJECT, CANCEL };
1041        int subcmd = LIST;
1042        int fid;
1043
1044        if( !files )
1045        {
1046                irc_usermsg( irc, "No pending transfers" );
1047                return;
1048        }
1049
1050        if( cmd[1] && ( strcmp( cmd[1], "reject" ) == 0 ) )
1051        {
1052                subcmd = REJECT;
1053        }
1054        else if( cmd[1] && ( strcmp( cmd[1], "cancel" ) == 0 ) && 
1055                 cmd[2] && ( sscanf( cmd[2], "%d", &fid ) == 1 ) )
1056        {
1057                subcmd = CANCEL;
1058        }
1059
1060        for( ; files; files = g_slist_next( files ) )
1061        {
1062                file_transfer_t *file = files->data;
1063               
1064                switch( subcmd ) {
1065                case LIST:
1066                        if ( file->status == FT_STATUS_LISTENING )
1067                                irc_usermsg( irc, 
1068                                        "Pending file(id %d): %s (Listening...)", file->local_id, file->file_name);
1069                        else 
1070                        {
1071                                int kb_per_s = 0;
1072                                time_t diff = time( NULL ) - file->started ? : 1;
1073                                if ( ( file->started > 0 ) && ( file->bytes_transferred > 0 ) )
1074                                        kb_per_s = file->bytes_transferred / 1024 / diff;
1075                                       
1076                                irc_usermsg( irc, 
1077                                        "Pending file(id %d): %s (%10zd/%zd kb, %d kb/s)", file->local_id, file->file_name, 
1078                                        file->bytes_transferred/1024, file->file_size/1024, kb_per_s);
1079                        }
1080                        break;
1081                case REJECT:
1082                        if( file->status == FT_STATUS_LISTENING )
1083                        {
1084                                irc_usermsg( irc, "Rejecting file transfer for %s", file->file_name );
1085                                imcb_file_canceled( file->ic, file, "Denied by user" );
1086                        }
1087                        break;
1088                case CANCEL:
1089                        if( file->local_id == fid )
1090                        {
1091                                irc_usermsg( irc, "Canceling file transfer for %s", file->file_name );
1092                                imcb_file_canceled( file->ic, file, "Canceled by user" );
1093                        }
1094                        break;
1095                }
1096        }
1097}
1098
1099/* IMPORTANT: Keep this list sorted! The short command logic needs that. */
1100const command_t commands[] = {
1101        { "account",        1, cmd_account,        0 },
1102        { "add",            2, cmd_add,            0 },
1103        { "allow",          1, cmd_allow,          0 },
1104        { "blist",          0, cmd_blist,          0 },
1105        { "block",          1, cmd_block,          0 },
1106        { "channel",        1, cmd_channel,        0 },
1107        { "chat",           1, cmd_chat,           0 },
1108        { "drop",           1, cmd_drop,           0 },
1109        { "ft",             0, cmd_transfer,       0 },
1110        { "help",           0, cmd_help,           0 }, 
1111        { "identify",       1, cmd_identify,       0 },
1112        { "info",           1, cmd_info,           0 },
1113        { "no",             0, cmd_yesno,          0 },
1114        { "qlist",          0, cmd_qlist,          0 },
1115        { "register",       1, cmd_register,       0 },
1116        { "remove",         1, cmd_remove,         0 },
1117        { "rename",         2, cmd_rename,         0 },
1118        { "save",           0, cmd_save,           0 },
1119        { "set",            0, cmd_set,            0 },
1120        { "transfer",       0, cmd_transfer,       0 },
1121        { "yes",            0, cmd_yesno,          0 },
1122        { NULL }
1123};
Note: See TracBrowser for help on using the repository browser.