source: root_commands.c @ dbb0ce3

Last change on this file since dbb0ce3 was dbb0ce3, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-11T19:38:45Z

Restored add/remove command and, oh, yes/no commands. Nice to be able to
answer the questions..

  • Property mode set to 100644
File size: 27.1 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
569static void cmd_add( irc_t *irc, char **cmd )
570{
571        account_t *a;
572        int add_on_server = 1;
573       
574        if( g_strcasecmp( cmd[1], "-tmp" ) == 0 )
575        {
576                MIN_ARGS( 3 );
577                add_on_server = 0;
578                cmd ++;
579        }
580       
581        if( !( a = account_get( irc->b, cmd[1] ) ) )
582        {
583                irc_usermsg( irc, "Invalid account" );
584                return;
585        }
586        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
587        {
588                irc_usermsg( irc, "That account is not on-line" );
589                return;
590        }
591       
592        if( cmd[3] )
593        {
594                if( !nick_ok( cmd[3] ) )
595                {
596                        irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] );
597                        return;
598                }
599                else if( irc_user_by_name( irc, cmd[3] ) )
600                {
601                        irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] );
602                        return;
603                }
604                else
605                {
606                        nick_set( a, cmd[2], cmd[3] );
607                }
608        }
609       
610        if( add_on_server )
611                a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL );
612        else
613                /* Only for add -tmp. For regular adds, this callback will
614                   be called once the IM server confirms. */
615                bee_user_new( irc->b, a->ic, cmd[2] );
616       
617        irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2]  );
618}
619
620static void cmd_remove( irc_t *irc, char **cmd )
621{
622        irc_user_t *iu;
623        bee_user_t *bu;
624        char *s;
625       
626        if( !( iu = irc_user_by_name( irc, cmd[1] ) ) || !( bu = iu->bu ) )
627        {
628                irc_usermsg( irc, "Buddy `%s' not found", cmd[1] );
629                return;
630        }
631        s = g_strdup( bu->handle );
632       
633        bu->ic->acc->prpl->remove_buddy( bu->ic, bu->handle, NULL );
634        nick_del( bu->ic->acc, bu->handle );
635        irc_user_free( irc, cmd[1] );
636       
637        irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] );
638        g_free( s );
639       
640        return;
641}
642
643#if 0
644static void cmd_info( irc_t *irc, char **cmd )
645{
646        struct im_connection *ic;
647        account_t *a;
648       
649        if( !cmd[2] )
650        {
651                user_t *u = user_find( irc, cmd[1] );
652                if( !u || !u->ic )
653                {
654                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
655                        return;
656                }
657                ic = u->ic;
658                cmd[2] = u->handle;
659        }
660        else if( !( a = account_get( irc, cmd[1] ) ) )
661        {
662                irc_usermsg( irc, "Invalid account" );
663                return;
664        }
665        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
666        {
667                irc_usermsg( irc, "That account is not on-line" );
668                return;
669        }
670       
671        if( !ic->acc->prpl->get_info )
672        {
673                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
674        }
675        else
676        {
677                ic->acc->prpl->get_info( ic, cmd[2] );
678        }
679}
680#endif
681
682static void cmd_rename( irc_t *irc, char **cmd )
683{
684        irc_user_t *iu;
685       
686        iu = irc_user_by_name( irc, cmd[1] );
687       
688        if( iu == NULL )
689        {
690                irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
691        }
692        else if( iu == irc->user )
693        {
694                irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] );
695        }
696        else if( !nick_ok( cmd[2] ) )
697        {
698                irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
699        }
700        else if( irc_user_by_name( irc, cmd[2] ) )
701        {
702                irc_usermsg( irc, "Nick `%s' already exists", cmd[2] );
703        }
704        else
705        {
706                if( !irc_user_set_nick( iu, cmd[2] ) )
707                {
708                        irc_usermsg( irc, "Error while changing nick" );
709                        return;
710                }
711               
712                if( iu == irc->root )
713                {
714                        /* If we're called internally (user did "set root_nick"),
715                           let's not go O(INF). :-) */
716                        if( strcmp( cmd[0], "set_rename" ) != 0 )
717                                set_setstr( &irc->b->set, "root_nick", cmd[2] );
718                }
719                else if( iu->bu )
720                {
721                        nick_set( iu->bu->ic->acc, iu->bu->handle, cmd[2] );
722                }
723               
724                irc_usermsg( irc, "Nick successfully changed" );
725        }
726}
727
728#if 0
729char *set_eval_root_nick( set_t *set, char *new_nick )
730{
731        irc_t *irc = set->data;
732       
733        if( strcmp( irc->mynick, new_nick ) != 0 )
734        {
735                char *cmd[] = { "set_rename", irc->mynick, new_nick, NULL };
736               
737                cmd_rename( irc, cmd );
738        }
739       
740        return strcmp( irc->mynick, new_nick ) == 0 ? new_nick : SET_INVALID;
741}
742
743char *set_eval_control_channel( set_t *set, char *new_name )
744{
745        irc_t *irc = set->data;
746       
747        if( strcmp( irc->channel, new_name ) != 0 )
748        {
749                char *cmd[] = { "set_rename", irc->channel, new_name, NULL };
750               
751                cmd_rename( irc, cmd );
752        }
753       
754        return strcmp( irc->channel, new_name ) == 0 ? new_name : SET_INVALID;
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#endif
876
877static void cmd_yesno( irc_t *irc, char **cmd )
878{
879        query_t *q = NULL;
880        int numq = 0;
881       
882        if( irc->queries == NULL )
883        {
884                irc_usermsg( irc, "Did I ask you something?" );
885                return;
886        }
887       
888        /* If there's an argument, the user seems to want to answer another question than the
889           first/last (depending on the query_order setting) one. */
890        if( cmd[1] )
891        {
892                if( sscanf( cmd[1], "%d", &numq ) != 1 )
893                {
894                        irc_usermsg( irc, "Invalid query number" );
895                        return;
896                }
897               
898                for( q = irc->queries; q; q = q->next, numq -- )
899                        if( numq == 0 )
900                                break;
901               
902                if( !q )
903                {
904                        irc_usermsg( irc, "Uhm, I never asked you something like that..." );
905                        return;
906                }
907        }
908       
909        if( g_strcasecmp( cmd[0], "yes" ) == 0 )
910                query_answer( irc, q, 1 );
911        else if( g_strcasecmp( cmd[0], "no" ) == 0 )
912                query_answer( irc, q, 0 );
913}
914
915static void cmd_set( irc_t *irc, char **cmd )
916{
917        cmd_set_real( irc, cmd, NULL, NULL );
918}
919
920#if 0
921static void cmd_blist( irc_t *irc, char **cmd )
922{
923        int online = 0, away = 0, offline = 0;
924        user_t *u;
925        char s[256];
926        char *format;
927        int n_online = 0, n_away = 0, n_offline = 0;
928       
929        if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 )
930                online = offline = away = 1;
931        else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 )
932                offline = 1;
933        else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 )
934                away = 1;
935        else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 )
936                online = 1;
937        else
938                online = away = 1;
939       
940        if( strchr( irc->umode, 'b' ) != NULL )
941                format = "%s\t%s\t%s";
942        else
943                format = "%-16.16s  %-40.40s  %s";
944       
945        irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" );
946       
947        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && !u->away )
948        {
949                if( online == 1 )
950                {
951                        char st[256] = "Online";
952                       
953                        if( u->status_msg )
954                                g_snprintf( st, sizeof( st ) - 1, "Online (%s)", u->status_msg );
955                       
956                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
957                        irc_usermsg( irc, format, u->nick, s, st );
958                }
959               
960                n_online ++;
961        }
962
963        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && u->away )
964        {
965                if( away == 1 )
966                {
967                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
968                        irc_usermsg( irc, format, u->nick, s, u->away );
969                }
970                n_away ++;
971        }
972       
973        for( u = irc->users; u; u = u->next ) if( u->ic && !u->online )
974        {
975                if( offline == 1 )
976                {
977                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
978                        irc_usermsg( irc, format, u->nick, s, "Offline" );
979                }
980                n_offline ++;
981        }
982       
983        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
984}
985
986static void cmd_qlist( irc_t *irc, char **cmd )
987{
988        query_t *q = irc->queries;
989        int num;
990       
991        if( !q )
992        {
993                irc_usermsg( irc, "There are no pending questions." );
994                return;
995        }
996       
997        irc_usermsg( irc, "Pending queries:" );
998       
999        for( num = 0; q; q = q->next, num ++ )
1000                if( q->ic ) /* Not necessary yet, but it might come later */
1001                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question );
1002                else
1003                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
1004}
1005
1006static set_t **cmd_chat_set_findhead( irc_t *irc, char *id )
1007{
1008        struct chat *c;
1009       
1010        if( ( c = chat_get( irc, id ) ) )
1011                return &c->set;
1012        else
1013                return NULL;
1014}
1015
1016static void cmd_chat( irc_t *irc, char **cmd )
1017{
1018        account_t *acc;
1019        struct chat *c;
1020       
1021        if( g_strcasecmp( cmd[1], "add" ) == 0 )
1022        {
1023                char *channel, *s;
1024               
1025                MIN_ARGS( 3 );
1026               
1027                if( !( acc = account_get( irc, cmd[2] ) ) )
1028                {
1029                        irc_usermsg( irc, "Invalid account" );
1030                        return;
1031                }
1032               
1033                if( cmd[4] == NULL )
1034                {
1035                        channel = g_strdup( cmd[3] );
1036                        if( ( s = strchr( channel, '@' ) ) )
1037                                *s = 0;
1038                }
1039                else
1040                {
1041                        channel = g_strdup( cmd[4] );
1042                }
1043               
1044                if( strchr( CTYPES, channel[0] ) == NULL )
1045                {
1046                        s = g_strdup_printf( "%c%s", CTYPES[0], channel );
1047                        g_free( channel );
1048                        channel = s;
1049                }
1050               
1051                if( ( c = chat_add( irc, acc, cmd[3], channel ) ) )
1052                        irc_usermsg( irc, "Chatroom added successfully." );
1053                else
1054                        irc_usermsg( irc, "Could not add chatroom." );
1055               
1056                g_free( channel );
1057        }
1058        else if( g_strcasecmp( cmd[1], "list" ) == 0 )
1059        {
1060                int i = 0;
1061               
1062                if( strchr( irc->umode, 'b' ) )
1063                        irc_usermsg( irc, "Chatroom list:" );
1064               
1065                for( c = irc->chatrooms; c; c = c->next )
1066                {
1067                        irc_usermsg( irc, "%2d. %s(%s) %s, %s", i, c->acc->prpl->name,
1068                                          c->acc->user, c->handle, c->channel );
1069                       
1070                        i ++;
1071                }
1072                irc_usermsg( irc, "End of chatroom list" );
1073        }
1074        else if( g_strcasecmp( cmd[1], "set" ) == 0 )
1075        {
1076                MIN_ARGS( 2 );
1077               
1078                cmd_set_real( irc, cmd + 1, cmd_chat_set_findhead, NULL );
1079        }
1080        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
1081        {
1082                MIN_ARGS( 2 );
1083               
1084                if( ( c = chat_get( irc, cmd[2] ) ) )
1085                {
1086                        chat_del( irc, c );
1087                }
1088                else
1089                {
1090                        irc_usermsg( irc, "Could not remove chat." );
1091                }
1092        }
1093        else if( g_strcasecmp( cmd[1], "with" ) == 0 )
1094        {
1095                user_t *u;
1096               
1097                MIN_ARGS( 2 );
1098               
1099                if( ( u = user_find( irc, cmd[2] ) ) && u->ic && u->ic->acc->prpl->chat_with )
1100                {
1101                        if( !u->ic->acc->prpl->chat_with( u->ic, u->handle ) )
1102                        {
1103                                irc_usermsg( irc, "(Possible) failure while trying to open "
1104                                                  "a groupchat with %s.", u->nick );
1105                        }
1106                }
1107                else
1108                {
1109                        irc_usermsg( irc, "Can't open a groupchat with %s.", cmd[2] );
1110                }
1111        }
1112        else
1113        {
1114                irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat", cmd[1] );
1115        }
1116}
1117
1118static void cmd_transfer( irc_t *irc, char **cmd )
1119{
1120        GSList *files = irc->file_transfers;
1121        enum { LIST, REJECT, CANCEL };
1122        int subcmd = LIST;
1123        int fid;
1124
1125        if( !files )
1126        {
1127                irc_usermsg( irc, "No pending transfers" );
1128                return;
1129        }
1130
1131        if( cmd[1] && ( strcmp( cmd[1], "reject" ) == 0 ) )
1132        {
1133                subcmd = REJECT;
1134        }
1135        else if( cmd[1] && ( strcmp( cmd[1], "cancel" ) == 0 ) &&
1136                 cmd[2] && ( sscanf( cmd[2], "%d", &fid ) == 1 ) )
1137        {
1138                subcmd = CANCEL;
1139        }
1140
1141        for( ; files; files = g_slist_next( files ) )
1142        {
1143                file_transfer_t *file = files->data;
1144               
1145                switch( subcmd ) {
1146                case LIST:
1147                        if ( file->status == FT_STATUS_LISTENING )
1148                                irc_usermsg( irc,
1149                                        "Pending file(id %d): %s (Listening...)", file->local_id, file->file_name);
1150                        else
1151                        {
1152                                int kb_per_s = 0;
1153                                time_t diff = time( NULL ) - file->started ? : 1;
1154                                if ( ( file->started > 0 ) && ( file->bytes_transferred > 0 ) )
1155                                        kb_per_s = file->bytes_transferred / 1024 / diff;
1156                                       
1157                                irc_usermsg( irc,
1158                                        "Pending file(id %d): %s (%10zd/%zd kb, %d kb/s)", file->local_id, file->file_name,
1159                                        file->bytes_transferred/1024, file->file_size/1024, kb_per_s);
1160                        }
1161                        break;
1162                case REJECT:
1163                        if( file->status == FT_STATUS_LISTENING )
1164                        {
1165                                irc_usermsg( irc, "Rejecting file transfer for %s", file->file_name );
1166                                imcb_file_canceled( file, "Denied by user" );
1167                        }
1168                        break;
1169                case CANCEL:
1170                        if( file->local_id == fid )
1171                        {
1172                                irc_usermsg( irc, "Canceling file transfer for %s", file->file_name );
1173                                imcb_file_canceled( file, "Canceled by user" );
1174                        }
1175                        break;
1176                }
1177        }
1178}
1179#endif
1180
1181const command_t commands[] = {
1182        { "help",           0, cmd_help,           0 }, 
1183        { "account",        1, cmd_account,        0 },
1184        { "identify",       1, cmd_identify,       0 },
1185        { "register",       1, cmd_register,       0 },
1186        { "drop",           1, cmd_drop,           0 },
1187        { "save",           0, cmd_save,           0 },
1188        { "add",            2, cmd_add,            0 },
1189        { "remove",         1, cmd_remove,         0 },
1190#if 0
1191        { "info",           1, cmd_info,           0 },
1192#endif
1193        { "rename",         2, cmd_rename,         0 },
1194#if 0
1195        { "block",          1, cmd_block,          0 },
1196        { "allow",          1, cmd_allow,          0 },
1197#endif
1198        { "set",            0, cmd_set,            0 },
1199        { "yes",            0, cmd_yesno,          0 },
1200        { "no",             0, cmd_yesno,          0 },
1201#if 0
1202        { "blist",          0, cmd_blist,          0 },
1203        { "nick",           1, cmd_nick,           0 },
1204        { "qlist",          0, cmd_qlist,          0 },
1205        { "join_chat",      2, cmd_join_chat,      0 },
1206        { "chat",           1, cmd_chat,           0 },
1207        { "transfer",       0, cmd_transfer,       0 },
1208#endif
1209        { NULL }
1210};
Note: See TracBrowser for help on using the repository browser.