source: root_commands.c @ 6c2404e

Last change on this file since 6c2404e was 6c2404e, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-06T21:44:52Z

First part of the handshake, including sending a file descriptor to the
IPC master.

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