text
stringlengths
0
357
return retry_later? 0 : 1;
}
static int fetch_from_single_folder(dc_imap_t* imap, const char* folder)
{
int r;
uint32_t uidvalidity = 0;
uint32_t lastseenuid = 0;
uint32_t new_lastseenuid = 0;
clist* fetch_result = NULL;
size_t read_cnt = 0;
size_t read_errors = 0;
clistiter* cur;
struct mailimap_set* set = NULL;
if (imap==NULL) {
goto cleanup;
}
if (imap->etpan==NULL) {
dc_log_info(imap->context, 0, "Cannot fetch from \"%s\" - not connected.", folder);
goto cleanup;
}
if (select_folder(imap, folder)==0) {
dc_log_warning(imap->context, 0, "Cannot select folder %s for fetching.", folder);
goto cleanup;
}
/* compare last seen UIDVALIDITY against the current one */
get_config_lastseenuid(imap, folder, &uidvalidity, &lastseenuid);
if (uidvalidity!=imap->etpan->imap_selection_info->sel_uidvalidity)
{
/* first time this folder is selected or UIDVALIDITY has changed, init lastseenuid and save it to config */
if (imap->etpan->imap_selection_info->sel_uidvalidity <= 0) {
dc_log_error(imap->context, 0, "Cannot get UIDVALIDITY for folder \"%s\".", folder);
goto cleanup;
}
if (imap->etpan->imap_selection_info->sel_has_exists) {
if (imap->etpan->imap_selection_info->sel_exists <= 0) {
dc_log_info(imap->context, 0, "Folder \"%s\" is empty.", folder);
if(imap->etpan->imap_selection_info->sel_exists==0) {
/* set lastseenuid=0 for empty folders.
id we do not do this here, we'll miss the first message
as we will get in here again and fetch from lastseenuid+1 then */
set_config_lastseenuid(imap, folder,
imap->etpan->imap_selection_info->sel_uidvalidity, 0);
}
goto cleanup;
}
/* `FETCH <message sequence number> (UID)` */
set = mailimap_set_new_single(imap->etpan->imap_selection_info->sel_exists);
}
else {
/* `FETCH * (UID)` - according to RFC 3501, `*` represents the largest message sequence number; if the mailbox is empty,
an error resp. an empty list is returned. */
dc_log_info(imap->context, 0, "EXISTS is missing for folder \"%s\", using fallback.", folder);
set = mailimap_set_new_single(0);
}
r = mailimap_fetch(imap->etpan, set, imap->fetch_type_prefetch, &fetch_result);
FREE_SET(set);
if (dc_imap_is_error(imap, r) || fetch_result==NULL) {
fetch_result = NULL;
dc_log_info(imap->context, 0, "No result returned for folder \"%s\".", folder);
goto cleanup; /* this might happen if the mailbox is empty an EXISTS does not work */
}
if ((cur=clist_begin(fetch_result))==NULL) {
dc_log_info(imap->context, 0, "Empty result returned for folder \"%s\".", folder);
goto cleanup; /* this might happen if the mailbox is empty an EXISTS does not work */
}
struct mailimap_msg_att* msg_att = (struct mailimap_msg_att*)clist_content(cur);
lastseenuid = peek_uid(msg_att);
FREE_FETCH_LIST(fetch_result);
if (lastseenuid <= 0) {
dc_log_error(imap->context, 0, "Cannot get largest UID for folder \"%s\"", folder);
goto cleanup;
}
/* if the UIDVALIDITY has _changed_, decrease lastseenuid by one to avoid gaps (well add 1 below) */
if (uidvalidity > 0 && lastseenuid > 1) {
lastseenuid -= 1;
}
/* store calculated uidvalidity/lastseenuid */
uidvalidity = imap->etpan->imap_selection_info->sel_uidvalidity;
set_config_lastseenuid(imap, folder, uidvalidity, lastseenuid);
dc_log_info(imap->context, 0, "lastseenuid initialized to %i for %s@%i", (int)lastseenuid, folder, (int)uidvalidity);
}
/* fetch messages with larger UID than the last one seen (`UID FETCH lastseenuid+1:*)`, see RFC 4549 */
/* CAVE: some servers return UID smaller or equal to the requested ones under some circumstances! */
set = mailimap_set_new_interval(lastseenuid+1, 0);
r = mailimap_uid_fetch(imap->etpan, set, imap->fetch_type_prefetch, &fetch_result);
FREE_SET(set);