Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * @file |
3 | | * Mailbox multiplexor |
4 | | * |
5 | | * @authors |
6 | | * Copyright (C) 1996-2002,2010,2013 Michael R. Elkins <me@mutt.org> |
7 | | * Copyright (C) 1999-2003 Thomas Roessler <roessler@does-not-exist.org> |
8 | | * Copyright (C) 2016-2023 Richard Russon <rich@flatcap.org> |
9 | | * Copyright (C) 2018 Mehdi Abaakouk <sileht@sileht.net> |
10 | | * Copyright (C) 2018-2022 Pietro Cerutti <gahr@gahr.ch> |
11 | | * Copyright (C) 2020 Austin Ray <austin@austinray.io> |
12 | | * Copyright (C) 2020 Reto Brunner <reto@slightlybroken.com> |
13 | | * Copyright (C) 2023-2024 Dennis Schön <mail@dennis-schoen.de> |
14 | | * |
15 | | * @copyright |
16 | | * This program is free software: you can redistribute it and/or modify it under |
17 | | * the terms of the GNU General Public License as published by the Free Software |
18 | | * Foundation, either version 2 of the License, or (at your option) any later |
19 | | * version. |
20 | | * |
21 | | * This program is distributed in the hope that it will be useful, but WITHOUT |
22 | | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
23 | | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
24 | | * details. |
25 | | * |
26 | | * You should have received a copy of the GNU General Public License along with |
27 | | * this program. If not, see <http://www.gnu.org/licenses/>. |
28 | | */ |
29 | | |
30 | | /** |
31 | | * @page neo_mx Mailbox multiplexor |
32 | | * |
33 | | * Mailbox multiplexor |
34 | | */ |
35 | | |
36 | | #include "config.h" |
37 | | #include <errno.h> |
38 | | #include <stdbool.h> |
39 | | #include <stdio.h> |
40 | | #include <string.h> |
41 | | #include <sys/stat.h> |
42 | | #include <time.h> |
43 | | #include <unistd.h> |
44 | | #include "mutt/lib.h" |
45 | | #include "address/lib.h" |
46 | | #include "config/lib.h" |
47 | | #include "email/lib.h" |
48 | | #include "core/lib.h" |
49 | | #include "alias/lib.h" |
50 | | #include "gui/lib.h" |
51 | | #include "mutt.h" |
52 | | #include "mx.h" |
53 | | #include "compmbox/lib.h" |
54 | | #include "imap/lib.h" |
55 | | #include "key/lib.h" |
56 | | #include "maildir/lib.h" |
57 | | #include "mbox/lib.h" |
58 | | #include "menu/lib.h" |
59 | | #include "mh/lib.h" |
60 | | #include "nntp/lib.h" |
61 | | #include "pop/lib.h" |
62 | | #include "question/lib.h" |
63 | | #include "copy.h" |
64 | | #include "external.h" |
65 | | #include "globals.h" |
66 | | #include "hook.h" |
67 | | #include "mutt_header.h" |
68 | | #include "mutt_logging.h" |
69 | | #include "mutt_mailbox.h" |
70 | | #include "muttlib.h" |
71 | | #include "nntp/mdata.h" // IWYU pragma: keep |
72 | | #include "protos.h" |
73 | | #ifdef USE_NOTMUCH |
74 | | #include "notmuch/lib.h" |
75 | | #endif |
76 | | #ifdef ENABLE_NLS |
77 | | #include <libintl.h> |
78 | | #endif |
79 | | |
80 | | /// Lookup table of mailbox types |
81 | | static const struct Mapping MboxTypeMap[] = { |
82 | | // clang-format off |
83 | | { "mbox", MUTT_MBOX, }, |
84 | | { "mmdf", MUTT_MMDF, }, |
85 | | { "mh", MUTT_MH, }, |
86 | | { "maildir", MUTT_MAILDIR, }, |
87 | | { NULL, 0, }, |
88 | | // clang-format on |
89 | | }; |
90 | | |
91 | | /// Data for the $mbox_type enumeration |
92 | | const struct EnumDef MboxTypeDef = { |
93 | | "mbox_type", |
94 | | 4, |
95 | | (struct Mapping *) &MboxTypeMap, |
96 | | }; |
97 | | |
98 | | /** |
99 | | * MxOps - All the Mailbox backends |
100 | | */ |
101 | | static const struct MxOps *MxOps[] = { |
102 | | /* These mailboxes can be recognised by their Url scheme */ |
103 | | &MxImapOps, |
104 | | #ifdef USE_NOTMUCH |
105 | | &MxNotmuchOps, |
106 | | #endif |
107 | | &MxPopOps, |
108 | | &MxNntpOps, |
109 | | |
110 | | /* Local mailboxes */ |
111 | | &MxMaildirOps, |
112 | | &MxMboxOps, |
113 | | &MxMhOps, |
114 | | &MxMmdfOps, |
115 | | |
116 | | /* If everything else fails... */ |
117 | | &MxCompOps, |
118 | | NULL, |
119 | | }; |
120 | | |
121 | | /** |
122 | | * mx_get_ops - Get mailbox operations |
123 | | * @param type Mailbox type |
124 | | * @retval ptr Mailbox function |
125 | | * @retval NULL Error |
126 | | */ |
127 | | const struct MxOps *mx_get_ops(enum MailboxType type) |
128 | 0 | { |
129 | 0 | for (const struct MxOps **ops = MxOps; *ops; ops++) |
130 | 0 | if ((*ops)->type == type) |
131 | 0 | return *ops; |
132 | | |
133 | 0 | return NULL; |
134 | 0 | } |
135 | | |
136 | | /** |
137 | | * mutt_is_spool - Is this the spool_file? |
138 | | * @param str Name to check |
139 | | * @retval true It is the spool_file |
140 | | */ |
141 | | static bool mutt_is_spool(const char *str) |
142 | 0 | { |
143 | 0 | const char *const c_spool_file = cs_subset_string(NeoMutt->sub, "spool_file"); |
144 | 0 | if (mutt_str_equal(str, c_spool_file)) |
145 | 0 | return true; |
146 | | |
147 | 0 | struct Url *ua = url_parse(str); |
148 | 0 | struct Url *ub = url_parse(c_spool_file); |
149 | |
|
150 | 0 | const bool is_spool = ua && ub && (ua->scheme == ub->scheme) && |
151 | 0 | mutt_istr_equal(ua->host, ub->host) && |
152 | 0 | mutt_istr_equal(ua->path, ub->path) && |
153 | 0 | (!ua->user || !ub->user || mutt_str_equal(ua->user, ub->user)); |
154 | |
|
155 | 0 | url_free(&ua); |
156 | 0 | url_free(&ub); |
157 | 0 | return is_spool; |
158 | 0 | } |
159 | | |
160 | | /** |
161 | | * mx_access - Wrapper for access, checks permissions on a given mailbox |
162 | | * @param path Path of mailbox |
163 | | * @param flags Flags, e.g. W_OK |
164 | | * @retval 0 Success, allowed |
165 | | * @retval <0 Failure, not allowed |
166 | | * |
167 | | * We may be interested in using ACL-style flags at some point, currently we |
168 | | * use the normal access() flags. |
169 | | */ |
170 | | int mx_access(const char *path, int flags) |
171 | 0 | { |
172 | 0 | if (imap_path_probe(path, NULL) == MUTT_IMAP) |
173 | 0 | return imap_access(path); |
174 | | |
175 | 0 | return access(path, flags); |
176 | 0 | } |
177 | | |
178 | | /** |
179 | | * mx_open_mailbox_append - Open a mailbox for appending |
180 | | * @param m Mailbox |
181 | | * @param flags Flags, see #OpenMailboxFlags |
182 | | * @retval true Success |
183 | | * @retval false Failure |
184 | | */ |
185 | | static bool mx_open_mailbox_append(struct Mailbox *m, OpenMailboxFlags flags) |
186 | 0 | { |
187 | 0 | if (!m) |
188 | 0 | return false; |
189 | | |
190 | 0 | struct stat st = { 0 }; |
191 | |
|
192 | 0 | m->append = true; |
193 | 0 | if ((m->type == MUTT_UNKNOWN) || (m->type == MUTT_MAILBOX_ERROR)) |
194 | 0 | { |
195 | 0 | m->type = mx_path_probe(mailbox_path(m)); |
196 | |
|
197 | 0 | if (m->type == MUTT_UNKNOWN) |
198 | 0 | { |
199 | 0 | if (flags & MUTT_APPEND) |
200 | 0 | { |
201 | 0 | m->type = MUTT_MAILBOX_ERROR; |
202 | 0 | } |
203 | 0 | else |
204 | 0 | { |
205 | 0 | mutt_error(_("%s is not a mailbox"), mailbox_path(m)); |
206 | 0 | return false; |
207 | 0 | } |
208 | 0 | } |
209 | | |
210 | 0 | if (m->type == MUTT_MAILBOX_ERROR) |
211 | 0 | { |
212 | 0 | if (stat(mailbox_path(m), &st) == -1) |
213 | 0 | { |
214 | 0 | if (errno == ENOENT) |
215 | 0 | { |
216 | 0 | if (mutt_comp_can_append(m)) |
217 | 0 | m->type = MUTT_COMPRESSED; |
218 | 0 | else |
219 | 0 | m->type = cs_subset_enum(NeoMutt->sub, "mbox_type"); |
220 | 0 | flags |= MUTT_APPENDNEW; |
221 | 0 | } |
222 | 0 | else |
223 | 0 | { |
224 | 0 | mutt_perror("%s", mailbox_path(m)); |
225 | 0 | return false; |
226 | 0 | } |
227 | 0 | } |
228 | 0 | else |
229 | 0 | { |
230 | 0 | return false; |
231 | 0 | } |
232 | 0 | } |
233 | | |
234 | 0 | m->mx_ops = mx_get_ops(m->type); |
235 | 0 | } |
236 | | |
237 | 0 | if (!m->mx_ops || !m->mx_ops->mbox_open_append) |
238 | 0 | return false; |
239 | | |
240 | 0 | const bool rc = m->mx_ops->mbox_open_append(m, flags); |
241 | 0 | m->opened++; |
242 | 0 | return rc; |
243 | 0 | } |
244 | | |
245 | | /** |
246 | | * mx_mbox_ac_link - Link a Mailbox to an existing or new Account |
247 | | * @param m Mailbox to link |
248 | | * @retval true Success |
249 | | * @retval false Failure |
250 | | */ |
251 | | bool mx_mbox_ac_link(struct Mailbox *m) |
252 | 0 | { |
253 | 0 | if (!m) |
254 | 0 | return false; |
255 | | |
256 | 0 | if (m->account) |
257 | 0 | return true; |
258 | | |
259 | 0 | struct Account *a = mx_ac_find(m); |
260 | 0 | const bool new_account = !a; |
261 | 0 | if (new_account) |
262 | 0 | { |
263 | 0 | a = account_new(NULL, NeoMutt->sub); |
264 | 0 | a->type = m->type; |
265 | 0 | } |
266 | 0 | if (!mx_ac_add(a, m)) |
267 | 0 | { |
268 | 0 | if (new_account) |
269 | 0 | { |
270 | 0 | account_free(&a); |
271 | 0 | } |
272 | 0 | return false; |
273 | 0 | } |
274 | 0 | if (new_account) |
275 | 0 | { |
276 | 0 | neomutt_account_add(NeoMutt, a); |
277 | 0 | } |
278 | 0 | return true; |
279 | 0 | } |
280 | | |
281 | | /** |
282 | | * mx_mbox_open - Open a mailbox and parse it |
283 | | * @param m Mailbox to open |
284 | | * @param flags Flags, see #OpenMailboxFlags |
285 | | * @retval true Success |
286 | | * @retval false Error |
287 | | */ |
288 | | bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags) |
289 | 0 | { |
290 | 0 | if (!m) |
291 | 0 | return false; |
292 | | |
293 | 0 | if ((m->type == MUTT_UNKNOWN) && (flags & MUTT_APPEND)) |
294 | 0 | { |
295 | 0 | m->type = cs_subset_enum(NeoMutt->sub, "mbox_type"); |
296 | 0 | m->mx_ops = mx_get_ops(m->type); |
297 | 0 | } |
298 | |
|
299 | 0 | const bool newly_linked_account = !m->account; |
300 | 0 | if (newly_linked_account) |
301 | 0 | { |
302 | 0 | if (!mx_mbox_ac_link(m)) |
303 | 0 | { |
304 | 0 | return false; |
305 | 0 | } |
306 | 0 | } |
307 | | |
308 | 0 | m->verbose = !(flags & MUTT_QUIET); |
309 | 0 | m->readonly = (flags & MUTT_READONLY); |
310 | 0 | m->peekonly = (flags & MUTT_PEEK); |
311 | |
|
312 | 0 | if (flags & MUTT_APPEND) |
313 | 0 | { |
314 | 0 | if (!mx_open_mailbox_append(m, flags)) |
315 | 0 | { |
316 | 0 | goto error; |
317 | 0 | } |
318 | 0 | return true; |
319 | 0 | } |
320 | | |
321 | 0 | if (m->opened > 0) |
322 | 0 | { |
323 | 0 | m->opened++; |
324 | 0 | return true; |
325 | 0 | } |
326 | | |
327 | 0 | m->size = 0; |
328 | 0 | m->msg_unread = 0; |
329 | 0 | m->msg_flagged = 0; |
330 | 0 | m->rights = MUTT_ACL_ALL; |
331 | |
|
332 | 0 | if (m->type == MUTT_UNKNOWN) |
333 | 0 | { |
334 | 0 | m->type = mx_path_probe(mailbox_path(m)); |
335 | 0 | m->mx_ops = mx_get_ops(m->type); |
336 | 0 | } |
337 | |
|
338 | 0 | if ((m->type == MUTT_UNKNOWN) || (m->type == MUTT_MAILBOX_ERROR) || !m->mx_ops) |
339 | 0 | { |
340 | 0 | if (m->type == MUTT_MAILBOX_ERROR) |
341 | 0 | mutt_perror("%s", mailbox_path(m)); |
342 | 0 | else if ((m->type == MUTT_UNKNOWN) || !m->mx_ops) |
343 | 0 | mutt_error(_("%s is not a mailbox"), mailbox_path(m)); |
344 | 0 | goto error; |
345 | 0 | } |
346 | | |
347 | 0 | mutt_make_label_hash(m); |
348 | | |
349 | | /* if the user has a 'push' command in their .neomuttrc, or in a folder-hook, |
350 | | * it will cause the progress messages not to be displayed because |
351 | | * mutt_refresh() will think we are in the middle of a macro. so set a |
352 | | * flag to indicate that we should really refresh the screen. */ |
353 | 0 | OptForceRefresh = true; |
354 | |
|
355 | 0 | if (m->verbose) |
356 | 0 | mutt_message(_("Reading %s..."), mailbox_path(m)); |
357 | | |
358 | | // Clear out any existing emails |
359 | 0 | for (int i = 0; i < m->email_max; i++) |
360 | 0 | { |
361 | 0 | email_free(&m->emails[i]); |
362 | 0 | } |
363 | |
|
364 | 0 | m->msg_count = 0; |
365 | 0 | m->msg_unread = 0; |
366 | 0 | m->msg_flagged = 0; |
367 | 0 | m->msg_new = 0; |
368 | 0 | m->msg_deleted = 0; |
369 | 0 | m->msg_tagged = 0; |
370 | 0 | m->vcount = 0; |
371 | |
|
372 | 0 | enum MxOpenReturns rc = m->mx_ops->mbox_open(m); |
373 | 0 | m->opened++; |
374 | |
|
375 | 0 | if ((rc == MX_OPEN_OK) || (rc == MX_OPEN_ABORT)) |
376 | 0 | { |
377 | 0 | if ((flags & MUTT_NOSORT) == 0) |
378 | 0 | { |
379 | | /* avoid unnecessary work since the mailbox is completely unthreaded |
380 | | * to begin with */ |
381 | 0 | OptSortSubthreads = false; |
382 | 0 | OptNeedRescore = false; |
383 | 0 | } |
384 | 0 | if (m->verbose) |
385 | 0 | mutt_clear_error(); |
386 | 0 | if (rc == MX_OPEN_ABORT) |
387 | 0 | { |
388 | 0 | mutt_error(_("Reading from %s interrupted..."), mailbox_path(m)); |
389 | 0 | } |
390 | 0 | } |
391 | 0 | else |
392 | 0 | { |
393 | 0 | goto error; |
394 | 0 | } |
395 | | |
396 | 0 | if (!m->peekonly) |
397 | 0 | m->has_new = false; |
398 | 0 | OptForceRefresh = false; |
399 | |
|
400 | 0 | return true; |
401 | | |
402 | 0 | error: |
403 | 0 | mx_fastclose_mailbox(m, newly_linked_account); |
404 | 0 | if (newly_linked_account) |
405 | 0 | account_mailbox_remove(m->account, m); |
406 | 0 | return false; |
407 | 0 | } |
408 | | |
409 | | /** |
410 | | * mx_fastclose_mailbox - Free up memory associated with the Mailbox |
411 | | * @param m Mailbox |
412 | | * @param keep_account Make sure not to remove the mailbox's account |
413 | | */ |
414 | | void mx_fastclose_mailbox(struct Mailbox *m, bool keep_account) |
415 | 0 | { |
416 | 0 | if (!m) |
417 | 0 | return; |
418 | | |
419 | 0 | m->opened--; |
420 | 0 | if (m->opened != 0) |
421 | 0 | return; |
422 | | |
423 | | /* never announce that a mailbox we've just left has new mail. |
424 | | * TODO: really belongs in mx_mbox_close, but this is a nice hook point */ |
425 | 0 | if (!m->peekonly) |
426 | 0 | mutt_mailbox_set_notified(m); |
427 | |
|
428 | 0 | if (m->mx_ops) |
429 | 0 | m->mx_ops->mbox_close(m); |
430 | |
|
431 | 0 | mutt_hash_free(&m->subj_hash); |
432 | 0 | mutt_hash_free(&m->id_hash); |
433 | 0 | mutt_hash_free(&m->label_hash); |
434 | |
|
435 | 0 | if (m->emails) |
436 | 0 | { |
437 | 0 | for (int i = 0; i < m->msg_count; i++) |
438 | 0 | { |
439 | 0 | if (!m->emails[i]) |
440 | 0 | break; |
441 | 0 | email_free(&m->emails[i]); |
442 | 0 | } |
443 | 0 | } |
444 | |
|
445 | 0 | if (!m->visible) |
446 | 0 | { |
447 | 0 | mx_ac_remove(m, keep_account); |
448 | 0 | } |
449 | 0 | } |
450 | | |
451 | | /** |
452 | | * sync_mailbox - Save changes to disk |
453 | | * @param m Mailbox |
454 | | * @retval enum #MxStatus |
455 | | */ |
456 | | static enum MxStatus sync_mailbox(struct Mailbox *m) |
457 | 0 | { |
458 | 0 | if (!m || !m->mx_ops || !m->mx_ops->mbox_sync) |
459 | 0 | return MX_STATUS_ERROR; |
460 | | |
461 | 0 | if (m->verbose) |
462 | 0 | { |
463 | | /* L10N: Displayed before/as a mailbox is being synced */ |
464 | 0 | mutt_message(_("Writing %s..."), mailbox_path(m)); |
465 | 0 | } |
466 | |
|
467 | 0 | enum MxStatus rc = m->mx_ops->mbox_sync(m); |
468 | 0 | if (rc != MX_STATUS_OK) |
469 | 0 | { |
470 | 0 | mutt_debug(LL_DEBUG2, "mbox_sync returned: %d\n", rc); |
471 | 0 | if ((rc == MX_STATUS_ERROR) && m->verbose) |
472 | 0 | { |
473 | | /* L10N: Displayed if a mailbox sync fails */ |
474 | 0 | mutt_error(_("Unable to write %s"), mailbox_path(m)); |
475 | 0 | } |
476 | 0 | } |
477 | |
|
478 | 0 | return rc; |
479 | 0 | } |
480 | | |
481 | | /** |
482 | | * trash_append - Move deleted mails to the trash folder |
483 | | * @param m Mailbox |
484 | | * @retval 0 Success |
485 | | * @retval -1 Failure |
486 | | */ |
487 | | static int trash_append(struct Mailbox *m) |
488 | 0 | { |
489 | 0 | if (!m) |
490 | 0 | return -1; |
491 | | |
492 | 0 | struct stat st = { 0 }; |
493 | 0 | struct stat stc = { 0 }; |
494 | 0 | int rc; |
495 | |
|
496 | 0 | const bool c_maildir_trash = cs_subset_bool(NeoMutt->sub, "maildir_trash"); |
497 | 0 | const char *const c_trash = cs_subset_string(NeoMutt->sub, "trash"); |
498 | 0 | if (!c_trash || (m->msg_deleted == 0) || ((m->type == MUTT_MAILDIR) && c_maildir_trash)) |
499 | 0 | { |
500 | 0 | return 0; |
501 | 0 | } |
502 | | |
503 | 0 | int delmsgcount = 0; |
504 | 0 | int first_del = -1; |
505 | 0 | for (int i = 0; i < m->msg_count; i++) |
506 | 0 | { |
507 | 0 | struct Email *e = m->emails[i]; |
508 | 0 | if (!e) |
509 | 0 | break; |
510 | | |
511 | 0 | if (e->deleted && !e->purge) |
512 | 0 | { |
513 | 0 | if (first_del < 0) |
514 | 0 | first_del = i; |
515 | 0 | delmsgcount++; |
516 | 0 | } |
517 | 0 | } |
518 | |
|
519 | 0 | if (delmsgcount == 0) |
520 | 0 | return 0; /* nothing to be done */ |
521 | | |
522 | | /* avoid the "append messages" prompt */ |
523 | 0 | const bool c_confirm_append = cs_subset_bool(NeoMutt->sub, "confirm_append"); |
524 | 0 | cs_subset_str_native_set(NeoMutt->sub, "confirm_append", false, NULL); |
525 | 0 | rc = mutt_save_confirm(c_trash, &st); |
526 | 0 | cs_subset_str_native_set(NeoMutt->sub, "confirm_append", c_confirm_append, NULL); |
527 | 0 | if (rc != 0) |
528 | 0 | { |
529 | | /* L10N: Although we know the precise number of messages, we do not show it to the user. |
530 | | So feel free to use a "generic plural" as plural translation if your language has one. */ |
531 | 0 | mutt_error(ngettext("message not deleted", "messages not deleted", delmsgcount)); |
532 | 0 | return -1; |
533 | 0 | } |
534 | | |
535 | 0 | if ((lstat(mailbox_path(m), &stc) == 0) && (stc.st_ino == st.st_ino) && |
536 | 0 | (stc.st_dev == st.st_dev) && (stc.st_rdev == st.st_rdev)) |
537 | 0 | { |
538 | 0 | return 0; /* we are in the trash folder: simple sync */ |
539 | 0 | } |
540 | | |
541 | 0 | if ((m->type == MUTT_IMAP) && (imap_path_probe(c_trash, NULL) == MUTT_IMAP)) |
542 | 0 | { |
543 | 0 | if (imap_fast_trash(m, c_trash) == 0) |
544 | 0 | return 0; |
545 | 0 | } |
546 | | |
547 | 0 | struct Mailbox *m_trash = mx_path_resolve(c_trash); |
548 | 0 | const bool old_append = m_trash->append; |
549 | 0 | if (!mx_mbox_open(m_trash, MUTT_APPEND)) |
550 | 0 | { |
551 | 0 | mutt_error(_("Can't open trash folder")); |
552 | 0 | mailbox_free(&m_trash); |
553 | 0 | return -1; |
554 | 0 | } |
555 | | |
556 | | /* continue from initial scan above */ |
557 | 0 | for (int i = first_del; i < m->msg_count; i++) |
558 | 0 | { |
559 | 0 | struct Email *e = m->emails[i]; |
560 | 0 | if (!e) |
561 | 0 | break; |
562 | | |
563 | 0 | if (e->deleted && !e->purge) |
564 | 0 | { |
565 | 0 | if (mutt_append_message(m_trash, m, e, NULL, MUTT_CM_NO_FLAGS, CH_NO_FLAGS) == -1) |
566 | 0 | { |
567 | 0 | mx_mbox_close(m_trash); |
568 | | // L10N: Displayed if appending to $trash fails when syncing or closing a mailbox |
569 | 0 | mutt_error(_("Unable to append to trash folder")); |
570 | 0 | m_trash->append = old_append; |
571 | 0 | mailbox_free(&m_trash); |
572 | 0 | return -1; |
573 | 0 | } |
574 | 0 | } |
575 | 0 | } |
576 | | |
577 | 0 | mx_mbox_close(m_trash); |
578 | 0 | m_trash->append = old_append; |
579 | 0 | mailbox_free(&m_trash); |
580 | |
|
581 | 0 | return 0; |
582 | 0 | } |
583 | | |
584 | | /** |
585 | | * mx_mbox_close - Save changes and close mailbox |
586 | | * @param m Mailbox |
587 | | * @retval enum #MxStatus |
588 | | * |
589 | | * @note The flag retvals come from a call to a backend sync function |
590 | | * |
591 | | * @note It's very important to ensure the mailbox is properly closed before |
592 | | * free'ing the context. For selected mailboxes, IMAP will cache the |
593 | | * context inside connection->adata until imap_close_mailbox() removes |
594 | | * it. Readonly, dontwrite, and append mailboxes are guaranteed to call |
595 | | * mx_fastclose_mailbox(), so for most of NeoMutt's code you won't see |
596 | | * return value checks for temporary contexts. |
597 | | */ |
598 | | enum MxStatus mx_mbox_close(struct Mailbox *m) |
599 | 0 | { |
600 | 0 | if (!m) |
601 | 0 | return MX_STATUS_ERROR; |
602 | | |
603 | 0 | const bool c_mail_check_recent = cs_subset_bool(NeoMutt->sub, "mail_check_recent"); |
604 | 0 | if (c_mail_check_recent && !m->peekonly) |
605 | 0 | m->has_new = false; |
606 | |
|
607 | 0 | if (m->readonly || m->dontwrite || m->append || m->peekonly) |
608 | 0 | { |
609 | 0 | mx_fastclose_mailbox(m, false); |
610 | 0 | return 0; |
611 | 0 | } |
612 | | |
613 | 0 | int i, read_msgs = 0; |
614 | 0 | enum MxStatus rc = MX_STATUS_ERROR; |
615 | 0 | enum QuadOption move_messages = MUTT_NO; |
616 | 0 | enum QuadOption purge = MUTT_YES; |
617 | 0 | struct Buffer *mbox = NULL; |
618 | 0 | struct Buffer *buf = buf_pool_get(); |
619 | |
|
620 | 0 | if ((m->msg_unread != 0) && (m->type == MUTT_NNTP)) |
621 | 0 | { |
622 | 0 | struct NntpMboxData *mdata = m->mdata; |
623 | |
|
624 | 0 | if (mdata && mdata->adata && mdata->group) |
625 | 0 | { |
626 | 0 | enum QuadOption ans = query_quadoption(_("Mark all articles read?"), |
627 | 0 | NeoMutt->sub, "catchup_newsgroup"); |
628 | 0 | if (ans == MUTT_ABORT) |
629 | 0 | goto cleanup; |
630 | 0 | if (ans == MUTT_YES) |
631 | 0 | mutt_newsgroup_catchup(m, mdata->adata, mdata->group); |
632 | 0 | } |
633 | 0 | } |
634 | | |
635 | 0 | const bool c_keep_flagged = cs_subset_bool(NeoMutt->sub, "keep_flagged"); |
636 | 0 | for (i = 0; i < m->msg_count; i++) |
637 | 0 | { |
638 | 0 | struct Email *e = m->emails[i]; |
639 | 0 | if (!e) |
640 | 0 | break; |
641 | | |
642 | 0 | if (!e->deleted && e->read && !(e->flagged && c_keep_flagged)) |
643 | 0 | read_msgs++; |
644 | 0 | } |
645 | | |
646 | | /* don't need to move articles from newsgroup */ |
647 | 0 | if (m->type == MUTT_NNTP) |
648 | 0 | read_msgs = 0; |
649 | |
|
650 | 0 | const enum QuadOption c_move = cs_subset_quad(NeoMutt->sub, "move"); |
651 | 0 | if ((read_msgs != 0) && (c_move != MUTT_NO)) |
652 | 0 | { |
653 | 0 | bool is_spool; |
654 | 0 | mbox = buf_pool_get(); |
655 | |
|
656 | 0 | char *p = mutt_find_hook(MUTT_MBOX_HOOK, mailbox_path(m)); |
657 | 0 | if (p) |
658 | 0 | { |
659 | 0 | is_spool = true; |
660 | 0 | buf_strcpy(mbox, p); |
661 | 0 | } |
662 | 0 | else |
663 | 0 | { |
664 | 0 | const char *const c_mbox = cs_subset_string(NeoMutt->sub, "mbox"); |
665 | 0 | buf_strcpy(mbox, c_mbox); |
666 | 0 | is_spool = mutt_is_spool(mailbox_path(m)) && !mutt_is_spool(buf_string(mbox)); |
667 | 0 | } |
668 | |
|
669 | 0 | if (is_spool && !buf_is_empty(mbox)) |
670 | 0 | { |
671 | 0 | buf_expand_path(mbox); |
672 | 0 | buf_printf(buf, |
673 | | /* L10N: The first argument is the number of read messages to be |
674 | | moved, the second argument is the target mailbox. */ |
675 | 0 | ngettext("Move %d read message to %s?", "Move %d read messages to %s?", read_msgs), |
676 | 0 | read_msgs, buf_string(mbox)); |
677 | 0 | move_messages = query_quadoption(buf_string(buf), NeoMutt->sub, "move"); |
678 | 0 | if (move_messages == MUTT_ABORT) |
679 | 0 | goto cleanup; |
680 | 0 | } |
681 | 0 | } |
682 | | |
683 | | /* There is no point in asking whether or not to purge if we are |
684 | | * just marking messages as "trash". */ |
685 | 0 | const bool c_maildir_trash = cs_subset_bool(NeoMutt->sub, "maildir_trash"); |
686 | 0 | if ((m->msg_deleted != 0) && !((m->type == MUTT_MAILDIR) && c_maildir_trash)) |
687 | 0 | { |
688 | 0 | buf_printf(buf, ngettext("Purge %d deleted message?", "Purge %d deleted messages?", m->msg_deleted), |
689 | 0 | m->msg_deleted); |
690 | 0 | purge = query_quadoption(buf_string(buf), NeoMutt->sub, "delete"); |
691 | 0 | if (purge == MUTT_ABORT) |
692 | 0 | goto cleanup; |
693 | 0 | } |
694 | | |
695 | 0 | const bool c_mark_old = cs_subset_bool(NeoMutt->sub, "mark_old"); |
696 | 0 | if (c_mark_old && !m->peekonly) |
697 | 0 | { |
698 | 0 | for (i = 0; i < m->msg_count; i++) |
699 | 0 | { |
700 | 0 | struct Email *e = m->emails[i]; |
701 | 0 | if (!e) |
702 | 0 | break; |
703 | 0 | if (!e->deleted && !e->old && !e->read) |
704 | 0 | mutt_set_flag(m, e, MUTT_OLD, true, true); |
705 | 0 | } |
706 | 0 | } |
707 | |
|
708 | 0 | if (move_messages) |
709 | 0 | { |
710 | 0 | if (m->verbose) |
711 | 0 | mutt_message(_("Moving read messages to %s..."), buf_string(mbox)); |
712 | | |
713 | | /* try to use server-side copy first */ |
714 | 0 | i = 1; |
715 | |
|
716 | 0 | if ((m->type == MUTT_IMAP) && (imap_path_probe(buf_string(mbox), NULL) == MUTT_IMAP)) |
717 | 0 | { |
718 | | /* add messages for moving, and clear old tags, if any */ |
719 | 0 | struct EmailArray ea = ARRAY_HEAD_INITIALIZER; |
720 | 0 | for (i = 0; i < m->msg_count; i++) |
721 | 0 | { |
722 | 0 | struct Email *e = m->emails[i]; |
723 | 0 | if (!e) |
724 | 0 | break; |
725 | | |
726 | 0 | if (e->read && !e->deleted && !(e->flagged && c_keep_flagged)) |
727 | 0 | { |
728 | 0 | e->tagged = true; |
729 | 0 | ARRAY_ADD(&ea, e); |
730 | 0 | } |
731 | 0 | else |
732 | 0 | { |
733 | 0 | e->tagged = false; |
734 | 0 | } |
735 | 0 | } |
736 | |
|
737 | 0 | i = imap_copy_messages(m, &ea, buf_string(mbox), SAVE_MOVE); |
738 | 0 | if (i == 0) |
739 | 0 | { |
740 | 0 | const bool c_delete_untag = cs_subset_bool(NeoMutt->sub, "delete_untag"); |
741 | 0 | if (c_delete_untag) |
742 | 0 | { |
743 | 0 | struct Email **ep = NULL; |
744 | 0 | ARRAY_FOREACH(ep, &ea) |
745 | 0 | { |
746 | 0 | mutt_set_flag(m, *ep, MUTT_TAG, false, true); |
747 | 0 | } |
748 | 0 | } |
749 | 0 | } |
750 | 0 | ARRAY_FREE(&ea); |
751 | 0 | } |
752 | |
|
753 | 0 | if (i == 0) /* success */ |
754 | 0 | { |
755 | 0 | mutt_clear_error(); |
756 | 0 | } |
757 | 0 | else if (i == -1) /* horrible error, bail */ |
758 | 0 | { |
759 | 0 | goto cleanup; |
760 | 0 | } |
761 | 0 | else /* use regular append-copy mode */ |
762 | 0 | { |
763 | 0 | struct Mailbox *m_read = mx_path_resolve(buf_string(mbox)); |
764 | 0 | if (!mx_mbox_open(m_read, MUTT_APPEND)) |
765 | 0 | { |
766 | 0 | mailbox_free(&m_read); |
767 | 0 | goto cleanup; |
768 | 0 | } |
769 | | |
770 | 0 | for (i = 0; i < m->msg_count; i++) |
771 | 0 | { |
772 | 0 | struct Email *e = m->emails[i]; |
773 | 0 | if (!e) |
774 | 0 | break; |
775 | 0 | if (e->read && !e->deleted && !(e->flagged && c_keep_flagged)) |
776 | 0 | { |
777 | 0 | if (mutt_append_message(m_read, m, e, NULL, MUTT_CM_NO_FLAGS, CH_UPDATE_LEN) == 0) |
778 | 0 | { |
779 | 0 | mutt_set_flag(m, e, MUTT_DELETE, true, true); |
780 | 0 | mutt_set_flag(m, e, MUTT_PURGE, true, true); |
781 | 0 | } |
782 | 0 | else |
783 | 0 | { |
784 | 0 | mx_mbox_close(m_read); |
785 | 0 | goto cleanup; |
786 | 0 | } |
787 | 0 | } |
788 | 0 | } |
789 | | |
790 | 0 | mx_mbox_close(m_read); |
791 | 0 | } |
792 | 0 | } |
793 | 0 | else if (!m->changed && (m->msg_deleted == 0)) |
794 | 0 | { |
795 | 0 | if (m->verbose) |
796 | 0 | mutt_message(_("Mailbox is unchanged")); |
797 | 0 | if ((m->type == MUTT_MBOX) || (m->type == MUTT_MMDF)) |
798 | 0 | mbox_reset_atime(m, NULL); |
799 | 0 | mx_fastclose_mailbox(m, false); |
800 | 0 | rc = MX_STATUS_OK; |
801 | 0 | goto cleanup; |
802 | 0 | } |
803 | | |
804 | | /* copy mails to the trash before expunging */ |
805 | 0 | const char *const c_trash = cs_subset_string(NeoMutt->sub, "trash"); |
806 | 0 | const struct Mailbox *m_trash = mx_mbox_find(m->account, c_trash); |
807 | 0 | if (purge && (m->msg_deleted != 0) && (m != m_trash)) |
808 | 0 | { |
809 | 0 | if (trash_append(m) != 0) |
810 | 0 | goto cleanup; |
811 | 0 | } |
812 | | |
813 | | /* allow IMAP to preserve the deleted flag across sessions */ |
814 | 0 | if (m->type == MUTT_IMAP) |
815 | 0 | { |
816 | 0 | const enum MxStatus check = imap_sync_mailbox(m, (purge != MUTT_NO), true); |
817 | 0 | if (check == MX_STATUS_ERROR) |
818 | 0 | { |
819 | 0 | rc = check; |
820 | 0 | goto cleanup; |
821 | 0 | } |
822 | 0 | } |
823 | 0 | else |
824 | 0 | { |
825 | 0 | if (purge == MUTT_NO) |
826 | 0 | { |
827 | 0 | for (i = 0; i < m->msg_count; i++) |
828 | 0 | { |
829 | 0 | struct Email *e = m->emails[i]; |
830 | 0 | if (!e) |
831 | 0 | break; |
832 | | |
833 | 0 | e->deleted = false; |
834 | 0 | e->purge = false; |
835 | 0 | } |
836 | 0 | m->msg_deleted = 0; |
837 | 0 | } |
838 | |
|
839 | 0 | if (m->changed || (m->msg_deleted != 0)) |
840 | 0 | { |
841 | 0 | enum MxStatus check = sync_mailbox(m); |
842 | 0 | if (check != MX_STATUS_OK) |
843 | 0 | { |
844 | 0 | rc = check; |
845 | 0 | goto cleanup; |
846 | 0 | } |
847 | 0 | } |
848 | 0 | } |
849 | | |
850 | 0 | if (m->verbose) |
851 | 0 | { |
852 | 0 | if (move_messages) |
853 | 0 | { |
854 | 0 | mutt_message(_("%d kept, %d moved, %d deleted"), |
855 | 0 | m->msg_count - m->msg_deleted, read_msgs, m->msg_deleted); |
856 | 0 | } |
857 | 0 | else |
858 | 0 | { |
859 | 0 | mutt_message(_("%d kept, %d deleted"), m->msg_count - m->msg_deleted, m->msg_deleted); |
860 | 0 | } |
861 | 0 | } |
862 | |
|
863 | 0 | const bool c_save_empty = cs_subset_bool(NeoMutt->sub, "save_empty"); |
864 | 0 | if ((m->msg_count == m->msg_deleted) && |
865 | 0 | ((m->type == MUTT_MMDF) || (m->type == MUTT_MBOX)) && |
866 | 0 | !mutt_is_spool(mailbox_path(m)) && !c_save_empty) |
867 | 0 | { |
868 | 0 | mutt_file_unlink_empty(mailbox_path(m)); |
869 | 0 | } |
870 | |
|
871 | 0 | if ((purge == MUTT_YES) && (m->msg_deleted != 0)) |
872 | 0 | { |
873 | 0 | for (i = 0; i < m->msg_count; i++) |
874 | 0 | { |
875 | 0 | struct Email *e = m->emails[i]; |
876 | 0 | if (!e) |
877 | 0 | break; |
878 | 0 | if (e->deleted && !e->read) |
879 | 0 | { |
880 | 0 | m->msg_unread--; |
881 | 0 | if (!e->old) |
882 | 0 | m->msg_new--; |
883 | 0 | } |
884 | 0 | if (e->deleted && e->flagged) |
885 | 0 | m->msg_flagged--; |
886 | 0 | } |
887 | 0 | } |
888 | |
|
889 | 0 | mx_fastclose_mailbox(m, false); |
890 | |
|
891 | 0 | rc = MX_STATUS_OK; |
892 | |
|
893 | 0 | cleanup: |
894 | 0 | buf_pool_release(&mbox); |
895 | 0 | buf_pool_release(&buf); |
896 | 0 | return rc; |
897 | 0 | } |
898 | | |
899 | | /** |
900 | | * mx_mbox_sync - Save changes to mailbox |
901 | | * @param[in] m Mailbox |
902 | | * @retval enum #MxStatus |
903 | | * |
904 | | * @note The flag retvals come from a call to a backend sync function |
905 | | */ |
906 | | enum MxStatus mx_mbox_sync(struct Mailbox *m) |
907 | 0 | { |
908 | 0 | if (!m) |
909 | 0 | return MX_STATUS_ERROR; |
910 | | |
911 | 0 | enum MxStatus rc = MX_STATUS_OK; |
912 | 0 | int purge = 1; |
913 | 0 | int msgcount, deleted; |
914 | |
|
915 | 0 | if (m->dontwrite) |
916 | 0 | { |
917 | 0 | struct Buffer *buf = buf_pool_get(); |
918 | 0 | struct Buffer *tmp = buf_pool_get(); |
919 | |
|
920 | 0 | if (km_expand_key(km_find_func(MENU_INDEX, OP_TOGGLE_WRITE), buf)) |
921 | 0 | buf_printf(tmp, _(" Press '%s' to toggle write"), buf_string(buf)); |
922 | 0 | else |
923 | 0 | buf_addstr(tmp, _("Use 'toggle-write' to re-enable write")); |
924 | |
|
925 | 0 | mutt_error(_("Mailbox is marked unwritable. %s"), buf_string(tmp)); |
926 | |
|
927 | 0 | buf_pool_release(&buf); |
928 | 0 | buf_pool_release(&tmp); |
929 | 0 | return MX_STATUS_ERROR; |
930 | 0 | } |
931 | 0 | else if (m->readonly) |
932 | 0 | { |
933 | 0 | mutt_error(_("Mailbox is read-only")); |
934 | 0 | return MX_STATUS_ERROR; |
935 | 0 | } |
936 | | |
937 | 0 | if (!m->changed && (m->msg_deleted == 0)) |
938 | 0 | { |
939 | 0 | if (m->verbose) |
940 | 0 | mutt_message(_("Mailbox is unchanged")); |
941 | 0 | return MX_STATUS_OK; |
942 | 0 | } |
943 | | |
944 | 0 | if (m->msg_deleted != 0) |
945 | 0 | { |
946 | 0 | char buf[128] = { 0 }; |
947 | |
|
948 | 0 | snprintf(buf, sizeof(buf), |
949 | 0 | ngettext("Purge %d deleted message?", "Purge %d deleted messages?", m->msg_deleted), |
950 | 0 | m->msg_deleted); |
951 | 0 | purge = query_quadoption(buf, NeoMutt->sub, "delete"); |
952 | 0 | if (purge == MUTT_ABORT) |
953 | 0 | return MX_STATUS_ERROR; |
954 | 0 | if (purge == MUTT_NO) |
955 | 0 | { |
956 | 0 | if (!m->changed) |
957 | 0 | return MX_STATUS_OK; /* nothing to do! */ |
958 | | /* let IMAP servers hold on to D flags */ |
959 | 0 | if (m->type != MUTT_IMAP) |
960 | 0 | { |
961 | 0 | for (int i = 0; i < m->msg_count; i++) |
962 | 0 | { |
963 | 0 | struct Email *e = m->emails[i]; |
964 | 0 | if (!e) |
965 | 0 | break; |
966 | 0 | e->deleted = false; |
967 | 0 | e->purge = false; |
968 | 0 | } |
969 | 0 | m->msg_deleted = 0; |
970 | 0 | } |
971 | 0 | } |
972 | 0 | mailbox_changed(m, NT_MAILBOX_UNTAG); |
973 | 0 | } |
974 | | |
975 | | /* really only for IMAP - imap_sync_mailbox results in a call to |
976 | | * ctx_update_tables, so m->msg_deleted is 0 when it comes back */ |
977 | 0 | msgcount = m->msg_count; |
978 | 0 | deleted = m->msg_deleted; |
979 | |
|
980 | 0 | const char *const c_trash = cs_subset_string(NeoMutt->sub, "trash"); |
981 | 0 | const struct Mailbox *m_trash = mx_mbox_find(m->account, c_trash); |
982 | 0 | if (purge && (m->msg_deleted != 0) && (m != m_trash)) |
983 | 0 | { |
984 | 0 | if (trash_append(m) != 0) |
985 | 0 | return MX_STATUS_OK; |
986 | 0 | } |
987 | | |
988 | 0 | if (m->type == MUTT_IMAP) |
989 | 0 | rc = imap_sync_mailbox(m, purge, false); |
990 | 0 | else |
991 | 0 | rc = sync_mailbox(m); |
992 | 0 | if (rc != MX_STATUS_ERROR) |
993 | 0 | { |
994 | 0 | if ((m->type == MUTT_IMAP) && !purge) |
995 | 0 | { |
996 | 0 | if (m->verbose) |
997 | 0 | mutt_message(_("Mailbox checkpointed")); |
998 | 0 | } |
999 | 0 | else |
1000 | 0 | { |
1001 | 0 | if (m->verbose) |
1002 | 0 | mutt_message(_("%d kept, %d deleted"), msgcount - deleted, deleted); |
1003 | 0 | } |
1004 | |
|
1005 | 0 | mutt_sleep(0); |
1006 | |
|
1007 | 0 | const bool c_save_empty = cs_subset_bool(NeoMutt->sub, "save_empty"); |
1008 | 0 | if ((m->msg_count == m->msg_deleted) && |
1009 | 0 | ((m->type == MUTT_MBOX) || (m->type == MUTT_MMDF)) && |
1010 | 0 | !mutt_is_spool(mailbox_path(m)) && !c_save_empty) |
1011 | 0 | { |
1012 | 0 | unlink(mailbox_path(m)); |
1013 | 0 | mx_fastclose_mailbox(m, false); |
1014 | 0 | return MX_STATUS_OK; |
1015 | 0 | } |
1016 | | |
1017 | | /* if we haven't deleted any messages, we don't need to resort |
1018 | | * ... except for certain folder formats which need "unsorted" |
1019 | | * sort order in order to synchronize folders. |
1020 | | * |
1021 | | * MH and maildir are safe. mbox-style seems to need re-sorting, |
1022 | | * at least with the new threading code. */ |
1023 | 0 | if (purge || ((m->type != MUTT_MAILDIR) && (m->type != MUTT_MH))) |
1024 | 0 | { |
1025 | | /* IMAP does this automatically after handling EXPUNGE */ |
1026 | 0 | if (m->type != MUTT_IMAP) |
1027 | 0 | { |
1028 | 0 | mailbox_changed(m, NT_MAILBOX_UPDATE); |
1029 | 0 | mailbox_changed(m, NT_MAILBOX_RESORT); |
1030 | 0 | } |
1031 | 0 | } |
1032 | 0 | } |
1033 | | |
1034 | 0 | return rc; |
1035 | 0 | } |
1036 | | |
1037 | | /** |
1038 | | * mx_msg_open_new - Open a new message |
1039 | | * @param m Destination mailbox |
1040 | | * @param e Message being copied (required for maildir support, because the filename depends on the message flags) |
1041 | | * @param flags Flags, see #MsgOpenFlags |
1042 | | * @retval ptr New Message |
1043 | | */ |
1044 | | struct Message *mx_msg_open_new(struct Mailbox *m, const struct Email *e, MsgOpenFlags flags) |
1045 | 0 | { |
1046 | 0 | if (!m) |
1047 | 0 | return NULL; |
1048 | | |
1049 | 0 | struct Address *p = NULL; |
1050 | 0 | struct Message *msg = NULL; |
1051 | |
|
1052 | 0 | if (!m->mx_ops || !m->mx_ops->msg_open_new) |
1053 | 0 | { |
1054 | 0 | mutt_debug(LL_DEBUG1, "function unimplemented for mailbox type %d\n", m->type); |
1055 | 0 | return NULL; |
1056 | 0 | } |
1057 | | |
1058 | 0 | msg = message_new(); |
1059 | 0 | msg->write = true; |
1060 | |
|
1061 | 0 | if (e) |
1062 | 0 | { |
1063 | 0 | msg->flags.flagged = e->flagged; |
1064 | 0 | msg->flags.replied = e->replied; |
1065 | 0 | msg->flags.read = e->read; |
1066 | 0 | msg->flags.draft = (flags & MUTT_SET_DRAFT); |
1067 | 0 | msg->received = e->received; |
1068 | 0 | } |
1069 | |
|
1070 | 0 | if (msg->received == 0) |
1071 | 0 | msg->received = mutt_date_now(); |
1072 | |
|
1073 | 0 | if (m->mx_ops->msg_open_new(m, msg, e)) |
1074 | 0 | { |
1075 | 0 | if (m->type == MUTT_MMDF) |
1076 | 0 | fputs(MMDF_SEP, msg->fp); |
1077 | |
|
1078 | 0 | if (((m->type == MUTT_MBOX) || (m->type == MUTT_MMDF)) && (flags & MUTT_ADD_FROM)) |
1079 | 0 | { |
1080 | 0 | if (e) |
1081 | 0 | { |
1082 | 0 | p = TAILQ_FIRST(&e->env->return_path); |
1083 | 0 | if (!p) |
1084 | 0 | p = TAILQ_FIRST(&e->env->sender); |
1085 | 0 | if (!p) |
1086 | 0 | p = TAILQ_FIRST(&e->env->from); |
1087 | 0 | } |
1088 | | |
1089 | | // Use C locale for the date, so that day/month names are in English |
1090 | 0 | char buf[64] = { 0 }; |
1091 | 0 | mutt_date_localtime_format_locale(buf, sizeof(buf), "%a %b %e %H:%M:%S %Y", |
1092 | 0 | msg->received, NeoMutt->time_c_locale); |
1093 | 0 | fprintf(msg->fp, "From %s %s\n", |
1094 | 0 | p ? buf_string(p->mailbox) : NONULL(NeoMutt->username), buf); |
1095 | 0 | } |
1096 | 0 | } |
1097 | 0 | else |
1098 | 0 | { |
1099 | 0 | message_free(&msg); |
1100 | 0 | } |
1101 | |
|
1102 | 0 | return msg; |
1103 | 0 | } |
1104 | | |
1105 | | /** |
1106 | | * mx_mbox_check - Check for new mail - Wrapper for MxOps::mbox_check() |
1107 | | * @param m Mailbox |
1108 | | * @retval enum #MxStatus |
1109 | | */ |
1110 | | enum MxStatus mx_mbox_check(struct Mailbox *m) |
1111 | 0 | { |
1112 | 0 | if (!m || !m->mx_ops) |
1113 | 0 | return MX_STATUS_ERROR; |
1114 | | |
1115 | 0 | const short c_mail_check = cs_subset_number(NeoMutt->sub, "mail_check"); |
1116 | |
|
1117 | 0 | time_t t = mutt_date_now(); |
1118 | 0 | if ((t - m->last_checked) < c_mail_check) |
1119 | 0 | return MX_STATUS_OK; |
1120 | | |
1121 | 0 | m->last_checked = t; |
1122 | |
|
1123 | 0 | enum MxStatus rc = m->mx_ops->mbox_check(m); |
1124 | 0 | if ((rc == MX_STATUS_NEW_MAIL) || (rc == MX_STATUS_REOPENED)) |
1125 | 0 | { |
1126 | 0 | mailbox_changed(m, NT_MAILBOX_INVALID); |
1127 | 0 | } |
1128 | |
|
1129 | 0 | return rc; |
1130 | 0 | } |
1131 | | |
1132 | | /** |
1133 | | * mx_msg_open - Return a stream pointer for a message |
1134 | | * @param m Mailbox |
1135 | | * @param e Email |
1136 | | * @retval ptr Message |
1137 | | * @retval NULL Error |
1138 | | */ |
1139 | | struct Message *mx_msg_open(struct Mailbox *m, struct Email *e) |
1140 | 0 | { |
1141 | 0 | if (!m || !e) |
1142 | 0 | return NULL; |
1143 | | |
1144 | 0 | if (!m->mx_ops || !m->mx_ops->msg_open) |
1145 | 0 | { |
1146 | 0 | mutt_debug(LL_DEBUG1, "function not implemented for mailbox type %d\n", m->type); |
1147 | 0 | return NULL; |
1148 | 0 | } |
1149 | | |
1150 | 0 | struct Message *msg = message_new(); |
1151 | 0 | if (!m->mx_ops->msg_open(m, msg, e)) |
1152 | 0 | message_free(&msg); |
1153 | |
|
1154 | 0 | return msg; |
1155 | 0 | } |
1156 | | |
1157 | | /** |
1158 | | * mx_msg_commit - Commit a message to a folder - Wrapper for MxOps::msg_commit() |
1159 | | * @param m Mailbox |
1160 | | * @param msg Message to commit |
1161 | | * @retval 0 Success |
1162 | | * @retval -1 Failure |
1163 | | */ |
1164 | | int mx_msg_commit(struct Mailbox *m, struct Message *msg) |
1165 | 0 | { |
1166 | 0 | if (!m || !m->mx_ops || !m->mx_ops->msg_commit || !msg) |
1167 | 0 | return -1; |
1168 | | |
1169 | 0 | if (!(msg->write && m->append)) |
1170 | 0 | { |
1171 | 0 | mutt_debug(LL_DEBUG1, "msg->write = %d, m->append = %d\n", msg->write, m->append); |
1172 | 0 | return -1; |
1173 | 0 | } |
1174 | | |
1175 | 0 | return m->mx_ops->msg_commit(m, msg); |
1176 | 0 | } |
1177 | | |
1178 | | /** |
1179 | | * mx_msg_close - Close a message |
1180 | | * @param[in] m Mailbox |
1181 | | * @param[out] ptr Message to close |
1182 | | * @retval 0 Success |
1183 | | * @retval -1 Failure |
1184 | | */ |
1185 | | int mx_msg_close(struct Mailbox *m, struct Message **ptr) |
1186 | 0 | { |
1187 | 0 | if (!m || !ptr || !*ptr) |
1188 | 0 | return 0; |
1189 | | |
1190 | 0 | int rc = 0; |
1191 | 0 | struct Message *msg = *ptr; |
1192 | |
|
1193 | 0 | if (m->mx_ops && m->mx_ops->msg_close) |
1194 | 0 | rc = m->mx_ops->msg_close(m, msg); |
1195 | |
|
1196 | 0 | if (msg->path) |
1197 | 0 | { |
1198 | 0 | mutt_debug(LL_DEBUG1, "unlinking %s\n", msg->path); |
1199 | 0 | unlink(msg->path); |
1200 | 0 | } |
1201 | |
|
1202 | 0 | message_free(ptr); |
1203 | 0 | return rc; |
1204 | 0 | } |
1205 | | |
1206 | | /** |
1207 | | * mx_alloc_memory - Create storage for the emails |
1208 | | * @param m Mailbox |
1209 | | * @param req_size Space required |
1210 | | */ |
1211 | | void mx_alloc_memory(struct Mailbox *m, int req_size) |
1212 | 0 | { |
1213 | 0 | if ((req_size + 1) <= m->email_max) |
1214 | 0 | return; |
1215 | | |
1216 | | // Step size to increase by |
1217 | | // Larger mailboxes get a larger step (limited to 1000) |
1218 | 0 | const int grow = CLAMP(m->email_max, 25, 1000); |
1219 | | |
1220 | | // Sanity checks |
1221 | 0 | req_size = ROUND_UP(req_size + 1, grow); |
1222 | |
|
1223 | 0 | const size_t s = MAX(sizeof(struct Email *), sizeof(int)); |
1224 | 0 | if ((req_size * s) < (m->email_max * s)) |
1225 | 0 | { |
1226 | 0 | mutt_error("%s", strerror(ENOMEM)); |
1227 | 0 | mutt_exit(1); |
1228 | 0 | } |
1229 | |
|
1230 | 0 | if (m->emails) |
1231 | 0 | { |
1232 | 0 | MUTT_MEM_REALLOC(&m->emails, req_size, struct Email *); |
1233 | 0 | MUTT_MEM_REALLOC(&m->v2r, req_size, int); |
1234 | 0 | } |
1235 | 0 | else |
1236 | 0 | { |
1237 | 0 | m->emails = MUTT_MEM_CALLOC(req_size, struct Email *); |
1238 | 0 | m->v2r = MUTT_MEM_CALLOC(req_size, int); |
1239 | 0 | } |
1240 | |
|
1241 | 0 | for (int i = m->email_max; i < req_size; i++) |
1242 | 0 | { |
1243 | 0 | m->emails[i] = NULL; |
1244 | 0 | m->v2r[i] = -1; |
1245 | 0 | } |
1246 | |
|
1247 | 0 | m->email_max = req_size; |
1248 | 0 | } |
1249 | | |
1250 | | /** |
1251 | | * mx_path_is_empty - Is the mailbox empty |
1252 | | * @param path Mailbox to check |
1253 | | * @retval 1 Mailbox is empty |
1254 | | * @retval 0 Mailbox contains mail |
1255 | | * @retval -1 Error |
1256 | | */ |
1257 | | int mx_path_is_empty(struct Buffer *path) |
1258 | 0 | { |
1259 | 0 | if (buf_is_empty(path)) |
1260 | 0 | return -1; |
1261 | | |
1262 | 0 | enum MailboxType type = mx_path_probe(buf_string(path)); |
1263 | 0 | const struct MxOps *ops = mx_get_ops(type); |
1264 | 0 | if (!ops || !ops->path_is_empty) |
1265 | 0 | return -1; |
1266 | | |
1267 | 0 | return ops->path_is_empty(path); |
1268 | 0 | } |
1269 | | |
1270 | | /** |
1271 | | * mx_tags_edit - Start the tag editor of the mailbox |
1272 | | * @param m Mailbox |
1273 | | * @param tags Existing tags |
1274 | | * @param buf Buffer for the results |
1275 | | * @retval -1 Error |
1276 | | * @retval 0 No valid user input |
1277 | | * @retval 1 Buffer set |
1278 | | */ |
1279 | | int mx_tags_edit(struct Mailbox *m, const char *tags, struct Buffer *buf) |
1280 | 0 | { |
1281 | 0 | if (!m || !buf) |
1282 | 0 | return -1; |
1283 | | |
1284 | 0 | if (m->mx_ops->tags_edit) |
1285 | 0 | return m->mx_ops->tags_edit(m, tags, buf); |
1286 | | |
1287 | 0 | mutt_message(_("Folder doesn't support tagging, aborting")); |
1288 | 0 | return -1; |
1289 | 0 | } |
1290 | | |
1291 | | /** |
1292 | | * mx_tags_commit - Save tags to the Mailbox - Wrapper for MxOps::tags_commit() |
1293 | | * @param m Mailbox |
1294 | | * @param e Email |
1295 | | * @param tags Tags to save |
1296 | | * @retval 0 Success |
1297 | | * @retval -1 Failure |
1298 | | */ |
1299 | | int mx_tags_commit(struct Mailbox *m, struct Email *e, const char *tags) |
1300 | 0 | { |
1301 | 0 | if (!m || !e || !tags) |
1302 | 0 | return -1; |
1303 | | |
1304 | 0 | if (m->mx_ops->tags_commit) |
1305 | 0 | return m->mx_ops->tags_commit(m, e, tags); |
1306 | | |
1307 | 0 | mutt_message(_("Folder doesn't support tagging, aborting")); |
1308 | 0 | return -1; |
1309 | 0 | } |
1310 | | |
1311 | | /** |
1312 | | * mx_tags_is_supported - Return true if mailbox support tagging |
1313 | | * @param m Mailbox |
1314 | | * @retval true Tagging is supported |
1315 | | */ |
1316 | | bool mx_tags_is_supported(struct Mailbox *m) |
1317 | 0 | { |
1318 | 0 | return m && m->mx_ops->tags_commit && m->mx_ops->tags_edit; |
1319 | 0 | } |
1320 | | |
1321 | | /** |
1322 | | * mx_path_probe - Find a mailbox that understands a path |
1323 | | * @param path Path to examine |
1324 | | * @retval enum MailboxType, e.g. #MUTT_IMAP |
1325 | | */ |
1326 | | enum MailboxType mx_path_probe(const char *path) |
1327 | 0 | { |
1328 | 0 | if (!path) |
1329 | 0 | return MUTT_UNKNOWN; |
1330 | | |
1331 | 0 | enum MailboxType rc = MUTT_UNKNOWN; |
1332 | | |
1333 | | // First, search the non-local Mailbox types (is_local == false) |
1334 | 0 | for (const struct MxOps **ops = MxOps; *ops; ops++) |
1335 | 0 | { |
1336 | 0 | if ((*ops)->is_local) |
1337 | 0 | continue; |
1338 | 0 | rc = (*ops)->path_probe(path, NULL); |
1339 | 0 | if (rc != MUTT_UNKNOWN) |
1340 | 0 | return rc; |
1341 | 0 | } |
1342 | | |
1343 | 0 | struct stat st = { 0 }; |
1344 | 0 | if (stat(path, &st) != 0) |
1345 | 0 | { |
1346 | 0 | mutt_debug(LL_DEBUG1, "unable to stat %s: %s (errno %d)\n", path, strerror(errno), errno); |
1347 | 0 | return MUTT_UNKNOWN; |
1348 | 0 | } |
1349 | | |
1350 | 0 | if (S_ISFIFO(st.st_mode)) |
1351 | 0 | { |
1352 | 0 | mutt_error(_("Can't open %s: it is a pipe"), path); |
1353 | 0 | return MUTT_UNKNOWN; |
1354 | 0 | } |
1355 | | |
1356 | | // Next, search the local Mailbox types (is_local == true) |
1357 | 0 | for (const struct MxOps **ops = MxOps; *ops; ops++) |
1358 | 0 | { |
1359 | 0 | if (!(*ops)->is_local) |
1360 | 0 | continue; |
1361 | 0 | rc = (*ops)->path_probe(path, &st); |
1362 | 0 | if (rc != MUTT_UNKNOWN) |
1363 | 0 | return rc; |
1364 | 0 | } |
1365 | | |
1366 | 0 | return rc; |
1367 | 0 | } |
1368 | | |
1369 | | /** |
1370 | | * mx_path_canon - Canonicalise a mailbox path - Wrapper for MxOps::path_canon() |
1371 | | */ |
1372 | | int mx_path_canon(struct Buffer *path, const char *folder, enum MailboxType *type) |
1373 | 0 | { |
1374 | 0 | if (buf_is_empty(path)) |
1375 | 0 | return -1; |
1376 | | |
1377 | 0 | for (size_t i = 0; i < 3; i++) |
1378 | 0 | { |
1379 | | /* Look for !! ! - < > or ^ followed by / or NUL */ |
1380 | 0 | if ((buf_at(path, 0) == '!') && (buf_at(path, 1) == '!')) |
1381 | 0 | { |
1382 | 0 | if (((buf_at(path, 2) == '/') || (buf_at(path, 2) == '\0'))) |
1383 | 0 | { |
1384 | 0 | buf_inline_replace(path, 0, 2, LastFolder); |
1385 | 0 | } |
1386 | 0 | } |
1387 | 0 | else if ((buf_at(path, 0) == '+') || (buf_at(path, 0) == '=')) |
1388 | 0 | { |
1389 | 0 | size_t folder_len = mutt_str_len(folder); |
1390 | 0 | if ((folder_len > 0) && (folder[folder_len - 1] != '/')) |
1391 | 0 | { |
1392 | 0 | path->data[0] = '/'; |
1393 | 0 | buf_inline_replace(path, 0, 0, folder); |
1394 | 0 | } |
1395 | 0 | else |
1396 | 0 | { |
1397 | 0 | buf_inline_replace(path, 0, 1, folder); |
1398 | 0 | } |
1399 | 0 | } |
1400 | 0 | else if ((buf_at(path, 1) == '/') || (buf_at(path, 1) == '\0')) |
1401 | 0 | { |
1402 | 0 | if (buf_at(path, 0) == '!') |
1403 | 0 | { |
1404 | 0 | const char *const c_spool_file = cs_subset_string(NeoMutt->sub, "spool_file"); |
1405 | 0 | buf_inline_replace(path, 0, 1, c_spool_file); |
1406 | 0 | } |
1407 | 0 | else if (buf_at(path, 0) == '-') |
1408 | 0 | { |
1409 | 0 | buf_inline_replace(path, 0, 1, LastFolder); |
1410 | 0 | } |
1411 | 0 | else if (buf_at(path, 0) == '<') |
1412 | 0 | { |
1413 | 0 | const char *const c_record = cs_subset_string(NeoMutt->sub, "record"); |
1414 | 0 | buf_inline_replace(path, 0, 1, c_record); |
1415 | 0 | } |
1416 | 0 | else if (buf_at(path, 0) == '>') |
1417 | 0 | { |
1418 | 0 | const char *const c_mbox = cs_subset_string(NeoMutt->sub, "mbox"); |
1419 | 0 | buf_inline_replace(path, 0, 1, c_mbox); |
1420 | 0 | } |
1421 | 0 | else if (buf_at(path, 0) == '^') |
1422 | 0 | { |
1423 | 0 | buf_inline_replace(path, 0, 1, CurrentFolder); |
1424 | 0 | } |
1425 | 0 | else if (buf_at(path, 0) == '~') |
1426 | 0 | { |
1427 | 0 | buf_inline_replace(path, 0, 1, NeoMutt->home_dir); |
1428 | 0 | } |
1429 | 0 | } |
1430 | 0 | else if (buf_at(path, 0) == '@') |
1431 | 0 | { |
1432 | | /* elm compatibility, @ expands alias to user name */ |
1433 | 0 | struct AddressList *al = alias_lookup(buf_string(path)); |
1434 | 0 | if (!al || TAILQ_EMPTY(al)) |
1435 | 0 | break; |
1436 | | |
1437 | 0 | struct Email *e = email_new(); |
1438 | 0 | e->env = mutt_env_new(); |
1439 | 0 | mutt_addrlist_copy(&e->env->from, al, false); |
1440 | 0 | mutt_addrlist_copy(&e->env->to, al, false); |
1441 | 0 | mutt_default_save(path, e); |
1442 | 0 | email_free(&e); |
1443 | 0 | break; |
1444 | 0 | } |
1445 | 0 | else |
1446 | 0 | { |
1447 | 0 | break; |
1448 | 0 | } |
1449 | 0 | } |
1450 | | |
1451 | | // if (!folder) //XXX - use inherited version, or pass NULL to backend? |
1452 | | // return -1; |
1453 | |
|
1454 | 0 | enum MailboxType type2 = mx_path_probe(buf_string(path)); |
1455 | 0 | if (type) |
1456 | 0 | *type = type2; |
1457 | 0 | const struct MxOps *ops = mx_get_ops(type2); |
1458 | 0 | if (!ops || !ops->path_canon) |
1459 | 0 | return -1; |
1460 | | |
1461 | 0 | if (ops->path_canon(path) < 0) |
1462 | 0 | { |
1463 | 0 | mutt_path_canon(path, NeoMutt->home_dir, true); |
1464 | 0 | } |
1465 | |
|
1466 | 0 | return 0; |
1467 | 0 | } |
1468 | | |
1469 | | /** |
1470 | | * mx_path_canon2 - Canonicalise the path to realpath |
1471 | | * @param m Mailbox |
1472 | | * @param folder Path to canonicalise |
1473 | | * @retval 0 Success |
1474 | | * @retval -1 Failure |
1475 | | */ |
1476 | | int mx_path_canon2(struct Mailbox *m, const char *folder) |
1477 | 0 | { |
1478 | 0 | if (!m) |
1479 | 0 | return -1; |
1480 | | |
1481 | 0 | struct Buffer *path = buf_pool_get(); |
1482 | |
|
1483 | 0 | if (m->realpath) |
1484 | 0 | buf_strcpy(path, m->realpath); |
1485 | 0 | else |
1486 | 0 | buf_strcpy(path, mailbox_path(m)); |
1487 | |
|
1488 | 0 | int rc = mx_path_canon(path, folder, &m->type); |
1489 | |
|
1490 | 0 | mutt_str_replace(&m->realpath, buf_string(path)); |
1491 | 0 | buf_pool_release(&path); |
1492 | |
|
1493 | 0 | if (rc >= 0) |
1494 | 0 | { |
1495 | 0 | m->mx_ops = mx_get_ops(m->type); |
1496 | 0 | buf_strcpy(&m->pathbuf, m->realpath); |
1497 | 0 | } |
1498 | |
|
1499 | 0 | return rc; |
1500 | 0 | } |
1501 | | |
1502 | | /** |
1503 | | * mx_msg_padding_size - Bytes of padding between messages - Wrapper for MxOps::msg_padding_size() |
1504 | | * @param m Mailbox |
1505 | | * @retval num Number of bytes of padding |
1506 | | * |
1507 | | * mmdf and mbox add separators, which leads a small discrepancy when computing |
1508 | | * vsize for a limited view. |
1509 | | */ |
1510 | | int mx_msg_padding_size(struct Mailbox *m) |
1511 | 0 | { |
1512 | 0 | if (!m || !m->mx_ops || !m->mx_ops->msg_padding_size) |
1513 | 0 | return 0; |
1514 | | |
1515 | 0 | return m->mx_ops->msg_padding_size(m); |
1516 | 0 | } |
1517 | | |
1518 | | /** |
1519 | | * mx_ac_find - Find the Account owning a Mailbox |
1520 | | * @param m Mailbox |
1521 | | * @retval ptr Account |
1522 | | * @retval NULL None found |
1523 | | */ |
1524 | | struct Account *mx_ac_find(struct Mailbox *m) |
1525 | 0 | { |
1526 | 0 | if (!m || !m->mx_ops || !m->realpath) |
1527 | 0 | return NULL; |
1528 | | |
1529 | 0 | struct Account *np = NULL; |
1530 | 0 | TAILQ_FOREACH(np, &NeoMutt->accounts, entries) |
1531 | 0 | { |
1532 | 0 | if (np->type != m->type) |
1533 | 0 | continue; |
1534 | | |
1535 | 0 | if (m->mx_ops->ac_owns_path(np, m->realpath)) |
1536 | 0 | return np; |
1537 | 0 | } |
1538 | | |
1539 | 0 | return NULL; |
1540 | 0 | } |
1541 | | |
1542 | | /** |
1543 | | * mx_mbox_find - Find a Mailbox on an Account |
1544 | | * @param a Account to search |
1545 | | * @param path Path to find |
1546 | | * @retval ptr Mailbox |
1547 | | */ |
1548 | | struct Mailbox *mx_mbox_find(struct Account *a, const char *path) |
1549 | 0 | { |
1550 | 0 | if (!a || !path) |
1551 | 0 | return NULL; |
1552 | | |
1553 | 0 | struct MailboxNode *np = NULL; |
1554 | 0 | struct Url *url_p = NULL; |
1555 | 0 | struct Url *url_a = NULL; |
1556 | |
|
1557 | 0 | const bool use_url = (a->type == MUTT_IMAP); |
1558 | 0 | if (use_url) |
1559 | 0 | { |
1560 | 0 | url_p = url_parse(path); |
1561 | 0 | if (!url_p) |
1562 | 0 | goto done; |
1563 | 0 | } |
1564 | | |
1565 | 0 | STAILQ_FOREACH(np, &a->mailboxes, entries) |
1566 | 0 | { |
1567 | 0 | if (!use_url) |
1568 | 0 | { |
1569 | 0 | if (mutt_str_equal(np->mailbox->realpath, path)) |
1570 | 0 | return np->mailbox; |
1571 | 0 | continue; |
1572 | 0 | } |
1573 | | |
1574 | 0 | url_free(&url_a); |
1575 | 0 | url_a = url_parse(np->mailbox->realpath); |
1576 | 0 | if (!url_a) |
1577 | 0 | continue; |
1578 | | |
1579 | 0 | if (!mutt_istr_equal(url_a->host, url_p->host)) |
1580 | 0 | continue; |
1581 | 0 | if (url_p->user && !mutt_istr_equal(url_a->user, url_p->user)) |
1582 | 0 | continue; |
1583 | 0 | if (a->type == MUTT_IMAP) |
1584 | 0 | { |
1585 | 0 | if (imap_mxcmp(url_a->path, url_p->path) == 0) |
1586 | 0 | break; |
1587 | 0 | } |
1588 | 0 | else |
1589 | 0 | { |
1590 | 0 | if (mutt_str_equal(url_a->path, url_p->path)) |
1591 | 0 | break; |
1592 | 0 | } |
1593 | 0 | } |
1594 | | |
1595 | 0 | done: |
1596 | 0 | url_free(&url_p); |
1597 | 0 | url_free(&url_a); |
1598 | |
|
1599 | 0 | if (!np) |
1600 | 0 | return NULL; |
1601 | 0 | return np->mailbox; |
1602 | 0 | } |
1603 | | |
1604 | | /** |
1605 | | * mx_mbox_find2 - Find a Mailbox on an Account |
1606 | | * @param path Path to find |
1607 | | * @retval ptr Mailbox |
1608 | | * @retval NULL No match |
1609 | | */ |
1610 | | struct Mailbox *mx_mbox_find2(const char *path) |
1611 | 0 | { |
1612 | 0 | if (!path) |
1613 | 0 | return NULL; |
1614 | | |
1615 | 0 | struct Buffer *buf = buf_new(path); |
1616 | 0 | const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder"); |
1617 | 0 | mx_path_canon(buf, c_folder, NULL); |
1618 | |
|
1619 | 0 | struct Account *np = NULL; |
1620 | 0 | TAILQ_FOREACH(np, &NeoMutt->accounts, entries) |
1621 | 0 | { |
1622 | 0 | struct Mailbox *m = mx_mbox_find(np, buf_string(buf)); |
1623 | 0 | if (m) |
1624 | 0 | { |
1625 | 0 | buf_free(&buf); |
1626 | 0 | return m; |
1627 | 0 | } |
1628 | 0 | } |
1629 | | |
1630 | 0 | buf_free(&buf); |
1631 | 0 | return NULL; |
1632 | 0 | } |
1633 | | |
1634 | | /** |
1635 | | * mx_path_resolve - Get a Mailbox for a path |
1636 | | * @param path Mailbox path |
1637 | | * @retval ptr Mailbox |
1638 | | * |
1639 | | * If there isn't a Mailbox for the path, one will be created. |
1640 | | */ |
1641 | | struct Mailbox *mx_path_resolve(const char *path) |
1642 | 0 | { |
1643 | 0 | if (!path) |
1644 | 0 | return NULL; |
1645 | | |
1646 | 0 | struct Mailbox *m = mx_mbox_find2(path); |
1647 | 0 | if (m) |
1648 | 0 | return m; |
1649 | | |
1650 | 0 | m = mailbox_new(); |
1651 | 0 | buf_strcpy(&m->pathbuf, path); |
1652 | 0 | const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder"); |
1653 | 0 | mx_path_canon2(m, c_folder); |
1654 | |
|
1655 | 0 | return m; |
1656 | 0 | } |
1657 | | |
1658 | | /** |
1659 | | * mx_mbox_find_by_name_ac - Find a Mailbox with given name under an Account |
1660 | | * @param a Account to search |
1661 | | * @param name Name to find |
1662 | | * @retval ptr Mailbox |
1663 | | */ |
1664 | | static struct Mailbox *mx_mbox_find_by_name_ac(struct Account *a, const char *name) |
1665 | 0 | { |
1666 | 0 | if (!a || !name) |
1667 | 0 | return NULL; |
1668 | | |
1669 | 0 | struct MailboxNode *np = NULL; |
1670 | |
|
1671 | 0 | STAILQ_FOREACH(np, &a->mailboxes, entries) |
1672 | 0 | { |
1673 | 0 | if (mutt_str_equal(np->mailbox->name, name)) |
1674 | 0 | return np->mailbox; |
1675 | 0 | } |
1676 | | |
1677 | 0 | return NULL; |
1678 | 0 | } |
1679 | | |
1680 | | /** |
1681 | | * mx_mbox_find_by_name - Find a Mailbox with given name |
1682 | | * @param name Name to search |
1683 | | * @retval ptr Mailbox |
1684 | | */ |
1685 | | static struct Mailbox *mx_mbox_find_by_name(const char *name) |
1686 | 0 | { |
1687 | 0 | if (!name) |
1688 | 0 | return NULL; |
1689 | | |
1690 | 0 | struct Account *np = NULL; |
1691 | 0 | TAILQ_FOREACH(np, &NeoMutt->accounts, entries) |
1692 | 0 | { |
1693 | 0 | struct Mailbox *m = mx_mbox_find_by_name_ac(np, name); |
1694 | 0 | if (m) |
1695 | 0 | return m; |
1696 | 0 | } |
1697 | | |
1698 | 0 | return NULL; |
1699 | 0 | } |
1700 | | |
1701 | | /** |
1702 | | * mx_resolve - Get a Mailbox from either a path or name |
1703 | | * @param path_or_name Mailbox path or name |
1704 | | * @retval ptr Mailbox |
1705 | | * |
1706 | | * Order of resolving: |
1707 | | * 1. Name |
1708 | | * 2. Path |
1709 | | */ |
1710 | | struct Mailbox *mx_resolve(const char *path_or_name) |
1711 | 0 | { |
1712 | 0 | if (!path_or_name) |
1713 | 0 | return NULL; |
1714 | | |
1715 | | // Order is name first because you can create a Mailbox from |
1716 | | // a path, but can't from a name. So fallback behavior creates |
1717 | | // a new Mailbox for us. |
1718 | 0 | struct Mailbox *m = mx_mbox_find_by_name(path_or_name); |
1719 | 0 | if (!m) |
1720 | 0 | m = mx_path_resolve(path_or_name); |
1721 | |
|
1722 | 0 | return m; |
1723 | 0 | } |
1724 | | |
1725 | | /** |
1726 | | * mx_ac_add - Add a Mailbox to an Account - Wrapper for MxOps::ac_add() |
1727 | | */ |
1728 | | bool mx_ac_add(struct Account *a, struct Mailbox *m) |
1729 | 0 | { |
1730 | 0 | if (!a || !m || !m->mx_ops || !m->mx_ops->ac_add) |
1731 | 0 | return false; |
1732 | | |
1733 | 0 | return m->mx_ops->ac_add(a, m) && account_mailbox_add(a, m); |
1734 | 0 | } |
1735 | | |
1736 | | /** |
1737 | | * mx_ac_remove - Remove a Mailbox from an Account and delete Account if empty |
1738 | | * @param m Mailbox to remove |
1739 | | * @param keep_account Make sure not to remove the mailbox's account |
1740 | | * @retval 0 Success |
1741 | | * @retval -1 Error |
1742 | | * |
1743 | | * @note The mailbox is NOT free'd |
1744 | | */ |
1745 | | int mx_ac_remove(struct Mailbox *m, bool keep_account) |
1746 | 0 | { |
1747 | 0 | if (!m || !m->account) |
1748 | 0 | return -1; |
1749 | | |
1750 | 0 | struct Account *a = m->account; |
1751 | 0 | account_mailbox_remove(m->account, m); |
1752 | 0 | if (!keep_account && STAILQ_EMPTY(&a->mailboxes)) |
1753 | 0 | { |
1754 | 0 | neomutt_account_remove(NeoMutt, a); |
1755 | 0 | } |
1756 | 0 | return 0; |
1757 | 0 | } |
1758 | | |
1759 | | /** |
1760 | | * mx_mbox_check_stats - Check the statistics for a mailbox - Wrapper for MxOps::mbox_check_stats() |
1761 | | * |
1762 | | * @note Emits: #NT_MAILBOX_CHANGE |
1763 | | */ |
1764 | | enum MxStatus mx_mbox_check_stats(struct Mailbox *m, uint8_t flags) |
1765 | 0 | { |
1766 | 0 | if (!m) |
1767 | 0 | return MX_STATUS_ERROR; |
1768 | | |
1769 | 0 | enum MxStatus rc = m->mx_ops->mbox_check_stats(m, flags); |
1770 | 0 | if (rc != MX_STATUS_ERROR) |
1771 | 0 | { |
1772 | 0 | struct EventMailbox ev_m = { m }; |
1773 | 0 | notify_send(m->notify, NT_MAILBOX, NT_MAILBOX_CHANGE, &ev_m); |
1774 | 0 | } |
1775 | |
|
1776 | 0 | return rc; |
1777 | 0 | } |
1778 | | |
1779 | | /** |
1780 | | * mx_save_hcache - Save message to the header cache - Wrapper for MxOps::msg_save_hcache() |
1781 | | * @param m Mailbox |
1782 | | * @param e Email |
1783 | | * @retval 0 Success |
1784 | | * @retval -1 Failure |
1785 | | * |
1786 | | * Write a single header out to the header cache. |
1787 | | */ |
1788 | | int mx_save_hcache(struct Mailbox *m, struct Email *e) |
1789 | 0 | { |
1790 | 0 | if (!m || !m->mx_ops || !m->mx_ops->msg_save_hcache || !e) |
1791 | 0 | return 0; |
1792 | | |
1793 | 0 | return m->mx_ops->msg_save_hcache(m, e); |
1794 | 0 | } |
1795 | | |
1796 | | /** |
1797 | | * mx_type - Return the type of the Mailbox |
1798 | | * @param m Mailbox |
1799 | | * @retval enum #MailboxType |
1800 | | */ |
1801 | | enum MailboxType mx_type(struct Mailbox *m) |
1802 | 0 | { |
1803 | 0 | return m ? m->type : MUTT_MAILBOX_ERROR; |
1804 | 0 | } |
1805 | | |
1806 | | /** |
1807 | | * mx_toggle_write - Toggle the mailbox's readonly flag |
1808 | | * @param m Mailbox |
1809 | | * @retval 0 Success |
1810 | | * @retval -1 Error |
1811 | | */ |
1812 | | int mx_toggle_write(struct Mailbox *m) |
1813 | 0 | { |
1814 | 0 | if (!m) |
1815 | 0 | return -1; |
1816 | | |
1817 | 0 | if (m->readonly) |
1818 | 0 | { |
1819 | 0 | mutt_error(_("Can't toggle write on a readonly mailbox")); |
1820 | 0 | return -1; |
1821 | 0 | } |
1822 | | |
1823 | 0 | if (m->dontwrite) |
1824 | 0 | { |
1825 | 0 | m->dontwrite = false; |
1826 | 0 | mutt_message(_("Changes to folder will be written on folder exit")); |
1827 | 0 | } |
1828 | 0 | else |
1829 | 0 | { |
1830 | 0 | m->dontwrite = true; |
1831 | 0 | mutt_message(_("Changes to folder will not be written")); |
1832 | 0 | } |
1833 | |
|
1834 | 0 | struct EventMailbox ev_m = { m }; |
1835 | 0 | notify_send(m->notify, NT_MAILBOX, NT_MAILBOX_CHANGE, &ev_m); |
1836 | 0 | return 0; |
1837 | 0 | } |