'Change a message\'s flags to `flags`.
Note that this function does not check against dryrun settings,
so you need to ensure that it is never called in a
dryrun mode.'

def savemessageflags(self, uid, flags):

    imapobj = self.imapserver.acquireconnection()
    try:
        result = self._store_to_imap(imapobj, str(uid), 'FLAGS', imaputil.flagsmaildir2imap(flags))
    except imapobj.readonly:
        self.ui.flagstoreadonly(self, [uid], flags)
        return
    finally:
        self.imapserver.releaseconnection(imapobj)
    if (not result):
        self.messagelist[uid]['flags'] = flags
    else:
        flags = imaputil.flags2hash(imaputil.imapsplit(result)[1])['FLAGS']
        self.messagelist[uid]['flags'] = imaputil.flagsimap2maildir(flags)
