source: root_commands.c @ b556e46

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

Merging main ui-fix.

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