src/pEpEngine.c
branchENGINE-293
changeset 2308 b7ef1c9005ae
parent 2306 5744a544ecd2
child 2311 688b925c3e73
     1.1 --- a/src/pEpEngine.c	Thu Dec 07 09:31:47 2017 +0100
     1.2 +++ b/src/pEpEngine.c	Sat Dec 09 13:04:40 2017 +0100
     1.3 @@ -47,7 +47,6 @@
     1.4      "select id, word from wordlist where lang = lower(?1) "
     1.5      "and id = ?2 ;";
     1.6  
     1.7 -
     1.8  static const char *sql_get_identity =  
     1.9      "select fpr, username, comm_type, lang,"
    1.10      "   identity.flags | pgp_keypair.flags,"
    1.11 @@ -64,6 +63,18 @@
    1.12      "          end) = 1"
    1.13      "   and identity.user_id = ?2;";
    1.14  
    1.15 +static const char *sql_get_identity_without_fpr =  
    1.16 +    "select main_key_id, username, comm_type, lang,"
    1.17 +    "   identity.flags, is_own"
    1.18 +    "   from identity"
    1.19 +    "   join person on id = identity.user_id"
    1.20 +    "   where (case when (address = ?1) then (1)"
    1.21 +    "               when (lower(address) = lower(?1)) then (1)"
    1.22 +    "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
    1.23 +    "               else 0"
    1.24 +    "          end) = 1"
    1.25 +    "   and identity.user_id = ?2;";
    1.26 +
    1.27  static const char *sql_replace_identities_fpr =  
    1.28      "update identity"
    1.29      "   set main_key_id = ?1 "
    1.30 @@ -692,6 +703,11 @@
    1.31              (int)strlen(sql_get_identity), &_session->get_identity, NULL);
    1.32      assert(int_result == SQLITE_OK);
    1.33  
    1.34 +    int_result = sqlite3_prepare_v2(_session->db, sql_get_identity_without_fpr,
    1.35 +            (int)strlen(sql_get_identity_without_fpr), 
    1.36 +            &_session->get_identity_without_fpr, NULL);
    1.37 +    assert(int_result == SQLITE_OK);
    1.38 +
    1.39      int_result = sqlite3_prepare_v2(_session->db, sql_get_own_userid,
    1.40              (int)strlen(sql_get_own_userid), &_session->get_own_userid, NULL);
    1.41      assert(int_result == SQLITE_OK);
    1.42 @@ -937,6 +953,8 @@
    1.43                  sqlite3_finalize(session->trustword);
    1.44              if (session->get_identity)
    1.45                  sqlite3_finalize(session->get_identity);
    1.46 +            if (session->get_identity_without_fpr)
    1.47 +                sqlite3_finalize(session->get_identity_without_fpr);    
    1.48              if (session->get_own_userid)
    1.49                  sqlite3_finalize(session->get_own_userid);
    1.50              if (session->replace_identities_fpr)
    1.51 @@ -1350,11 +1368,13 @@
    1.52      return status;
    1.53  }
    1.54  
    1.55 -DYNAMIC_API PEP_STATUS get_identity(
    1.56 +
    1.57 +static PEP_STATUS _get_identity_internal(
    1.58          PEP_SESSION session,
    1.59          const char *address,
    1.60          const char *user_id,
    1.61 -        pEp_identity **identity
    1.62 +        pEp_identity **identity,
    1.63 +        sqlite3_stmt* get_id_stmt
    1.64      )
    1.65  {
    1.66      PEP_STATUS status = PEP_STATUS_OK;
    1.67 @@ -1370,27 +1390,27 @@
    1.68  
    1.69      *identity = NULL;
    1.70  
    1.71 -    sqlite3_reset(session->get_identity);
    1.72 -    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
    1.73 -    sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
    1.74 -
    1.75 -    const int result = sqlite3_step(session->get_identity);
    1.76 +    sqlite3_reset(get_id_stmt);
    1.77 +    sqlite3_bind_text(get_id_stmt, 1, address, -1, SQLITE_STATIC);
    1.78 +    sqlite3_bind_text(get_id_stmt, 2, user_id, -1, SQLITE_STATIC);
    1.79 +
    1.80 +    const int result = sqlite3_step(get_id_stmt);
    1.81      switch (result) {
    1.82      case SQLITE_ROW:
    1.83          _identity = new_identity(
    1.84                  address,
    1.85 -                (const char *) sqlite3_column_text(session->get_identity, 0),
    1.86 +                (const char *) sqlite3_column_text(get_id_stmt, 0),
    1.87                  user_id,
    1.88 -                (const char *) sqlite3_column_text(session->get_identity, 1)
    1.89 +                (const char *) sqlite3_column_text(get_id_stmt, 1)
    1.90                  );
    1.91          assert(_identity);
    1.92          if (_identity == NULL)
    1.93              return PEP_OUT_OF_MEMORY;
    1.94  
    1.95          _identity->comm_type = (PEP_comm_type)
    1.96 -            sqlite3_column_int(session->get_identity, 2);
    1.97 +            sqlite3_column_int(get_id_stmt, 2);
    1.98          const char* const _lang = (const char *)
    1.99 -            sqlite3_column_text(session->get_identity, 3);
   1.100 +            sqlite3_column_text(get_id_stmt, 3);
   1.101          if (_lang && _lang[0]) {
   1.102              assert(_lang[0] >= 'a' && _lang[0] <= 'z');
   1.103              assert(_lang[1] >= 'a' && _lang[1] <= 'z');
   1.104 @@ -1400,9 +1420,9 @@
   1.105              _identity->lang[2] = 0;
   1.106          }
   1.107          _identity->flags = (unsigned int)
   1.108 -            sqlite3_column_int(session->get_identity, 4);
   1.109 +            sqlite3_column_int(get_id_stmt, 4);
   1.110          _identity->me = (unsigned int)
   1.111 -            sqlite3_column_int(session->get_identity, 5);
   1.112 +            sqlite3_column_int(get_id_stmt, 5);
   1.113      
   1.114          *identity = _identity;
   1.115          break;
   1.116 @@ -1411,10 +1431,33 @@
   1.117          *identity = NULL;
   1.118      }
   1.119  
   1.120 -    sqlite3_reset(session->get_identity);
   1.121 +    sqlite3_reset(get_id_stmt);
   1.122      return status;
   1.123  }
   1.124  
   1.125 +DYNAMIC_API PEP_STATUS get_identity(
   1.126 +        PEP_SESSION session,
   1.127 +        const char *address,
   1.128 +        const char *user_id,
   1.129 +        pEp_identity **identity
   1.130 +    )
   1.131 +{
   1.132 +    return _get_identity_internal(session, address, user_id, identity,
   1.133 +                                  session->get_identity);
   1.134 +}
   1.135 +
   1.136 +PEP_STATUS get_identity_without_fpr(
   1.137 +        PEP_SESSION session,
   1.138 +        const char *address,
   1.139 +        const char *user_id,
   1.140 +        pEp_identity **identity
   1.141 +    )
   1.142 +{
   1.143 +    return _get_identity_internal(session, address, user_id, identity,
   1.144 +                                  session->get_identity_without_fpr);
   1.145 +}
   1.146 +
   1.147 +
   1.148  DYNAMIC_API PEP_STATUS set_identity(
   1.149          PEP_SESSION session, const pEp_identity *identity
   1.150      )