summaryrefslogtreecommitdiffstats
path: root/libmail/imapidle.C
blob: 24097544f46f8680aa70c037f756ea69d0ab595a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
** Copyright 2003, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "libmail_config.h"
#include "imap.H"
#include "imaphandler.H"
#include "imapidle.H"

using namespace std;

mail::imapIdleHandler::imapIdleHandler(bool idleOnOffArg,
				       mail::callback *callbackArg)
	: idleOnOff(idleOnOffArg), idling(false),
	  shouldTerminate(false), terminating(false),
	  callback(callbackArg)
{
}

const char *mail::imapIdleHandler::getName()
{
	return "IDLE";
}

bool mail::imapIdleHandler::getTimeout(imap &imapAccount,
				       int &ioTimeout)
{
	if (waiting)
	{
		struct timeval tv;

		gettimeofday(&tv, NULL);

		if (tv.tv_sec > waitingUntil.tv_sec ||
		    (tv.tv_sec == waitingUntil.tv_sec &&
		     tv.tv_usec >= waitingUntil.tv_usec))
		{
			waiting=false;

			imapAccount.imapcmd("IDLE", "IDLE\r\n");
			ioTimeout=0;
			return false;
		}
		else
		{
			struct timeval t=waitingUntil;

			t.tv_usec -= tv.tv_usec;

			if (t.tv_usec < 0)
			{
				t.tv_usec += 1000000;
				--t.tv_sec;
			}
			t.tv_sec -= tv.tv_sec;

			ioTimeout=t.tv_sec * 1000 + t.tv_usec / 1000;

			if (ioTimeout == 0)
				ioTimeout=100;
			return false;
		}
	}

	ioTimeout = 15 * 60 * 1000;
	return false;
}

void mail::imapIdleHandler::timedOut(const char *errmsg)
{
	mail::callback *c=callback;

	callback=NULL;

	if (c)
		callbackTimedOut(*c, errmsg);
}

mail::imapIdleHandler::~imapIdleHandler()
{
	mail::callback *c=callback;

	callback=NULL;

	if (c)
		c->success("OK");
}

bool mail::imapIdleHandler::untaggedMessage(imap &imapAccount,
					    std::string name)
{
	return false;
}

void mail::imapIdleHandler::installed(imap &imapAccount)
{
	if (!idleOnOff || !imapAccount.wantIdleMode
	    || shouldTerminate || !imapAccount.task_queue.empty())
	{
		imapAccount.uninstallHandler(this);
		return;
	}

	waiting=true;

	// Wait 1/10th of a second before issuing an IDLE.  When we're in
	// IDLE mode, and a new task is started, we terminate and a requeue
	// ourselves after the pending command.  We don't want to immediately
	// reenter the IDLE mode because if the first task queues up a second
	// task we would have to immediately get out of IDLE mode right away,
	// so wait 1/10th of a second to see if anything is up.

	gettimeofday(&waitingUntil, NULL);
	if ((waitingUntil.tv_usec += 100000) > 1000000)
	{
		waitingUntil.tv_usec %= 1000000;
		++waitingUntil.tv_sec;
	}
}


bool mail::imapIdleHandler::taggedMessage(imap &imapAccount, std::string name,
					  std::string message,
					  bool okfail, std::string errmsg)
{
	mail::callback *c=callback;

	callback=NULL;

	imapAccount.uninstallHandler(this);

	if (imapAccount.wantIdleMode)
	{
		// Reinstall, when we're done

		imapAccount.installForegroundTask(new imapIdleHandler(true,
								      NULL));
		c=NULL;
	}

	if (c)
	{
		if (okfail)
			c->success(errmsg);
		else
			c->fail(errmsg);
	}
	return true;
}
		
bool mail::imapIdleHandler::continuationRequest(imap &imapAccount,
					    std::string request)
{
	idling=true;
	if (shouldTerminate)
		terminateIdle(imapAccount);

	mail::callback *c=callback;

	callback=NULL;

	if (c)
		c->success("Monitoring folder for changes...");

	return true;
}

void mail::imapIdleHandler::anotherHandlerInstalled(imap &imapAccount)
{
	if (waiting)
	{
		imapAccount.uninstallHandler(this);
		return;
	}

	shouldTerminate=true;
	if (idling)
		terminateIdle(imapAccount);
}

void mail::imapIdleHandler::terminateIdle(imap &imapAccount)
{
	if (terminating)
		return;
	terminating=true;

	mail::callback *c=callback;

	callback=NULL;

	if (c)
		c->success("OK");

	imapAccount.imapcmd("", "DONE\r\n");
}