summaryrefslogtreecommitdiffstats
path: root/autocrypt/autocrypt_schema.c
blob: cb9d63d618b61f750130b6f68bbc0b118758cee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (C) 2019 Kevin J. McCarthy <kevin@8t8.us>
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include "mutt.h"
#include "autocrypt.h"
#include "autocrypt_private.h"

int mutt_autocrypt_schema_init (void)
{
  const char *schema;
  char *errmsg = NULL;

  schema =
    "BEGIN TRANSACTION; "

    "CREATE TABLE account ("
    "email_addr text primary key not null, "
    "keyid text, "
    "keydata text, "
    "prefer_encrypt int, "
    "enabled int);"

    "CREATE TABLE peer ("
    "email_addr text primary key not null, "
    "last_seen int, "
    "autocrypt_timestamp int, "
    "keyid text, "
    "keydata text, "
    "prefer_encrypt int, "
    "gossip_timestamp int, "
    "gossip_keyid text, "
    "gossip_keydata text);"

    "CREATE TABLE peer_history ("
    "peer_email_addr text not null, "
    "email_msgid text, "
    "timestamp int, "
    "keydata text);"

    "CREATE INDEX peer_history_email "
    "ON peer_history ("
    "peer_email_addr);"

    "CREATE TABLE gossip_history ("
    "peer_email_addr text not null, "
    "sender_email_addr text, "
    "email_msgid text, "
    "timestamp int, "
    "gossip_keydata text);"

    "CREATE INDEX gossip_history_email "
    "ON gossip_history ("
    "peer_email_addr);"

    "CREATE TABLE schema ("
    "version int);"

    "INSERT into schema (version) values (1);"

    "COMMIT TRANSACTION";

  if (sqlite3_exec (AutocryptDB, schema, NULL, NULL, &errmsg) != SQLITE_OK)
  {
    dprint (1, (debugfile, "mutt_autocrypt_schema_init() returned %s\n", errmsg));
    sqlite3_free (errmsg);
    return -1;
  }
  return 0;
}

int mutt_autocrypt_schema_update (void)
{
  sqlite3_stmt *stmt = NULL;
  int rv = -1, version;

  if (sqlite3_prepare_v2 (
        AutocryptDB,
        "SELECT version FROM schema;",
        -1,
        &stmt,
        NULL) != SQLITE_OK)
    goto cleanup;

  if (sqlite3_step (stmt) != SQLITE_ROW)
    goto cleanup;

  version = sqlite3_column_int (stmt, 0);

  if (version > 1)
  {
    /* L10N:
       The autocrypt database keeps track of schema version numbers.
       This error occurs if the version number is too high.
       Presumably because this is an old version of mutt and the
       database was upgraded by a future version.
    */
    mutt_error _("Autocrypt database version is too new");
    mutt_sleep (0);
    goto cleanup;
  }

  /* TODO: schema version upgrades go here.
   * Bump one by one, each update inside a transaction. */

  rv = 0;

cleanup:
  sqlite3_finalize (stmt);
  return rv;
}