add outgoing_message_rating_preview() for performance optimization
authorVolker Birk <vb@pep.foundation>
Fri, 07 Sep 2018 11:12:28 +0200
changeset 2929133ea04d3ea3
parent 2928 6d8e51098e7a
child 2930 05a79c61fc56
add outgoing_message_rating_preview() for performance optimization
src/message_api.c
src/message_api.h
     1.1 --- a/src/message_api.c	Thu Sep 06 17:08:17 2018 +0200
     1.2 +++ b/src/message_api.c	Fri Sep 07 11:12:28 2018 +0200
     1.3 @@ -1329,6 +1329,45 @@
     1.4      }
     1.5  }
     1.6  
     1.7 +static PEP_comm_type _get_comm_type_preview(
     1.8 +    PEP_SESSION session,
     1.9 +    PEP_comm_type max_comm_type,
    1.10 +    pEp_identity *ident
    1.11 +    )
    1.12 +{
    1.13 +    assert(session);
    1.14 +    assert(ident);
    1.15 +
    1.16 +    PEP_STATUS status = PEP_STATUS_OK;
    1.17 +
    1.18 +    if (max_comm_type == PEP_ct_compromised)
    1.19 +        return PEP_ct_compromised;
    1.20 +
    1.21 +    if (max_comm_type == PEP_ct_mistrusted)
    1.22 +        return PEP_ct_mistrusted;
    1.23 +
    1.24 +    PEP_comm_type comm_type = PEP_ct_unknown;
    1.25 +    if (ident && !EMPTYSTR(ident->address) && !EMPTYSTR(ident->user_id)) {
    1.26 +        pEp_identity *ident2;
    1.27 +        status = get_identity(session, ident->address, ident->user_id, &ident2);
    1.28 +        comm_type = ident2 ? ident2->comm_type : PEP_ct_unknown;
    1.29 +        free_identity(ident2);
    1.30 +
    1.31 +        if (status == PEP_STATUS_OK) {
    1.32 +            if (comm_type == PEP_ct_compromised)
    1.33 +                comm_type = PEP_ct_compromised;
    1.34 +            else if (comm_type == PEP_ct_mistrusted)
    1.35 +                comm_type = PEP_ct_mistrusted;
    1.36 +            else
    1.37 +                comm_type = _MIN(max_comm_type, comm_type);
    1.38 +        }
    1.39 +        else {
    1.40 +            comm_type = PEP_ct_unknown;
    1.41 +        }
    1.42 +    }
    1.43 +    return comm_type;
    1.44 +}
    1.45 +
    1.46  static void free_bl_entry(bloblist_t *bl)
    1.47  {
    1.48      if (bl) {
    1.49 @@ -3571,6 +3610,24 @@
    1.50      }
    1.51  }
    1.52  
    1.53 +static void _max_comm_type_from_identity_list_preview(
    1.54 +        identity_list *identities,
    1.55 +        PEP_SESSION session,
    1.56 +        PEP_comm_type *max_comm_type,
    1.57 +        bool *comm_type_determined
    1.58 +    )
    1.59 +{
    1.60 +    identity_list * il;
    1.61 +    for (il = identities; il != NULL; il = il->next)
    1.62 +    {
    1.63 +        if (il->ident)
    1.64 +        {   
    1.65 +            *max_comm_type = _get_comm_type_preview(session, *max_comm_type,
    1.66 +                il->ident);
    1.67 +        }
    1.68 +    }
    1.69 +}
    1.70 +
    1.71  DYNAMIC_API PEP_STATUS outgoing_message_rating(
    1.72          PEP_SESSION session,
    1.73          message *msg,
    1.74 @@ -3614,6 +3671,49 @@
    1.75      return PEP_STATUS_OK;
    1.76  }
    1.77  
    1.78 +DYNAMIC_API PEP_STATUS outgoing_message_rating_preview(
    1.79 +        PEP_SESSION session,
    1.80 +        message *msg,
    1.81 +        PEP_rating *rating
    1.82 +    )
    1.83 +{
    1.84 +    PEP_comm_type max_comm_type = PEP_ct_pEp;
    1.85 +    bool comm_type_determined = false;
    1.86 +
    1.87 +    assert(session);
    1.88 +    assert(msg);
    1.89 +    assert(msg->dir == PEP_dir_outgoing);
    1.90 +    assert(rating);
    1.91 +
    1.92 +    if (!(session && msg && rating))
    1.93 +        return PEP_ILLEGAL_VALUE;
    1.94 +
    1.95 +    if (msg->dir != PEP_dir_outgoing)
    1.96 +        return PEP_ILLEGAL_VALUE;
    1.97 +
    1.98 +    *rating = PEP_rating_undefined;
    1.99 +
   1.100 +    _max_comm_type_from_identity_list_preview(msg->to, session,
   1.101 +                                      &max_comm_type, &comm_type_determined);
   1.102 +
   1.103 +    _max_comm_type_from_identity_list_preview(msg->cc, session,
   1.104 +                                      &max_comm_type, &comm_type_determined);
   1.105 +
   1.106 +    _max_comm_type_from_identity_list_preview(msg->bcc, session,
   1.107 +                                      &max_comm_type, &comm_type_determined);
   1.108 +
   1.109 +    if (comm_type_determined == false) {
   1.110 +        // likely means there was a massive screwup with no sender or recipient
   1.111 +        // keys
   1.112 +        *rating = PEP_rating_undefined;
   1.113 +    }
   1.114 +    else
   1.115 +        *rating = _MAX(_rating(max_comm_type, PEP_rating_undefined),
   1.116 +                               PEP_rating_unencrypted);
   1.117 +
   1.118 +    return PEP_STATUS_OK;
   1.119 +}
   1.120 +
   1.121  DYNAMIC_API PEP_STATUS identity_rating(
   1.122          PEP_SESSION session,
   1.123          pEp_identity *ident,
     2.1 --- a/src/message_api.h	Thu Sep 06 17:08:17 2018 +0200
     2.2 +++ b/src/message_api.h	Fri Sep 07 11:12:28 2018 +0200
     2.3 @@ -435,6 +435,26 @@
     2.4      );
     2.5  
     2.6  
     2.7 +// outgoing_message_rating_preview() - get rating preview
     2.8 +//
     2.9 +//  parameters:
    2.10 +//      session (in)        session handle
    2.11 +//      msg (in)            message to get the rating for
    2.12 +//      rating (out)        rating preview for the message
    2.13 +//
    2.14 +//  return value:
    2.15 +//      error status or PEP_STATUS_OK on success
    2.16 +//
    2.17 +//  caveat:
    2.18 +//      msg->from must point to a valid pEp_identity
    2.19 +//      msg->dir must be PEP_dir_outgoing
    2.20 +//      the ownership of msg remains with the caller
    2.21 +DYNAMIC_API PEP_STATUS outgoing_message_rating_preview(
    2.22 +        PEP_SESSION session,
    2.23 +        message *msg,
    2.24 +        PEP_rating *rating
    2.25 +    );
    2.26 +
    2.27  // identity_rating() - get rating for a single identity
    2.28  //
    2.29  //  parameters: