source: root_commands.c @ d7db346

Last change on this file since d7db346 was d7db346, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-08T22:44:16Z

Some cleanup improvements.

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