source: root_commands.c @ f1c2b21

Last change on this file since f1c2b21 was f1c2b21, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-10T15:08:02Z

Cleanup. Move some code to a more appropriate location, and show the old
connection a quit message instead of just breaking the connection.

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