source: root_commands.c @ 134a02c

Last change on this file since 134a02c was 134a02c, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-27T16:04:28Z

irc_channel_name_strip() instead of nick_strip().

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