source: root_commands.c @ 17a6ee9

Last change on this file since 17a6ee9 was 1f92a58, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-10T02:27:50Z

Restore the storage module.

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