Changeset 673a54c for root_commands.c


Ignore:
Timestamp:
2009-03-12T19:33:28Z (15 years ago)
Author:
Sven Moritz Hallberg <pesco@…>
Branches:
master
Children:
fc34fb5
Parents:
823de9d (diff), 9e768da (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

pretty blind try at merging in the latest trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • root_commands.c

    r823de9d r673a54c  
    7979}
    8080
     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
    8193void root_command( irc_t *irc, char *cmd[] )
    8294{       
     
    89101                if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 )
    90102                {
    91                         if( !cmd[commands[i].required_parameters] )
    92                         {
    93                                 irc_usermsg( irc, "Not enough parameters given (need %d)", commands[i].required_parameters );
    94                                 return;
    95                         }
     103                        MIN_ARGS( commands[i].required_parameters );
     104                       
    96105                        commands[i].execute( irc, cmd );
    97106                        return;
     
    251260}
    252261
     262typedef set_t** (*cmd_set_findhead)( irc_t*, char* );
     263typedef int (*cmd_set_checkflags)( irc_t*, set_t *set );
     264
     265static int cmd_set_real( irc_t *irc, char **cmd, cmd_set_findhead findhead, cmd_set_checkflags checkflags )
     266{
     267        char *set_full = NULL, *set_name = NULL, *tmp;
     268        set_t **head;
     269       
     270        if( cmd[1] && g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
     271        {
     272                MIN_ARGS( 2, 0 );
     273                set_full = cmd[2];
     274        }
     275        else
     276                set_full = cmd[1];
     277       
     278        if( findhead == NULL )
     279        {
     280                set_name = set_full;
     281               
     282                head = &irc->set;
     283        }
     284        else
     285        {
     286                char *id;
     287               
     288                if( ( tmp = strchr( set_full, '/' ) ) )
     289                {
     290                        id = g_strndup( set_full, ( tmp - set_full ) );
     291                        set_name = tmp + 1;
     292                }
     293                else
     294                {
     295                        id = g_strdup( set_full );
     296                }
     297               
     298                if( ( head = findhead( irc, id ) ) == NULL )
     299                {
     300                        g_free( id );
     301                        irc_usermsg( irc, "Could not find setting." );
     302                        return 0;
     303                }
     304                g_free( id );
     305        }
     306       
     307        if( cmd[1] && cmd[2] && set_name )
     308        {
     309                set_t *s = set_find( head, set_name );
     310                int st;
     311               
     312                if( checkflags && checkflags( irc, s ) == 0 )
     313                        return 0;
     314               
     315                if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
     316                        st = set_reset( head, set_name );
     317                else
     318                        st = set_setstr( head, set_name, cmd[2] );
     319               
     320                if( set_getstr( head, set_name ) == NULL )
     321                {
     322                        if( st )
     323                                irc_usermsg( irc, "Setting changed successfully" );
     324                        else
     325                                irc_usermsg( irc, "Failed to change setting" );
     326                }
     327                else
     328                {
     329                        cmd_showset( irc, head, set_name );
     330                }
     331        }
     332        else if( set_name )
     333        {
     334                cmd_showset( irc, head, set_name );
     335        }
     336        else
     337        {
     338                set_t *s = *head;
     339                while( s )
     340                {
     341                        cmd_showset( irc, &s, s->key );
     342                        s = s->next;
     343                }
     344        }
     345       
     346        return 1;
     347}
     348
     349static set_t **cmd_account_set_findhead( irc_t *irc, char *id )
     350{
     351        account_t *a;
     352       
     353        if( ( a = account_get( irc, id ) ) )
     354                return &a->set;
     355        else
     356                return NULL;
     357}
     358
     359static int cmd_account_set_checkflags( irc_t *irc, set_t *s )
     360{
     361        account_t *a = s->data;
     362       
     363        if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY )
     364        {
     365                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" );
     366                return 0;
     367        }
     368        else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY )
     369        {
     370                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" );
     371                return 0;
     372        }
     373       
     374        return 1;
     375}
     376
    253377static void cmd_account( irc_t *irc, char **cmd )
    254378{
     
    265389                struct prpl *prpl;
    266390               
    267                 if( cmd[2] == NULL || cmd[3] == NULL || cmd[4] == NULL )
    268                 {
    269                         irc_usermsg( irc, "Not enough parameters" );
    270                         return;
    271                 }
    272                
    273                 prpl = find_protocol(cmd[2]);
     391                MIN_ARGS( 4 );
     392               
     393                prpl = find_protocol( cmd[2] );
    274394               
    275395                if( prpl == NULL )
     
    295415        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
    296416        {
    297                 if( !cmd[2] )
    298                 {
    299                         irc_usermsg( irc, "Not enough parameters given (need %d)", 2 );
    300                 }
    301                 else if( !( a = account_get( irc, cmd[2] ) ) )
     417                MIN_ARGS( 2 );
     418
     419                if( !( a = account_get( irc, cmd[2] ) ) )
    302420                {
    303421                        irc_usermsg( irc, "Invalid account" );
     
    427545        else if( g_strcasecmp( cmd[1], "set" ) == 0 )
    428546        {
    429                 char *acc_handle, *set_name = NULL, *tmp;
    430                
    431                 if( !cmd[2] )
    432                 {
    433                         irc_usermsg( irc, "Not enough parameters given (need %d)", 2 );
    434                         return;
    435                 }
    436                
    437                 if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
    438                         acc_handle = g_strdup( cmd[3] );
    439                 else
    440                         acc_handle = g_strdup( cmd[2] );
    441                
    442                 if( !acc_handle )
    443                 {
    444                         irc_usermsg( irc, "Not enough parameters given (need %d)", 3 );
    445                         return;
    446                 }
    447                
    448                 if( ( tmp = strchr( acc_handle, '/' ) ) )
    449                 {
    450                         *tmp = 0;
    451                         set_name = tmp + 1;
    452                 }
    453                
    454                 if( ( a = account_get( irc, acc_handle ) ) == NULL )
    455                 {
    456                         g_free( acc_handle );
    457                         irc_usermsg( irc, "Invalid account" );
    458                         return;
    459                 }
    460                
    461                 if( cmd[3] && set_name )
    462                 {
    463                         set_t *s = set_find( &a->set, set_name );
    464                         int st;
    465                        
    466                         if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY )
    467                         {
    468                                 g_free( acc_handle );
    469                                 irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" );
    470                                 return;
    471                         }
    472                         else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY )
    473                         {
    474                                 g_free( acc_handle );
    475                                 irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" );
    476                                 return;
    477                         }
    478                        
    479                         if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
    480                                 st = set_reset( &a->set, set_name );
    481                         else
    482                                 st = set_setstr( &a->set, set_name, cmd[3] );
    483                        
    484                         if( set_getstr( &a->set, set_name ) == NULL )
    485                         {
    486                                 if( st )
    487                                         irc_usermsg( irc, "Setting changed successfully" );
    488                                 else
    489                                         irc_usermsg( irc, "Failed to change setting" );
    490                         }
    491                         else
    492                         {
    493                                 cmd_showset( irc, &a->set, set_name );
    494                         }
    495                 }
    496                 else if( set_name )
    497                 {
    498                         cmd_showset( irc, &a->set, set_name );
    499                 }
    500                 else
    501                 {
    502                         set_t *s = a->set;
    503                         while( s )
    504                         {
    505                                 cmd_showset( irc, &s, s->key );
    506                                 s = s->next;
    507                         }
    508                 }
    509                
    510                 g_free( acc_handle );
    511         }
    512         else
    513         {
    514                 irc_usermsg( irc, "Unknown command: account %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[1] );
     547                MIN_ARGS( 2 );
     548               
     549                cmd_set_real( irc, cmd + 1, cmd_account_set_findhead, cmd_account_set_checkflags );
     550        }
     551        else
     552        {
     553                irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "account", cmd[1] );
    515554        }
    516555}
     
    523562        if( g_strcasecmp( cmd[1], "-tmp" ) == 0 )
    524563        {
     564                MIN_ARGS( 3 );
    525565                add_on_server = 0;
    526566                cmd ++;
     
    842882static void cmd_set( irc_t *irc, char **cmd )
    843883{
    844         char *set_name = cmd[1];
    845        
    846         if( cmd[1] && cmd[2] )
    847         {
    848                 int st;
    849                
    850                 if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
    851                 {
    852                         st = set_reset( &irc->set, cmd[2] );
    853                         set_name = cmd[2];
    854                 }
    855                 else
    856                 {
    857                         st = set_setstr( &irc->set, cmd[1], cmd[2] );
    858                 }
    859                
    860                 /* Normally we just show the variable's new/unchanged
    861                    value as feedback to the user, but this has always
    862                    caused confusion when changing the password. Give
    863                    other feedback instead: */
    864                 if( set_getstr( &irc->set, set_name ) == NULL )
    865                 {
    866                         if( st )
    867                                 irc_usermsg( irc, "Setting changed successfully" );
    868                         else
    869                                 irc_usermsg( irc, "Failed to change setting" );
    870                 }
    871                 else
    872                 {
    873                         cmd_showset( irc, &irc->set, set_name );
    874                 }
    875         }
    876         else if( set_name )
    877         {
    878                 cmd_showset( irc, &irc->set, set_name );
    879 
    880                 if( strchr( set_name, '/' ) )
    881                         irc_usermsg( irc, "Warning: / found in setting name, you're probably looking for the `account set' command." );
    882         }
    883         else
    884         {
    885                 set_t *s = irc->set;
    886                 while( s )
    887                 {
    888                         cmd_showset( irc, &s, s->key );
    889                         s = s->next;
    890                 }
    891         }
     884        cmd_set_real( irc, cmd, NULL, NULL );
    892885}
    893886
     
    10121005static void cmd_join_chat( irc_t *irc, char **cmd )
    10131006{
     1007        irc_usermsg( irc, "This command is now obsolete. "
     1008                          "Please try the `chat' command instead." );
     1009}
     1010
     1011static set_t **cmd_chat_set_findhead( irc_t *irc, char *id )
     1012{
     1013        struct chat *c;
     1014       
     1015        if( ( c = chat_get( irc, id ) ) )
     1016                return &c->set;
     1017        else
     1018                return NULL;
     1019}
     1020
     1021static void cmd_chat( irc_t *irc, char **cmd )
     1022{
     1023        account_t *acc;
     1024        struct chat *c;
     1025       
     1026        if( g_strcasecmp( cmd[1], "add" ) == 0 )
     1027        {
     1028                char *channel, *s;
     1029               
     1030                MIN_ARGS( 3 );
     1031               
     1032                if( !( acc = account_get( irc, cmd[2] ) ) )
     1033                {
     1034                        irc_usermsg( irc, "Invalid account" );
     1035                        return;
     1036                }
     1037               
     1038                if( cmd[4] == NULL )
     1039                {
     1040                        channel = g_strdup( cmd[3] );
     1041                        if( ( s = strchr( channel, '@' ) ) )
     1042                                *s = 0;
     1043                }
     1044                else
     1045                {
     1046                        channel = g_strdup( cmd[4] );
     1047                }
     1048               
     1049                if( strchr( CTYPES, channel[0] ) == NULL )
     1050                {
     1051                        s = g_strdup_printf( "%c%s", CTYPES[0], channel );
     1052                        g_free( channel );
     1053                        channel = s;
     1054                }
     1055               
     1056                if( ( c = chat_add( irc, acc, cmd[3], channel ) ) )
     1057                        irc_usermsg( irc, "Chatroom added successfully." );
     1058                else
     1059                        irc_usermsg( irc, "Could not add chatroom." );
     1060               
     1061                g_free( channel );
     1062        }
     1063        else if( g_strcasecmp( cmd[1], "list" ) == 0 )
     1064        {
     1065                int i = 0;
     1066               
     1067                if( strchr( irc->umode, 'b' ) )
     1068                        irc_usermsg( irc, "Chatroom list:" );
     1069               
     1070                for( c = irc->chatrooms; c; c = c->next )
     1071                {
     1072                        irc_usermsg( irc, "%2d. %s(%s) %s, %s", i, c->acc->prpl->name,
     1073                                          c->acc->user, c->handle, c->channel );
     1074                       
     1075                        i ++;
     1076                }
     1077                irc_usermsg( irc, "End of chatroom list" );
     1078        }
     1079        else if( g_strcasecmp( cmd[1], "set" ) == 0 )
     1080        {
     1081                cmd_set_real( irc, cmd + 1, cmd_chat_set_findhead, NULL );
     1082        }
     1083        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
     1084        {
     1085                MIN_ARGS( 2 );
     1086               
     1087                if( ( c = chat_get( irc, cmd[2] ) ) )
     1088                {
     1089                        chat_del( irc, c );
     1090                }
     1091                else
     1092                {
     1093                        irc_usermsg( irc, "Could not remove chat." );
     1094                }
     1095        }
     1096        else if( g_strcasecmp( cmd[1], "with" ) == 0 )
     1097        {
     1098                user_t *u;
     1099               
     1100                MIN_ARGS( 2 );
     1101               
     1102                if( ( u = user_find( irc, cmd[2] ) ) && u->ic && u->ic->acc->prpl->chat_with )
     1103                {
     1104                        if( !u->ic->acc->prpl->chat_with( u->ic, u->handle ) )
     1105                        {
     1106                                irc_usermsg( irc, "(Possible) failure while trying to open "
     1107                                                  "a groupchat with %s.", u->nick );
     1108                        }
     1109                }
     1110                else
     1111                {
     1112                        irc_usermsg( irc, "Can't open a groupchat with %s.", cmd[2] );
     1113                }
     1114        }
     1115        else
     1116        {
     1117                irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat", cmd[1] );
     1118        }
     1119
     1120
     1121
     1122#if 0
    10141123        account_t *a;
    10151124        struct im_connection *ic;
     
    10371146        if( cmd[3] )
    10381147        {
    1039                 if( cmd[3][0] != '#' && cmd[3][0] != '&' )
     1148                if( strchr( CTYPES, cmd[3][0] ) == NULL )
    10401149                        channel = g_strdup_printf( "&%s", cmd[3] );
    10411150                else
     
    10801189                g_free( channel );
    10811190        }
     1191#endif
    10821192}
    10831193
     
    11031213        { "join_chat",      2, cmd_join_chat,      0 },
    11041214        { "otr",            1, cmd_otr,            0 },
     1215        { "chat",           1, cmd_chat,           0 },
    11051216        { NULL }
    11061217};
Note: See TracChangeset for help on using the changeset viewer.