source: root_commands.c @ b95932e

Last change on this file since b95932e was ba7d16f, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-24T17:12:53Z

Now seems like a pretty good time to finally kill crypting.c and storage_text.
This means people won't be able to upgrade from BitlBee 1.0 to this version
anymore but only via 1.2.

1.0 is old enough that I don't really expect this to be a problem.

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