src/pEpEngine.c
changeset 46 7471e31bb278
parent 39 66b5cc6cb987
child 62 ad5e484720e1
     1.1 --- a/src/pEpEngine.c	Fri Jan 16 18:39:27 2015 +0100
     1.2 +++ b/src/pEpEngine.c	Sat Jan 24 15:26:40 2015 +0100
     1.3 @@ -29,8 +29,8 @@
     1.4  	_session->version = PEP_ENGINE_VERSION;
     1.5  
     1.6      init_cryptotech(_session);
     1.7 -    init_transport_system(_session->transports);
     1.8 -
     1.9 +    init_transport_system(_session);
    1.10 +    
    1.11      assert(LOCAL_DB);
    1.12      if (LOCAL_DB == NULL) {
    1.13          release_transport_system(_session);
    1.14 @@ -232,27 +232,26 @@
    1.15  DYNAMIC_API void release(PEP_SESSION session)
    1.16  {
    1.17  	assert(session);
    1.18 -	pEpSession *_session = (pEpSession *) session;
    1.19  
    1.20 -	if (_session) {
    1.21 -		if (_session->db) {
    1.22 -			sqlite3_finalize(_session->safeword);
    1.23 -			sqlite3_finalize(_session->log);
    1.24 -			sqlite3_finalize(_session->get_identity);
    1.25 -			sqlite3_finalize(_session->set_identity);
    1.26 -            sqlite3_finalize(_session->set_person);
    1.27 -            sqlite3_finalize(_session->set_pgp_keypair);
    1.28 -            sqlite3_finalize(_session->set_trust);
    1.29 -            sqlite3_finalize(_session->get_trust);
    1.30 +	if (session) {
    1.31 +		if (session->db) {
    1.32 +			sqlite3_finalize(session->safeword);
    1.33 +			sqlite3_finalize(session->log);
    1.34 +			sqlite3_finalize(session->get_identity);
    1.35 +			sqlite3_finalize(session->set_identity);
    1.36 +            sqlite3_finalize(session->set_person);
    1.37 +            sqlite3_finalize(session->set_pgp_keypair);
    1.38 +            sqlite3_finalize(session->set_trust);
    1.39 +            sqlite3_finalize(session->get_trust);
    1.40  
    1.41 -			sqlite3_close_v2(_session->db);
    1.42 -			sqlite3_close_v2(_session->system_db);
    1.43 +			sqlite3_close_v2(session->db);
    1.44 +			sqlite3_close_v2(session->system_db);
    1.45  		}
    1.46  
    1.47 -        release_transport_system(_session);
    1.48 -        release_cryptotech(_session);
    1.49 +        release_transport_system(session);
    1.50 +        release_cryptotech(session);
    1.51      }
    1.52 -	free(_session);
    1.53 +	free(session);
    1.54  }
    1.55  
    1.56  stringlist_t *new_stringlist(const char *value)
    1.57 @@ -363,32 +362,31 @@
    1.58          const char *description, const char *comment
    1.59      )
    1.60  {
    1.61 -	pEpSession *_session = (pEpSession *) session;
    1.62  	PEP_STATUS status = PEP_STATUS_OK;
    1.63  	int result;
    1.64  
    1.65 -	assert(_session);
    1.66 +	assert(session);
    1.67  	assert(title);
    1.68  	assert(entity);
    1.69  
    1.70 -	sqlite3_reset(_session->log);
    1.71 -	sqlite3_bind_text(_session->log, 1, title, -1, SQLITE_STATIC);
    1.72 -	sqlite3_bind_text(_session->log, 2, entity, -1, SQLITE_STATIC);
    1.73 +	sqlite3_reset(session->log);
    1.74 +	sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
    1.75 +	sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
    1.76  	if (description)
    1.77 -        sqlite3_bind_text(_session->log, 3, description, -1, SQLITE_STATIC);
    1.78 +        sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
    1.79  	else
    1.80 -		sqlite3_bind_null(_session->log, 3);
    1.81 +		sqlite3_bind_null(session->log, 3);
    1.82  	if (comment)
    1.83 -		sqlite3_bind_text(_session->log, 4, comment, -1, SQLITE_STATIC);
    1.84 +		sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
    1.85  	else
    1.86 -		sqlite3_bind_null(_session->log, 4);
    1.87 +		sqlite3_bind_null(session->log, 4);
    1.88  	do {
    1.89 -		result = sqlite3_step(_session->log);
    1.90 +		result = sqlite3_step(session->log);
    1.91  		assert(result == SQLITE_DONE || result == SQLITE_BUSY);
    1.92  		if (result != SQLITE_DONE && result != SQLITE_BUSY)
    1.93  			status = PEP_UNKNOWN_ERROR;
    1.94  	} while (result == SQLITE_BUSY);
    1.95 -	sqlite3_reset(_session->log);
    1.96 +	sqlite3_reset(session->log);
    1.97  
    1.98  	return status;
    1.99  }
   1.100 @@ -398,11 +396,10 @@
   1.101              char **word, size_t *wsize
   1.102          )
   1.103  {
   1.104 -	pEpSession *_session = (pEpSession *) session;
   1.105  	PEP_STATUS status = PEP_STATUS_OK;
   1.106  	int result;
   1.107  
   1.108 -	assert(_session);
   1.109 +	assert(session);
   1.110  	assert(word);
   1.111  	assert(wsize);
   1.112  
   1.113 @@ -418,22 +415,22 @@
   1.114              || (lang[1] >= 'a' && lang[1] <= 'z'));
   1.115  	assert(lang[2] == 0);
   1.116  
   1.117 -	sqlite3_reset(_session->safeword);
   1.118 -    sqlite3_bind_text(_session->safeword, 1, lang, -1, SQLITE_STATIC);
   1.119 -	sqlite3_bind_int(_session->safeword, 2, value);
   1.120 +	sqlite3_reset(session->safeword);
   1.121 +    sqlite3_bind_text(session->safeword, 1, lang, -1, SQLITE_STATIC);
   1.122 +	sqlite3_bind_int(session->safeword, 2, value);
   1.123  
   1.124 -	result = sqlite3_step(_session->safeword);
   1.125 +	result = sqlite3_step(session->safeword);
   1.126  	if (result == SQLITE_ROW) {
   1.127 -        *word = strdup((const char *) sqlite3_column_text(_session->safeword,
   1.128 +        *word = strdup((const char *) sqlite3_column_text(session->safeword,
   1.129                      1));
   1.130  		if (*word)
   1.131 -            *wsize = sqlite3_column_bytes(_session->safeword, 1);
   1.132 +            *wsize = sqlite3_column_bytes(session->safeword, 1);
   1.133  		else
   1.134  			status = PEP_SAFEWORD_NOT_FOUND;
   1.135  	} else
   1.136  		status = PEP_SAFEWORD_NOT_FOUND;
   1.137  
   1.138 -	sqlite3_reset(_session->safeword);
   1.139 +	sqlite3_reset(session->safeword);
   1.140  	return status;
   1.141  }
   1.142  
   1.143 @@ -607,7 +604,6 @@
   1.144          pEp_identity **identity
   1.145      )
   1.146  {
   1.147 -	pEpSession *_session = (pEpSession *) session;
   1.148  	PEP_STATUS status = PEP_STATUS_OK;
   1.149  	static pEp_identity *_identity;
   1.150  	int result;
   1.151 @@ -617,24 +613,24 @@
   1.152  	assert(address);
   1.153      assert(address[0]);
   1.154  
   1.155 -    sqlite3_reset(_session->get_identity);
   1.156 -    sqlite3_bind_text(_session->get_identity, 1, address, -1, SQLITE_STATIC);
   1.157 +    sqlite3_reset(session->get_identity);
   1.158 +    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
   1.159  
   1.160 -    result = sqlite3_step(_session->get_identity);
   1.161 +    result = sqlite3_step(session->get_identity);
   1.162  	switch (result) {
   1.163  	case SQLITE_ROW:
   1.164          _identity = new_identity(
   1.165                  address,
   1.166 -                (const char *) sqlite3_column_text(_session->get_identity, 0),
   1.167 -                (const char *) sqlite3_column_text(_session->get_identity, 1),
   1.168 -                (const char *) sqlite3_column_text(_session->get_identity, 2)
   1.169 +                (const char *) sqlite3_column_text(session->get_identity, 0),
   1.170 +                (const char *) sqlite3_column_text(session->get_identity, 1),
   1.171 +                (const char *) sqlite3_column_text(session->get_identity, 2)
   1.172                  );
   1.173          assert(_identity);
   1.174          if (_identity == NULL)
   1.175              return PEP_OUT_OF_MEMORY;
   1.176  
   1.177 -        _identity->comm_type = (PEP_comm_type) sqlite3_column_int(_session->get_identity, 3);
   1.178 -        _lang = (const char *) sqlite3_column_text(_session->get_identity, 4);
   1.179 +        _identity->comm_type = (PEP_comm_type) sqlite3_column_int(session->get_identity, 3);
   1.180 +        _lang = (const char *) sqlite3_column_text(session->get_identity, 4);
   1.181          if (_lang && _lang[0]) {
   1.182  			assert(_lang[0] >= 'a' && _lang[0] <= 'z');
   1.183  			assert(_lang[1] >= 'a' && _lang[1] <= 'z');
   1.184 @@ -650,7 +646,7 @@
   1.185  		*identity = NULL;
   1.186  	}
   1.187  
   1.188 -    sqlite3_reset(_session->get_identity);
   1.189 +    sqlite3_reset(session->get_identity);
   1.190  	return status;
   1.191  }
   1.192  
   1.193 @@ -658,7 +654,6 @@
   1.194          PEP_SESSION session, const pEp_identity *identity
   1.195      )
   1.196  {
   1.197 -	pEpSession *_session = (pEpSession *) session;
   1.198  	int result;
   1.199  
   1.200  	assert(session);
   1.201 @@ -668,63 +663,63 @@
   1.202  	assert(identity->user_id);
   1.203  	assert(identity->username);
   1.204  
   1.205 -	sqlite3_exec(_session->db, "BEGIN ;", NULL, NULL, NULL);
   1.206 +	sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
   1.207  
   1.208 -	sqlite3_reset(_session->set_person);
   1.209 -    sqlite3_bind_text(_session->set_person, 1, identity->user_id, -1,
   1.210 +	sqlite3_reset(session->set_person);
   1.211 +    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
   1.212              SQLITE_STATIC);
   1.213 -    sqlite3_bind_text(_session->set_person, 2, identity->username, -1,
   1.214 +    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
   1.215              SQLITE_STATIC);
   1.216  	if (identity->lang[0])
   1.217 -        sqlite3_bind_text(_session->set_person, 3, identity->lang, 1,
   1.218 +        sqlite3_bind_text(session->set_person, 3, identity->lang, 1,
   1.219                  SQLITE_STATIC);
   1.220  	else
   1.221 -		sqlite3_bind_null(_session->set_person, 3);
   1.222 -	result = sqlite3_step(_session->set_person);
   1.223 -	sqlite3_reset(_session->set_person);
   1.224 +		sqlite3_bind_null(session->set_person, 3);
   1.225 +	result = sqlite3_step(session->set_person);
   1.226 +	sqlite3_reset(session->set_person);
   1.227  	if (result != SQLITE_DONE) {
   1.228 -		sqlite3_exec(_session->db, "ROLLBACK ;", NULL, NULL, NULL);
   1.229 +		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   1.230  		return PEP_CANNOT_SET_PERSON;
   1.231  	}
   1.232  
   1.233 -	sqlite3_reset(_session->set_pgp_keypair);
   1.234 -    sqlite3_bind_text(_session->set_pgp_keypair, 1, identity->fpr, -1,
   1.235 +	sqlite3_reset(session->set_pgp_keypair);
   1.236 +    sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
   1.237              SQLITE_STATIC);
   1.238 -	result = sqlite3_step(_session->set_pgp_keypair);
   1.239 -	sqlite3_reset(_session->set_pgp_keypair);
   1.240 +	result = sqlite3_step(session->set_pgp_keypair);
   1.241 +	sqlite3_reset(session->set_pgp_keypair);
   1.242  	if (result != SQLITE_DONE) {
   1.243 -		sqlite3_exec(_session->db, "ROLLBACK ;", NULL, NULL, NULL);
   1.244 +		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   1.245  		return PEP_CANNOT_SET_PGP_KEYPAIR;
   1.246  	}
   1.247  
   1.248 -	sqlite3_reset(_session->set_identity);
   1.249 -    sqlite3_bind_text(_session->set_identity, 1, identity->address, -1,
   1.250 +	sqlite3_reset(session->set_identity);
   1.251 +    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
   1.252              SQLITE_STATIC);
   1.253 -    sqlite3_bind_text(_session->set_identity, 2, identity->fpr, -1,
   1.254 +    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
   1.255              SQLITE_STATIC);
   1.256 -    sqlite3_bind_text(_session->set_identity, 3, identity->user_id, -1,
   1.257 +    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
   1.258              SQLITE_STATIC);
   1.259 -	result = sqlite3_step(_session->set_identity);
   1.260 -	sqlite3_reset(_session->set_identity);
   1.261 +	result = sqlite3_step(session->set_identity);
   1.262 +	sqlite3_reset(session->set_identity);
   1.263  	if (result != SQLITE_DONE) {
   1.264 -		sqlite3_exec(_session->db, "ROLLBACK ;", NULL, NULL, NULL);
   1.265 +		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   1.266  		return PEP_CANNOT_SET_IDENTITY;
   1.267  	}
   1.268  
   1.269 -	sqlite3_reset(_session->set_trust);
   1.270 -    sqlite3_bind_text(_session->set_trust, 1, identity->user_id, -1,
   1.271 +	sqlite3_reset(session->set_trust);
   1.272 +    sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
   1.273              SQLITE_STATIC);
   1.274 -    sqlite3_bind_text(_session->set_trust, 2, identity->fpr, -1,
   1.275 +    sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
   1.276              SQLITE_STATIC);
   1.277 -	sqlite3_bind_int(_session->set_trust, 3, identity->comm_type);
   1.278 -	result = sqlite3_step(_session->set_trust);
   1.279 -	sqlite3_reset(_session->set_trust);
   1.280 +	sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
   1.281 +	result = sqlite3_step(session->set_trust);
   1.282 +	sqlite3_reset(session->set_trust);
   1.283  	if (result != SQLITE_DONE) {
   1.284 -		sqlite3_exec(_session->db, "ROLLBACK ;", NULL, NULL, NULL);
   1.285 +		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   1.286  		return PEP_CANNOT_SET_IDENTITY;
   1.287  	}
   1.288  
   1.289 -    result = sqlite3_exec(_session->db, "COMMIT ;", NULL, NULL, NULL);
   1.290 +    result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
   1.291  	if (result == SQLITE_OK)
   1.292  		return PEP_STATUS_OK;
   1.293  	else
   1.294 @@ -738,7 +733,6 @@
   1.295  
   1.296  DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
   1.297  {
   1.298 -    pEpSession *_session = (pEpSession *) session;
   1.299      PEP_STATUS status = PEP_STATUS_OK;
   1.300      int result;
   1.301  
   1.302 @@ -751,15 +745,15 @@
   1.303  
   1.304      identity->comm_type = PEP_ct_unknown;
   1.305  
   1.306 -    sqlite3_reset(_session->get_trust);
   1.307 -    sqlite3_bind_text(_session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
   1.308 -    sqlite3_bind_text(_session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
   1.309 +    sqlite3_reset(session->get_trust);
   1.310 +    sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
   1.311 +    sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
   1.312  
   1.313 -    result = sqlite3_step(_session->get_trust);
   1.314 +    result = sqlite3_step(session->get_trust);
   1.315      switch (result) {
   1.316      case SQLITE_ROW: {
   1.317 -        const char * user_id = (const char *) sqlite3_column_text(_session->get_trust, 1);
   1.318 -        int comm_type = (PEP_comm_type) sqlite3_column_int(_session->get_trust, 2);
   1.319 +        const char * user_id = (const char *) sqlite3_column_text(session->get_trust, 1);
   1.320 +        int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust, 2);
   1.321  
   1.322          if (strcmp(user_id, identity->user_id) != 0) {
   1.323              free(identity->user_id);
   1.324 @@ -776,7 +770,7 @@
   1.325          status = PEP_CANNOT_FIND_IDENTITY;
   1.326      }
   1.327  
   1.328 -    sqlite3_reset(_session->get_trust);
   1.329 +    sqlite3_reset(session->get_trust);
   1.330      return status;
   1.331  }
   1.332  
   1.333 @@ -785,8 +779,7 @@
   1.334      char **ptext, size_t *psize, stringlist_t **keylist
   1.335      )
   1.336  {
   1.337 -    pEpSession *_session = (pEpSession *) session;
   1.338 -    return _session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(session, ctext, csize, ptext, psize, keylist);
   1.339 +    return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(session, ctext, csize, ptext, psize, keylist);
   1.340  }
   1.341  
   1.342  DYNAMIC_API PEP_STATUS encrypt_and_sign(
   1.343 @@ -794,8 +787,7 @@
   1.344      size_t psize, char **ctext, size_t *csize
   1.345      )
   1.346  {
   1.347 -    pEpSession *_session = (pEpSession *) session;
   1.348 -    return _session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session, keylist, ptext, psize, ctext, csize);
   1.349 +    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session, keylist, ptext, psize, ctext, csize);
   1.350  }
   1.351  
   1.352  DYNAMIC_API PEP_STATUS verify_text(
   1.353 @@ -803,64 +795,55 @@
   1.354      const char *signature, size_t sig_size, stringlist_t **keylist
   1.355      )
   1.356  {
   1.357 -    pEpSession *_session = (pEpSession *) session;
   1.358 -    return _session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text, size, signature, sig_size, keylist);
   1.359 +    return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text, size, signature, sig_size, keylist);
   1.360  }
   1.361  
   1.362  DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
   1.363  {
   1.364 -    pEpSession *_session = (pEpSession *) session;
   1.365 -    return _session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
   1.366 +    return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
   1.367  }
   1.368  
   1.369  DYNAMIC_API PEP_STATUS export_key(
   1.370 -    PEP_SESSION session, const char *fpr, char **key_data, size_t *size
   1.371 +        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
   1.372      )
   1.373  {
   1.374 -    pEpSession *_session = (pEpSession *) session;
   1.375 -    return _session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr, key_data, size);
   1.376 +    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr, key_data, size);
   1.377  }
   1.378  
   1.379  DYNAMIC_API PEP_STATUS find_keys(
   1.380 -    PEP_SESSION session, const char *pattern, stringlist_t **keylist
   1.381 +        PEP_SESSION session, const char *pattern, stringlist_t **keylist
   1.382      )
   1.383  {
   1.384 -    pEpSession *_session = (pEpSession *) session;
   1.385 -    return _session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern, keylist);
   1.386 +    return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern, keylist);
   1.387  }
   1.388  
   1.389  DYNAMIC_API PEP_STATUS generate_keypair(
   1.390 -    PEP_SESSION session, pEp_identity *identity
   1.391 +        PEP_SESSION session, pEp_identity *identity
   1.392      )
   1.393  {
   1.394 -    pEpSession *_session = (pEpSession *) session;
   1.395 -    return _session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session, identity);
   1.396 +    return session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session, identity);
   1.397  }
   1.398  
   1.399  DYNAMIC_API PEP_STATUS get_key_rating(
   1.400 -    PEP_SESSION session,
   1.401 -    const char *fpr,
   1.402 -    PEP_comm_type *comm_type
   1.403 +        PEP_SESSION session,
   1.404 +        const char *fpr,
   1.405 +        PEP_comm_type *comm_type
   1.406      )
   1.407  {
   1.408 -    pEpSession *_session = (pEpSession *) session;
   1.409 -    return _session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr, comm_type);
   1.410 +    return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr, comm_type);
   1.411  }
   1.412  
   1.413  DYNAMIC_API PEP_STATUS import_key(PEP_SESSION session, const char *key_data, size_t size)
   1.414  {
   1.415 -    pEpSession *_session = (pEpSession *) session;
   1.416 -    return _session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data, size);
   1.417 +    return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data, size);
   1.418  }
   1.419  
   1.420  DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
   1.421  {
   1.422 -    pEpSession *_session = (pEpSession *) session;
   1.423 -    return _session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
   1.424 +    return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
   1.425  }
   1.426  
   1.427  DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
   1.428  {
   1.429 -    pEpSession *_session = (pEpSession *) session;
   1.430 -    return _session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
   1.431 +    return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
   1.432  }