'Common code to savemessagelabels and addmessagelabels'

def _messagelabels_aux(self, arg, uidlist, labels):

    labels = (labels - self.ignorelabels)
    uidlist = [uid for uid in uidlist if (uid > 0)]
    if (len(uidlist) > 0):
        imapobj = self.imapserver.acquireconnection()
        try:
            labels_str = (('(' + '    '.join([imaputil.quote(lb) for lb in labels])) + ')')
            uid_str = imaputil.uid_sequence(uidlist)
            result = self._store_to_imap(imapobj, uid_str, arg, labels_str)
        except imapobj.readonly:
            self.ui.labelstoreadonly(self, uidlist, labels)
            return None
        finally:
            self.imapserver.releaseconnection(imapobj)
        if result:
            retlabels = imaputil.flags2hash(imaputil.imapsplit(result)[1])['X-GM-LABELS']
            retlabels = set([imaputil.dequote(lb) for lb in imaputil.imapsplit(retlabels)])
            return retlabels
    return None
