'Determines sequence numbers of messages to be fetched.
Message sequence numbers (MSNs) are more easily compacted
into ranges which makes transactions slightly faster.
Arguments:
- imapobj: instance of IMAPlib
- min_date (optional): a time_struct; only fetch messages newer than this
- min_uid (optional): only fetch messages with UID >= min_uid
This function should be called with at MOST one of min_date OR
min_uid set but not BOTH.
Returns: range(s) for messages or None if no messages
are to be fetched.'

def _msgs_to_fetch(self, imapobj, min_date=None, min_uid=None):

    def search(search_conditions):
        'Actually    request    the    server    with    the    specified    conditions.\n\n                                                Returns:    range(s)    for    messages    or    None    if    no    messages\n                                                are    to    be    fetched.'
        (res_type, res_data) = imapobj.search(None, search_conditions)
        if (res_type != 'OK'):
            raise OfflineImapError(("SEARCH    in    folder    [%s]%s    failed.    Search    string    was    '%s'.    Server    responded    '[%s]    %s'" % (self.getrepository(), self, search_cond, res_type, res_data)), OfflineImapError.ERROR.FOLDER)
        if (('    ' in res_data[0]) or (res_data[0] == '')):
            res_data = res_data[0].split()
        if (0 in res_data):
            self.ui.warn('server    returned    UID    with    0;    ignoring.')
            res_data.remove(0)
        return res_data
    (res_type, imapdata) = imapobj.select(self.getfullname(), True, True)
    if ((imapdata == [None]) or (imapdata[0] == '0')):
        return None
    conditions = []
    if (min_uid != None):
        conditions.append(('UID    %d:*' % min_uid))
    elif (min_date != None):
        conditions.append(('SINCE    %02d-%s-%d' % (min_date[2], MonthNames[min_date[1]], min_date[0])))
    maxsize = self.getmaxsize()
    if (maxsize != None):
        conditions.append(('SMALLER    %d' % maxsize))
    if (len(conditions) >= 1):
        search_cond = ('(%s)' % '    '.join(conditions))
        search_result = search(search_cond)
        return imaputil.uid_sequence(search_result)
    return '1:*'
