NeoMutt  2024-03-23-147-g885fbc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
msg_set.c
Go to the documentation of this file.
1
37#include "config.h"
38#include <stdio.h>
39#include "private.h"
40#include "mutt/lib.h"
41#include "msg_set.h"
42#include "sort.h"
43
50int ImapMaxCmdlen = 8192;
51
55int imap_sort_uid(const void *a, const void *b, void *sdata)
56{
57 unsigned int ua = *(unsigned int *) a;
58 unsigned int ub = *(unsigned int *) b;
59
60 return mutt_numeric_cmp(ua, ub);
61}
62
73int imap_make_msg_set(struct UidArray *uida, struct Buffer *buf, int *pos)
74{
75 if (!uida || !buf || !pos)
76 return 0;
77
78 const size_t array_size = ARRAY_SIZE(uida);
79 if ((array_size == 0) || (*pos >= array_size))
80 return 0;
81
82 int count = 1; // Number of UIDs added to the set
83 size_t i = *pos;
84 unsigned int start = *ARRAY_GET(uida, i);
85 unsigned int prev = start;
86
87 for (i++; (i < array_size) && (buf_len(buf) < ImapMaxCmdlen); i++, count++)
88 {
89 unsigned int uid = *ARRAY_GET(uida, i);
90
91 // Keep adding to current set
92 if (uid == (prev + 1))
93 {
94 prev = uid;
95 continue;
96 }
97
98 // End the current set
99 if (start == prev)
100 buf_add_printf(buf, "%u,", start);
101 else
102 buf_add_printf(buf, "%u:%u,", start, prev);
103
104 // Start a new set
105 start = uid;
106 prev = uid;
107 }
108
109 if (start == prev)
110 buf_add_printf(buf, "%u", start);
111 else
112 buf_add_printf(buf, "%u:%u", start, prev);
113
114 *pos = i;
115
116 return count;
117}
118
133int imap_exec_msg_set(struct ImapAccountData *adata, const char *pre,
134 const char *post, struct UidArray *uida)
135{
136 struct Buffer *cmd = buf_pool_get();
137
138 int count = 0;
139 int pos = 0;
140 int rc = 0;
141
142 do
143 {
144 buf_reset(cmd);
145 buf_add_printf(cmd, "%s ", pre);
146 rc = imap_make_msg_set(uida, cmd, &pos);
147 if (rc > 0)
148 {
149 buf_add_printf(cmd, " %s", post);
151 {
152 rc = -1;
153 goto out;
154 }
155 count += rc;
156 }
157 } while (rc > 0);
158
159 rc = count;
160
161out:
162 buf_pool_release(&cmd);
163 return rc;
164}
#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:203
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:490
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:75
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
int imap_sort_uid(const void *a, const void *b, void *sdata)
Compare two UIDs - Implements sort_t -.
Definition: msg_set.c:55
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:1307
@ IMAP_EXEC_SUCCESS
Imap command executed or queued successfully.
Definition: private.h:82
#define IMAP_CMD_QUEUE
Queue a command, do not execute.
Definition: private.h:73
int ImapMaxCmdlen
Maximum length of IMAP commands before they must be split.
Definition: msg_set.c:50
int imap_make_msg_set(struct UidArray *uida, struct Buffer *buf, int *pos)
Generate a compressed message set of UIDs.
Definition: msg_set.c:73
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:133
IMAP Message Sets.
Convenience wrapper for the library headers.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
GUI display the mailboxes in a side panel.
Assorted sorting methods.
#define mutt_numeric_cmp(a, b)
Definition: sort.h:35
String manipulation buffer.
Definition: buffer.h:36
IMAP-specific Account data -.
Definition: adata.h:40