Teaching an old dog new tricks
No Matches
msg_set.c File Reference

IMAP Message Sets. More...

#include "config.h"
#include <stdio.h>
#include "private.h"
#include "mutt/lib.h"
#include "msg_set.h"
#include "sort.h"
+ Include dependency graph for msg_set.c:

Go to the source code of this file.


int imap_sort_uid (const void *a, const void *b, void *sdata)
 Compare two UIDs - Implements sort_t -.
int imap_make_msg_set (struct UidArray *uida, struct Buffer *buf, int *pos)
 Generate a compressed message set of UIDs.
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.


int ImapMaxCmdlen = 8192
 Maximum length of IMAP commands before they must be split.

Detailed Description

IMAP Message Sets.

  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see

Definition in file msg_set.c.

Function Documentation

◆ imap_make_msg_set()

int imap_make_msg_set ( struct UidArray *  uida,
struct Buffer buf,
int *  pos 

Generate a compressed message set of UIDs.

uidaArray of UIDs
bufBuffer for message set
posCursor used for multiple calls to this function
Return values
numNumber of UIDs processed

Compress a sorted list of UIDs, e.g.

  • 1,2,3,4,6,8,9,10 becomes 1:4,6,8:10

Definition at line 72 of file msg_set.c.

74 if (!uida || !buf || !pos)
75 return 0;
77 const size_t array_size = ARRAY_SIZE(uida);
78 if ((array_size == 0) || (*pos >= array_size))
79 return 0;
81 int count = 1; // Number of UIDs added to the set
82 size_t i = *pos;
83 unsigned int start = *ARRAY_GET(uida, i);
84 unsigned int prev = start;
86 for (i++; (i < array_size) && (buf_len(buf) < ImapMaxCmdlen); i++, count++)
87 {
88 unsigned int uid = *ARRAY_GET(uida, i);
90 // Keep adding to current set
91 if (uid == (prev + 1))
92 {
93 prev = uid;
94 continue;
95 }
97 // End the current set
98 if (start == prev)
99 buf_add_printf(buf, "%u,", start);
100 else
101 buf_add_printf(buf, "%u:%u,", start, prev);
103 // Start a new set
104 start = uid;
105 prev = uid;
106 }
108 if (start == prev)
109 buf_add_printf(buf, "%u", start);
110 else
111 buf_add_printf(buf, "%u:%u", start, prev);
113 *pos = i;
115 return count;
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:86
#define ARRAY_GET(head, idx)
Return the element at index.
Definition: array.h:108
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:216
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:466
int ImapMaxCmdlen
Maximum length of IMAP commands before they must be split.
Definition: msg_set.c:49
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ imap_exec_msg_set()

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.

adataImap Account data
prePrefix commands
postPostfix commands
uidaSorted array of UIDs
Return values
numNumber of messages sent

Commands are of the form: TAG PRE MESSAGE-SET POST e.g. A01 UID COPY 1:4 MAILBOX

Must be flushed with imap_exec()

Definition at line 132 of file msg_set.c.

135 struct Buffer cmd = buf_make(ImapMaxCmdlen);
137 int count = 0;
138 int pos = 0;
139 int rc = 0;
141 do
142 {
143 buf_reset(&cmd);
144 buf_add_printf(&cmd, "%s ", pre);
145 rc = imap_make_msg_set(uida, &cmd, &pos);
146 if (rc > 0)
147 {
148 buf_add_printf(&cmd, " %s", post);
150 {
151 rc = -1;
152 goto out;
153 }
154 count += rc;
155 }
156 } while (rc > 0);
158 rc = count;
161 buf_dealloc(&cmd);
162 return rc;
void buf_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:389
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:88
struct Buffer buf_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:70
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:93
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:1304
Imap command executed or queued successfully.
Definition: private.h:82
Queue a command, do not execute.
Definition: private.h:73
int imap_make_msg_set(struct UidArray *uida, struct Buffer *buf, int *pos)
Generate a compressed message set of UIDs.
Definition: msg_set.c:72
String manipulation buffer.
Definition: buffer.h:34
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ ImapMaxCmdlen

int ImapMaxCmdlen = 8192

Maximum length of IMAP commands before they must be split.

This is suggested in RFC7162 (dated 2014).

Definition at line 49 of file msg_set.c.