src/pEpEngine.c
branchENGINE-559
changeset 3729 1c6553767712
parent 3728 3e8c536a1634
child 3730 922720c27d9a
     1.1 --- a/src/pEpEngine.c	Fri May 17 17:44:19 2019 +0200
     1.2 +++ b/src/pEpEngine.c	Sun May 19 15:13:55 2019 +0200
     1.3 @@ -83,7 +83,7 @@
     1.4  static const char *sql_get_identity =  
     1.5      "select fpr, username, comm_type, lang,"
     1.6      "   identity.flags | pgp_keypair.flags,"
     1.7 -    "   is_own, pEp_version"
     1.8 +    "   is_own"
     1.9      "   from identity"
    1.10      "   join person on id = identity.user_id"
    1.11      "   join pgp_keypair on fpr = identity.main_key_id"
    1.12 @@ -101,7 +101,7 @@
    1.13  static const char *sql_get_identities_by_main_key_id =  
    1.14      "select address, identity.user_id, username, comm_type, lang,"
    1.15      "   identity.flags | pgp_keypair.flags,"
    1.16 -    "   is_own, pEp_version"
    1.17 +    "   is_own"
    1.18      "   from identity"
    1.19      "   join person on id = identity.user_id"
    1.20      "   join pgp_keypair on fpr = identity.main_key_id"
    1.21 @@ -113,7 +113,7 @@
    1.22  
    1.23  static const char *sql_get_identity_without_trust_check =  
    1.24      "select identity.main_key_id, username, lang,"
    1.25 -    "   identity.flags, is_own, pEp_version"
    1.26 +    "   identity.flags, is_own"
    1.27      "   from identity"
    1.28      "   join person on id = identity.user_id"
    1.29      "   where (case when (address = ?1) then (1)"
    1.30 @@ -127,7 +127,7 @@
    1.31  
    1.32  static const char *sql_get_identities_by_address =  
    1.33      "select user_id, identity.main_key_id, username, lang,"
    1.34 -    "   identity.flags, is_own, pEp_version"
    1.35 +    "   identity.flags, is_own"
    1.36      "   from identity"
    1.37      "   join person on id = identity.user_id"
    1.38      "   where (case when (address = ?1) then (1)"
    1.39 @@ -141,7 +141,7 @@
    1.40  static const char *sql_get_identities_by_userid =  
    1.41      "select address, fpr, username, comm_type, lang,"
    1.42      "   identity.flags | pgp_keypair.flags,"
    1.43 -    "   is_own, pEp_version"
    1.44 +    "   is_own"
    1.45      "   from identity"
    1.46      "   join person on id = identity.user_id"
    1.47      "   join pgp_keypair on fpr = identity.main_key_id"
    1.48 @@ -239,22 +239,20 @@
    1.49  static const char *sql_set_identity_entry = 
    1.50      "insert into identity ("
    1.51      "       address, main_key_id, "
    1.52 -    "       user_id, flags, is_own, pEp_version"
    1.53 +    "       user_id, flags, is_own"
    1.54      "   ) values ("
    1.55      "       ?1,"
    1.56      "       upper(replace(?2,' ','')),"
    1.57      "       ?3,"
    1.58      "       ?4,"
    1.59 -    "       ?5,"
    1.60 -    "       ?6"
    1.61 +    "       ?5"
    1.62      "   );";
    1.63      
    1.64  static const char* sql_update_identity_entry =    
    1.65      "update identity "
    1.66      "   set main_key_id = upper(replace(?2,' ','')), "
    1.67      "       flags = ?4, " 
    1.68 -    "       is_own = ?5, "
    1.69 -    "       pEp_version = ?6 "
    1.70 +    "       is_own = ?5 "
    1.71      "   where (case when (address = ?1) then (1)"
    1.72      "               when (lower(address) = lower(?1)) then (1)"
    1.73      "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1) "
    1.74 @@ -303,6 +301,16 @@
    1.75      "          end) = 1"
    1.76      "          and user_id = ?3 ;";
    1.77  
    1.78 +static const char *sql_set_pEp_version =
    1.79 +    "update identity "
    1.80 +    "   set pEp_version = ?1 "
    1.81 +    "   where (case when (address = ?2) then (1)"
    1.82 +    "               when (lower(address) = lower(?2)) then (1)"
    1.83 +    "               when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1) "
    1.84 +    "               else 0 "
    1.85 +    "          end) = 1 "
    1.86 +    "          and user_id = ?3 ;";
    1.87 +
    1.88  static const char *sql_set_trust =
    1.89      "insert into trust (user_id, pgp_keypair_fpr, comm_type) "
    1.90      "values (?1, upper(replace(?2,' ','')), ?3) ;";
    1.91 @@ -355,7 +363,7 @@
    1.92  
    1.93  static const char *sql_i18n_token = 
    1.94      "select phrase from i18n_token where lang = lower(?1) and id = ?2 ;";
    1.95 -
    1.96 +    
    1.97  // blacklist
    1.98  static const char *sql_blacklist_add = 
    1.99      "insert or ignore into blacklist_keys (fpr) values (upper(replace(?1,' ',''))) ;"
   1.100 @@ -395,7 +403,7 @@
   1.101  
   1.102  static const char *sql_own_identities_retrieve =  
   1.103      "select address, fpr, identity.user_id, username,"
   1.104 -    "   lang, identity.flags | pgp_keypair.flags, pEp_version"
   1.105 +    "   lang, identity.flags | pgp_keypair.flags"
   1.106      "   from identity"
   1.107      "   join person on id = identity.user_id"
   1.108      "   join pgp_keypair on fpr = identity.main_key_id"
   1.109 @@ -906,7 +914,7 @@
   1.110      sqlite3_busy_timeout(_session->system_db, 1000);
   1.111  
   1.112  // increment this when patching DDL
   1.113 -#define _DDL_USER_VERSION "12"
   1.114 +#define _DDL_USER_VERSION "11"
   1.115  
   1.116      if (in_first) {
   1.117  
   1.118 @@ -967,7 +975,7 @@
   1.119                  "   comment text,\n"
   1.120                  "   flags integer default 0,\n"
   1.121                  "   is_own integer default 0,\n"
   1.122 -                "   pEp_version real default 0.0,\n"                
   1.123 +                "   pEp_version real default 0,\n"
   1.124                  "   timestamp integer default (datetime('now')),\n"
   1.125                  "   primary key (address, user_id)\n"
   1.126                  ");\n"
   1.127 @@ -1073,9 +1081,9 @@
   1.128          // Sometimes the user_version wasn't set correctly. 
   1.129          if (version == 1) {
   1.130              bool version_changed = true;
   1.131 -            if (table_contains_column(_session, "identity", "pEp_version") > 0) {
   1.132 +            if (table_contains_column(_session, "identity", "pEp_version")) {
   1.133                  version = 12;
   1.134 -            } // N.B. Version 11 was a DB internal fix; there's no identifying information, but this is only one extra exec.
   1.135 +            }
   1.136              else if (db_contains_table(_session, "social_graph") > 0) {
   1.137                  if (!table_contains_column(_session, "person", "device_group"))
   1.138                      version = 10;
   1.139 @@ -1433,12 +1441,13 @@
   1.140                  int_result = sqlite3_exec(
   1.141                      _session->db,
   1.142                      "alter table identity\n"
   1.143 -                    "   add column pEp_version real default 0.0;\n",
   1.144 +                    "   add column pEp_version real default 0\n",
   1.145                      NULL,
   1.146                      NULL,
   1.147                      NULL
   1.148                  );
   1.149 -                assert(int_result == SQLITE_OK);                
   1.150 +                if (status != PEP_STATUS_OK)
   1.151 +                    return status;                
   1.152              }
   1.153          }        
   1.154          else { 
   1.155 @@ -1638,6 +1647,11 @@
   1.156              NULL);
   1.157      assert(int_result == SQLITE_OK);
   1.158  
   1.159 +    int_result = sqlite3_prepare_v2(_session->db, sql_set_pEp_version,
   1.160 +            (int)strlen(sql_set_pEp_version), &_session->set_pEp_version,
   1.161 +            NULL);
   1.162 +    assert(int_result == SQLITE_OK);
   1.163 +
   1.164      int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
   1.165              (int)strlen(sql_set_trust), &_session->set_trust, NULL);
   1.166      assert(int_result == SQLITE_OK);
   1.167 @@ -2468,8 +2482,6 @@
   1.168              sqlite3_column_int(session->get_identity, 4);
   1.169          _identity->me = (unsigned int)
   1.170              sqlite3_column_int(session->get_identity, 5);
   1.171 -        _identity->pEp_version = (float)
   1.172 -            sqlite3_column_double(session->get_identity, 6);
   1.173      
   1.174          *identity = _identity;
   1.175          break;
   1.176 @@ -2544,8 +2556,6 @@
   1.177              sqlite3_column_int(session->get_identities_by_userid, 5);
   1.178          ident->me = (unsigned int)
   1.179              sqlite3_column_int(session->get_identities_by_userid, 6);
   1.180 -        ident->pEp_version = (float)
   1.181 -            sqlite3_column_double(session->get_identities_by_userid, 7);
   1.182      
   1.183          identity_list_add(*identities, ident);
   1.184          ident = NULL;
   1.185 @@ -2612,8 +2622,6 @@
   1.186              sqlite3_column_int(session->get_identities_by_main_key_id, 5);
   1.187          ident->me = (unsigned int)
   1.188              sqlite3_column_int(session->get_identities_by_main_key_id, 6);
   1.189 -        ident->pEp_version = (float)
   1.190 -            sqlite3_column_double(session->get_identities_by_main_key_id, 7);
   1.191      
   1.192          identity_list_add(*identities, ident);
   1.193          ident = NULL;
   1.194 @@ -2684,8 +2692,6 @@
   1.195              sqlite3_column_int(session->get_identity_without_trust_check, 3);
   1.196          _identity->me = (unsigned int)
   1.197              sqlite3_column_int(session->get_identity_without_trust_check, 4);
   1.198 -        _identity->pEp_version = (float)
   1.199 -            sqlite3_column_double(session->get_identity_without_trust_check, 5);
   1.200      
   1.201          *identity = _identity;
   1.202          break;
   1.203 @@ -2751,8 +2757,6 @@
   1.204              sqlite3_column_int(session->get_identities_by_address, 4);
   1.205          ident->me = (unsigned int)
   1.206              sqlite3_column_int(session->get_identities_by_address, 5);
   1.207 -        ident->pEp_version = (float)
   1.208 -            sqlite3_column_double(session->get_identities_by_address, 6);
   1.209      
   1.210          if (ident_list)
   1.211              identity_list_add(ident_list, ident);
   1.212 @@ -2909,7 +2913,6 @@
   1.213              SQLITE_STATIC);
   1.214      sqlite3_bind_int(set_or_update, 4, identity->flags);
   1.215      sqlite3_bind_int(set_or_update, 5, identity->me);
   1.216 -    sqlite3_bind_double(set_or_update, 6, identity->pEp_version);
   1.217      int result = sqlite3_step(set_or_update);
   1.218      sqlite3_reset(set_or_update);
   1.219      if (result != SQLITE_DONE)
   1.220 @@ -3023,7 +3026,7 @@
   1.221                                         guard_transaction);
   1.222  }
   1.223  
   1.224 -// This will NOT call set_as_pEp_user; you have to do that separately.
   1.225 +// This will NOT call set_as_pEp_user, nor set_pEp_version; you have to do that separately.
   1.226  DYNAMIC_API PEP_STATUS set_identity(
   1.227          PEP_SESSION session, const pEp_identity *identity
   1.228      )
   1.229 @@ -3155,6 +3158,29 @@
   1.230      return status;
   1.231  }
   1.232  
   1.233 +// This ONLY sets the version flag. Must be called outside of a transaction.
   1.234 +PEP_STATUS set_pEp_version(PEP_SESSION session, pEp_identity* ident, float new_pEp_version) {
   1.235 +    assert(session);
   1.236 +    assert(!EMPTYSTR(ident->user_id));
   1.237 +    assert(!EMPTYSTR(ident->address));
   1.238 +    
   1.239 +    sqlite3_reset(session->set_pEp_version);
   1.240 +    sqlite3_bind_double(session->set_pEp_version, 1, new_pEp_version);
   1.241 +    sqlite3_bind_text(session->set_pEp_version, 2, ident->address, -1,
   1.242 +            SQLITE_STATIC);
   1.243 +    sqlite3_bind_text(session->set_pEp_version, 3, ident->user_id, -1,
   1.244 +            SQLITE_STATIC);
   1.245 +    
   1.246 +    int result = sqlite3_step(session->set_pEp_version);
   1.247 +    sqlite3_reset(session->set_pEp_version);
   1.248 +        
   1.249 +    if (result != SQLITE_DONE)
   1.250 +        return PEP_CANNOT_SET_PEP_VERSION;
   1.251 +    
   1.252 +    return PEP_STATUS_OK;
   1.253 +}
   1.254 +
   1.255 +
   1.256  PEP_STATUS exists_person(PEP_SESSION session, pEp_identity* identity,
   1.257                           bool* exists) {            
   1.258