1 /**
2 * @file
3 * IMAP MSN helper functions
4 *
5 * @authors
6 * Copyright (C) 2020 Pietro Cerutti <gahr@gahr.ch>
7 *
8 * @copyright
9 * This program is free software: you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 2 of the License, or (at your option) any later
12 * version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 /**
24 * @page imap_msn MSN helper functions
25 *
26 * IMAP MSN helper functions
27 */
28
29 #include <limits.h>
30 #include <stdlib.h>
31 #include "mutt/lib.h"
32 #include "msn.h"
33 #include "mdata.h" // IWYU pragma: keep
34
35 /**
36 * imap_msn_reserve - Create / reallocate the cache
37 * @param msn MSN structure
38 * @param num Number of MSNs to make room for
39 */
imap_msn_reserve(struct MSN * msn,size_t num)40 void imap_msn_reserve(struct MSN *msn, size_t num)
41 {
42 /* This is a conservative check to protect against a malicious imap
43 * server. Most likely size_t is bigger than an unsigned int, but
44 * if msn_count is this big, we have a serious problem. */
45 if (num >= (UINT_MAX / sizeof(struct Email *)))
46 {
47 mutt_error(_("Out of memory"));
48 mutt_exit(1);
49 }
50
51 ARRAY_RESERVE(msn, num);
52 }
53
54 /**
55 * imap_msn_free - Free the cache
56 * @param msn MSN structure
57 */
imap_msn_free(struct MSN * msn)58 void imap_msn_free(struct MSN *msn)
59 {
60 ARRAY_FREE(msn);
61 }
62
63 /**
64 * imap_msn_highest - Return the highest MSN in use
65 * @param msn MSN structure
66 * @retval num The highest MSN in use
67 */
imap_msn_highest(const struct MSN * msn)68 size_t imap_msn_highest(const struct MSN *msn)
69 {
70 return ARRAY_SIZE(msn);
71 }
72
73 /**
74 * imap_msn_get - Return the Email associated with an msn
75 * @param msn MSN structure
76 * @param idx Index to retrieve
77 * @retval ptr Pointer to Email or NULL
78 */
imap_msn_get(const struct MSN * msn,size_t idx)79 struct Email *imap_msn_get(const struct MSN *msn, size_t idx)
80 {
81 struct Email **ep = ARRAY_GET(msn, idx);
82 return ep ? *ep : NULL;
83 }
84
85 /**
86 * imap_msn_set - Cache an Email into a given position
87 * @param msn MSN structure
88 * @param idx Index in the cache
89 * @param e Email to cache
90 */
imap_msn_set(struct MSN * msn,size_t idx,struct Email * e)91 void imap_msn_set(struct MSN *msn, size_t idx, struct Email *e)
92 {
93 ARRAY_SET(msn, idx, e);
94 }
95
96 /**
97 * imap_msn_shrink - Remove a number of entries from the end of the cache
98 * @param msn MSN structure
99 * @param num Number of entries to remove
100 * @retval num Number of entries actually removed
101 */
imap_msn_shrink(struct MSN * msn,size_t num)102 size_t imap_msn_shrink(struct MSN *msn, size_t num)
103 {
104 return ARRAY_SHRINK(msn, num);
105 }
106
107 /**
108 * imap_msn_remove - Remove an entry from the cache
109 * @param msn MSN structure
110 * @param idx Index to invalidate
111 */
imap_msn_remove(struct MSN * msn,size_t idx)112 void imap_msn_remove(struct MSN *msn, size_t idx)
113 {
114 struct Email **ep = ARRAY_GET(msn, idx);
115 if (ep)
116 *ep = NULL;
117 }
118