first try preview_rating
authorVolker Birk <vb@pep.foundation>
Tue, 20 Mar 2018 15:49:13 +0100
branchpreview_rating
changeset 257873da15bd0c14
parent 2576 2eafd0cc0c8c
first try
src/keymanagement.c
src/keymanagement.h
src/message_api.c
src/message_api.h
src/pEpEngine.c
     1.1 --- a/src/keymanagement.c	Mon Mar 19 17:33:16 2018 +0100
     1.2 +++ b/src/keymanagement.c	Tue Mar 20 15:49:13 2018 +0100
     1.3 @@ -492,6 +492,86 @@
     1.4      return status;
     1.5  }
     1.6  
     1.7 +PEP_rating _rating(PEP_comm_type ct, PEP_rating rating)
     1.8 +{
     1.9 +    if (ct == PEP_ct_unknown)
    1.10 +        return PEP_rating_undefined;
    1.11 +
    1.12 +    else if (ct == PEP_ct_key_not_found)
    1.13 +        return PEP_rating_have_no_key;
    1.14 +
    1.15 +    else if (ct == PEP_ct_compromized)
    1.16 +        return PEP_rating_under_attack;
    1.17 +
    1.18 +    else if (ct == PEP_ct_mistrusted)
    1.19 +        return PEP_rating_mistrust;
    1.20 +
    1.21 +    if (rating == PEP_rating_unencrypted_for_some)
    1.22 +        return PEP_rating_unencrypted_for_some;
    1.23 +
    1.24 +    if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel ||
    1.25 +        ct == PEP_ct_my_key_not_included) {
    1.26 +        if (rating > PEP_rating_unencrypted_for_some)
    1.27 +            return PEP_rating_unencrypted_for_some;
    1.28 +        else
    1.29 +            return PEP_rating_unencrypted;
    1.30 +    }
    1.31 +
    1.32 +    if (rating == PEP_rating_unencrypted)
    1.33 +        return PEP_rating_unencrypted_for_some;
    1.34 +
    1.35 +    if (ct >= PEP_ct_confirmed_enc_anon)
    1.36 +        return PEP_rating_trusted_and_anonymized;
    1.37 +
    1.38 +    else if (ct >= PEP_ct_strong_encryption)
    1.39 +        return PEP_rating_trusted;
    1.40 +
    1.41 +    else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
    1.42 +        return PEP_rating_reliable;
    1.43 +
    1.44 +    else
    1.45 +        return PEP_rating_unreliable;
    1.46 +}
    1.47 +
    1.48 +DYNAMIC_API PEP_STATUS preview_rating(
    1.49 +    PEP_SESSION session, pEp_identity * identity, PEP_rating *rating
    1.50 +)
    1.51 +{
    1.52 +    PEP_STATUS status = PEP_STATUS_OK;
    1.53 +    
    1.54 +    assert(session && identity && rating);
    1.55 +    assert(!EMPTYSTR(identity->address));
    1.56 +
    1.57 +    if (!(session && identity && rating && !EMPTYSTR(identity->address)))
    1.58 +        return PEP_ILLEGAL_VALUE;
    1.59 +
    1.60 +    if (identity->me)
    1.61 +        return PEP_ILLEGAL_VALUE;
    1.62 +
    1.63 +    identity->comm_type = PEP_ct_unknown;
    1.64 +    *rating = PEP_rating_undefined;
    1.65 +
    1.66 +    if (!EMPTYSTR(identity->user_id)) {
    1.67 +        pEp_identity *stored_ident = NULL;
    1.68 +        status = get_identity_without_trust_check(session, identity->address, identity->user_id, &stored_ident);
    1.69 +        if (!status)
    1.70 +            identity->comm_type = stored_ident->comm_type;
    1.71 +        free_identity(stored_ident);
    1.72 +    }
    1.73 +    else {
    1.74 +        identity_list *id_list = NULL;
    1.75 +        status = get_identities_by_address(session, identity->address, &id_list);
    1.76 +        if (!status) {
    1.77 +            if (identity_list_length(id_list) == 1)
    1.78 +                identity->comm_type = id_list->ident->comm_type;
    1.79 +        }
    1.80 +        free_identity_list(id_list);
    1.81 +    }
    1.82 +
    1.83 +    *rating = _rating(identity->comm_type, PEP_rating_undefined);
    1.84 +    return status;
    1.85 +}
    1.86 +
    1.87  DYNAMIC_API PEP_STATUS update_identity(
    1.88          PEP_SESSION session, pEp_identity * identity
    1.89      )
    1.90 @@ -524,6 +604,9 @@
    1.91          // (we're gonna update the trust/fpr anyway, so we use the no-fpr-from-trust-db variant)
    1.92          //      * do get_identity() to retrieve stored identity information
    1.93          status = get_identity_without_trust_check(session, identity->address, identity->user_id, &stored_ident);
    1.94 +        if (stored_ident)
    1.95 +            stored_ident->comm_type = PEP_ct_unknown; // FIXME: why is this necessary?
    1.96 +
    1.97  
    1.98          // Before we start - if there was no stored identity, we should check to make sure we don't
    1.99          // have a stored identity with a temporary user_id that differs from the input user_id. This
   1.100 @@ -531,7 +614,6 @@
   1.101          if (!stored_ident) {
   1.102              identity_list* id_list = NULL;
   1.103              status = get_identities_by_address(session, identity->address, &id_list);
   1.104 -
   1.105              if (id_list) {
   1.106                  identity_list* id_curr = id_list;
   1.107                  while (id_curr) {
     2.1 --- a/src/keymanagement.h	Mon Mar 19 17:33:16 2018 +0100
     2.2 +++ b/src/keymanagement.h	Tue Mar 20 15:49:13 2018 +0100
     2.3 @@ -9,6 +9,7 @@
     2.4  extern "C" {
     2.5  #endif
     2.6  
     2.7 +
     2.8  // update_identity() - update identity information
     2.9  //
    2.10  //  parameters:
    2.11 @@ -54,6 +55,42 @@
    2.12          PEP_SESSION session, pEp_identity * identity
    2.13      );
    2.14  
    2.15 +
    2.16 +typedef enum _PEP_rating {
    2.17 +    PEP_rating_undefined = 0,
    2.18 +    PEP_rating_cannot_decrypt,
    2.19 +    PEP_rating_have_no_key,
    2.20 +    PEP_rating_unencrypted,
    2.21 +    PEP_rating_unencrypted_for_some,
    2.22 +    PEP_rating_unreliable,
    2.23 +    PEP_rating_reliable,
    2.24 +    PEP_rating_trusted,
    2.25 +    PEP_rating_trusted_and_anonymized,
    2.26 +    PEP_rating_fully_anonymous,
    2.27 +
    2.28 +    PEP_rating_mistrust = -1,
    2.29 +    PEP_rating_b0rken = -2,
    2.30 +    PEP_rating_under_attack = -3
    2.31 +} PEP_rating;
    2.32 +
    2.33 +PEP_rating _rating(PEP_comm_type ct, PEP_rating rating);
    2.34 +
    2.35 +// preview_rating() - quicker preview version of update_identity for use in
    2.36 +//                    outgoing_rating only
    2.37 +//
    2.38 +//  parameters:
    2.39 +//      session (in)        session to use
    2.40 +//      identity (inout)    identity information of communication partner
    2.41 +//                          at least .address must be set
    2.42 +//      rating (out)        calculated rating
    2.43 +
    2.44 +// quicker preview version of update_identity for use in outgoing_rating only
    2.45 +
    2.46 +DYNAMIC_API PEP_STATUS preview_rating(
    2.47 +    PEP_SESSION session, pEp_identity * identity, PEP_rating *rating
    2.48 +);
    2.49 +
    2.50 +
    2.51  // TODO: remove
    2.52  // initialise_own_identities () - ensures that an own identity is complete
    2.53  //
     3.1 --- a/src/message_api.c	Mon Mar 19 17:33:16 2018 +0100
     3.2 +++ b/src/message_api.c	Tue Mar 20 15:49:13 2018 +0100
     3.3 @@ -1050,48 +1050,6 @@
     3.4      return status;
     3.5  }
     3.6  
     3.7 -
     3.8 -static PEP_rating _rating(PEP_comm_type ct, PEP_rating rating)
     3.9 -{
    3.10 -    if (ct == PEP_ct_unknown)
    3.11 -        return PEP_rating_undefined;
    3.12 -
    3.13 -    else if (ct == PEP_ct_key_not_found)
    3.14 -        return PEP_rating_have_no_key;
    3.15 -
    3.16 -    else if (ct == PEP_ct_compromized)
    3.17 -        return PEP_rating_under_attack;
    3.18 -
    3.19 -    else if (ct == PEP_ct_mistrusted)
    3.20 -        return PEP_rating_mistrust;
    3.21 -
    3.22 -    if (rating == PEP_rating_unencrypted_for_some)
    3.23 -        return PEP_rating_unencrypted_for_some;
    3.24 -
    3.25 -    if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel ||
    3.26 -            ct == PEP_ct_my_key_not_included) {
    3.27 -        if (rating > PEP_rating_unencrypted_for_some)
    3.28 -            return PEP_rating_unencrypted_for_some;
    3.29 -        else
    3.30 -            return PEP_rating_unencrypted;
    3.31 -    }
    3.32 -
    3.33 -    if (rating == PEP_rating_unencrypted)
    3.34 -        return PEP_rating_unencrypted_for_some;
    3.35 -
    3.36 -    if (ct >= PEP_ct_confirmed_enc_anon)
    3.37 -        return PEP_rating_trusted_and_anonymized;
    3.38 -
    3.39 -    else if (ct >= PEP_ct_strong_encryption)
    3.40 -        return PEP_rating_trusted;
    3.41 -
    3.42 -    else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
    3.43 -        return PEP_rating_reliable;
    3.44 -
    3.45 -    else
    3.46 -        return PEP_rating_unreliable;
    3.47 -}
    3.48 -
    3.49  static bool is_encrypted_attachment(const bloblist_t *blob)
    3.50  {
    3.51      assert(blob);
     4.1 --- a/src/message_api.h	Mon Mar 19 17:33:16 2018 +0100
     4.2 +++ b/src/message_api.h	Tue Mar 20 15:49:13 2018 +0100
     4.3 @@ -178,24 +178,6 @@
     4.4      PEP_encrypt_flags_t flags
     4.5  );
     4.6  
     4.7 -
     4.8 -typedef enum _PEP_rating {
     4.9 -    PEP_rating_undefined = 0,
    4.10 -    PEP_rating_cannot_decrypt,
    4.11 -    PEP_rating_have_no_key,
    4.12 -    PEP_rating_unencrypted,
    4.13 -    PEP_rating_unencrypted_for_some,
    4.14 -    PEP_rating_unreliable,
    4.15 -    PEP_rating_reliable,
    4.16 -    PEP_rating_trusted,
    4.17 -    PEP_rating_trusted_and_anonymized,
    4.18 -    PEP_rating_fully_anonymous,   
    4.19 -
    4.20 -    PEP_rating_mistrust = -1,
    4.21 -    PEP_rating_b0rken = -2,
    4.22 -    PEP_rating_under_attack = -3
    4.23 -} PEP_rating;
    4.24 -
    4.25  typedef enum _PEP_color {
    4.26      PEP_color_no_color = 0,
    4.27      PEP_color_yellow,
     5.1 --- a/src/pEpEngine.c	Mon Mar 19 17:33:16 2018 +0100
     5.2 +++ b/src/pEpEngine.c	Tue Mar 20 15:49:13 2018 +0100
     5.3 @@ -98,9 +98,10 @@
     5.4      "   timestamp desc; ";
     5.5  
     5.6  static const char *sql_get_identity_without_trust_check =  
     5.7 -    "select identity.main_key_id, username, lang,"
     5.8 +    "select identity.main_key_id, username, comm_type, lang,"
     5.9      "   identity.flags, is_own"
    5.10      "   from identity"
    5.11 +    "   join trust on id = trust.user_id"
    5.12      "   join person on id = identity.user_id"
    5.13      "   where (case when (address = ?1) then (1)"
    5.14      "               when (lower(address) = lower(?1)) then (1)"
    5.15 @@ -2019,9 +2020,9 @@
    5.16              return PEP_OUT_OF_MEMORY;
    5.17          }
    5.18  
    5.19 -        _identity->comm_type = PEP_ct_unknown;
    5.20 +        _identity->comm_type = sqlite3_column_int(session->get_identity_without_trust_check, 2);
    5.21          const char* const _lang = (const char *)
    5.22 -            sqlite3_column_text(session->get_identity_without_trust_check, 2);
    5.23 +            sqlite3_column_text(session->get_identity_without_trust_check, 3);
    5.24          if (_lang && _lang[0]) {
    5.25              assert(_lang[0] >= 'a' && _lang[0] <= 'z');
    5.26              assert(_lang[1] >= 'a' && _lang[1] <= 'z');
    5.27 @@ -2031,9 +2032,9 @@
    5.28              _identity->lang[2] = 0;
    5.29          }
    5.30          _identity->flags = (unsigned int)
    5.31 -            sqlite3_column_int(session->get_identity_without_trust_check, 3);
    5.32 +            sqlite3_column_int(session->get_identity_without_trust_check, 4);
    5.33          _identity->me = (unsigned int)
    5.34 -            sqlite3_column_int(session->get_identity_without_trust_check, 4);
    5.35 +            sqlite3_column_int(session->get_identity_without_trust_check, 5);
    5.36      
    5.37          *identity = _identity;
    5.38          break;