source: root_commands.c @ c8eeadd

Last change on this file since c8eeadd was c8eeadd, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-04T10:16:07Z

Added automatic joining of channels. Auto-rejoin functionality for
groupchats not reimplemented yet but that's the next step.

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