isync

mailbox synchronization program
git clone https://git.code.sf.net/p/isync/isync
Log | Files | Refs | README | LICENSE

commit f2450cc4b8a62948c159490934bd55ae2bb4390e
parent d789f0c1ce65d8f3e5acf75667ad4838ead45cfb
Author: Oswald Buddenhagen <ossi@users.sf.net>
Date:   Sun, 26 Dec 2021 12:42:16 +0100

centralize message flag pretty-printing somewhat

the flag values themselves are central, so we can centralize their most
common representation (Maildir's) just as well.

Diffstat:
Msrc/driver.c | 14++++++++++++++
Msrc/driver.h | 6++++--
Msrc/drv_imap.c | 4+---
Msrc/drv_maildir.c | 23+++++++++--------------
Msrc/drv_proxy.c | 35+++++++++--------------------------
Msrc/sync.c | 21++-------------------
6 files changed, 39 insertions(+), 64 deletions(-)

diff --git a/src/driver.c b/src/driver.c @@ -9,6 +9,20 @@ driver_t *drivers[N_DRIVERS] = { &maildir_driver, &imap_driver }; +// Keep the MESSAGE_FLAGS in sync (grep that)! +const char MsgFlags[] = { 'D', 'F', 'P', 'R', 'S', 'T' }; + +void +make_flags( uchar flags, char *buf ) +{ + uint i, d; + + for (i = d = 0; i < as(MsgFlags); i++) + if (flags & (1 << i)) + buf[d++] = MsgFlags[i]; + buf[d] = 0; +} + uint count_generic_messages( message_t *msgs ) { diff --git a/src/driver.h b/src/driver.h @@ -31,8 +31,7 @@ typedef struct store_conf { } store_conf_t; /* For message->flags */ -/* Keep the mailbox driver flag definitions in sync: */ -/* grep for MAILBOX_DRIVER_FLAG */ +// Keep the MESSAGE_FLAGS in sync (grep that)! /* The order is according to alphabetical maildir flag sort */ #define F_DRAFT (1<<0) /* Draft */ #define F_FLAGGED (1<<1) /* Flagged */ @@ -42,6 +41,9 @@ typedef struct store_conf { #define F_DELETED (1<<5) /* Trashed */ #define NUM_FLAGS 6 +extern const char MsgFlags[NUM_FLAGS]; +void make_flags( uchar flags, char *buf ); + /* For message->status */ #define M_RECENT (1<<0) /* unsyncable flag; maildir_* depend on this being 1<<0 */ #define M_DEAD (1<<1) /* expunged */ diff --git a/src/drv_imap.c b/src/drv_imap.c @@ -266,9 +266,7 @@ static int imap_deref( imap_store_t *ctx ); static void imap_invoke_bad_callback( imap_store_t *ctx ); -/* Keep the mailbox driver flag definitions in sync: */ -/* grep for MAILBOX_DRIVER_FLAG */ -/* The order is according to alphabetical maildir flag sort */ +// Keep the MESSAGE_FLAGS in sync (grep that)! static const char *Flags[] = { "\\Draft", /* 'D' */ "\\Flagged", /* 'F' */ diff --git a/src/drv_maildir.c b/src/drv_maildir.c @@ -87,11 +87,6 @@ static struct flock lck; static int MaildirCount; -/* Keep the mailbox driver flag definitions in sync: */ -/* grep for MAILBOX_DRIVER_FLAG */ -/* The order is according to alphabetical maildir flag sort */ -static const char Flags[] = { 'D', 'F', 'P', 'R', 'S', 'T' }; - static uchar maildir_parse_flags( const char *info_prefix, const char *base ) { @@ -101,8 +96,8 @@ maildir_parse_flags( const char *info_prefix, const char *base ) flags = 0; if ((s = strstr( base, info_prefix ))) - for (s += 3, i = 0; i < as(Flags); i++) - if (strchr( s, Flags[i] )) + for (s += 3, i = 0; i < as(MsgFlags); i++) + if (strchr( s, MsgFlags[i] )) flags |= (1 << i); return flags; } @@ -1570,9 +1565,9 @@ maildir_make_flags( char info_delimiter, uchar flags, char *buf ) buf[0] = info_delimiter; buf[1] = '2'; buf[2] = ','; - for (d = 3, i = 0; i < (int)as(Flags); i++) + for (d = 3, i = 0; i < (int)as(MsgFlags); i++) if (flags & (1 << i)) - buf[d++] = Flags[i]; + buf[d++] = MsgFlags[i]; buf[d] = 0; return d; } @@ -1690,24 +1685,24 @@ maildir_set_msg_flags( store_t *gctx, message_t *gmsg, uint uid ATTR_UNUSED, int for (;;) { bl = bbl + nfsnprintf( buf + bbl, _POSIX_PATH_MAX - bbl, "%s/", subdirs[gmsg->status & M_RECENT] ); ol = strlen( msg->base ); - if (_POSIX_PATH_MAX - bl < ol + 3 + NUM_FLAGS) + if (_POSIX_PATH_MAX - bl < ol + 3 + (int)as(MsgFlags)) oob(); memcpy( buf + bl, msg->base, (size_t)ol + 1 ); memcpy( nbuf + bl, msg->base, (size_t)ol + 1 ); if ((s = strstr( nbuf + bl, conf->info_prefix ))) { s += 3; fl = ol - (s - (nbuf + bl)); - for (i = 0; i < as(Flags); i++) { - if ((p = strchr( s, Flags[i] ))) { + for (i = 0; i < as(MsgFlags); i++) { + if ((p = strchr( s, MsgFlags[i] ))) { if (del & (1 << i)) { memmove( p, p + 1, (size_t)fl - (size_t)(p - s) ); fl--; } } else if (add & (1 << i)) { - for (j = 0; j < fl && Flags[i] > s[j]; j++); + for (j = 0; j < fl && MsgFlags[i] > s[j]; j++); fl++; memmove( s + j + 1, s + j, (size_t)(fl - j) ); - s[j] = Flags[i]; + s[j] = MsgFlags[i]; } } tl = ol + 3 + fl; diff --git a/src/drv_proxy.c b/src/drv_proxy.c @@ -27,23 +27,6 @@ typedef union proxy_store { }; } proxy_store_t; -/* Keep the mailbox driver flag definitions in sync: */ -/* grep for MAILBOX_DRIVER_FLAG */ -/* The order is according to alphabetical maildir flag sort */ -static const char Flags[] = { 'D', 'F', 'P', 'R', 'S', 'T' }; - -static char * -proxy_make_flags( uchar flags, char *buf ) -{ - uint i, d; - - for (d = 0, i = 0; i < as(Flags); i++) - if (flags & (1 << i)) - buf[d++] = Flags[i]; - buf[d] = 0; - return buf; -} - static void proxy_store_deref( proxy_store_t *ctx ) { @@ -274,10 +257,10 @@ static @type@proxy_@name@( store_t *gctx@decl_args@, void (*cb)( @decl_cb_args@v //# DEFINE load_box_print_pass_cb_args , cmd->sts, cmd->total_msgs, cmd->recent_msgs //# DEFINE load_box_print_cb_args if (cmd->sts == DRV_OK) { - char fbuf[as(Flags) + 1]; + char fbuf[as(MsgFlags) + 1]; for (message_t *msg = cmd->msgs; msg; msg = msg->next) debug( " uid=%-5u flags=%-4s size=%-6u tuid=%." stringify(TUIDL) "s\n", - msg->uid, (msg->status & M_FLAGS) ? (proxy_make_flags( msg->flags, fbuf ), fbuf) : "?", msg->size, *msg->tuid ? msg->tuid : "?" ); + msg->uid, (msg->status & M_FLAGS) ? (make_flags( msg->flags, fbuf ), fbuf) : "?", msg->size, *msg->tuid ? msg->tuid : "?" ); } //# END @@ -299,8 +282,8 @@ static @type@proxy_@name@( store_t *gctx@decl_args@, void (*cb)( @decl_cb_args@v //# DEFINE fetch_msg_print_fmt_args , uid=%u, want_flags=%s, want_date=%s //# DEFINE fetch_msg_print_pass_args , msg->uid, !(msg->status & M_FLAGS) ? "yes" : "no", data->date ? "yes" : "no" //# DEFINE fetch_msg_pre_print_cb_args - char fbuf[as(Flags) + 1]; - proxy_make_flags( cmd->data->flags, fbuf ); + char fbuf[as(MsgFlags) + 1]; + make_flags( cmd->data->flags, fbuf ); //# END //# DEFINE fetch_msg_print_fmt_cb_args , flags=%s, date=%lld, size=%u //# DEFINE fetch_msg_print_pass_cb_args , fbuf, (long long)cmd->data->date, cmd->data->len @@ -314,8 +297,8 @@ static @type@proxy_@name@( store_t *gctx@decl_args@, void (*cb)( @decl_cb_args@v //# END //# DEFINE store_msg_pre_print_args - char fbuf[as(Flags) + 1]; - proxy_make_flags( data->flags, fbuf ); + char fbuf[as(MsgFlags) + 1]; + make_flags( data->flags, fbuf ); //# END //# DEFINE store_msg_print_fmt_args , flags=%s, date=%lld, size=%u, to_trash=%s //# DEFINE store_msg_print_pass_args , fbuf, (long long)data->date, data->len, to_trash ? "yes" : "no" @@ -329,9 +312,9 @@ static @type@proxy_@name@( store_t *gctx@decl_args@, void (*cb)( @decl_cb_args@v //# END //# DEFINE set_msg_flags_pre_print_args - char fbuf1[as(Flags) + 1], fbuf2[as(Flags) + 1]; - proxy_make_flags( add, fbuf1 ); - proxy_make_flags( del, fbuf2 ); + char fbuf1[as(MsgFlags) + 1], fbuf2[as(MsgFlags) + 1]; + make_flags( add, fbuf1 ); + make_flags( del, fbuf2 ); //# END //# DEFINE set_msg_flags_print_fmt_args , uid=%u, add=%s, del=%s //# DEFINE set_msg_flags_print_pass_args , uid, fbuf1, fbuf2 diff --git a/src/sync.c b/src/sync.c @@ -29,19 +29,14 @@ int trash_total[2], trash_done[2]; const char *str_fn[] = { "far side", "near side" }, *str_hl[] = { "push", "pull" }; -/* Keep the mailbox driver flag definitions in sync: */ -/* grep for MAILBOX_DRIVER_FLAG */ -/* The order is according to alphabetical maildir flag sort */ -static const char Flags[] = { 'D', 'F', 'P', 'R', 'S', 'T' }; - static uchar parse_flags( const char *buf ) { uint i, d; uchar flags; - for (flags = i = d = 0; i < as(Flags); i++) { - if (buf[d] == Flags[i]) { + for (flags = i = d = 0; i < as(MsgFlags); i++) { + if (buf[d] == MsgFlags[i]) { flags |= (1 << i); d++; } @@ -49,18 +44,6 @@ parse_flags( const char *buf ) return flags; } -static uint -make_flags( uchar flags, char *buf ) -{ - uint i, d; - - for (i = d = 0; i < as(Flags); i++) - if (flags & (1 << i)) - buf[d++] = Flags[i]; - buf[d] = 0; - return d; -} - // This is the (mostly) persistent status of the sync record. // Most of these bits are actually mutually exclusive. It is a // bitfield to allow for easy testing for multiple states.