src/pEpEngine.c
branchENGINE-289
changeset 2326 6d2a523aad35
parent 2324 de5b5578fc16
child 2329 b9b75cd73c2b
     1.1 --- a/src/pEpEngine.c	Mon Dec 18 21:31:22 2017 +0100
     1.2 +++ b/src/pEpEngine.c	Mon Dec 18 23:00:56 2017 +0100
     1.3 @@ -76,7 +76,7 @@
     1.4      "   and identity.user_id = ?2;";
     1.5  
     1.6  static const char *sql_get_identities_by_address =  
     1.7 -    "select main_key_id, username, comm_type, lang,"
     1.8 +    "select user_id, main_key_id, username, comm_type, lang,"
     1.9      "   identity.flags, is_own"
    1.10      "   from identity"
    1.11      "   where (case when (address = ?1) then (1)"
    1.12 @@ -89,6 +89,10 @@
    1.13      "update identity"
    1.14      "   set main_key_id = ?1 "
    1.15      "   where main_key_id = ?2 ;";
    1.16 +    
    1.17 +static const char *sql_remove_fpr_as_default =
    1.18 +    "update person set main_key_id = NULL where main_key_id = ?1 ;"
    1.19 +    "update identity set main_key_id = NULL where main_key_id = ?1 ;";
    1.20  
    1.21  // Set person, but if already exist, only update.
    1.22  // if main_key_id already set, don't touch.
    1.23 @@ -690,6 +694,7 @@
    1.24                  // person->id replacements (for temp ids like "TOFU_")
    1.25                  // so here we go...
    1.26                  int_result = sqlite3_exec(
    1.27 +                    _session->db,
    1.28                      "PRAGMA foreign_keys=off;\n"
    1.29                      "BEGIN TRANSACTION;\n"
    1.30                      "ALTER TABLE identity RENAME TO _identity_old;\n"
    1.31 @@ -790,6 +795,11 @@
    1.32              (int)strlen(sql_replace_identities_fpr), 
    1.33              &_session->replace_identities_fpr, NULL);
    1.34      assert(int_result == SQLITE_OK);
    1.35 +    
    1.36 +    int_result = sqlite3_prepare_v2(_session->db, sql_remove_fpr_as_default,
    1.37 +            (int)strlen(sql_remove_fpr_as_default), 
    1.38 +            &_session->remove_fpr_as_default, NULL);
    1.39 +    assert(int_result == SQLITE_OK);
    1.40  
    1.41      int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
    1.42              (int)strlen(sql_set_person), &_session->set_person, NULL);
    1.43 @@ -1041,6 +1051,8 @@
    1.44                  sqlite3_finalize(session->get_own_userid);
    1.45              if (session->replace_identities_fpr)
    1.46                  sqlite3_finalize(session->replace_identities_fpr);        
    1.47 +            if (session->remove_fpr_as_default)
    1.48 +                sqlite3_finalize(session->remove_fpr_as_default);            
    1.49              if (session->set_person)
    1.50                  sqlite3_finalize(session->set_person);
    1.51              if (session->set_device_group)
    1.52 @@ -1559,38 +1571,39 @@
    1.53      *id_list = NULL;
    1.54      identity_list* ident_list = NULL;
    1.55  
    1.56 -    sqlite3_reset(sql->get_identities_by_address);
    1.57 -    sqlite3_bind_text(sql->get_identities_by_address, 1, address, -1, SQLITE_STATIC);
    1.58 +    sqlite3_reset(session->get_identities_by_address);
    1.59 +    sqlite3_bind_text(session->get_identities_by_address, 1, address, -1, SQLITE_STATIC);
    1.60      int result;
    1.61  
    1.62 -    while ((result = sqlite3_step(sql->get_identities_by_address)) == SQLITE_ROW) {
    1.63 -        
    1.64 +    while ((result = sqlite3_step(session->get_identities_by_address)) == SQLITE_ROW) {
    1.65 +        //"select user_id, main_key_id, username, comm_type, lang,"
    1.66 +        //"   identity.flags, is_own"
    1.67          ident = new_identity(
    1.68                  address,
    1.69 -                (const char *) sqlite3_column_text(sql->get_identities_by_address, 0),
    1.70 -                user_id,
    1.71 -                (const char *) sqlite3_column_text(sql->get_identities_by_address, 1)
    1.72 +                (const char *) sqlite3_column_text(session->get_identities_by_address, 1),
    1.73 +                (const char *) sqlite3_column_text(session->get_identities_by_address, 0),
    1.74 +                (const char *) sqlite3_column_text(session->get_identities_by_address, 2)
    1.75                  );
    1.76 -        assert(_identity);
    1.77 +        assert(ident);
    1.78          if (ident == NULL)
    1.79              return PEP_OUT_OF_MEMORY;
    1.80  
    1.81          ident->comm_type = (PEP_comm_type)
    1.82 -            sqlite3_column_int(sql->get_identities_by_address, 2);
    1.83 +            sqlite3_column_int(session->get_identities_by_address, 3);
    1.84          const char* const _lang = (const char *)
    1.85 -            sqlite3_column_text(sql->get_identities_by_address, 3);
    1.86 +            sqlite3_column_text(session->get_identities_by_address, 4);
    1.87          if (_lang && _lang[0]) {
    1.88              assert(_lang[0] >= 'a' && _lang[0] <= 'z');
    1.89              assert(_lang[1] >= 'a' && _lang[1] <= 'z');
    1.90              assert(_lang[2] == 0);
    1.91 -            _identity->lang[0] = _lang[0];
    1.92 -            _identity->lang[1] = _lang[1];
    1.93 -            _identity->lang[2] = 0;
    1.94 +            ident->lang[0] = _lang[0];
    1.95 +            ident->lang[1] = _lang[1];
    1.96 +            ident->lang[2] = 0;
    1.97          }
    1.98          ident->flags = (unsigned int)
    1.99 -            sqlite3_column_int(sql->get_identities_by_address, 4);
   1.100 +            sqlite3_column_int(session->get_identities_by_address, 5);
   1.101          ident->me = (unsigned int)
   1.102 -            sqlite3_column_int(sql->get_identities_by_address, 5);
   1.103 +            sqlite3_column_int(session->get_identities_by_address, 6);
   1.104      
   1.105          if (ident_list)
   1.106              identity_list_add(ident_list, ident);
   1.107 @@ -1598,7 +1611,7 @@
   1.108              ident_list = new_identity_list(ident);
   1.109      }
   1.110  
   1.111 -    sqlite3_reset(sql->get_identities_by_address);
   1.112 +    sqlite3_reset(session->get_identities_by_address);
   1.113      
   1.114      *id_list = ident_list;
   1.115      
   1.116 @@ -1719,6 +1732,28 @@
   1.117          return PEP_COMMIT_FAILED;
   1.118  }
   1.119  
   1.120 +PEP_STATUS remove_fpr_as_default(PEP_SESSION session, 
   1.121 +                                 const char* fpr) 
   1.122 +{
   1.123 +    assert(fpr);
   1.124 +    
   1.125 +    if (!session || !fpr)
   1.126 +        return PEP_ILLEGAL_VALUE;
   1.127 +            
   1.128 +    sqlite3_reset(session->remove_fpr_as_default);
   1.129 +    sqlite3_bind_text(session->remove_fpr_as_default, 1, fpr, -1,
   1.130 +                      SQLITE_STATIC);
   1.131 +
   1.132 +    int result = sqlite3_step(session->remove_fpr_as_default);
   1.133 +    sqlite3_reset(session->remove_fpr_as_default);
   1.134 +    
   1.135 +    if (result != SQLITE_DONE)
   1.136 +        return PEP_CANNOT_SET_IDENTITY; // misleading - could also be person
   1.137 +
   1.138 +    return PEP_STATUS_OK;
   1.139 +}
   1.140 +
   1.141 +
   1.142  PEP_STATUS replace_identities_fpr(PEP_SESSION session, 
   1.143                                   const char* old_fpr, 
   1.144                                   const char* new_fpr) 
   1.145 @@ -1874,8 +1909,10 @@
   1.146      sqlite3_bind_text(session->set_identity_flags, 2, identity->address, -1,
   1.147              SQLITE_STATIC);
   1.148      sqlite3_bind_text(session->set_identity_flags, 3, identity->user_id, -1,
   1.149 -        result = sqlite3_step(session->set_identity_flags);
   1.150 -            SQLITE_STATIC);
   1.151 +        SQLITE_STATIC);
   1.152 +        
   1.153 +    result = sqlite3_step(session->set_identity_flags);
   1.154 +
   1.155      sqlite3_reset(session->set_identity_flags);
   1.156      if (result != SQLITE_DONE)
   1.157          return PEP_CANNOT_SET_IDENTITY;
   1.158 @@ -1929,12 +1966,12 @@
   1.159      int result;
   1.160  
   1.161      sqlite3_reset(session->replace_userid);
   1.162 -    sqlite3_bind_text(session->replace_userid, 1, new_id, -1,
   1.163 +    sqlite3_bind_text(session->replace_userid, 1, new_uid, -1,
   1.164              SQLITE_STATIC);
   1.165 -    sqlite3_bind_text(session->replace_userid, 2, old_id, -1,
   1.166 +    sqlite3_bind_text(session->replace_userid, 2, old_uid, -1,
   1.167              SQLITE_STATIC);
   1.168 -    result = sqlite3_step(session->replace);
   1.169 -    sqlite3_reset(session->replace);
   1.170 +    result = sqlite3_step(session->replace_userid);
   1.171 +    sqlite3_reset(session->replace_userid);
   1.172      if (result != SQLITE_DONE)
   1.173          return PEP_CANNOT_SET_PERSON; // May need clearer retval
   1.174