src/pEpEngine.c
changeset 1370 274b7141afb6
parent 1365 9e45d9a7583b
child 1381 f84211f27e5b
     1.1 --- a/src/pEpEngine.c	Wed Nov 09 15:28:45 2016 +0100
     1.2 +++ b/src/pEpEngine.c	Thu Nov 10 21:10:24 2016 +0100
     1.3 @@ -52,6 +52,7 @@
     1.4      // Own keys
     1.5      static const char *sql_own_key_is_listed;
     1.6      static const char *sql_own_identities_retrieve;
     1.7 +    static const char *sql_keys_retrieve_by_flag;
     1.8  
     1.9      // Sequence
    1.10      static const char *sql_sequence_value1;
    1.11 @@ -331,8 +332,8 @@
    1.12          sql_get_device_group = "select device_group from person "
    1.13                                 "where id = '" PEP_OWN_USERID "';";
    1.14  
    1.15 -        sql_set_pgp_keypair = "insert or replace into pgp_keypair (fpr) "
    1.16 -                              "values (upper(replace(?1,' ',''))) ;";
    1.17 +        sql_set_pgp_keypair = "insert or replace into pgp_keypair (fpr, flags) "
    1.18 +                              "values (upper(replace(?1,' ','')), ?2) ;";
    1.19  
    1.20          sql_set_identity = "insert or replace into identity (address, main_key_id, "
    1.21                             "user_id, flags) values (?1, upper(replace(?2,' ','')),"
    1.22 @@ -380,8 +381,7 @@
    1.23                  
    1.24          // Own keys
    1.25          
    1.26 -        sql_own_key_is_listed =
    1.27 -                                "select count(*) from ("
    1.28 +        sql_own_key_is_listed = "select count(*) from ("
    1.29                                  " select main_key_id from person "
    1.30                                  "   where main_key_id = upper(replace(?1,' ',''))"
    1.31                                  "    and id = '" PEP_OWN_USERID "' "
    1.32 @@ -390,7 +390,8 @@
    1.33                                  "   where main_key_id = upper(replace(?1,' ',''))"
    1.34                                  "    and user_id = '" PEP_OWN_USERID "' );";
    1.35  
    1.36 -        sql_own_identities_retrieve =  "select address, fpr, username, "
    1.37 +        sql_own_identities_retrieve =  
    1.38 +                            "select address, fpr, username, "
    1.39                              "   lang, identity.flags | pgp_keypair.flags"
    1.40                              "   from identity"
    1.41                              "   join person on id = identity.user_id"
    1.42 @@ -399,6 +400,10 @@
    1.43                              "       and pgp_keypair_fpr = identity.main_key_id"
    1.44                              "   where identity.user_id = '" PEP_OWN_USERID "';";
    1.45          
    1.46 +        sql_keys_retrieve_by_flag =  
    1.47 +                            "select fpr from pgp_keypair"
    1.48 +                            "  where (flags & ?1) = ?1;";
    1.49 +
    1.50          sql_sequence_value1 = "insert or replace into sequences (name, value, own) "
    1.51                                "values (?1, "
    1.52                                "(select coalesce((select value + 1 from sequences "
    1.53 @@ -526,6 +531,11 @@
    1.54              &_session->own_identities_retrieve, NULL);
    1.55      assert(int_result == SQLITE_OK);
    1.56   
    1.57 +    int_result = sqlite3_prepare_v2(_session->db, sql_keys_retrieve_by_flag,
    1.58 +            (int)strlen(sql_keys_retrieve_by_flag),
    1.59 +            &_session->keys_retrieve_by_flag, NULL);
    1.60 +    assert(int_result == SQLITE_OK);
    1.61 + 
    1.62      // Sequence
    1.63  
    1.64      int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value1,
    1.65 @@ -652,6 +662,8 @@
    1.66                  sqlite3_finalize(session->own_key_is_listed);
    1.67              if (session->own_identities_retrieve)
    1.68                  sqlite3_finalize(session->own_identities_retrieve);
    1.69 +            if (session->keys_retrieve_by_flag)
    1.70 +                sqlite3_finalize(session->keys_retrieve_by_flag);
    1.71              if (session->sequence_value1)
    1.72                  sqlite3_finalize(session->sequence_value1);
    1.73              if (session->sequence_value2)
    1.74 @@ -1124,6 +1136,9 @@
    1.75      sqlite3_reset(session->set_pgp_keypair);
    1.76      sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
    1.77              SQLITE_STATIC);
    1.78 +    sqlite3_bind_int(session->set_pgp_keypair, 2, 
    1.79 +                     strcmp(identity->user_id, PEP_OWN_USERID) == 0 ?
    1.80 +                        PEP_kpf_own_key : 0);
    1.81      result = sqlite3_step(session->set_pgp_keypair);
    1.82      sqlite3_reset(session->set_pgp_keypair);
    1.83      if (result != SQLITE_DONE) {