'Pass 3: Flag synchronization.
Compare flag mismatches in self with those in statusfolder. If
msg has a valid UID and exists on dstfolder (has not e.g. been
deleted there), sync the flag change to both dstfolder and
statusfolder.
This function checks and protects us from action in ryrun mode.'

def __syncmessagesto_flags(self, dstfolder, statusfolder):

    addflaglist = {}
    delflaglist = {}
    for uid in self.getmessageuidlist():
        if ((uid < 0) or (not dstfolder.uidexists(uid))):
            continue
        if statusfolder.uidexists(uid):
            statusflags = statusfolder.getmessageflags(uid)
        else:
            statusflags = set()
        selfflags = self.combine_flags_and_keywords(uid, dstfolder)
        addflags = (selfflags - statusflags)
        delflags = (statusflags - selfflags)
        for flag in addflags:
            if (not (flag in addflaglist)):
                addflaglist[flag] = []
            addflaglist[flag].append(uid)
        for flag in delflags:
            if (not (flag in delflaglist)):
                delflaglist[flag] = []
            delflaglist[flag].append(uid)
    for (flag, uids) in addflaglist.items():
        self.ui.addingflags(uids, flag, dstfolder)
        if self.repository.account.dryrun:
            continue
        dstfolder.addmessagesflags(uids, set(flag))
        statusfolder.addmessagesflags(uids, set(flag))
    for (flag, uids) in delflaglist.items():
        self.ui.deletingflags(uids, flag, dstfolder)
        if self.repository.account.dryrun:
            continue
        dstfolder.deletemessagesflags(uids, set(flag))
        statusfolder.deletemessagesflags(uids, set(flag))
