source: root_commands.c @ c7eb771

Last change on this file since c7eb771 was c7eb771, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-28T00:18:40Z

Hacky support for short subcommands (i.e. "ac l" instead of "account list".).

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