source: root_commands.c @ f3b6764

Last change on this file since f3b6764 was b556e46, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-09T23:25:07Z

Merging main ui-fix.

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