source: root_commands.c @ 84c3a72

Last change on this file since 84c3a72 was 84c3a72, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-27T12:39:07Z

Import chatrooms configured in older BitlBee versions. Settings are currently
ignored though. Also removing the old chat.[ch] files since they're really not
important anymore.

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