+/* Update the IMAP server to reflect the flags a single message. */
+
+int imap_sync_message (IMAP_DATA *idata, HEADER *hdr, BUFFER *cmd,
+ int *err_continue)
+{
+ char flags[LONG_STRING];
+ char uid[11];
+
+ hdr->changed = 0;
+
+ snprintf (uid, sizeof (uid), "%u", HEADER_DATA(hdr)->uid);
+ cmd->dptr = cmd->data;
+ mutt_buffer_addstr (cmd, "UID STORE ");
+ mutt_buffer_addstr (cmd, uid);
+
+ flags[0] = '\0';
+
+ imap_set_flag (idata, ACL_SEEN, hdr->read, "\\Seen ",
+ flags, sizeof (flags));
+ imap_set_flag (idata, ACL_WRITE, hdr->flagged,
+ "\\Flagged ", flags, sizeof (flags));
+ imap_set_flag (idata, ACL_WRITE, hdr->replied,
+ "\\Answered ", flags, sizeof (flags));
+ imap_set_flag (idata, ACL_DELETE, hdr->deleted,
+ "\\Deleted ", flags, sizeof (flags));
+
+ /* now make sure we don't lose custom tags */
+ if (mutt_bit_isset (idata->rights, ACL_WRITE))
+ imap_add_keywords (flags, hdr, idata->flags, sizeof (flags));
+
+ m_strrtrim(flags);
+
+ /* UW-IMAP is OK with null flags, Cyrus isn't. The only solution is to
+ * explicitly revoke all system flags (if we have permission) */
+ if (!*flags)
+ {
+ imap_set_flag (idata, ACL_SEEN, 1, "\\Seen ", flags, sizeof (flags));
+ imap_set_flag (idata, ACL_WRITE, 1, "\\Flagged ", flags, sizeof (flags));
+ imap_set_flag (idata, ACL_WRITE, 1, "\\Answered ", flags, sizeof (flags));
+ imap_set_flag (idata, ACL_DELETE, 1, "\\Deleted ", flags, sizeof (flags));
+
+ m_strrtrim(flags);
+
+ mutt_buffer_addstr (cmd, " -FLAGS.SILENT (");
+ } else
+ mutt_buffer_addstr (cmd, " FLAGS.SILENT (");
+
+ mutt_buffer_addstr (cmd, flags);
+ mutt_buffer_addstr (cmd, ")");
+
+ /* dumb hack for bad UW-IMAP 4.7 servers spurious FLAGS updates */
+ hdr->active = 0;
+
+ /* after all this it's still possible to have no flags, if you
+ * have no ACL rights */
+ if (*flags && (imap_exec (idata, cmd->data, 0) != 0) &&
+ err_continue && (*err_continue != M_YES))
+ {
+ *err_continue = imap_continue ("imap_sync_message: STORE failed",
+ idata->cmd.buf);
+ if (*err_continue != M_YES)
+ return -1;
+ }
+
+ hdr->active = 1;
+ idata->ctx->changed--;
+
+ return 0;
+}
+