New method for OAuth2 authentication and receiving of access token from David Fletche...
[claws.git] / src / prefs_migration.c
1 /*
2  * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3  * Copyright (C) 2016-2022 the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #ifdef ENABLE_NLS
25 #include <glib/gi18n.h>
26 #else
27 #define _(a) (a)
28 #define N_(a) (a)
29 #endif
30
31 #include "defs.h"
32 #include "account.h"
33 #include "folder_item_prefs.h"
34 #include "prefs_account.h"
35 #include "prefs_common.h"
36 #include "alertpanel.h"
37
38 static gint starting_config_version = 0;
39
40 gboolean _version_check(gint ver)
41 {
42         if (ver > CLAWS_CONFIG_VERSION) {
43                 gchar *msg;
44                 gchar *markup;
45                 AlertValue av;
46
47                 markup = g_strdup_printf(
48                         "<a href=\"%s\"><span underline=\"none\">",
49                         CONFIG_VERSIONS_URI);
50                 msg = g_strdup_printf(
51                         _("Your Claws Mail configuration is from a newer "
52                           "version than the version which you are currently "
53                           "using.\n\n"
54                           "This is not recommended.\n\n"
55                           "For further information see the %sClaws Mail "
56                           "website%s.\n\n"
57                           "Do you want to exit now?"),
58                           markup, "</span></a>");
59                 g_free(markup);
60                 av = alertpanel_full(_("Configuration warning"), msg,
61                                         NULL, _("_No"), NULL, _("_Yes"),
62                                         NULL, NULL, ALERTFOCUS_SECOND,
63                                         FALSE, NULL, ALERT_ERROR);
64                 g_free(msg);
65
66                 if (av != G_ALERTDEFAULT)
67                         return FALSE; /* abort startup */
68
69                 return TRUE; /* hic sunt dracones */
70         }
71
72         return TRUE;
73 }
74
75 static void _update_config_common(gint version)
76 {
77         debug_print("Updating config version %d to %d.\n", version, version + 1);
78
79         switch (version) {
80                 case 1:
81
82                         /* The autochk_interval preference is now
83                          * interpreted as seconds instead of minutes */
84                         prefs_common.autochk_itv *= 60;
85
86                         break;
87
88                 default:
89
90                         /* NOOP */
91
92                         break;
93         }
94 }
95
96 static void _update_config_account(PrefsAccount *ac_prefs, gint version)
97 {
98         debug_print("Account '%s': Updating config version from %d to %d.\n",
99                         ac_prefs->account_name, version, version + 1);
100
101         switch (version) {
102                 case 0:
103
104                         /* Removing A_APOP and A_RPOP from RecvProtocol enum,
105                          * protocol numbers above A_POP3 need to be adjusted.
106                          *
107                          * In config_version=0:
108                          * A_POP3 is 0,
109                          * A_APOP is 1,
110                          * A_RPOP is 2,
111                          * A_IMAP and the rest are from 3 up.
112                          * We can't use the macros, since they may change in the
113                          * future. Numbers do not change. :) */
114                         if (ac_prefs->protocol == 1) {
115                                 ac_prefs->protocol = 0;
116                         } else if (ac_prefs->protocol > 2) {
117                                 /* A_IMAP and above gets bumped down by 2. */
118                                 ac_prefs->protocol -= 2;
119                         }
120
121                         break;
122
123                 case 2:
124
125                         /* Introducing per-account mail check intervals, and separating
126                          * recv_at_getall from autocheck function.
127                          *
128                          * If recv_at_getall is TRUE, the account's autocheck will be
129                          * enabled, following global autocheck interval.
130                          *
131                          * The account's own autocheck interval will be set to the
132                          * same value as the global interval, but will not be used.
133                          *
134                          * recv_at_getall will remain enabled, but will only be used
135                          * to determine whether or not to include this account for
136                          * manual 'Get all' check. */
137                         ac_prefs->autochk_itv = prefs_common_get_prefs()->autochk_itv;
138                         ac_prefs->autochk_use_custom = FALSE;
139                         if (ac_prefs->recv_at_getall) {
140                                 ac_prefs->autochk_use_default = TRUE;
141                         } else {
142                                 ac_prefs->autochk_use_default = FALSE;
143                         }
144
145                         break;
146
147                 case 3:
148                         /* With the introduction of OAuth2 support, the APOP option
149                          * (use_apop_auth) has been swallowed into a combobox and
150                          * renamed */
151                         if (ac_prefs->use_apop_auth == 1) {
152                                 ac_prefs->use_pop_auth = 1;
153                                 ac_prefs->pop_auth_type = 1;
154                         }
155                         break;
156                 default:
157
158                         /* NOOP */
159
160                         break;
161         }
162
163         ac_prefs->config_version = version + 1;
164 }
165
166 static void _update_config_password_store(gint version)
167 {
168         debug_print("Password store: Updating config version from %d to %d.\n",
169                         version, version + 1);
170
171         switch (version) {
172                 /* nothing here yet */
173
174                 default:
175
176                         /* NOOP */
177
178                         break;
179         }
180 }
181
182 static void _update_config_folderlist(gint version)
183 {
184         debug_print("Folderlist: Updating config version from %d to %d.\n",
185                         version, version + 1);
186
187         switch (version) {
188                 /* nothing here yet */
189
190                 default:
191
192                         /* NOOP */
193
194                         break;
195         }
196 }
197
198 static void _update_config_folder_item(FolderItem *item,
199                 gint version)
200 {
201         debug_print("Updating config_version from %d to %d.\n",
202                         version, version + 1);
203
204         switch (version) {
205                 /* nothing here yet */
206
207                 default:
208
209                         /* NOOP */
210
211                         break;
212         }
213         item->prefs->config_version = version + 1;
214         folder_item_prefs_save_config(item);
215 }
216
217 int prefs_update_config_version_common()
218 {
219         gint ver = prefs_common_get_prefs()->config_version;
220
221         /* Store the starting version number for other components'
222          * migration functions. */
223         starting_config_version = ver;
224
225         if (!_version_check(ver))
226                 return -1;
227
228         debug_print("Starting config update at config_version %d.\n", ver);
229         if (ver == CLAWS_CONFIG_VERSION) {
230                 debug_print("No update necessary, already at latest config_version.\n");
231                 return 0; /* nothing to do */
232         }
233
234         while (ver < CLAWS_CONFIG_VERSION) {
235                 _update_config_common(ver++);
236                 prefs_common_get_prefs()->config_version = ver;
237         }
238
239         debug_print("Config update done.\n");
240         return 1; /* update done */
241 }
242
243 int prefs_update_config_version_accounts()
244 {
245         GList *cur;
246         PrefsAccount *ac_prefs;
247
248         for (cur = account_get_list(); cur != NULL; cur = cur->next) {
249                 ac_prefs = (PrefsAccount *)cur->data;
250
251                 if (ac_prefs->config_version == -1) {
252                         /* There was no config_version stored in the config, let's
253                          * assume config_version same as what clawsrc started at
254                          * this session, to avoid breaking the configuration by
255                          * "upgrading" it unnecessarily. */
256                         debug_print("Account '%s': config_version not saved, using one from clawsrc: %d\n", ac_prefs->account_name, starting_config_version);
257                         ac_prefs->config_version = starting_config_version;
258                 }
259
260                 gint ver = ac_prefs->config_version;
261
262                 debug_print("Account '%s': Starting config update at config_version %d.\n", ac_prefs->account_name, ver);
263
264                 if (!_version_check(ver))
265                         return -1;
266
267                 if (ver == CLAWS_CONFIG_VERSION) {
268                         debug_print("Account '%s': No update necessary, already at latest config_version.\n", ac_prefs->account_name);
269                         continue;
270                 }
271
272                 while (ver < CLAWS_CONFIG_VERSION) {
273                         _update_config_account(ac_prefs, ver++);
274                 }
275         }
276
277         debug_print("Accounts config update done.\n");
278         return 1;
279 }
280
281 int prefs_update_config_version_password_store(gint from_version)
282 {
283         gint ver = from_version;
284
285         if (ver == -1) {
286                 /* There was no config_version stored in the config, let's assume
287                  * config_version same as what clawsrc started at this session,
288                  * to avoid breaking the configuration by "upgrading" it unnecessarily. */
289                 debug_print("Password store: config_version not saved, using one from clawsrc: %d\n", starting_config_version);
290                 ver = starting_config_version;
291         }
292
293         debug_print("Starting config update at config_version %d.\n", ver);
294
295         if (!_version_check(ver))
296                 return -1;
297
298         if (ver == CLAWS_CONFIG_VERSION) {
299                 debug_print("No update necessary, already at latest config_version.\n");
300                 return 0; /* nothing to do */
301         }
302
303         while (ver < CLAWS_CONFIG_VERSION) {
304                 _update_config_password_store(ver++);
305         }
306
307         debug_print("Passwordstore config update done.\n");
308         return 1;
309 }
310
311 int prefs_update_config_version_folderlist(gint from_version)
312 {
313         gint ver = from_version;
314
315         if (ver == -1) {
316                 /* There was no config_version stored in the config, let's assume
317                  * config_version same as what clawsrc started at this session,
318                  * to avoid breaking the configuration by "upgrading" it unnecessarily. */
319                 debug_print("Folderlist: config_version not saved, using one from clawsrc: %d\n", starting_config_version);
320                 ver = starting_config_version;
321         }
322
323         debug_print("Starting config_update at config_version %d,\n", ver);
324
325         if (!_version_check(ver))
326                 return -1;
327
328         if (ver == CLAWS_CONFIG_VERSION) {
329                 debug_print("No update necessary, already at latest config_version.\n");
330                 return 0; /* nothing to do */
331         }
332
333         while (ver < CLAWS_CONFIG_VERSION) {
334                 _update_config_folderlist(ver++);
335         }
336
337         debug_print("Folderlist config update done.\n");
338         return 1;
339 }
340
341 int prefs_update_config_version_folder_item(FolderItem *item)
342 {
343         gint ver;
344         gchar *id;
345
346         cm_return_val_if_fail(item != NULL, 0);
347
348         id = folder_item_get_identifier(item);
349
350         if (item->prefs->config_version == -1) {
351                 /* There was no config_version stored in the config, let's assume
352                  * config_version same as what clawsrc started at this session,
353                  * to avoid breaking the configuration by "upgrading" it unnecessarily. */
354                 debug_print("Folder item '%s': config_version not saved, using one from clawsrc: %d\n", id, starting_config_version);
355                 item->prefs->config_version = starting_config_version;
356         }
357
358         ver = item->prefs->config_version;
359
360         if (!_version_check(ver)) {
361                 g_free(id);
362                 return -1;
363         }
364
365         if (ver == CLAWS_CONFIG_VERSION) {
366                 debug_print("Folder item '%s': No update necessary, already at latest config_version %d.\n", id, ver);
367                 g_free(id);
368                 return 0; /* nothing to do */
369         }
370
371         debug_print("Folder item '%s': starting config_update at version %d.\n",
372                         id, ver);
373         g_free(id);
374
375         while (ver < CLAWS_CONFIG_VERSION) {
376                 _update_config_folder_item(item, ver++);
377         }
378
379         debug_print("Folder item config update done.\n");
380         return 0;
381 }