EXPORTED const message_t *mailbox_iter_step(struct mailbox_iter *iter)
{
    if (mailbox_wait_cb) mailbox_wait_cb(mailbox_wait_cb_rock);

    for (iter->recno++; iter->recno <= iter->num_records; iter->recno++) {
        message_set_from_mailbox(iter->mailbox, iter->recno, iter->msg);
        const struct index_record *record = msg_record(iter->msg);
        if (!record->uid) continue; /* can happen on damaged mailboxes */
        if ((record->system_flags & iter->skipflags)) continue;
        if ((record->internal_flags & iter->skipflags)) continue;
        if (iter->changedsince && record->modseq <= iter->changedsince) continue;
        if (iter->uidset) {
            if (record->uid > seqset_last(iter->uidset)) return NULL;
            if (!seqset_ismember(iter->uidset, record->uid)) continue;
        }
        return iter->msg;
    }

    /* guess we're done */
    return NULL;
}
EXPORTED const message_t *mailbox_iter_step(struct mailbox_iter *iter)
{
    if (mailbox_wait_cb) mailbox_wait_cb(mailbox_wait_cb_rock);

    for (iter->recno++; iter->recno <= iter->num_records; iter->recno++) {
        message_set_from_mailbox(iter->mailbox, iter->recno, iter->msg);
        const struct index_record *record = msg_record(iter->msg);
        if (!record->uid) continue;  
        if ((record->system_flags & iter->skipflags)) continue;
        if ((record->internal_flags & iter->skipflags)) continue;
        if (iter->changedsince && record->modseq <= iter->changedsince) continue;
        if (iter->uidset) {
            if (record->uid > seqset_last(iter->uidset)) return NULL;
            if (!seqset_ismember(iter->uidset, record->uid)) continue;
        }
        return iter->msg;
    }

     
    return NULL;
}