text
stringlengths
0
357
while (do_fake_idle)
{
// wait a moment: every 5 seconds in the first 3 minutes after a new message, after that every 60 seconds
seconds_to_wait = (time(NULL)-fake_idle_start_time < 3*60)? 5 : 60;
pthread_mutex_lock(&imap->watch_condmutex);
int r = 0;
struct timespec wakeup_at;
memset(&wakeup_at, 0, sizeof(wakeup_at));
wakeup_at.tv_sec = time(NULL)+seconds_to_wait;
while (imap->watch_condflag==0 && r==0) {
r = pthread_cond_timedwait(&imap->watch_cond, &imap->watch_condmutex, &wakeup_at); /* unlock mutex -> wait -> lock mutex */
if (imap->watch_condflag) {
do_fake_idle = 0;
}
}
imap->watch_condflag = 0;
pthread_mutex_unlock(&imap->watch_condmutex);
if (do_fake_idle==0) {
return;
}
// check for new messages. fetch_from_single_folder() has the side-effect that messages
// are also downloaded, however, typically this would take place in the FETCH command
// following IDLE otherwise, so this seems okay here.
if (setup_handle_if_needed(imap)) { // the handle may not be set up if configure is not yet done
if (fetch_from_single_folder(imap, imap->watch_folder)) {
do_fake_idle = 0;
}
}
else {
// if we cannot connect, set the starting time to a small value which will
// result in larger timeouts (60 instead of 5 seconds) for re-checking the availablility of network.
// to get the _exact_ moment of re-available network, the ui should call interrupt_idle()
fake_idle_start_time = 0;
}
}
}
void dc_imap_idle(dc_imap_t* imap)
{
int r = 0;
int r2 = 0;
if (imap==NULL) {
goto cleanup;
}
if (imap->can_idle)
{
setup_handle_if_needed(imap);
if (imap->idle_set_up==0 && imap->etpan && imap->etpan->imap_stream) {
r = mailstream_setup_idle(imap->etpan->imap_stream);
if (dc_imap_is_error(imap, r)) {
dc_log_warning(imap->context, 0, "IMAP-IDLE: Cannot setup.");
fake_idle(imap);
goto cleanup;
}
imap->idle_set_up = 1;
}
if (!imap->idle_set_up || !select_folder(imap, imap->watch_folder)) {
dc_log_warning(imap->context, 0, "IMAP-IDLE not setup.");
fake_idle(imap);
goto cleanup;
}
r = mailimap_idle(imap->etpan);
if (dc_imap_is_error(imap, r)) {
dc_log_warning(imap->context, 0, "IMAP-IDLE: Cannot start.");
fake_idle(imap);
goto cleanup;
}
// most servers do not allow more than ~28 minutes; stay clearly below that.
// a good value that is also used by other MUAs is 23 minutes.
// if needed, the ui can call dc_imap_interrupt_idle() to trigger a reconnect.
#define IDLE_DELAY_SECONDS (23*60)
r = mailstream_wait_idle(imap->etpan->imap_stream, IDLE_DELAY_SECONDS);
r2 = mailimap_idle_done(imap->etpan);
if (r==MAILSTREAM_IDLE_ERROR /*0*/ || r==MAILSTREAM_IDLE_CANCELLED /*4*/) {
dc_log_info(imap->context, 0, "IMAP-IDLE wait cancelled, r=%i, r2=%i; we'll reconnect soon.", r, r2);
imap->should_reconnect = 1;
}
else if (r==MAILSTREAM_IDLE_INTERRUPTED /*1*/) {
dc_log_info(imap->context, 0, "IMAP-IDLE interrupted.");
}
else if (r== MAILSTREAM_IDLE_HASDATA /*2*/) {
dc_log_info(imap->context, 0, "IMAP-IDLE has data.");
}
else if (r==MAILSTREAM_IDLE_TIMEOUT /*3*/) {
dc_log_info(imap->context, 0, "IMAP-IDLE timeout.");
}
else {