NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
schema.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stddef.h>
31 #include <sqlite3.h>
32 #include "private.h"
33 #include "mutt/lib.h"
34 
39 {
40  char *errmsg = NULL;
41 
42  const char *schema = "BEGIN TRANSACTION; "
43 
44  "CREATE TABLE account ("
45  "email_addr text primary key not null, "
46  "keyid text, "
47  "keydata text, "
48  "prefer_encrypt int, "
49  "enabled int);"
50 
51  "CREATE TABLE peer ("
52  "email_addr text primary key not null, "
53  "last_seen int, "
54  "autocrypt_timestamp int, "
55  "keyid text, "
56  "keydata text, "
57  "prefer_encrypt int, "
58  "gossip_timestamp int, "
59  "gossip_keyid text, "
60  "gossip_keydata text);"
61 
62  "CREATE TABLE peer_history ("
63  "peer_email_addr text not null, "
64  "email_msgid text, "
65  "timestamp int, "
66  "keydata text);"
67 
68  "CREATE INDEX peer_history_email "
69  "ON peer_history ("
70  "peer_email_addr);"
71 
72  "CREATE TABLE gossip_history ("
73  "peer_email_addr text not null, "
74  "sender_email_addr text, "
75  "email_msgid text, "
76  "timestamp int, "
77  "gossip_keydata text);"
78 
79  "CREATE INDEX gossip_history_email "
80  "ON gossip_history ("
81  "peer_email_addr);"
82 
83  "CREATE TABLE schema ("
84  "version int);"
85 
86  "INSERT into schema (version) values (1);"
87 
88  "COMMIT TRANSACTION";
89 
90  if (sqlite3_exec(AutocryptDB, schema, NULL, NULL, &errmsg) != SQLITE_OK)
91  {
92  mutt_debug(LL_DEBUG1, "mutt_autocrypt_schema_init() returned %s\n", errmsg);
93  sqlite3_free(errmsg);
94  return -1;
95  }
96  return 0;
97 }
98 
105 {
106  sqlite3_stmt *stmt = NULL;
107  int rc = -1;
108 
109  if (sqlite3_prepare_v2(AutocryptDB, "SELECT version FROM schema;", -1, &stmt, NULL) != SQLITE_OK)
110  goto cleanup;
111 
112  if (sqlite3_step(stmt) != SQLITE_ROW)
113  goto cleanup;
114 
115  int version = sqlite3_column_int(stmt, 0);
116 
117  if (version > 1)
118  {
119  /* L10N: The autocrypt database keeps track of schema version numbers.
120  This error occurs if the version number is too high.
121  Presumably because this is an old version of NeoMutt and the
122  database was upgraded by a future version. */
123  mutt_error(_("Autocrypt database version is too new"));
124  goto cleanup;
125  }
126 
127  /* TODO: schema version upgrades go here.
128  * Bump one by one, each update inside a transaction. */
129 
130  rc = 0;
131 
132 cleanup:
133  sqlite3_finalize(stmt);
134  return rc;
135 }
sqlite3 * AutocryptDB
Definition: db.c:52
#define mutt_error(...)
Definition: logging.h:88
int mutt_autocrypt_schema_init(void)
Set up an Autocrypt database.
Definition: schema.c:38
#define _(a)
Definition: message.h:28
int mutt_autocrypt_schema_update(void)
Update the version number of the Autocrypt database schema.
Definition: schema.c:104
Shared constants/structs that are private to Autocrypt.
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 1.
Definition: logging.h:40
Convenience wrapper for the library headers.