Changeset 85d7b85 for storage_text.c
- Timestamp:
- 2008-04-02T14:22:57Z (16 years ago)
- Branches:
- master
- Children:
- f9dbc99
- Parents:
- 875ad42 (diff), dd34575 (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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
storage_text.c
r875ad42 r85d7b85 36 36 #endif 37 37 38 /* DO NOT USE THIS FUNCTION IN NEW CODE. This39 * function is here merely because the save/load code still uses40 * ids rather than names */41 static struct prpl *find_protocol_by_id(int id)42 {43 switch (id) {44 case 0: case 1: case 3: return find_protocol("oscar");45 case 4: return find_protocol("msn");46 case 2: return find_protocol("yahoo");47 case 8: return find_protocol("jabber");48 default: break;49 }50 return NULL;51 }52 53 static int find_protocol_id(const char *name)54 {55 if (!strcmp(name, "oscar")) return 1;56 if (!strcmp(name, "msn")) return 4;57 if (!strcmp(name, "yahoo")) return 2;58 if (!strcmp(name, "jabber")) return 8;59 60 return -1;61 }62 63 64 38 static void text_init (void) 65 39 { 66 if( access( global.conf->configdir, F_OK ) != 0 )67 log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", CONFIG );68 else if( access( global.conf->configdir, R_OK ) != 0 || access( global.conf->configdir, W_OK ) != 0 )69 log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir );40 /* Don't complain about the configuration directory anymore, leave it 41 up to the XML storage module, which uses the same directory for it 42 anyway. Nobody should be using just the text plugin anymore since 43 it's read only! */ 70 44 } 71 45 … … 78 52 FILE *fp; 79 53 user_t *ru = user_find( irc, ROOT_NICK ); 54 account_t *acc, *acc_lookup[9]; 80 55 81 if( irc->status >=USTATUS_IDENTIFIED )56 if( irc->status & USTATUS_IDENTIFIED ) 82 57 return( 1 ); 83 58 … … 88 63 fscanf( fp, "%32[^\n]s", s ); 89 64 90 if (checkpass (password, s) != 0)65 if( checkpass( password, s ) != 0 ) 91 66 { 92 67 fclose( fp ); … … 96 71 /* Do this now. If the user runs with AuthMode = Registered, the 97 72 account command will not work otherwise. */ 98 irc->status = USTATUS_IDENTIFIED;73 irc->status |= USTATUS_IDENTIFIED; 99 74 100 75 while( fscanf( fp, "%511[^\n]s", s ) > 0 ) … … 108 83 fclose( fp ); 109 84 85 /* Build a list with the first listed account of every protocol 86 number. So if the user had nicks defined for a second account on 87 the same IM network, those nicks will be added to the wrong 88 account, and the user should rename those buddies again. But at 89 least from now on things will be saved properly. */ 90 memset( acc_lookup, 0, sizeof( acc_lookup ) ); 91 for( acc = irc->accounts; acc; acc = acc->next ) 92 { 93 if( acc_lookup[0] == NULL && strcmp( acc->prpl->name, "oscar" ) == 0 ) 94 acc_lookup[0] = acc_lookup[1] = acc_lookup[3] = acc; 95 else if( acc_lookup[2] == NULL && strcmp( acc->prpl->name, "yahoo" ) == 0 ) 96 acc_lookup[2] = acc; 97 else if( acc_lookup[4] == NULL && strcmp( acc->prpl->name, "msn" ) == 0 ) 98 acc_lookup[4] = acc; 99 else if( acc_lookup[8] == NULL && strcmp( acc->prpl->name, "jabber" ) == 0 ) 100 acc_lookup[8] = acc; 101 } 102 110 103 g_snprintf( s, 511, "%s%s%s", global.conf->configdir, my_nick, ".nicks" ); 111 104 fp = fopen( s, "r" ); … … 113 106 while( fscanf( fp, "%s %d %s", s, &proto, nick ) > 0 ) 114 107 { 115 struct prpl *prpl; 116 117 prpl = find_protocol_by_id(proto); 118 119 if (!prpl) 108 if( proto < 0 || proto > 8 || ( acc = acc_lookup[proto] ) == NULL ) 120 109 continue; 121 110 122 111 http_decode( s ); 123 nick_set( irc, s, prpl, nick );112 nick_set( acc, s, nick ); 124 113 } 125 114 fclose( fp ); 126 127 if( set_getint( irc, "auto_connect" ) )128 {129 strcpy( s, "account on" ); /* Can't do this directly because r_c_s alters the string */130 root_command_string( irc, ru, s, 0 );131 }132 133 return STORAGE_OK;134 }135 136 static storage_status_t text_save( irc_t *irc, int overwrite )137 {138 char s[512];139 char path[512], new_path[512];140 char *line;141 nick_t *n;142 set_t *set;143 mode_t ou = umask( 0077 );144 account_t *a;145 FILE *fp;146 char *hash;147 148 if (!overwrite) {149 g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" );150 if (access( path, F_OK ) != -1)151 return STORAGE_ALREADY_EXISTS;152 153 g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" );154 if (access( path, F_OK ) != -1)155 return STORAGE_ALREADY_EXISTS;156 }157 158 /*\159 * [SH] Nothing should be saved if no password is set, because the160 * password is not set if it was wrong, or if one is not identified161 * yet. This means that a malicious user could easily overwrite162 * files owned by someone else:163 * a Bad Thing, methinks164 \*/165 166 /* [WVG] No? Really? */167 168 /*\169 * [SH] Okay, okay, it wasn't really Wilmer who said that, it was170 * me. I just thought it was funny.171 \*/172 173 hash = hashpass( irc->password );174 if( hash == NULL )175 {176 irc_usermsg( irc, "Please register yourself if you want to save your settings." );177 return STORAGE_OTHER_ERROR;178 }179 180 g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".nicks~" );181 fp = fopen( path, "w" );182 if( !fp ) return STORAGE_OTHER_ERROR;183 for( n = irc->nicks; n; n = n->next )184 {185 strcpy( s, n->handle );186 s[169] = 0; /* Prevent any overflow (169 ~ 512 / 3) */187 http_encode( s );188 g_snprintf( s + strlen( s ), 510 - strlen( s ), " %d %s", find_protocol_id(n->proto->name), n->nick );189 if( fprintf( fp, "%s\n", s ) != strlen( s ) + 1 )190 {191 irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );192 fclose( fp );193 return STORAGE_OTHER_ERROR;194 }195 }196 if( fclose( fp ) != 0 )197 {198 irc_usermsg( irc, "fclose() reported an error. Disk full?" );199 return STORAGE_OTHER_ERROR;200 }201 202 g_snprintf( new_path, 512, "%s%s%s", global.conf->configdir, irc->nick, ".nicks" );203 if( unlink( new_path ) != 0 )204 {205 if( errno != ENOENT )206 {207 irc_usermsg( irc, "Error while removing old .nicks file" );208 return STORAGE_OTHER_ERROR;209 }210 }211 if( rename( path, new_path ) != 0 )212 {213 irc_usermsg( irc, "Error while renaming new .nicks file" );214 return STORAGE_OTHER_ERROR;215 }216 217 g_snprintf( path, 511, "%s%s%s", global.conf->configdir, irc->nick, ".accounts~" );218 fp = fopen( path, "w" );219 if( !fp ) return STORAGE_OTHER_ERROR;220 if( fprintf( fp, "%s", hash ) != strlen( hash ) )221 {222 irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );223 fclose( fp );224 return STORAGE_OTHER_ERROR;225 }226 g_free( hash );227 228 for( a = irc->accounts; a; a = a->next )229 {230 if( !strcmp(a->prpl->name, "oscar") )231 g_snprintf( s, sizeof( s ), "account add oscar \"%s\" \"%s\" %s", a->user, a->pass, a->server );232 else233 g_snprintf( s, sizeof( s ), "account add %s \"%s\" \"%s\" \"%s\"",234 a->prpl->name, a->user, a->pass, a->server ? a->server : "" );235 236 line = obfucrypt( s, irc->password );237 if( *line )238 {239 if( fprintf( fp, "%s\n", line ) != strlen( line ) + 1 )240 {241 irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );242 fclose( fp );243 return STORAGE_OTHER_ERROR;244 }245 }246 g_free( line );247 }248 249 for( set = irc->set; set; set = set->next )250 {251 if( set->value && set->def )252 {253 g_snprintf( s, sizeof( s ), "set %s \"%s\"", set->key, set->value );254 line = obfucrypt( s, irc->password );255 if( *line )256 {257 if( fprintf( fp, "%s\n", line ) != strlen( line ) + 1 )258 {259 irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );260 fclose( fp );261 return STORAGE_OTHER_ERROR;262 }263 }264 g_free( line );265 }266 }267 268 if( strcmp( irc->mynick, ROOT_NICK ) != 0 )269 {270 g_snprintf( s, sizeof( s ), "rename %s %s", ROOT_NICK, irc->mynick );271 line = obfucrypt( s, irc->password );272 if( *line )273 {274 if( fprintf( fp, "%s\n", line ) != strlen( line ) + 1 )275 {276 irc_usermsg( irc, "fprintf() wrote too little. Disk full?" );277 fclose( fp );278 return STORAGE_OTHER_ERROR;279 }280 }281 g_free( line );282 }283 if( fclose( fp ) != 0 )284 {285 irc_usermsg( irc, "fclose() reported an error. Disk full?" );286 return STORAGE_OTHER_ERROR;287 }288 289 g_snprintf( new_path, 512, "%s%s%s", global.conf->configdir, irc->nick, ".accounts" );290 if( unlink( new_path ) != 0 )291 {292 if( errno != ENOENT )293 {294 irc_usermsg( irc, "Error while removing old .accounts file" );295 return STORAGE_OTHER_ERROR;296 }297 }298 if( rename( path, new_path ) != 0 )299 {300 irc_usermsg( irc, "Error while renaming new .accounts file" );301 return STORAGE_OTHER_ERROR;302 }303 304 umask( ou );305 115 306 116 return STORAGE_OK; … … 351 161 .check_pass = text_check_pass, 352 162 .remove = text_remove, 353 .load = text_load, 354 .save = text_save 163 .load = text_load 355 164 };
Note: See TracChangeset
for help on using the changeset viewer.