Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • root_commands.c

    r0baed0d r7cd2e8a  
    2626#define BITLBEE_CORE
    2727#include "commands.h"
    28 #include "crypting.h"
    2928#include "bitlbee.h"
    3029#include "help.h"
     
    3332#include <string.h>
    3433
    35 void root_command_string( irc_t *irc, user_t *u, char *command, int flags )
    36 {
    37         char *cmd[IRC_MAX_ARGS];
    38         char *s;
    39         int k;
    40         char q = 0;
    41        
    42         memset( cmd, 0, sizeof( cmd ) );
    43         cmd[0] = command;
    44         k = 1;
    45         for( s = command; *s && k < ( IRC_MAX_ARGS - 1 ); s ++ )
    46                 if( *s == ' ' && !q )
    47                 {
    48                         *s = 0;
    49                         while( *++s == ' ' );
    50                         if( *s == '"' || *s == '\'' )
    51                         {
    52                                 q = *s;
    53                                 s ++;
    54                         }
    55                         if( *s )
    56                         {
    57                                 cmd[k++] = s;
    58                                 s --;
    59                         }
    60                         else
    61                         {
    62                                 break;
    63                         }
    64                 }
    65                 else if( *s == '\\' && ( ( !q && s[1] ) || ( q && q == s[1] ) ) )
    66                 {
    67                         char *cpy;
    68                        
    69                         for( cpy = s; *cpy; cpy ++ )
    70                                 cpy[0] = cpy[1];
    71                 }
    72                 else if( *s == q )
    73                 {
    74                         q = *s = 0;
    75                 }
    76         cmd[k] = NULL;
    77        
    78         root_command( irc, cmd );
     34void root_command_string( irc_t *irc, char *command )
     35{
     36        root_command( irc, split_command_parts( command ) );
    7937}
    8038
     
    9351void root_command( irc_t *irc, char *cmd[] )
    9452{       
    95         int i;
     53        int i, len;
    9654       
    9755        if( !cmd[0] )
    9856                return;
    9957       
     58        len = strlen( cmd[0] );
    10059        for( i = 0; commands[i].command; i++ )
    101                 if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 )
    102                 {
     60                if( g_strncasecmp( commands[i].command, cmd[0], len ) == 0 )
     61                {
     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                       
    10367                        MIN_ARGS( commands[i].required_parameters );
    10468                       
     
    141105static void cmd_identify( irc_t *irc, char **cmd )
    142106{
    143         storage_status_t status = storage_load( irc, cmd[1] );
     107        storage_status_t status;
    144108        char *account_on[] = { "account", "on", NULL };
    145        
    146         if( strchr( irc->umode, 'R' ) != NULL )
     109        gboolean load = TRUE;
     110        char *password = cmd[1];
     111       
     112        if( irc->status & USTATUS_IDENTIFIED )
    147113        {
    148114                irc_usermsg( irc, "You're already logged in." );
    149115                return;
    150116        }
     117       
     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 );
    151146       
    152147        switch (status) {
     
    158153                break;
    159154        case STORAGE_OK:
    160                 irc_usermsg( irc, "Password accepted, settings and accounts loaded" );
    161                 irc_setpass( irc, cmd[1] );
     155                irc_usermsg( irc, "Password accepted%s",
     156                             load ? ", settings and accounts loaded" : "" );
     157                irc_setpass( irc, password );
    162158                irc->status |= USTATUS_IDENTIFIED;
    163159                irc_umode_set( irc, "+R", 1 );
    164                 if( set_getbool( &irc->set, "auto_connect" ) )
     160                if( load && set_getbool( &irc->b->set, "auto_connect" ) )
    165161                        cmd_account( irc, account_on );
    166162                break;
     
    202198        storage_status_t status;
    203199       
    204         status = storage_remove (irc->nick, cmd[1]);
     200        status = storage_remove (irc->user->nick, cmd[1]);
    205201        switch (status) {
    206202        case STORAGE_NO_SUCH_USER:
     
    214210                irc->status &= ~USTATUS_IDENTIFIED;
    215211                irc_umode_set( irc, "-R", 1 );
    216                 irc_usermsg( irc, "Account `%s' removed", irc->nick );
     212                irc_usermsg( irc, "Account `%s' removed", irc->user->nick );
    217213                break;
    218214        default:
     
    222218}
    223219
    224 struct cmd_account_del_data
    225 {
    226         account_t *a;
    227         irc_t *irc;
    228 };
    229 
    230 void cmd_account_del_yes( void *data )
    231 {
    232         struct cmd_account_del_data *cad = data;
    233         account_t *a;
    234        
    235         for( a = cad->irc->accounts; a && a != cad->a; a = a->next );
    236        
    237         if( a == NULL )
    238         {
    239                 irc_usermsg( cad->irc, "Account already deleted" );
    240         }
    241         else if( a->ic )
    242         {
    243                 irc_usermsg( cad->irc, "Account is still logged in, can't delete" );
    244         }
    245         else
    246         {
    247                 account_del( cad->irc, a );
    248                 irc_usermsg( cad->irc, "Account deleted" );
    249         }
    250         g_free( data );
    251 }
    252 
    253 void cmd_account_del_no( void *data )
    254 {
    255         g_free( data );
     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!" );
    256228}
    257229
     
    286258                set_name = set_full;
    287259               
    288                 head = &irc->set;
     260                head = &irc->b->set;
    289261        }
    290262        else
     
    357329        account_t *a;
    358330       
    359         if( ( a = account_get( irc, id ) ) )
     331        if( ( a = account_get( irc->b, id ) ) )
    360332                return &a->set;
    361333        else
     
    405377                }
    406378
    407                 a = account_add( irc, prpl, cmd[3], cmd[4] );
     379                a = account_add( irc->b, prpl, cmd[3], cmd[4] );
    408380                if( cmd[5] )
    409381                {
     
    419391                MIN_ARGS( 2 );
    420392
    421                 if( !( a = account_get( irc, cmd[2] ) ) )
     393                if( !( a = account_get( irc->b, cmd[2] ) ) )
    422394                {
    423395                        irc_usermsg( irc, "Invalid account" );
     
    429401                else
    430402                {
    431                         struct cmd_account_del_data *cad;
    432                         char *msg;
    433                        
    434                         cad = g_malloc( sizeof( struct cmd_account_del_data ) );
    435                         cad->a = a;
    436                         cad->irc = irc;
    437                        
    438                         msg = g_strdup_printf( "If you remove this account (%s(%s)), BitlBee will "
    439                                                "also forget all your saved nicknames. If you want "
    440                                                "to change your username/password, use the `account "
    441                                                "set' command. Are you sure you want to delete this "
    442                                                "account?", a->prpl->name, a->user );
    443                         query_add( irc, NULL, msg, cmd_account_del_yes, cmd_account_del_no, cad );
    444                         g_free( msg );
     403                        account_del( irc->b, a );
     404                        irc_usermsg( irc, "Account deleted" );
    445405                }
    446406        }
     
    452412                        irc_usermsg( irc, "Account list:" );
    453413               
    454                 for( a = irc->accounts; a; a = a->next )
     414                for( a = irc->b->accounts; a; a = a->next )
    455415                {
    456416                        char *con;
     
    475435                if( cmd[2] )
    476436                {
    477                         if( ( a = account_get( irc, cmd[2] ) ) )
     437                        if( ( a = account_get( irc->b, cmd[2] ) ) )
    478438                        {
    479439                                if( a->ic )
     
    484444                                else
    485445                                {
    486                                         account_on( irc, a );
     446                                        account_on( irc->b, a );
    487447                                }
    488448                        }
     
    495455                else
    496456                {
    497                         if ( irc->accounts ) {
     457                        if ( irc->b->accounts )
     458                        {
    498459                                irc_usermsg( irc, "Trying to get all accounts connected..." );
    499460                       
    500                                 for( a = irc->accounts; a; a = a->next )
     461                                for( a = irc->b->accounts; a; a = a->next )
    501462                                        if( !a->ic && a->auto_connect )
    502                                                 account_on( irc, a );
     463                                                account_on( irc->b, a );
    503464                        }
    504465                        else
     
    514475                        irc_usermsg( irc, "Deactivating all active (re)connections..." );
    515476                       
    516                         for( a = irc->accounts; a; a = a->next )
     477                        for( a = irc->b->accounts; a; a = a->next )
    517478                        {
    518479                                if( a->ic )
    519                                         account_off( irc, a );
     480                                        account_off( irc->b, a );
    520481                                else if( a->reconnect )
    521482                                        cancel_auto_reconnect( a );
    522483                        }
    523484                }
    524                 else if( ( a = account_get( irc, cmd[2] ) ) )
     485                else if( ( a = account_get( irc->b, cmd[2] ) ) )
    525486                {
    526487                        if( a->ic )
    527488                        {
    528                                 account_off( irc, a );
     489                                account_off( irc->b, a );
    529490                        }
    530491                        else if( a->reconnect )
     
    557518}
    558519
     520static set_t **cmd_channel_set_findhead( irc_t *irc, char *id )
     521{
     522        irc_channel_t *ic;
     523       
     524        if( ( ic = irc_channel_get( irc, id ) ) )
     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        }
     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        }
     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                                          irc->default_channel->name );
     575        }
     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
    559582static void cmd_add( irc_t *irc, char **cmd )
    560583{
     
    569592        }
    570593       
    571         if( !( a = account_get( irc, cmd[1] ) ) )
     594        if( !( a = account_get( irc->b, cmd[1] ) ) )
    572595        {
    573596                irc_usermsg( irc, "Invalid account" );
     
    587610                        return;
    588611                }
    589                 else if( user_find( irc, cmd[3] ) )
     612                else if( irc_user_by_name( irc, cmd[3] ) )
    590613                {
    591614                        irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] );
     
    599622       
    600623        if( add_on_server )
    601                 a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL );
    602         else
    603                 /* Yeah, officially this is a call-*back*... So if we just
    604                    called add_buddy, we'll wait for the IM server to respond
    605                    before we do this. */
    606                 imcb_add_buddy( a->ic, cmd[2], NULL );
     624                a->prpl->add_buddy( a->ic, cmd[2], NULL );
     625        else
     626                /* Only for add -tmp. For regular adds, this callback will
     627                   be called once the IM server confirms. */
     628                bee_user_new( irc->b, a->ic, cmd[2], BEE_USER_LOCAL );
    607629       
    608630        irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2]  );
     631}
     632
     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 );
     648        //TODO(wilmer): bee_user_free() and/or let the IM mod do it? irc_user_free( irc, cmd[1] );
     649       
     650        irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] );
     651        g_free( s );
     652       
     653        return;
    609654}
    610655
     
    616661        if( !cmd[2] )
    617662        {
    618                 user_t *u = user_find( irc, cmd[1] );
    619                 if( !u || !u->ic )
     663                irc_user_t *iu = irc_user_by_name( irc, cmd[1] );
     664                if( !iu || !iu->bu )
    620665                {
    621666                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
    622667                        return;
    623668                }
    624                 ic = u->ic;
    625                 cmd[2] = u->handle;
    626         }
    627         else if( !( a = account_get( irc, cmd[1] ) ) )
     669                ic = iu->bu->ic;
     670                cmd[2] = iu->bu->handle;
     671        }
     672        else if( !( a = account_get( irc->b, cmd[1] ) ) )
    628673        {
    629674                irc_usermsg( irc, "Invalid account" );
     
    648693static void cmd_rename( irc_t *irc, char **cmd )
    649694{
    650         user_t *u;
    651        
    652         if( g_strcasecmp( cmd[1], irc->nick ) == 0 )
     695        irc_user_t *iu;
     696       
     697        iu = irc_user_by_name( irc, cmd[1] );
     698       
     699        if( iu == NULL )
     700        {
     701                irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
     702        }
     703        else if( iu == irc->user )
    653704        {
    654705                irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] );
    655706        }
    656         else if( g_strcasecmp( cmd[1], irc->channel ) == 0 )
    657         {
    658                 if( strchr( CTYPES, cmd[2][0] ) && nick_ok( cmd[2] + 1 ) )
    659                 {
    660                         u = user_find( irc, irc->nick );
    661                        
    662                         irc_part( irc, u, irc->channel );
    663                         g_free( irc->channel );
    664                         irc->channel = g_strdup( cmd[2] );
    665                         irc_join( irc, u, irc->channel );
    666                        
    667                         if( strcmp( cmd[0], "set_rename" ) != 0 )
    668                                 set_setstr( &irc->set, "control_channel", cmd[2] );
    669                 }
    670         }
    671         else if( user_find( irc, cmd[2] ) && ( nick_cmp( cmd[1], cmd[2] ) != 0 ) )
     707        else if( !nick_ok( cmd[2] ) )
     708        {
     709                irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
     710        }
     711        else if( irc_user_by_name( irc, cmd[2] ) )
    672712        {
    673713                irc_usermsg( irc, "Nick `%s' already exists", cmd[2] );
    674714        }
    675         else if( !nick_ok( cmd[2] ) )
    676         {
    677                 irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
    678         }
    679         else if( !( u = user_find( irc, cmd[1] ) ) )
    680         {
    681                 irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
    682         }
    683         else
    684         {
    685                 user_rename( irc, cmd[1], cmd[2] );
    686                 irc_write( irc, ":%s!%s@%s NICK %s", cmd[1], u->user, u->host, cmd[2] );
    687                 if( g_strcasecmp( cmd[1], irc->mynick ) == 0 )
    688                 {
    689                         g_free( irc->mynick );
    690                         irc->mynick = g_strdup( cmd[2] );
    691                        
     715        else
     716        {
     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 )
     724                {
    692725                        /* If we're called internally (user did "set root_nick"),
    693726                           let's not go O(INF). :-) */
    694727                        if( strcmp( cmd[0], "set_rename" ) != 0 )
    695                                 set_setstr( &irc->set, "root_nick", cmd[2] );
    696                 }
    697                 else if( u->send_handler == buddy_send_handler )
    698                 {
    699                         nick_set( u->ic->acc, u->handle, cmd[2] );
     728                                set_setstr( &irc->b->set, "root_nick", cmd[2] );
     729                }
     730                else if( iu->bu )
     731                {
     732                        nick_set( iu->bu->ic->acc, iu->bu->handle, cmd[2] );
    700733                }
    701734               
     
    708741        irc_t *irc = set->data;
    709742       
    710         if( strcmp( irc->mynick, new_nick ) != 0 )
    711         {
    712                 char *cmd[] = { "set_rename", irc->mynick, new_nick, NULL };
     743        if( strcmp( irc->root->nick, new_nick ) != 0 )
     744        {
     745                char *cmd[] = { "set_rename", irc->root->nick, new_nick, NULL };
    713746               
    714747                cmd_rename( irc, cmd );
    715748        }
    716749       
    717         return strcmp( irc->mynick, new_nick ) == 0 ? new_nick : SET_INVALID;
    718 }
    719 
    720 char *set_eval_control_channel( set_t *set, char *new_name )
    721 {
    722         irc_t *irc = set->data;
    723        
    724         if( strcmp( irc->channel, new_name ) != 0 )
    725         {
    726                 char *cmd[] = { "set_rename", irc->channel, new_name, NULL };
    727                
    728                 cmd_rename( irc, cmd );
    729         }
    730        
    731         return strcmp( irc->channel, new_name ) == 0 ? new_name : SET_INVALID;
    732 }
    733 
    734 static void cmd_remove( irc_t *irc, char **cmd )
    735 {
    736         user_t *u;
    737         char *s;
    738        
    739         if( !( u = user_find( irc, cmd[1] ) ) || !u->ic )
    740         {
    741                 irc_usermsg( irc, "Buddy `%s' not found", cmd[1] );
    742                 return;
    743         }
    744         s = g_strdup( u->handle );
    745        
    746         u->ic->acc->prpl->remove_buddy( u->ic, u->handle, NULL );
    747         nick_del( u->ic->acc, u->handle );
    748         user_del( irc, cmd[1] );
    749        
    750         irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] );
    751         g_free( s );
    752        
    753         return;
     750        return strcmp( irc->root->nick, new_nick ) == 0 ? new_nick : SET_INVALID;
    754751}
    755752
     
    759756        account_t *a;
    760757       
    761         if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
     758        if( !cmd[2] && ( a = account_get( irc->b, cmd[1] ) ) && a->ic )
    762759        {
    763760                char *format;
     
    772769                for( l = a->ic->deny; l; l = l->next )
    773770                {
    774                         user_t *u = user_findhandle( a->ic, l->data );
    775                         irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
     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)" );
    776774                }
    777775                irc_usermsg( irc, "End of list." );
     
    781779        else if( !cmd[2] )
    782780        {
    783                 user_t *u = user_find( irc, cmd[1] );
    784                 if( !u || !u->ic )
     781                irc_user_t *iu = irc_user_by_name( irc, cmd[1] );
     782                if( !iu || !iu->bu )
    785783                {
    786784                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
    787785                        return;
    788786                }
    789                 ic = u->ic;
    790                 cmd[2] = u->handle;
    791         }
    792         else if( !( a = account_get( irc, cmd[1] ) ) )
     787                ic = iu->bu->ic;
     788                cmd[2] = iu->bu->handle;
     789        }
     790        else if( !( a = account_get( irc->b, cmd[1] ) ) )
    793791        {
    794792                irc_usermsg( irc, "Invalid account" );
     
    818816        account_t *a;
    819817       
    820         if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
     818        if( !cmd[2] && ( a = account_get( irc->b, cmd[1] ) ) && a->ic )
    821819        {
    822820                char *format;
     
    831829                for( l = a->ic->permit; l; l = l->next )
    832830                {
    833                         user_t *u = user_findhandle( a->ic, l->data );
    834                         irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
     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)" );
    835834                }
    836835                irc_usermsg( irc, "End of list." );
     
    840839        else if( !cmd[2] )
    841840        {
    842                 user_t *u = user_find( irc, cmd[1] );
    843                 if( !u || !u->ic )
     841                irc_user_t *iu = irc_user_by_name( irc, cmd[1] );
     842                if( !iu || !iu->bu )
    844843                {
    845844                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
    846845                        return;
    847846                }
    848                 ic = u->ic;
    849                 cmd[2] = u->handle;
    850         }
    851         else if( !( a = account_get( irc, cmd[1] ) ) )
     847                ic = iu->bu->ic;
     848                cmd[2] = iu->bu->handle;
     849        }
     850        else if( !( a = account_get( irc->b, cmd[1] ) ) )
    852851        {
    853852                irc_usermsg( irc, "Invalid account" );
     
    916915}
    917916
    918 static void cmd_save( irc_t *irc, char **cmd )
    919 {
    920         if( ( irc->status & USTATUS_IDENTIFIED ) == 0 )
    921                 irc_usermsg( irc, "Please create an account first" );
    922         else if( storage_save( irc, NULL, TRUE ) == STORAGE_OK )
    923                 irc_usermsg( irc, "Configuration saved" );
    924         else
    925                 irc_usermsg( irc, "Configuration could not be saved!" );
    926 }
    927 
    928917static void cmd_blist( irc_t *irc, char **cmd )
    929918{
    930919        int online = 0, away = 0, offline = 0;
    931         user_t *u;
     920        GSList *l;
    932921        char s[256];
    933922        char *format;
     
    950939                format = "%-16.16s  %-40.40s  %s";
    951940       
    952         irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" );
    953        
    954         for( u = irc->users; u; u = u->next ) if( u->ic && u->online && !u->away )
    955         {
     941        irc_usermsg( irc, format, "Nick", "Handle/Account", "Status" );
     942       
     943        for( l = irc->users; l; l = l->next )
     944        {
     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               
    956951                if( online == 1 )
    957952                {
    958953                        char st[256] = "Online";
    959954                       
    960                         if( u->status_msg )
    961                                 g_snprintf( st, sizeof( st ) - 1, "Online (%s)", u->status_msg );
     955                        if( bu->status_msg )
     956                                g_snprintf( st, sizeof( st ) - 1, "Online (%s)", bu->status_msg );
    962957                       
    963                         g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
    964                         irc_usermsg( irc, format, u->nick, s, st );
     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 );
    965960                }
    966961               
     
    968963        }
    969964
    970         for( u = irc->users; u; u = u->next ) if( u->ic && u->online && u->away )
    971         {
     965        for( l = irc->users; l; l = l->next )
     966        {
     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               
    972973                if( away == 1 )
    973974                {
    974                         g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
    975                         irc_usermsg( irc, format, u->nick, s, u->away );
     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 ) );
    976977                }
    977978                n_away ++;
    978979        }
    979980       
    980         for( u = irc->users; u; u = u->next ) if( u->ic && !u->online )
    981         {
     981        for( l = irc->users; l; l = l->next )
     982        {
     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               
    982989                if( offline == 1 )
    983990                {
    984                         g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
    985                         irc_usermsg( irc, format, u->nick, s, "Offline" );
     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" );
    986993                }
    987994                n_offline ++;
     
    989996       
    990997        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
    991 }
    992 
    993 static void cmd_nick( irc_t *irc, char **cmd )
    994 {
    995         account_t *a;
    996 
    997         if( !cmd[1] || !( a = account_get( irc, cmd[1] ) ) )
    998         {
    999                 irc_usermsg( irc, "Invalid account");
    1000         }
    1001         else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
    1002         {
    1003                 irc_usermsg( irc, "That account is not on-line" );
    1004         }
    1005         else if ( !cmd[2] )
    1006         {
    1007                 irc_usermsg( irc, "Your name is `%s'" , a->ic->displayname ? a->ic->displayname : "NULL" );
    1008         }
    1009         else if ( !a->prpl->set_my_name )
    1010         {
    1011                 irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
    1012         }
    1013         else
    1014         {
    1015                 irc_usermsg( irc, "Setting your name to `%s'", cmd[2] );
    1016                
    1017                 a->prpl->set_my_name( a->ic, cmd[2] );
    1018         }
    1019998}
    1020999
     
    10391018}
    10401019
    1041 static void cmd_join_chat( irc_t *irc, char **cmd )
    1042 {
    1043         irc_usermsg( irc, "This command is now obsolete. "
    1044                           "Please try the `chat' command instead." );
    1045 }
    1046 
    1047 static set_t **cmd_chat_set_findhead( irc_t *irc, char *id )
    1048 {
    1049         struct chat *c;
    1050        
    1051         if( ( c = chat_get( irc, id ) ) )
    1052                 return &c->set;
    1053         else
    1054                 return NULL;
    1055 }
    1056 
    10571020static void cmd_chat( irc_t *irc, char **cmd )
    10581021{
    10591022        account_t *acc;
    1060         struct chat *c;
    10611023       
    10621024        if( g_strcasecmp( cmd[1], "add" ) == 0 )
    10631025        {
    10641026                char *channel, *s;
     1027                struct irc_channel *ic;
    10651028               
    10661029                MIN_ARGS( 3 );
    10671030               
    1068                 if( !( acc = account_get( irc, cmd[2] ) ) )
     1031                if( !( acc = account_get( irc->b, cmd[2] ) ) )
    10691032                {
    10701033                        irc_usermsg( irc, "Invalid account" );
     1034                        return;
     1035                }
     1036                else if( !acc->prpl->chat_join )
     1037                {
     1038                        irc_usermsg( irc, "Named chatrooms not supported on that account." );
    10711039                        return;
    10721040                }
     
    10851053                if( strchr( CTYPES, channel[0] ) == NULL )
    10861054                {
    1087                         s = g_strdup_printf( "%c%s", CTYPES[0], channel );
     1055                        s = g_strdup_printf( "#%s", channel );
    10881056                        g_free( channel );
    10891057                        channel = s;
    10901058                }
    10911059               
    1092                 if( ( c = chat_add( irc, acc, cmd[3], channel ) ) )
    1093                         irc_usermsg( irc, "Chatroom added successfully." );
    1094                 else
     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
     1068                {
     1069                        if( ic )
     1070                                irc_channel_free( ic );
     1071                       
    10951072                        irc_usermsg( irc, "Could not add chatroom." );
    1096                
     1073                }
    10971074                g_free( channel );
    10981075        }
    1099         else if( g_strcasecmp( cmd[1], "list" ) == 0 )
    1100         {
    1101                 int i = 0;
    1102                
    1103                 if( strchr( irc->umode, 'b' ) )
    1104                         irc_usermsg( irc, "Chatroom list:" );
    1105                
    1106                 for( c = irc->chatrooms; c; c = c->next )
    1107                 {
    1108                         irc_usermsg( irc, "%2d. %s(%s) %s, %s", i, c->acc->prpl->name,
    1109                                           c->acc->user, c->handle, c->channel );
    1110                        
    1111                         i ++;
    1112                 }
    1113                 irc_usermsg( irc, "End of chatroom list" );
    1114         }
    1115         else if( g_strcasecmp( cmd[1], "set" ) == 0 )
    1116         {
     1076        else if( g_strcasecmp( cmd[1], "with" ) == 0 )
     1077        {
     1078                irc_user_t *iu;
     1079               
    11171080                MIN_ARGS( 2 );
    11181081               
    1119                 cmd_set_real( irc, cmd + 1, cmd_chat_set_findhead, NULL );
    1120         }
    1121         else if( g_strcasecmp( cmd[1], "del" ) == 0 )
    1122         {
    1123                 MIN_ARGS( 2 );
    1124                
    1125                 if( ( c = chat_get( irc, cmd[2] ) ) )
    1126                 {
    1127                         chat_del( irc, c );
    1128                 }
    1129                 else
    1130                 {
    1131                         irc_usermsg( irc, "Could not remove chat." );
    1132                 }
    1133         }
    1134         else if( g_strcasecmp( cmd[1], "with" ) == 0 )
    1135         {
    1136                 user_t *u;
    1137                
    1138                 MIN_ARGS( 2 );
    1139                
    1140                 if( ( u = user_find( irc, cmd[2] ) ) && u->ic && u->ic->acc->prpl->chat_with )
    1141                 {
    1142                         if( !u->ic->acc->prpl->chat_with( u->ic, u->handle ) )
     1082                if( ( iu = irc_user_by_name( irc, cmd[2] ) ) &&
     1083                    iu->bu && iu->bu->ic->acc->prpl->chat_with )
     1084                {
     1085                        if( !iu->bu->ic->acc->prpl->chat_with( iu->bu->ic, iu->bu->handle ) )
    11431086                        {
    11441087                                irc_usermsg( irc, "(Possible) failure while trying to open "
    1145                                                   "a groupchat with %s.", u->nick );
     1088                                                  "a groupchat with %s.", iu->nick );
    11461089                        }
    11471090                }
     
    11511094                }
    11521095        }
     1096        else if( g_strcasecmp( cmd[1], "list" ) == 0 ||
     1097                 g_strcasecmp( cmd[1], "set" ) == 0 ||
     1098                 g_strcasecmp( cmd[1], "del" ) == 0 )
     1099        {
     1100                irc_usermsg( irc, "Warning: The \002chat\002 command was mostly replaced with the \002channel\002 command." );
     1101                cmd_channel( irc, cmd );
     1102        }
    11531103        else
    11541104        {
     
    11571107}
    11581108
     1109static void cmd_transfer( irc_t *irc, char **cmd )
     1110{
     1111        GSList *files = irc->file_transfers;
     1112        enum { LIST, REJECT, CANCEL };
     1113        int subcmd = LIST;
     1114        int fid;
     1115
     1116        if( !files )
     1117        {
     1118                irc_usermsg( irc, "No pending transfers" );
     1119                return;
     1120        }
     1121
     1122        if( cmd[1] && ( strcmp( cmd[1], "reject" ) == 0 ) )
     1123        {
     1124                subcmd = REJECT;
     1125        }
     1126        else if( cmd[1] && ( strcmp( cmd[1], "cancel" ) == 0 ) &&
     1127                 cmd[2] && ( sscanf( cmd[2], "%d", &fid ) == 1 ) )
     1128        {
     1129                subcmd = CANCEL;
     1130        }
     1131
     1132        for( ; files; files = g_slist_next( files ) )
     1133        {
     1134                file_transfer_t *file = files->data;
     1135               
     1136                switch( subcmd ) {
     1137                case LIST:
     1138                        if ( file->status == FT_STATUS_LISTENING )
     1139                                irc_usermsg( irc,
     1140                                        "Pending file(id %d): %s (Listening...)", file->local_id, file->file_name);
     1141                        else
     1142                        {
     1143                                int kb_per_s = 0;
     1144                                time_t diff = time( NULL ) - file->started ? : 1;
     1145                                if ( ( file->started > 0 ) && ( file->bytes_transferred > 0 ) )
     1146                                        kb_per_s = file->bytes_transferred / 1024 / diff;
     1147                                       
     1148                                irc_usermsg( irc,
     1149                                        "Pending file(id %d): %s (%10zd/%zd kb, %d kb/s)", file->local_id, file->file_name,
     1150                                        file->bytes_transferred/1024, file->file_size/1024, kb_per_s);
     1151                        }
     1152                        break;
     1153                case REJECT:
     1154                        if( file->status == FT_STATUS_LISTENING )
     1155                        {
     1156                                irc_usermsg( irc, "Rejecting file transfer for %s", file->file_name );
     1157                                imcb_file_canceled( file->ic, file, "Denied by user" );
     1158                        }
     1159                        break;
     1160                case CANCEL:
     1161                        if( file->local_id == fid )
     1162                        {
     1163                                irc_usermsg( irc, "Canceling file transfer for %s", file->file_name );
     1164                                imcb_file_canceled( file->ic, file, "Canceled by user" );
     1165                        }
     1166                        break;
     1167                }
     1168        }
     1169}
     1170
     1171/* IMPORTANT: Keep this list sorted! The short command logic needs that. */
    11591172const command_t commands[] = {
     1173        { "account",        1, cmd_account,        0 },
     1174        { "add",            2, cmd_add,            0 },
     1175        { "allow",          1, cmd_allow,          0 },
     1176        { "blist",          0, cmd_blist,          0 },
     1177        { "block",          1, cmd_block,          0 },
     1178        { "channel",        1, cmd_channel,        0 },
     1179        { "chat",           1, cmd_chat,           0 },
     1180        { "drop",           1, cmd_drop,           0 },
     1181        { "ft",             0, cmd_transfer,       0 },
    11601182        { "help",           0, cmd_help,           0 },
    11611183        { "identify",       1, cmd_identify,       0 },
     1184        { "info",           1, cmd_info,           0 },
     1185        { "no",             0, cmd_yesno,          0 },
     1186        { "qlist",          0, cmd_qlist,          0 },
    11621187        { "register",       1, cmd_register,       0 },
    1163         { "drop",           1, cmd_drop,           0 },
    1164         { "account",        1, cmd_account,        0 },
    1165         { "add",            2, cmd_add,            0 },
    1166         { "info",           1, cmd_info,           0 },
     1188        { "remove",         1, cmd_remove,         0 },
    11671189        { "rename",         2, cmd_rename,         0 },
    1168         { "remove",         1, cmd_remove,         0 },
    1169         { "block",          1, cmd_block,          0 },
    1170         { "allow",          1, cmd_allow,          0 },
    11711190        { "save",           0, cmd_save,           0 },
    11721191        { "set",            0, cmd_set,            0 },
     1192        { "transfer",       0, cmd_transfer,       0 },
    11731193        { "yes",            0, cmd_yesno,          0 },
    1174         { "no",             0, cmd_yesno,          0 },
    1175         { "blist",          0, cmd_blist,          0 },
    1176         { "nick",           1, cmd_nick,           0 },
    1177         { "qlist",          0, cmd_qlist,          0 },
    1178         { "join_chat",      2, cmd_join_chat,      0 },
    1179         { "chat",           1, cmd_chat,           0 },
    11801194        { NULL }
    11811195};
Note: See TracChangeset for help on using the changeset viewer.