enable_identity_for_sync() sync
authorVolker Birk <vb@pep-project.org>
Thu, 30 Jan 2020 12:57:23 +0100
branchsync
changeset 43749b1574786c91
parent 4372 c6e15260180c
child 4375 5d1ec3eaeee2
enable_identity_for_sync()
src/sync_api.c
src/sync_api.h
     1.1 --- a/src/sync_api.c	Thu Jan 30 11:23:11 2020 +0100
     1.2 +++ b/src/sync_api.c	Thu Jan 30 12:57:23 2020 +0100
     1.3 @@ -260,35 +260,48 @@
     1.4  DYNAMIC_API PEP_STATUS enable_identity_for_sync(PEP_SESSION session,
     1.5          pEp_identity *ident)
     1.6  {
     1.7 -    assert(session && ident);
     1.8 -    if (!(session && ident))
     1.9 +    assert(session && ident && ident->user_id && ident->user_id[0] && ident->address && ident->address[0]);
    1.10 +    if (!(session && ident && ident->user_id && ident->user_id[0] && ident->address && ident->address[0]))
    1.11          return PEP_ILLEGAL_VALUE;
    1.12  
    1.13 -    // Find out if flags are already set. If there's no identity, this won't
    1.14 -    // create one.
    1.15 -    PEP_STATUS status = _myself(session, ident, false, true, false);
    1.16 -    if (status != PEP_STATUS_OK && status != PEP_KEY_NOT_FOUND && status != PEP_GET_KEY_FAILED)
    1.17 +    // safeguard: in case the delivered identity is not valid fetch flags from the database
    1.18 +    //            while doing this check if this is an own identity and return an error if not
    1.19 +
    1.20 +    pEp_identity *stored_ident = NULL;
    1.21 +    PEP_STATUS status = get_identity(session, ident->address, ident->user_id, &stored_ident);
    1.22 +    if (status)
    1.23          return status;
    1.24 +    assert(stored_ident);
    1.25 +    if (!stored_ident->me) {
    1.26 +        free_identity(stored_ident);
    1.27 +        return PEP_ILLEGAL_VALUE;
    1.28 +    }
    1.29 +    ident->flags = stored_ident->flags;
    1.30 +    free_identity(stored_ident);
    1.31  
    1.32 +    // if we're grouped and this identity is enabled already we can stop here
    1.33      if ((ident->flags & PEP_idf_devicegroup) && !(ident->flags & PEP_idf_not_for_sync))
    1.34          return PEP_STATUS_OK;
    1.35  
    1.36 -    // Ok, this is kind of annoying - if the flags aren't set, we won't create 
    1.37 -    // a keygen event anyway, so we're going to have to call myself and issue 
    1.38 -    // an unconditional keygen event.
    1.39 -    status = myself(session, ident);
    1.40 -    if (status != PEP_STATUS_OK)
    1.41 +    // if the identity is marked not for sync unset this to enable
    1.42 +    if (ident->flags & PEP_idf_not_for_sync) {
    1.43 +        status = unset_identity_flags(session, ident, PEP_idf_not_for_sync);
    1.44 +        if (status)
    1.45 +            return status;
    1.46 +    }
    1.47 +
    1.48 +    // if we're grouped then add the identity to the group
    1.49 +    bool grouped = false;
    1.50 +    status = deviceGrouped(session, &grouped);
    1.51 +    if (status)
    1.52          return status;
    1.53 -
    1.54 -    status = unset_identity_flags(session, ident, PEP_idf_not_for_sync);
    1.55 -    if (status != PEP_STATUS_OK) // explicit. sorry, but lazy makes mistakes in C
    1.56 -        return status;
    1.57 -
    1.58 -    status = set_identity_flags(session, ident, PEP_idf_devicegroup);    
    1.59 -    if (status != PEP_STATUS_OK)
    1.60 -        return status;
    1.61 -
    1.62 -    signal_Sync_event(session, Sync_PR_keysync, KeyGen, NULL);
    1.63 +    if (grouped) {
    1.64 +        status = set_identity_flags(session, ident, PEP_idf_devicegroup);    
    1.65 +        if (status)
    1.66 +            return status;
    1.67 +        // signal we have a new identity in the group
    1.68 +        signal_Sync_event(session, Sync_PR_keysync, KeyGen, NULL);
    1.69 +    }
    1.70  
    1.71      return PEP_STATUS_OK;
    1.72  }
     2.1 --- a/src/sync_api.h	Thu Jan 30 11:23:11 2020 +0100
     2.2 +++ b/src/sync_api.h	Thu Jan 30 12:57:23 2020 +0100
     2.3 @@ -222,7 +222,7 @@
     2.4  //      ident                   identity to enable
     2.5  //
     2.6  //  caveat:
     2.7 -//      do not call this function without sync running
     2.8 +//      ident must be an own identity
     2.9  
    2.10  DYNAMIC_API PEP_STATUS enable_identity_for_sync(PEP_SESSION session,
    2.11          pEp_identity *ident);