src/pEpEngine.c
branchENGINE-559
changeset 3730 922720c27d9a
parent 3729 1c6553767712
child 3732 fb7371ca6f90
     1.1 --- a/src/pEpEngine.c	Sun May 19 15:13:55 2019 +0200
     1.2 +++ b/src/pEpEngine.c	Mon May 20 07:33:05 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"
     1.8 +    "   is_own, pEp_version_major, pEp_version_minor"
     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"
    1.17 +    "   is_own, pEp_version_major, pEp_version_minor"
    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"
    1.26 +    "   identity.flags, is_own, pEp_version_major, pEp_version_minor"
    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"
    1.35 +    "   identity.flags, is_own, pEp_version_major, pEp_version_minor"
    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"
    1.44 +    "   is_own, pEp_version_major, pEp_version_minor"
    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,20 +239,25 @@
    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"
    1.53 +    "       user_id, flags, is_own,"
    1.54 +    "       pEp_version_major, pEp_version_minor"
    1.55      "   ) values ("
    1.56      "       ?1,"
    1.57      "       upper(replace(?2,' ','')),"
    1.58      "       ?3,"
    1.59      "       ?4,"
    1.60 -    "       ?5"
    1.61 +    "       ?5,"
    1.62 +    "       ?6,"
    1.63 +    "       ?7"
    1.64      "   );";
    1.65      
    1.66  static const char* sql_update_identity_entry =    
    1.67      "update identity "
    1.68      "   set main_key_id = upper(replace(?2,' ','')), "
    1.69      "       flags = ?4, " 
    1.70 -    "       is_own = ?5 "
    1.71 +    "       is_own = ?5, "
    1.72 +    "       pEp_version_major = ?6, "
    1.73 +    "       pEp_version_major = ?7 "    
    1.74      "   where (case when (address = ?1) then (1)"
    1.75      "               when (lower(address) = lower(?1)) then (1)"
    1.76      "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1) "
    1.77 @@ -303,13 +308,25 @@
    1.78  
    1.79  static const char *sql_set_pEp_version =
    1.80      "update identity "
    1.81 -    "   set pEp_version = ?1 "
    1.82 -    "   where (case when (address = ?2) then (1)"
    1.83 -    "               when (lower(address) = lower(?2)) then (1)"
    1.84 -    "               when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1) "
    1.85 +    "   set pEp_version_major = ?1, "
    1.86 +    "       pEp_version_minor = ?2 "
    1.87 +    "   where (case when (address = ?3) then (1)"
    1.88 +    "               when (lower(address) = lower(?3)) then (1)"
    1.89 +    "               when (replace(lower(address),'.','') = replace(lower(?3),'.','')) then (1) "
    1.90      "               else 0 "
    1.91      "          end) = 1 "
    1.92 -    "          and user_id = ?3 ;";
    1.93 +    "          and user_id = ?4 ;";
    1.94 +
    1.95 +static const char *sql_upgrade_pEp_version_by_user_id =
    1.96 +    "update identity "
    1.97 +    "   set pEp_version_major = ?1, "
    1.98 +    "       pEp_version_minor = ?2 "
    1.99 +    "       where user_id = ?3 "
   1.100 +    "           and (case when (pEp_version_major < ?1) then (1)"
   1.101 +    "                     when (pEp_version_major > ?1) then (0)"
   1.102 +    "                     when (pEp_version_minor < ?2) then (1)"
   1.103 +    "                     else 0 "
   1.104 +    "           end) = 1 ;";
   1.105  
   1.106  static const char *sql_set_trust =
   1.107      "insert into trust (user_id, pgp_keypair_fpr, comm_type) "
   1.108 @@ -403,7 +420,7 @@
   1.109  
   1.110  static const char *sql_own_identities_retrieve =  
   1.111      "select address, fpr, identity.user_id, username,"
   1.112 -    "   lang, identity.flags | pgp_keypair.flags"
   1.113 +    "   lang, identity.flags | pgp_keypair.flags, pEp_version_major, pEp_version_minor"
   1.114      "   from identity"
   1.115      "   join person on id = identity.user_id"
   1.116      "   join pgp_keypair on fpr = identity.main_key_id"
   1.117 @@ -975,7 +992,8 @@
   1.118                  "   comment text,\n"
   1.119                  "   flags integer default 0,\n"
   1.120                  "   is_own integer default 0,\n"
   1.121 -                "   pEp_version real default 0,\n"
   1.122 +                "   pEp_version_major integer default 0,\n"
   1.123 +                "   pEp_version_minor integer default 0,\n"                
   1.124                  "   timestamp integer default (datetime('now')),\n"
   1.125                  "   primary key (address, user_id)\n"
   1.126                  ");\n"
   1.127 @@ -1081,7 +1099,7 @@
   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")) {
   1.132 +            if (table_contains_column(_session, "identity", "pEp_version_major")) {
   1.133                  version = 12;
   1.134              }
   1.135              else if (db_contains_table(_session, "social_graph") > 0) {
   1.136 @@ -1441,13 +1459,66 @@
   1.137                  int_result = sqlite3_exec(
   1.138                      _session->db,
   1.139                      "alter table identity\n"
   1.140 -                    "   add column pEp_version real default 0\n",
   1.141 +                    "   add column pEp_version_major integer default 0;\n"
   1.142 +                    "alter table identity\n"
   1.143 +                    "   add column pEp_version_minor integer default 0;\n",                    
   1.144                      NULL,
   1.145                      NULL,
   1.146                      NULL
   1.147                  );
   1.148                  if (status != PEP_STATUS_OK)
   1.149 -                    return status;                
   1.150 +                    return status;  
   1.151 +      
   1.152 +                int_result = sqlite3_exec(
   1.153 +                    _session->db,
   1.154 +                    "update identity\n"
   1.155 +                    "   set pEp_version_major = 2\n"
   1.156 +                    "   where exists (select * from person\n"
   1.157 +                    "                     where identity.user_id = person.id\n"
   1.158 +                    "                     and identity.is_own = 0\n"
   1.159 +                    "                     and person.is_pEp_user = 1);\n",
   1.160 +                    NULL,
   1.161 +                    NULL,
   1.162 +                    NULL
   1.163 +                );
   1.164 +                if (status != PEP_STATUS_OK)
   1.165 +                    return status;  
   1.166 +                
   1.167 +                // N.B. WE DEFINE PEP_VERSION - IF WE'RE AT 9-DIGIT MAJOR OR MINOR VERSIONS, ER, BAD.
   1.168 +                char major_buf[10];
   1.169 +                char minor_buf[10];
   1.170 +                if (sscanf("%s.%s", major_buf, minor_buf) != 2)
   1.171 +                    return PEP_UNKNOWN_ERROR; // DO BETTER
   1.172 +                size_t major_len = strlen(major_buf);
   1.173 +                size_t minor_len = strlen(minor_buf);
   1.174 +                    
   1.175 +                const char* _ver_12_startstr =                     
   1.176 +                    "update identity\n"
   1.177 +                    "    set pEp_version_major = ";
   1.178 +                const char* _ver_12_midstr = ",\n"
   1.179 +                    "        pEp_version_minor = ";
   1.180 +                const char* _ver_12_endstr =     
   1.181 +                    "\n"
   1.182 +                    "    where identity.is_own = 1;\n";
   1.183 +                    
   1.184 +                size_t new_stringlen = strlen(_ver_12_startstr) + major_len +
   1.185 +                                       strlen(_ver_12_midstr) + minor_len +
   1.186 +                                       strlen(_ver_12_endstr);
   1.187 +                                       
   1.188 +                char* _ver_12_stmt = calloc(new_stringlen + 1, 1);
   1.189 +                snprintf(_ver_12_stmt, new_stringlen + 1, "%s%s%s%s%s",
   1.190 +                         _ver_12_startstr, major_buf, _ver_12_midstr, minor_buf, _ver_12_endstr);
   1.191 +                
   1.192 +                int_result = sqlite3_exec(
   1.193 +                    _session->db,
   1.194 +                    _ver_12_stmt,
   1.195 +                    NULL,
   1.196 +                    NULL,
   1.197 +                    NULL
   1.198 +                );
   1.199 +                free(_ver_12_stmt);
   1.200 +                if (status != PEP_STATUS_OK)
   1.201 +                    return status;                      
   1.202              }
   1.203          }        
   1.204          else { 
   1.205 @@ -1651,6 +1722,11 @@
   1.206              (int)strlen(sql_set_pEp_version), &_session->set_pEp_version,
   1.207              NULL);
   1.208      assert(int_result == SQLITE_OK);
   1.209 +    
   1.210 +    int_result = sqlite3_prepare_v2(_session->db, sql_upgrade_pEp_version_by_user_id,
   1.211 +            (int)strlen(sql_upgrade_pEp_version_by_user_id), &_session->upgrade_pEp_version_by_user_id,
   1.212 +            NULL);
   1.213 +    assert(int_result == SQLITE_OK);
   1.214  
   1.215      int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
   1.216              (int)strlen(sql_set_trust), &_session->set_trust, NULL);
   1.217 @@ -1892,6 +1968,8 @@
   1.218                  sqlite3_finalize(session->delete_person);                
   1.219              if (session->set_as_pEp_user)
   1.220                  sqlite3_finalize(session->set_as_pEp_user);
   1.221 +            if (session->upgrade_pEp_version_by_user_id)
   1.222 +                sqlite3_finalize(session->upgrade_pEp_version_by_user_id);
   1.223              if (session->is_pEp_user)
   1.224                  sqlite3_finalize(session->is_pEp_user);
   1.225              if (session->exists_person)
   1.226 @@ -1920,6 +1998,8 @@
   1.227                  sqlite3_finalize(session->set_identity_flags);
   1.228              if (session->unset_identity_flags)
   1.229                  sqlite3_finalize(session->unset_identity_flags);
   1.230 +            if (session->set_pEp_version)
   1.231 +                sqlite3_finalize(session->set_pEp_version);                
   1.232              if (session->exists_trust_entry)
   1.233                  sqlite3_finalize(session->exists_trust_entry);                                
   1.234              if (session->set_trust)
   1.235 @@ -2482,6 +2562,10 @@
   1.236              sqlite3_column_int(session->get_identity, 4);
   1.237          _identity->me = (unsigned int)
   1.238              sqlite3_column_int(session->get_identity, 5);
   1.239 +        _identity->major_ver =
   1.240 +            sqlite3_column_int(session->get_identity, 6);
   1.241 +        _identity->minor_ver =
   1.242 +            sqlite3_column_int(session->get_identity, 7);
   1.243      
   1.244          *identity = _identity;
   1.245          break;
   1.246 @@ -2556,6 +2640,10 @@
   1.247              sqlite3_column_int(session->get_identities_by_userid, 5);
   1.248          ident->me = (unsigned int)
   1.249              sqlite3_column_int(session->get_identities_by_userid, 6);
   1.250 +        ident->major_ver =
   1.251 +            sqlite3_column_int(session->get_identities_by_userid, 6);
   1.252 +        ident->minor_ver =
   1.253 +            sqlite3_column_int(session->get_identities_by_userid, 7);
   1.254      
   1.255          identity_list_add(*identities, ident);
   1.256          ident = NULL;
   1.257 @@ -2622,6 +2710,10 @@
   1.258              sqlite3_column_int(session->get_identities_by_main_key_id, 5);
   1.259          ident->me = (unsigned int)
   1.260              sqlite3_column_int(session->get_identities_by_main_key_id, 6);
   1.261 +        ident->major_ver =
   1.262 +            sqlite3_column_int(session->get_identities_by_main_key_id, 7);
   1.263 +        ident->minor_ver =
   1.264 +            sqlite3_column_int(session->get_identities_by_main_key_id, 8);
   1.265      
   1.266          identity_list_add(*identities, ident);
   1.267          ident = NULL;
   1.268 @@ -2692,6 +2784,10 @@
   1.269              sqlite3_column_int(session->get_identity_without_trust_check, 3);
   1.270          _identity->me = (unsigned int)
   1.271              sqlite3_column_int(session->get_identity_without_trust_check, 4);
   1.272 +        _identity->major_ver =
   1.273 +            sqlite3_column_int(session->get_identity_without_trust_check, 6);
   1.274 +        _identity->minor_ver =
   1.275 +            sqlite3_column_int(session->get_identity_without_trust_check, 7);
   1.276      
   1.277          *identity = _identity;
   1.278          break;
   1.279 @@ -2757,6 +2853,10 @@
   1.280              sqlite3_column_int(session->get_identities_by_address, 4);
   1.281          ident->me = (unsigned int)
   1.282              sqlite3_column_int(session->get_identities_by_address, 5);
   1.283 +        ident->major_ver =
   1.284 +            sqlite3_column_int(session->get_identities_by_address, 6);
   1.285 +        ident->minor_ver =
   1.286 +            sqlite3_column_int(session->get_identities_by_address, 7);
   1.287      
   1.288          if (ident_list)
   1.289              identity_list_add(ident_list, ident);
   1.290 @@ -2913,6 +3013,9 @@
   1.291              SQLITE_STATIC);
   1.292      sqlite3_bind_int(set_or_update, 4, identity->flags);
   1.293      sqlite3_bind_int(set_or_update, 5, identity->me);
   1.294 +    sqlite3_bind_int(set_or_update, 6, identity->major_ver);
   1.295 +    sqlite3_bind_int(set_or_update, 7, identity->minor_ver);
   1.296 +        
   1.297      int result = sqlite3_step(set_or_update);
   1.298      sqlite3_reset(set_or_update);
   1.299      if (result != SQLITE_DONE)
   1.300 @@ -3117,7 +3220,9 @@
   1.301      if (result != SQLITE_DONE)
   1.302          return PEP_CANNOT_SET_TRUST;
   1.303  
   1.304 -    return PEP_STATUS_OK;
   1.305 +    PEP_STATUS status = upgrade_pEp_version_by_user_id(session, user, 2, 0);
   1.306 +    
   1.307 +    return status;
   1.308  }
   1.309  
   1.310  
   1.311 @@ -3159,16 +3264,17 @@
   1.312  }
   1.313  
   1.314  // This ONLY sets the version flag. Must be called outside of a transaction.
   1.315 -PEP_STATUS set_pEp_version(PEP_SESSION session, pEp_identity* ident, float new_pEp_version) {
   1.316 +PEP_STATUS set_pEp_version(PEP_SESSION session, pEp_identity* ident, unsigned int new_ver_major, unsigned int new_ver_minor) {
   1.317      assert(session);
   1.318      assert(!EMPTYSTR(ident->user_id));
   1.319      assert(!EMPTYSTR(ident->address));
   1.320      
   1.321      sqlite3_reset(session->set_pEp_version);
   1.322 -    sqlite3_bind_double(session->set_pEp_version, 1, new_pEp_version);
   1.323 -    sqlite3_bind_text(session->set_pEp_version, 2, ident->address, -1,
   1.324 +    sqlite3_bind_double(session->set_pEp_version, 1, new_ver_major);
   1.325 +    sqlite3_bind_double(session->set_pEp_version, 2, new_ver_minor);    
   1.326 +    sqlite3_bind_text(session->set_pEp_version, 3, ident->address, -1,
   1.327              SQLITE_STATIC);
   1.328 -    sqlite3_bind_text(session->set_pEp_version, 3, ident->user_id, -1,
   1.329 +    sqlite3_bind_text(session->set_pEp_version, 4, ident->user_id, -1,
   1.330              SQLITE_STATIC);
   1.331      
   1.332      int result = sqlite3_step(session->set_pEp_version);
   1.333 @@ -3180,6 +3286,30 @@
   1.334      return PEP_STATUS_OK;
   1.335  }
   1.336  
   1.337 +// Generally ONLY called by set_as_pEp_user, and ONLY from < 2.0 to 2.0.
   1.338 +PEP_STATUS upgrade_pEp_version_by_user_id(PEP_SESSION session, 
   1.339 +        pEp_identity* ident, 
   1.340 +        unsigned int new_ver_major,
   1.341 +        unsigned int new_ver_minor
   1.342 +    ) 
   1.343 +{
   1.344 +    assert(session);
   1.345 +    assert(!EMPTYSTR(ident->user_id));
   1.346 +    
   1.347 +    sqlite3_reset(session->upgrade_pEp_version_by_user_id);
   1.348 +    sqlite3_bind_int(session->upgrade_pEp_version_by_user_id, 1, new_ver_major);
   1.349 +    sqlite3_bind_int(session->upgrade_pEp_version_by_user_id, 2, new_ver_minor);    
   1.350 +    sqlite3_bind_text(session->upgrade_pEp_version_by_user_id, 3, ident->user_id, -1,
   1.351 +            SQLITE_STATIC);
   1.352 +    
   1.353 +    int result = sqlite3_step(session->upgrade_pEp_version_by_user_id);
   1.354 +    sqlite3_reset(session->upgrade_pEp_version_by_user_id);
   1.355 +        
   1.356 +    if (result != SQLITE_DONE)
   1.357 +        return PEP_CANNOT_SET_PEP_VERSION;
   1.358 +    
   1.359 +    return PEP_STATUS_OK;    
   1.360 +}
   1.361  
   1.362  PEP_STATUS exists_person(PEP_SESSION session, pEp_identity* identity,
   1.363                           bool* exists) {