Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • root_commands.c

    r23c4e64 re9cf291  
    2929#include "bitlbee.h"
    3030#include "help.h"
     31#include "chat.h"
    3132
    3233#include <string.h>
     
    7879}
    7980
     81#define MIN_ARGS( x, y... )                                                    \
     82        do                                                                     \
     83        {                                                                      \
     84                int blaat;                                                     \
     85                for( blaat = 0; blaat <= x; blaat ++ )                         \
     86                        if( cmd[blaat] == NULL )                               \
     87                        {                                                      \
     88                                irc_usermsg( irc, "Not enough parameters given (need %d).", x ); \
     89                                return y;                                      \
     90                        }                                                      \
     91        } while( 0 )
     92
    8093void root_command( irc_t *irc, char *cmd[] )
    8194{       
     
    88101                if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 )
    89102                {
    90                         if( !cmd[commands[i].required_parameters] )
    91                         {
    92                                 irc_usermsg( irc, "Not enough parameters given (need %d)", commands[i].required_parameters );
    93                                 return;
    94                         }
     103                        MIN_ARGS( commands[i].required_parameters );
     104                       
    95105                        commands[i].execute( irc, cmd );
    96106                        return;
     
    131141static void cmd_identify( irc_t *irc, char **cmd )
    132142{
    133         storage_status_t status = storage_load( irc->nick, cmd[1], irc );
     143        storage_status_t status = storage_load( irc, cmd[1] );
    134144        char *account_on[] = { "account", "on", NULL };
     145       
     146        if( strchr( irc->umode, 'R' ) != NULL )
     147        {
     148                irc_usermsg( irc, "You're already logged in." );
     149                return;
     150        }
    135151       
    136152        switch (status) {
     
    143159        case STORAGE_OK:
    144160                irc_usermsg( irc, "Password accepted, settings and accounts loaded" );
     161                irc_setpass( irc, cmd[1] );
     162                irc->status |= USTATUS_IDENTIFIED;
    145163                irc_umode_set( irc, "+R", 1 );
    146164                if( set_getbool( &irc->set, "auto_connect" ) )
     
    162180        }
    163181
    164         irc_setpass( irc, cmd[1] );
    165         switch( storage_save( irc, FALSE )) {
     182        switch( storage_save( irc, cmd[1], FALSE ) ) {
    166183                case STORAGE_ALREADY_EXISTS:
    167184                        irc_usermsg( irc, "Nick is already registered" );
     
    170187                case STORAGE_OK:
    171188                        irc_usermsg( irc, "Account successfully created" );
     189                        irc_setpass( irc, cmd[1] );
    172190                        irc->status |= USTATUS_IDENTIFIED;
    173191                        irc_umode_set( irc, "+R", 1 );
     
    238256}
    239257
     258static void cmd_showset( irc_t *irc, set_t **head, char *key )
     259{
     260        char *val;
     261       
     262        if( ( val = set_getstr( head, key ) ) )
     263                irc_usermsg( irc, "%s = `%s'", key, val );
     264        else
     265                irc_usermsg( irc, "%s is empty", key );
     266}
     267
     268typedef set_t** (*cmd_set_findhead)( irc_t*, char* );
     269typedef int (*cmd_set_checkflags)( irc_t*, set_t *set );
     270
     271static int cmd_set_real( irc_t *irc, char **cmd, cmd_set_findhead findhead, cmd_set_checkflags checkflags )
     272{
     273        char *set_full = NULL, *set_name = NULL, *tmp;
     274        set_t **head;
     275       
     276        if( cmd[1] && g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
     277        {
     278                MIN_ARGS( 2, 0 );
     279                set_full = cmd[2];
     280        }
     281        else
     282                set_full = cmd[1];
     283       
     284        if( findhead == NULL )
     285        {
     286                set_name = set_full;
     287               
     288                head = &irc->set;
     289        }
     290        else
     291        {
     292                char *id;
     293               
     294                if( ( tmp = strchr( set_full, '/' ) ) )
     295                {
     296                        id = g_strndup( set_full, ( tmp - set_full ) );
     297                        set_name = tmp + 1;
     298                }
     299                else
     300                {
     301                        id = g_strdup( set_full );
     302                }
     303               
     304                if( ( head = findhead( irc, id ) ) == NULL )
     305                {
     306                        g_free( id );
     307                        irc_usermsg( irc, "Could not find setting." );
     308                        return 0;
     309                }
     310                g_free( id );
     311        }
     312       
     313        if( cmd[1] && cmd[2] && set_name )
     314        {
     315                set_t *s = set_find( head, set_name );
     316                int st;
     317               
     318                if( s && checkflags && checkflags( irc, s ) == 0 )
     319                        return 0;
     320               
     321                if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
     322                        st = set_reset( head, set_name );
     323                else
     324                        st = set_setstr( head, set_name, cmd[2] );
     325               
     326                if( set_getstr( head, set_name ) == NULL )
     327                {
     328                        if( st )
     329                                irc_usermsg( irc, "Setting changed successfully" );
     330                        else
     331                                irc_usermsg( irc, "Failed to change setting" );
     332                }
     333                else
     334                {
     335                        cmd_showset( irc, head, set_name );
     336                }
     337        }
     338        else if( set_name )
     339        {
     340                cmd_showset( irc, head, set_name );
     341        }
     342        else
     343        {
     344                set_t *s = *head;
     345                while( s )
     346                {
     347                        cmd_showset( irc, &s, s->key );
     348                        s = s->next;
     349                }
     350        }
     351       
     352        return 1;
     353}
     354
     355static set_t **cmd_account_set_findhead( irc_t *irc, char *id )
     356{
     357        account_t *a;
     358       
     359        if( ( a = account_get( irc, id ) ) )
     360                return &a->set;
     361        else
     362                return NULL;
     363}
     364
     365static int cmd_account_set_checkflags( irc_t *irc, set_t *s )
     366{
     367        account_t *a = s->data;
     368       
     369        if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY )
     370        {
     371                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" );
     372                return 0;
     373        }
     374        else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY )
     375        {
     376                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" );
     377                return 0;
     378        }
     379       
     380        return 1;
     381}
     382
    240383static void cmd_account( irc_t *irc, char **cmd )
    241384{
     
    252395                struct prpl *prpl;
    253396               
    254                 if( cmd[2] == NULL || cmd[3] == NULL || cmd[4] == NULL )
    255                 {
    256                         irc_usermsg( irc, "Not enough parameters" );
    257                         return;
    258                 }
    259                
    260                 prpl = find_protocol(cmd[2]);
     397                MIN_ARGS( 4 );
     398               
     399                prpl = find_protocol( cmd[2] );
    261400               
    262401                if( prpl == NULL )
     
    278417        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
    279418        {
    280                 if( !cmd[2] )
    281                 {
    282                         irc_usermsg( irc, "Not enough parameters given (need %d)", 2 );
    283                 }
    284                 else if( !( a = account_get( irc, cmd[2] ) ) )
     419                MIN_ARGS( 2 );
     420
     421                if( !( a = account_get( irc, cmd[2] ) ) )
    285422                {
    286423                        irc_usermsg( irc, "Invalid account" );
     
    410547        else if( g_strcasecmp( cmd[1], "set" ) == 0 )
    411548        {
    412                 char *acc_handle, *set_name = NULL, *tmp;
    413                
    414                 if( !cmd[2] )
    415                 {
    416                         irc_usermsg( irc, "Not enough parameters given (need %d)", 2 );
    417                         return;
    418                 }
    419                
    420                 if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
    421                         acc_handle = g_strdup( cmd[3] );
    422                 else
    423                         acc_handle = g_strdup( cmd[2] );
    424                
    425                 if( !acc_handle )
    426                 {
    427                         irc_usermsg( irc, "Not enough parameters given (need %d)", 3 );
    428                         return;
    429                 }
    430                
    431                 if( ( tmp = strchr( acc_handle, '/' ) ) )
    432                 {
    433                         *tmp = 0;
    434                         set_name = tmp + 1;
    435                 }
    436                
    437                 if( ( a = account_get( irc, acc_handle ) ) == NULL )
    438                 {
    439                         g_free( acc_handle );
    440                         irc_usermsg( irc, "Invalid account" );
    441                         return;
    442                 }
    443                
    444                 if( cmd[3] && set_name )
    445                 {
    446                         set_t *s = set_find( &a->set, set_name );
    447                        
    448                         if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY )
    449                         {
    450                                 g_free( acc_handle );
    451                                 irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" );
    452                                 return;
    453                         }
    454                         else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY )
    455                         {
    456                                 g_free( acc_handle );
    457                                 irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" );
    458                                 return;
    459                         }
    460                        
    461                         if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
    462                                 set_reset( &a->set, set_name );
    463                         else
    464                                 set_setstr( &a->set, set_name, cmd[3] );
    465                 }
    466                 if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */
    467                 {
    468                         char *s = set_getstr( &a->set, set_name );
    469                         if( s )
    470                                 irc_usermsg( irc, "%s = `%s'", set_name, s );
    471                         else
    472                                 irc_usermsg( irc, "%s is empty", set_name );
    473                 }
    474                 else
    475                 {
    476                         set_t *s = a->set;
    477                         while( s )
    478                         {
    479                                 if( s->value || s->def )
    480                                         irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def );
    481                                 else
    482                                         irc_usermsg( irc, "%s is empty", s->key );
    483                                 s = s->next;
    484                         }
    485                 }
    486                
    487                 g_free( acc_handle );
    488         }
    489         else
    490         {
    491                 irc_usermsg( irc, "Unknown command: account %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[1] );
     549                MIN_ARGS( 2 );
     550               
     551                cmd_set_real( irc, cmd + 1, cmd_account_set_findhead, cmd_account_set_checkflags );
     552        }
     553        else
     554        {
     555                irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "account", cmd[1] );
    492556        }
    493557}
     
    500564        if( g_strcasecmp( cmd[1], "-tmp" ) == 0 )
    501565        {
     566                MIN_ARGS( 3 );
    502567                add_on_server = 0;
    503568                cmd ++;
     
    610675                        irc->mynick = g_strdup( cmd[2] );
    611676                       
     677                        /* If we're called internally (user did "set root_nick"),
     678                           let's not go O(INF). :-) */
    612679                        if( strcmp( cmd[0], "set_rename" ) != 0 )
    613680                                set_setstr( &irc->set, "root_nick", cmd[2] );
     
    633700        }
    634701       
    635         return strcmp( irc->mynick, new_nick ) == 0 ? new_nick : NULL;
     702        return strcmp( irc->mynick, new_nick ) == 0 ? new_nick : SET_INVALID;
    636703}
    637704
     
    817884static void cmd_set( irc_t *irc, char **cmd )
    818885{
    819         char *set_name = cmd[1];
    820        
    821         if( cmd[1] && cmd[2] )
    822         {
    823                 if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
    824                 {
    825                         set_reset( &irc->set, cmd[2] );
    826                         set_name = cmd[2];
    827                 }
    828                 else
    829                 {
    830                         set_setstr( &irc->set, cmd[1], cmd[2] );
    831                 }
    832         }
    833         if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */
    834         {
    835                 char *s = set_getstr( &irc->set, set_name );
    836                 if( s )
    837                         irc_usermsg( irc, "%s = `%s'", set_name, s );
    838                 else
    839                         irc_usermsg( irc, "%s is empty", set_name );
    840 
    841                 if( strchr( set_name, '/' ) )
    842                         irc_usermsg( irc, "Warning: / found in setting name, you're probably looking for the `account set' command." );
    843         }
    844         else
    845         {
    846                 set_t *s = irc->set;
    847                 while( s )
    848                 {
    849                         if( s->value || s->def )
    850                                 irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def );
    851                         else
    852                                 irc_usermsg( irc, "%s is empty", s->key );
    853                         s = s->next;
    854                 }
    855         }
     886        cmd_set_real( irc, cmd, NULL, NULL );
    856887}
    857888
    858889static void cmd_save( irc_t *irc, char **cmd )
    859890{
    860         if( storage_save( irc, TRUE ) == STORAGE_OK )
     891        if( ( irc->status & USTATUS_IDENTIFIED ) == 0 )
     892                irc_usermsg( irc, "Please create an account first" );
     893        else if( storage_save( irc, NULL, TRUE ) == STORAGE_OK )
    861894                irc_usermsg( irc, "Configuration saved" );
    862895        else
     
    9741007static void cmd_join_chat( irc_t *irc, char **cmd )
    9751008{
    976         account_t *a;
    977         struct im_connection *ic;
    978         char *chat, *channel, *nick = NULL, *password = NULL;
    979         struct groupchat *c;
    980        
    981         if( !( a = account_get( irc, cmd[1] ) ) )
    982         {
    983                 irc_usermsg( irc, "Invalid account" );
    984                 return;
    985         }
    986         else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
    987         {
    988                 irc_usermsg( irc, "That account is not on-line" );
    989                 return;
    990         }
    991         else if( a->prpl->chat_join == NULL )
    992         {
    993                 irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
    994                 return;
    995         }
    996         ic = a->ic;
    997        
    998         chat = cmd[2];
    999         if( cmd[3] )
    1000         {
    1001                 if( cmd[3][0] != '#' && cmd[3][0] != '&' )
    1002                         channel = g_strdup_printf( "&%s", cmd[3] );
    1003                 else
     1009        irc_usermsg( irc, "This command is now obsolete. "
     1010                          "Please try the `chat' command instead." );
     1011}
     1012
     1013static set_t **cmd_chat_set_findhead( irc_t *irc, char *id )
     1014{
     1015        struct chat *c;
     1016       
     1017        if( ( c = chat_get( irc, id ) ) )
     1018                return &c->set;
     1019        else
     1020                return NULL;
     1021}
     1022
     1023static void cmd_chat( irc_t *irc, char **cmd )
     1024{
     1025        account_t *acc;
     1026        struct chat *c;
     1027       
     1028        if( g_strcasecmp( cmd[1], "add" ) == 0 )
     1029        {
     1030                char *channel, *s;
     1031               
     1032                MIN_ARGS( 3 );
     1033               
     1034                if( !( acc = account_get( irc, cmd[2] ) ) )
     1035                {
     1036                        irc_usermsg( irc, "Invalid account" );
     1037                        return;
     1038                }
     1039               
     1040                if( cmd[4] == NULL )
     1041                {
    10041042                        channel = g_strdup( cmd[3] );
    1005         }
    1006         else
    1007         {
    1008                 char *s;
    1009                
    1010                 channel = g_strdup_printf( "&%s", chat );
    1011                 if( ( s = strchr( channel, '@' ) ) )
    1012                         *s = 0;
    1013         }
    1014         if( cmd[3] && cmd[4] )
    1015                 nick = cmd[4];
    1016         else
    1017                 nick = irc->nick;
    1018         if( cmd[3] && cmd[4] && cmd[5] )
    1019                 password = cmd[5];
    1020        
    1021         if( !nick_ok( channel + 1 ) )
    1022         {
    1023                 irc_usermsg( irc, "Invalid channel name: %s", channel );
     1043                        if( ( s = strchr( channel, '@' ) ) )
     1044                                *s = 0;
     1045                }
     1046                else
     1047                {
     1048                        channel = g_strdup( cmd[4] );
     1049                }
     1050               
     1051                if( strchr( CTYPES, channel[0] ) == NULL )
     1052                {
     1053                        s = g_strdup_printf( "%c%s", CTYPES[0], channel );
     1054                        g_free( channel );
     1055                        channel = s;
     1056                }
     1057               
     1058                if( ( c = chat_add( irc, acc, cmd[3], channel ) ) )
     1059                        irc_usermsg( irc, "Chatroom added successfully." );
     1060                else
     1061                        irc_usermsg( irc, "Could not add chatroom." );
     1062               
    10241063                g_free( channel );
    1025                 return;
    1026         }
    1027         else if( g_strcasecmp( channel, irc->channel ) == 0 || irc_chat_by_channel( irc, channel ) )
    1028         {
    1029                 irc_usermsg( irc, "Channel already exists: %s", channel );
    1030                 g_free( channel );
    1031                 return;
    1032         }
    1033        
    1034         if( ( c = a->prpl->chat_join( ic, chat, nick, password ) ) )
    1035         {
    1036                 g_free( c->channel );
    1037                 c->channel = channel;
    1038         }
    1039         else
    1040         {
    1041                 irc_usermsg( irc, "Tried to join chat, not sure if this was successful" );
    1042                 g_free( channel );
     1064        }
     1065        else if( g_strcasecmp( cmd[1], "list" ) == 0 )
     1066        {
     1067                int i = 0;
     1068               
     1069                if( strchr( irc->umode, 'b' ) )
     1070                        irc_usermsg( irc, "Chatroom list:" );
     1071               
     1072                for( c = irc->chatrooms; c; c = c->next )
     1073                {
     1074                        irc_usermsg( irc, "%2d. %s(%s) %s, %s", i, c->acc->prpl->name,
     1075                                          c->acc->user, c->handle, c->channel );
     1076                       
     1077                        i ++;
     1078                }
     1079                irc_usermsg( irc, "End of chatroom list" );
     1080        }
     1081        else if( g_strcasecmp( cmd[1], "set" ) == 0 )
     1082        {
     1083                MIN_ARGS( 2 );
     1084               
     1085                cmd_set_real( irc, cmd + 1, cmd_chat_set_findhead, NULL );
     1086        }
     1087        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
     1088        {
     1089                MIN_ARGS( 2 );
     1090               
     1091                if( ( c = chat_get( irc, cmd[2] ) ) )
     1092                {
     1093                        chat_del( irc, c );
     1094                }
     1095                else
     1096                {
     1097                        irc_usermsg( irc, "Could not remove chat." );
     1098                }
     1099        }
     1100        else if( g_strcasecmp( cmd[1], "with" ) == 0 )
     1101        {
     1102                user_t *u;
     1103               
     1104                MIN_ARGS( 2 );
     1105               
     1106                if( ( u = user_find( irc, cmd[2] ) ) && u->ic && u->ic->acc->prpl->chat_with )
     1107                {
     1108                        if( !u->ic->acc->prpl->chat_with( u->ic, u->handle ) )
     1109                        {
     1110                                irc_usermsg( irc, "(Possible) failure while trying to open "
     1111                                                  "a groupchat with %s.", u->nick );
     1112                        }
     1113                }
     1114                else
     1115                {
     1116                        irc_usermsg( irc, "Can't open a groupchat with %s.", cmd[2] );
     1117                }
     1118        }
     1119        else
     1120        {
     1121                irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat", cmd[1] );
    10431122        }
    10441123}
     
    10641143        { "qlist",          0, cmd_qlist,          0 },
    10651144        { "join_chat",      2, cmd_join_chat,      0 },
     1145        { "chat",           1, cmd_chat,           0 },
    10661146        { NULL }
    10671147};
Note: See TracChangeset for help on using the changeset viewer.