NeoMutt  2025-12-11-911-gd8d604
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
msg_set.c
Go to the documentation of this file.
1
22
36
37#include "config.h"
38#include "private.h"
39#include "mutt/lib.h"
40#include "config/lib.h"
41#include "core/lib.h"
42#include "msg_set.h"
43#include "module_data.h"
44
48int imap_sort_uid(const void *a, const void *b, void *sdata)
49{
50 unsigned int ua = *(unsigned int *) a;
51 unsigned int ub = *(unsigned int *) b;
52
53 return mutt_numeric_cmp(ua, ub);
54}
55
66int imap_make_msg_set(struct UidArray *uida, struct Buffer *buf, int *pos)
67{
68 if (!uida || !buf || !pos)
69 return 0;
70
71 const int array_size = ARRAY_SIZE(uida);
72 if ((array_size == 0) || (*pos >= array_size))
73 return 0;
74
75 int count = 1; // Number of UIDs added to the set
76 int i = *pos;
77 unsigned int start = *ARRAY_GET(uida, i);
78 unsigned int prev = start;
79
81 for (i++; (i < array_size) && (buf_len(buf) < mod_data->imap_max_cmdlen); i++, count++)
82 {
83 unsigned int uid = *ARRAY_GET(uida, i);
84
85 // Keep adding to current set
86 if (uid == (prev + 1))
87 {
88 prev = uid;
89 continue;
90 }
91
92 // End the current set
93 if (start == prev)
94 buf_add_printf(buf, "%u,", start);
95 else
96 buf_add_printf(buf, "%u:%u,", start, prev);
97
98 // Start a new set
99 start = uid;
100 prev = uid;
101 }
102
103 if (start == prev)
104 buf_add_printf(buf, "%u", start);
105 else
106 buf_add_printf(buf, "%u:%u", start, prev);
107
108 *pos = i;
109
110 return count;
111}
112
127int imap_exec_msg_set(struct ImapAccountData *adata, const char *pre,
128 const char *post, struct UidArray *uida)
129{
130 struct Buffer *cmd = buf_pool_get();
131
132 int count = 0;
133 int pos = 0;
134 int rc = 0;
135
136 do
137 {
138 buf_reset(cmd);
139 buf_add_printf(cmd, "%s ", pre);
140 rc = imap_make_msg_set(uida, cmd, &pos);
141 if (rc > 0)
142 {
143 buf_add_printf(cmd, " %s", post);
145 {
146 rc = -1;
147 goto out;
148 }
149 count += rc;
150 }
151 } while (rc > 0);
152
153 rc = count;
154
155out:
156 buf_pool_release(&cmd);
157 return rc;
158}
#define ARRAY_SIZE(head)
The number of elements stored.
Definition array.h:87
#define ARRAY_GET(head, idx)
Return the element at index.
Definition array.h:109
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:204
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition buffer.c:491
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
Convenience wrapper for the config headers.
#define mutt_numeric_cmp(a, b)
Compare two numbers, return -1, 0, or 1.
Definition sort.h:27
Convenience wrapper for the core headers.
int imap_sort_uid(const void *a, const void *b, void *sdata)
Compare two UIDs - Implements sort_t -.
Definition msg_set.c:48
int imap_exec(struct ImapAccountData *adata, const char *cmdstr, ImapCmdFlags flags)
Execute a command and wait for the response from the server.
Definition command.c:1420
Imap private Module data.
Shared constants/structs that are private to IMAP.
@ IMAP_EXEC_SUCCESS
Imap command executed or queued successfully.
Definition private.h:95
@ IMAP_CMD_QUEUE
Queue a command, do not execute.
Definition private.h:84
@ MODULE_ID_IMAP
ModuleImap, Imap Mailbox
Definition module_api.h:71
int imap_make_msg_set(struct UidArray *uida, struct Buffer *buf, int *pos)
Generate a compressed message set of UIDs.
Definition msg_set.c:66
int imap_exec_msg_set(struct ImapAccountData *adata, const char *pre, const char *post, struct UidArray *uida)
Execute a command using a set of UIDs.
Definition msg_set.c:127
IMAP Message Sets.
Convenience wrapper for the library headers.
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:663
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
String manipulation buffer.
Definition buffer.h:36
IMAP-specific Account data -.
Definition adata.h:40
Imap private Module data.
Definition module_data.h:30
int imap_max_cmdlen
Maximum length of IMAP commands before split.
Definition module_data.h:32
Container for Accounts, Notifications.
Definition neomutt.h:41