AutocryptDB = NULL;
}
+void mutt_autocrypt_db_normalize_addrlist(struct Address *addrlist)
+{
+ struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
+ mutt_addrlist_append(&al, addrlist);
+
+ mutt_addrlist_to_local(&al);
+ mutt_addrlist_to_intl(&al, NULL);
+
+ TAILQ_REMOVE(&al, addrlist, entries);
+}
+
/* The autocrypt spec says email addresses should be
* normalized to lower case and stored in idna form.
*
*
* The return value must be freed.
*/
-static char *normalize_email_addr(struct Address *addr)
+static struct Address *copy_normalize_addr(struct Address *addr)
{
- struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
- struct Address *norm_addr = mutt_addr_copy(addr);
- mutt_addrlist_append(&al, norm_addr);
-
- mutt_addrlist_to_local(&al);
- mutt_str_strlower(norm_addr->mailbox);
- mutt_addrlist_to_intl(&al, NULL);
-
- char *email = mutt_str_strdup(TAILQ_FIRST(&al)->mailbox);
- mutt_addrlist_clear(&al);
-
- return email;
+ struct Address *norm_addr = NULL;
+
+ /* NOTE: the db functions expect a single address, so in
+ * this function we copy only the address passed in.
+ *
+ * The normalize_addrlist above is extended to work on a list
+ * because of requirements in autocrypt.c
+ */
+
+ norm_addr = mutt_addr_new();
+ norm_addr->mailbox = mutt_str_strdup(addr->mailbox);
+ norm_addr->is_intl = addr->is_intl;
+ norm_addr->intl_checked = addr->intl_checked;
+
+ mutt_autocrypt_db_normalize_addrlist(norm_addr);
+ return norm_addr;
}
/* Helper that converts to char * and mutt_str_strdups the result */
int mutt_autocrypt_db_account_get(struct Address *addr, struct AutocryptAccount **account)
{
int rv = -1, result;
- char *email = NULL;
+ struct Address *norm_addr = NULL;
- email = normalize_email_addr(addr);
+ norm_addr = copy_normalize_addr(addr);
*account = NULL;
if (!AccountGetStmt)
goto cleanup;
}
- if (sqlite3_bind_text(AccountGetStmt, 1, email, -1, SQLITE_STATIC) != SQLITE_OK)
+ if (sqlite3_bind_text(AccountGetStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
goto cleanup;
result = sqlite3_step(AccountGetStmt);
rv = 1;
cleanup:
- FREE(&email);
+ mutt_addr_free(&norm_addr);
sqlite3_reset(AccountGetStmt);
return rv;
}
const char *keydata, int prefer_encrypt)
{
int rv = -1;
- char *email = NULL;
+ struct Address *norm_addr = NULL;
- email = normalize_email_addr(addr);
+ norm_addr = copy_normalize_addr(addr);
if (!AccountInsertStmt)
{
goto cleanup;
}
- if (sqlite3_bind_text(AccountInsertStmt, 1, email, -1, SQLITE_STATIC) != SQLITE_OK)
+ if (sqlite3_bind_text(AccountInsertStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
goto cleanup;
if (sqlite3_bind_text(AccountInsertStmt, 2, keyid, -1, SQLITE_STATIC) != SQLITE_OK)
goto cleanup;
rv = 0;
cleanup:
- FREE(&email);
+ mutt_addr_free(&norm_addr);
sqlite3_reset(AccountInsertStmt);
return rv;
}
int mutt_autocrypt_db_peer_get(struct Address *addr, struct AutocryptPeer **peer)
{
int rv = -1, result;
- char *email = NULL;
+ struct Address *norm_addr = NULL;
- email = normalize_email_addr(addr);
+ norm_addr = copy_normalize_addr(addr);
*peer = NULL;
if (!PeerGetStmt)
goto cleanup;
}
- if (sqlite3_bind_text(PeerGetStmt, 1, email, -1, SQLITE_STATIC) != SQLITE_OK)
+ if (sqlite3_bind_text(PeerGetStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
goto cleanup;
result = sqlite3_step(PeerGetStmt);
rv = 1;
cleanup:
- FREE(&email);
+ mutt_addr_free(&norm_addr);
sqlite3_reset(PeerGetStmt);
return rv;
}
int mutt_autocrypt_db_peer_insert(struct Address *addr, struct AutocryptPeer *peer)
{
int rv = -1;
- char *email = NULL;
+ struct Address *norm_addr = NULL;
- email = normalize_email_addr(addr);
+ norm_addr = copy_normalize_addr(addr);
if (!PeerInsertStmt)
{
goto cleanup;
}
- if (sqlite3_bind_text(PeerInsertStmt, 1, email, -1, SQLITE_STATIC) != SQLITE_OK)
+ if (sqlite3_bind_text(PeerInsertStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
goto cleanup;
if (sqlite3_bind_int64(PeerInsertStmt, 2, peer->last_seen) != SQLITE_OK)
goto cleanup;
rv = 0;
cleanup:
- FREE(&email);
+ mutt_addr_free(&norm_addr);
sqlite3_reset(PeerInsertStmt);
return rv;
}
int mutt_autocrypt_db_peer_update(struct Address *addr, struct AutocryptPeer *peer)
{
int rv = -1;
- char *email = NULL;
+ struct Address *norm_addr = NULL;
- email = normalize_email_addr(addr);
+ norm_addr = copy_normalize_addr(addr);
if (!PeerUpdateStmt)
{
goto cleanup;
if (sqlite3_bind_text(PeerUpdateStmt, 8, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
goto cleanup;
- if (sqlite3_bind_text(PeerUpdateStmt, 9, email, -1, SQLITE_STATIC) != SQLITE_OK)
+ if (sqlite3_bind_text(PeerUpdateStmt, 9, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
goto cleanup;
if (sqlite3_step(PeerUpdateStmt) != SQLITE_DONE)
rv = 0;
cleanup:
- FREE(&email);
+ mutt_addr_free(&norm_addr);
sqlite3_reset(PeerUpdateStmt);
return rv;
}
struct AutocryptPeerHistory *peerhist)
{
int rv = -1;
- char *email = NULL;
+ struct Address *norm_addr = NULL;
- email = normalize_email_addr(addr);
+ norm_addr = copy_normalize_addr(addr);
if (!PeerHistoryInsertStmt)
{
goto cleanup;
}
- if (sqlite3_bind_text(PeerHistoryInsertStmt, 1, email, -1, SQLITE_STATIC) != SQLITE_OK)
+ if (sqlite3_bind_text(PeerHistoryInsertStmt, 1, norm_addr->mailbox, -1, SQLITE_STATIC) != SQLITE_OK)
goto cleanup;
if (sqlite3_bind_text(PeerHistoryInsertStmt, 2, peerhist->email_msgid, -1,
SQLITE_STATIC) != SQLITE_OK)
rv = 0;
cleanup:
- FREE(&email);
+ mutt_addr_free(&norm_addr);
sqlite3_reset(PeerHistoryInsertStmt);
return rv;
}
struct AutocryptGossipHistory *gossip_hist)
{
int rv = -1;
- char *email = NULL;
+ struct Address *norm_addr = NULL;
- email = normalize_email_addr(addr);
+ norm_addr = copy_normalize_addr(addr);
if (!GossipHistoryInsertStmt)
{
goto cleanup;
}
- if (sqlite3_bind_text(GossipHistoryInsertStmt, 1, email, -1, SQLITE_STATIC) != SQLITE_OK)
+ if (sqlite3_bind_text(GossipHistoryInsertStmt, 1, norm_addr->mailbox, -1,
+ SQLITE_STATIC) != SQLITE_OK)
goto cleanup;
if (sqlite3_bind_text(GossipHistoryInsertStmt, 2, gossip_hist->sender_email_addr,
-1, SQLITE_STATIC) != SQLITE_OK)
rv = 0;
cleanup:
- FREE(&email);
+ mutt_addr_free(&norm_addr);
sqlite3_reset(GossipHistoryInsertStmt);
return rv;
}