Changeset a1f17d4


Ignore:
Timestamp:
2005-12-08T14:14:28Z (18 years ago)
Author:
Jelmer Vernooij <jelmer@…>
Branches:
master
Children:
7989fcf3
Parents:
1ee6c18
Message:

Simplify storage API a bit

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • commands.c

    r1ee6c18 ra1f17d4  
    116116        }
    117117
    118         if( !global.storage->exists( irc->nick ))
    119         {
    120                 setpassnc( irc, cmd[1] );
    121                 root_command_string( irc, user_find( irc, irc->mynick ), "save", 0 );
    122                 irc->status = USTATUS_IDENTIFIED;
    123         }
    124         else
    125         {
    126                 irc_usermsg( irc, "Nick is already registered" );
     118        setpassnc( irc, cmd[1] );
     119        switch( global.storage->save( irc, FALSE )) {
     120                case STORAGE_ALREADY_EXISTS:
     121                        irc_usermsg( irc, "Nick is already registered" );
     122                        break;
     123                       
     124                case STORAGE_OK:
     125                        irc->status = USTATUS_IDENTIFIED;
     126                        break;
     127
     128                default:
     129                        irc_usermsg( irc, "Error registering" );
     130                        break;
    127131        }
    128132       
     
    132136int cmd_drop( irc_t *irc, char **cmd )
    133137{
    134         if( ! global.storage->exists (irc->nick) )
    135         {
     138        storage_status_t status;
     139       
     140        status = global.storage->remove (irc->nick, cmd[1]);
     141        switch (status) {
     142        case STORAGE_NO_SUCH_USER:
    136143                irc_usermsg( irc, "That account does not exist" );
    137144                return( 0 );
    138         }
    139 
    140         if ( global.storage->check_pass (irc->nick, cmd[1]) )
    141         {
     145        case STORAGE_INVALID_PASSWORD:
    142146                irc_usermsg( irc, "Password invalid" );
    143147                return( 0 );
    144         }
    145        
    146         global.storage->remove (irc->nick);
    147        
    148         setpassnc( irc, NULL );
    149         irc_usermsg( irc, "Files belonging to account `%s' removed", irc->nick );
    150        
    151         return( 0 );
     148        case STORAGE_OK:
     149                setpassnc( irc, NULL );
     150                irc_usermsg( irc, "Account `%s' removed", irc->nick );
     151                return( 0 );
     152        default:
     153                irc_usermsg( irc, "Error: '%d'", status );
     154                return( 0 );
     155        }
    152156}
    153157
     
    614618int cmd_save( irc_t *irc, char **cmd )
    615619{
    616         if( global.storage->save( irc ) )
     620        if( global.storage->save( irc, TRUE ) )
    617621                irc_usermsg( irc, "Configuration saved" );
    618622        else
  • irc.c

    r1ee6c18 ra1f17d4  
    154154       
    155155        if( irc->status >= USTATUS_IDENTIFIED && set_getint( irc, "save_on_quit" ) )
    156                 if( !global.storage->save( irc ) )
     156                if( !global.storage->save( irc, TRUE ) )
    157157                        irc_usermsg( irc, "Error while saving settings!" );
    158158       
  • storage.h

    r1ee6c18 ra1f17d4  
    2727#define __STORAGE_H__
    2828
     29typedef enum {
     30        STORAGE_OK = 0,
     31        STORAGE_NO_SUCH_USER,
     32        STORAGE_INVALID_PASSWORD,
     33        STORAGE_ALREADY_EXISTS,
     34        STORAGE_OTHER_ERROR /* Error that isn't caused by user input, such as
     35                                                   a database that is unreachable. log() will be
     36                                                   used for the exact error message */
     37} storage_status_t;
     38
    2939typedef struct {
    3040        const char *name;
     
    3343        void (*init) (void);
    3444
    35         int (*load) (const char *nick, const char *password, irc_t * irc);
    36         int (*exists) (const char *nick);
    37         int (*save) (irc_t *irc);
    38         int (*remove) (const char *nick);
    39         int (*check_pass) (const char *nick, const char *pass);
     45        storage_status_t (*load) (const char *nick, const char *password, irc_t * irc);
     46        storage_status_t (*save) (irc_t *irc, int overwrite);
     47        storage_status_t (*remove) (const char *nick, const char *password);
    4048
    4149        /* May be NULL if not supported by backend */
    42         int (*rename) (const char *onick, const char *nnick, const char *password);
     50        storage_status_t (*rename) (const char *onick, const char *nnick, const char *password);
    4351} storage_t;
    4452
  • storage_text.c

    r1ee6c18 ra1f17d4  
    3636}
    3737
    38 static int text_load ( const char *my_nick, const char* password, irc_t *irc )
     38static storage_status_t text_load ( const char *my_nick, const char* password, irc_t *irc )
    3939{
    4040        char s[512];
     
    5050        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, my_nick, ".accounts" );
    5151        fp = fopen( s, "r" );
    52         if( !fp ) return( 0 );
     52        if( !fp ) return STORAGE_NO_SUCH_USER;
    5353       
    5454        fscanf( fp, "%32[^\n]s", s );
     
    5656        {
    5757                fclose( fp );
    58                 return( -1 );
     58                return STORAGE_INVALID_PASSWORD;
    5959        }
    6060       
     
    7474        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, my_nick, ".nicks" );
    7575        fp = fopen( s, "r" );
    76         if( !fp ) return( 0 );
     76        if( !fp ) return STORAGE_NO_SUCH_USER;
    7777        while( fscanf( fp, "%s %d %s", s, &proto, nick ) > 0 )
    7878        {
     
    8888        }
    8989       
    90         return( 1 );
    91 }
    92 
    93 static int text_save( irc_t *irc )
     90        return STORAGE_OK;
     91}
     92
     93static storage_status_t text_save( irc_t *irc, int overwrite )
    9494{
    9595        char s[512];
     
    102102        FILE *fp;
    103103        char *hash;
     104
     105        if (!overwrite) {
     106                g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" );
     107                if (access( path, F_OK ) != -1)
     108                        return STORAGE_ALREADY_EXISTS;
     109       
     110                g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" );
     111                if (access( path, F_OK ) != -1)
     112                        return STORAGE_ALREADY_EXISTS;
     113        }
    104114       
    105115        /*\
     
    127137        g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks~" );
    128138        fp = fopen( path, "w" );
    129         if( !fp ) return( 0 );
     139        if( !fp ) return STORAGE_OTHER_ERROR;
    130140        for( n = irc->nicks; n; n = n->next )
    131141        {
     
    138148                        irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    139149                        fclose( fp );
    140                         return( 0 );
     150                        return STORAGE_OTHER_ERROR;
    141151                }
    142152        }
     
    144154        {
    145155                irc_usermsg( irc, "fclose() reported an error. Disk full?" );
    146                 return( 0 );
     156                return STORAGE_OTHER_ERROR;
    147157        }
    148158 
     
    153163                {
    154164                        irc_usermsg( irc, "Error while removing old .nicks file" );
    155                         return( 0 );
     165                        return STORAGE_OTHER_ERROR;
    156166                }
    157167        }
     
    159169        {
    160170                irc_usermsg( irc, "Error while renaming new .nicks file" );
    161                 return( 0 );
     171                return STORAGE_OTHER_ERROR;
    162172        }
    163173       
    164174        g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts~" );
    165175        fp = fopen( path, "w" );
    166         if( !fp ) return( 0 );
     176        if( !fp ) return STORAGE_OTHER_ERROR;
    167177        if( fprintf( fp, "%s", hash ) != strlen( hash ) )
    168178        {
    169179                irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    170180                fclose( fp );
    171                 return( 0 );
     181                return STORAGE_OTHER_ERROR;
    172182        }
    173183        g_free( hash );
     
    188198                                irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    189199                                fclose( fp );
    190                                 return( 0 );
     200                                return STORAGE_OTHER_ERROR;
    191201                        }
    192202                }
     
    206216                                        irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    207217                                        fclose( fp );
    208                                         return( 0 );
     218                                        return STORAGE_OTHER_ERROR;
    209219                                }
    210220                        }
     
    223233                                irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );
    224234                                fclose( fp );
    225                                 return( 0 );
     235                                return STORAGE_OTHER_ERROR;
    226236                        }
    227237                }
     
    231241        {
    232242                irc_usermsg( irc, "fclose() reported an error. Disk full?" );
    233                 return( 0 );
     243                return STORAGE_OTHER_ERROR;
    234244        }
    235245       
     
    240250                {
    241251                        irc_usermsg( irc, "Error while removing old .accounts file" );
    242                         return( 0 );
     252                        return STORAGE_OTHER_ERROR;
    243253                }
    244254        }
     
    246256        {
    247257                irc_usermsg( irc, "Error while renaming new .accounts file" );
    248                 return( 0 );
     258                return STORAGE_OTHER_ERROR;
    249259        }
    250260       
    251261        umask( ou );
    252262       
    253         return( 1 );
    254 }
    255 
    256 static int text_exists( const char *nick )
    257 {
    258         char path[512];
    259         int checkie;
    260 
    261         g_snprintf( path, 511, "%s%s%s", global.conf->configdir, nick, ".accounts" );
    262         checkie = access( path, F_OK );
    263        
    264         g_snprintf( path, 511, "%s%s%s", global.conf->configdir, nick, ".nicks" );
    265         checkie += access( path, F_OK );
    266        
    267         return ( checkie != -2 );
    268 }
    269 
    270 static int text_remove( const char *nick )
     263        return STORAGE_OK;
     264}
     265
     266static storage_status_t text_remove( const char *nick, const char *password )
    271267{
    272268        char s[512];
     269        FILE *fp;
     270       
     271        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".nicks" );
     272        fp = fopen( s, "r" );
     273        if (!fp)
     274                return STORAGE_NO_SUCH_USER;
     275
     276        fscanf( fp, "%32[^\n]s", s );
     277        fclose( fp );
     278
     279        /*FIXME Test if password is correct */
    273280
    274281        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".accounts" );
    275282        if (unlink( s ) == -1)
    276                 return( 1 );
     283                return STORAGE_OTHER_ERROR;
    277284       
    278285        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".nicks" );
    279286        if (unlink( s ) == -1)
    280                 return( 1 );
    281 
    282         return( 0 );
    283 }
    284 
    285 static int text_check_pass( const char *nick, const char *password )
    286 {
    287         char s[512];
    288         FILE *fp;
    289        
    290         g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".nicks" );
    291         fp = fopen( s, "r" );
    292 
    293         fscanf( fp, "%32[^\n]s", s );
    294         fclose( fp );
    295 
    296         /* FIXME */
    297         return( 0 );
     287                return STORAGE_OTHER_ERROR;
     288
     289        return STORAGE_OK;
    298290}
    299291
     
    301293        .name = "text",
    302294        .init = text_init,
    303         .exists = text_exists,
    304         .check_pass = text_check_pass,
    305295        .remove = text_remove,
    306296        .load = text_load,
Note: See TracChangeset for help on using the changeset viewer.