adding get_own_addresses() keysync
authorVolker Birk <vb@pep.foundation>
Sat, 20 Aug 2016 15:29:43 +0200
branchkeysync
changeset 104410a56596be7d
parent 1043 f786ee22e281
child 1045 3b77f5e254c1
adding get_own_addresses()
src/keymanagement.c
src/pEpEngine.c
src/pEpEngine.h
src/pEp_internal.h
     1.1 --- a/src/keymanagement.c	Sat Aug 20 14:45:19 2016 +0200
     1.2 +++ b/src/keymanagement.c	Sat Aug 20 15:29:43 2016 +0200
     1.3 @@ -356,11 +356,12 @@
     1.4  
     1.5      assert(session);
     1.6      assert(identity);
     1.7 +    assert(!EMPTYSTR(identity->address));
     1.8  
     1.9      assert(EMPTYSTR(identity->user_id) ||
    1.10             strcmp(identity->user_id, PEP_OWN_USERID) == 0);
    1.11  
    1.12 -    if (!(session && identity &&
    1.13 +    if (!(session && identity && !EMPTYSTR(identity->address) &&
    1.14              (EMPTYSTR(identity->user_id) ||
    1.15              strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
    1.16          return PEP_ILLEGAL_VALUE;
    1.17 @@ -377,17 +378,8 @@
    1.18              return PEP_OUT_OF_MEMORY;
    1.19      }
    1.20  
    1.21 -    if (!identity->address)
    1.22 -    {
    1.23 -        identity->address = strdup("");
    1.24 -        assert(identity->address);
    1.25 -        if (!identity->address)
    1.26 -            return PEP_OUT_OF_MEMORY;
    1.27 -    }
    1.28 -
    1.29 -    DEBUG_LOG("myself", "debug", EMPTYSTR(identity->address) ?
    1.30 -            "<default address>" : identity->address);
    1.31 -    
    1.32 +    DEBUG_LOG("myself", "debug", identity->address);
    1.33 + 
    1.34      status = get_identity(session,
    1.35                            identity->address,
    1.36                            identity->user_id,
     2.1 --- a/src/pEpEngine.c	Sat Aug 20 14:45:19 2016 +0200
     2.2 +++ b/src/pEpEngine.c	Sat Aug 20 15:29:43 2016 +0200
     2.3 @@ -38,6 +38,7 @@
     2.4      static const char *sql_crashdump;
     2.5      static const char *sql_languagelist;
     2.6      static const char *sql_i18n_token;
     2.7 +    static const char *sql_get_own_addresses;
     2.8  
     2.9      // blacklist
    2.10      static const char *sql_blacklist_add;
    2.11 @@ -336,6 +337,8 @@
    2.12  
    2.13          sql_i18n_token = "select phrase from i18n_token where lang = lower(?1) and id = ?2 ;";
    2.14  
    2.15 +        sql_get_own_addresses = "select address from identity where user_id = '" PEP_OWN_USERID "';";
    2.16 +
    2.17          // blacklist
    2.18  
    2.19          sql_blacklist_add = "insert or replace into blacklist_keys (fpr) values (upper(replace(?1,' ',''))) ;"
    2.20 @@ -444,6 +447,11 @@
    2.21              (int)strlen(sql_i18n_token), &_session->i18n_token, NULL);
    2.22      assert(int_result == SQLITE_OK);
    2.23  
    2.24 +    int_result = sqlite3_prepare_v2(_session->db, sql_get_own_addresses,
    2.25 +            (int)strlen(sql_get_own_addresses), &_session->get_own_addresses,
    2.26 +            NULL);
    2.27 +    assert(int_result == SQLITE_OK);
    2.28 +
    2.29      // blacklist
    2.30  
    2.31      int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_add,
    2.32 @@ -1723,6 +1731,49 @@
    2.33      return status;
    2.34  }
    2.35  
    2.36 +DYNAMIC_API PEP_STATUS get_own_addresses(
    2.37 +        PEP_SESSION session,
    2.38 +        stringlist_t **addresses
    2.39 +    )
    2.40 +{
    2.41 +    PEP_STATUS status = PEP_STATUS_OK;
    2.42 +
    2.43 +    assert(session && addresses);
    2.44 +    if (!(session && addresses))
    2.45 +        return PEP_ILLEGAL_VALUE;
    2.46 +
    2.47 +    stringlist_t *_addresses = new_stringlist(NULL);
    2.48 +    if (!_addresses)
    2.49 +        return PEP_OUT_OF_MEMORY;
    2.50 +
    2.51 +    sqlite3_reset(session->get_own_addresses);
    2.52 +    int result;
    2.53 +    stringlist_t *_a = _addresses;
    2.54 +    do {
    2.55 +        result = sqlite3_step(session->get_own_addresses);
    2.56 +        char *address;
    2.57 +        switch (result) {
    2.58 +            case SQLITE_ROW:
    2.59 +                sqlite3_bind_text(session->get_own_addresses, 1, address, -1,
    2.60 +                        SQLITE_STATIC);
    2.61 +                _a = stringlist_add(_a, address);
    2.62 +                if (!_a) {
    2.63 +                    free_stringlist(_addresses);
    2.64 +                    return PEP_OUT_OF_MEMORY;
    2.65 +                }
    2.66 +                break;
    2.67 +
    2.68 +            default:
    2.69 +                status = PEP_UNKNOWN_ERROR;
    2.70 +        }
    2.71 +    } while (result == SQLITE_ROW);
    2.72 +
    2.73 +    sqlite3_reset(session->get_own_addresses);
    2.74 +    if (status == PEP_STATUS_OK)
    2.75 +        *addresses = _addresses;
    2.76 +    return status;
    2.77 +}
    2.78 +
    2.79  DYNAMIC_API PEP_STATUS set_revoked(
    2.80         PEP_SESSION session,
    2.81         const char *revoked_fpr,
     3.1 --- a/src/pEpEngine.h	Sat Aug 20 14:45:19 2016 +0200
     3.2 +++ b/src/pEpEngine.h	Sat Aug 20 15:29:43 2016 +0200
     3.3 @@ -868,7 +868,18 @@
     3.4          int32_t *value
     3.5      );
     3.6  
     3.7 -    
     3.8 +
     3.9 +// get_own_addresses() - get a list of own addresses
    3.10 +//
    3.11 +//  parameters:
    3.12 +//      session (in)            session handle
    3.13 +//      addresses (out)         stringlist with addresses
    3.14 +
    3.15 +DYNAMIC_API PEP_STATUS get_own_addresses(
    3.16 +        PEP_SESSION session,
    3.17 +        stringlist_t **addresses
    3.18 +    );
    3.19 +
    3.20  // set_revoked() - records relation between a revoked key and its replacement
    3.21  //
    3.22  //  parameters:
     4.1 --- a/src/pEp_internal.h	Sat Aug 20 14:45:19 2016 +0200
     4.2 +++ b/src/pEp_internal.h	Sat Aug 20 15:29:43 2016 +0200
     4.3 @@ -106,6 +106,7 @@
     4.4      sqlite3_stmt *crashdump;
     4.5      sqlite3_stmt *languagelist;
     4.6      sqlite3_stmt *i18n_token;
     4.7 +    sqlite3_stmt *get_own_addresses;
     4.8  
     4.9      // blacklist
    4.10      sqlite3_stmt *blacklist_add;