| 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
 | /*
** Copyright 2002-2004, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "libmail_config.h"
#include "misc.H"
#include "imapstatushandler.H"
#include "imapfolders.H"
#include "smapstatus.H"
#include <stdlib.h>
#include <string.h>
#include <sstream>
using namespace std;
// Invoked from mail::imapFolder::readFolderInfo()
void mail::imap::folderStatus(string path,
			      mail::callback::folderInfo &callback1,
			      mail::callback &callback2)
{
	if (!ready(callback2))
		return;
	if (smap)
	{
		mail::smapSTATUS *s=
			new mail::smapSTATUS(path, callback1, callback2);
		installForegroundTask(s);
	}
	else
	{
		mail::imapStatusHandler *s=
			new mail::imapStatusHandler(callback1, callback2,
						    path);
		installForegroundTask(s);
	}
}
mail::imapStatusHandler
::imapStatusHandler(mail::callback::folderInfo &myCallback,
			  mail::callback &myCallback2,
			  string myHier)
	: callback1(myCallback), callback2(myCallback2), hier(myHier)
{
}
mail::imapStatusHandler::~imapStatusHandler()
{
}
const char *mail::imapStatusHandler::getName()
{
	return "STATUS";
}
void mail::imapStatusHandler::timedOut(const char *errmsg)
{
	callback2.fail(errmsg ? errmsg:"Server timed out.");
}
void mail::imapStatusHandler::installed(mail::imap &imapAccount)
{
	imapAccount.imapcmd("STATUS", "STATUS " + imapAccount.quoteSimple(hier)
		     + " (MESSAGES UNSEEN)\r\n");
}
LIBMAIL_START
class imapSTATUS : public imapHandlerStructured {
	mail::callback::folderInfo &callback1;
	string expectedHier;
	string attrName;
	void (imapSTATUS::*next_func)(imap &, Token);
public:
	imapSTATUS(string hier, mail::callback::folderInfo &callback);
	~imapSTATUS();
	void installed(imap &imapAccount);
private:
	const char *getName();
	void timedOut(const char *errmsg);
	void process(imap &imapAccount, Token t);
	void get_hier_name(imap &imapAccount, Token t);
	void get_attr_list(imap &imapAccount, Token t);
	void get_attr_name(imap &imapAccount, Token t);
	void get_attr_value(imap &imapAccount, Token t);
};
LIBMAIL_END
bool mail::imapStatusHandler::untaggedMessage(mail::imap &imapAccount, string name)
{
	if (name != "STATUS")
		return false;
	imapAccount.installBackgroundTask( new mail::imapSTATUS(hier, callback1));
	return true;
}
bool mail::imapStatusHandler::taggedMessage(mail::imap &imapAccount, string name,
					  string message,
					  bool okfail,
					  string errmsg)
{
	if (name != "STATUS")
		return false;
	if (okfail)
		callback2.success(errmsg);
	else
		callback2.fail(errmsg);
	imapAccount.uninstallHandler(this);
	return true;
}
///////////////////////////////////////////////////////////////////////////
//
// * STATUS parsing
mail::imapSTATUS::imapSTATUS(string hierArg,
				   mail::callback::folderInfo &callbackArg)
	: callback1(callbackArg), expectedHier(hierArg),
	  next_func( &mail::imapSTATUS::get_hier_name)
{
	callback1.messageCount=0;
	callback1.unreadCount=0;
}
mail::imapSTATUS::~imapSTATUS()
{
}
void mail::imapSTATUS::installed(mail::imap &imapAccount)
{
}
const char *mail::imapSTATUS::getName()
{
	return "* STATUS";
}
void mail::imapSTATUS::timedOut(const char *errmsg)
{
}
void mail::imapSTATUS::process(mail::imap &imapAccount, Token t)
{
	(this->*next_func)(imapAccount, t);
}
void mail::imapSTATUS::get_hier_name(mail::imap &imapAccount, Token t)
{
	if (t == ATOM || t == STRING)
	{
		next_func= &mail::imapSTATUS::get_attr_list;
		if (t.text != expectedHier)
			error(imapAccount);	// Sanity check
	}
	else
		error(imapAccount);
}
void mail::imapSTATUS::get_attr_list(mail::imap &imapAccount, Token t)
{
	if (t == NIL)
	{
		done();
		callback1.success();
		return;
	}
	if (t != '(')
	{
		error(imapAccount);
		return;
	}
	next_func= &mail::imapSTATUS::get_attr_name;
}
void mail::imapSTATUS::get_attr_name(mail::imap &imapAccount, Token t)
{
	if (t == ')')
	{
		done();
		callback1.success();
		return;
	}
	if (t != ATOM && t != STRING)
	{
		error(imapAccount);
		return;
	}
	attrName=t.text;
	upper(attrName);
	next_func= &mail::imapSTATUS::get_attr_value;
}
void mail::imapSTATUS::get_attr_value(mail::imap &imapAccount, Token t)
{
	if (t != ATOM && t != STRING)
	{
		error(imapAccount);
		return;
	}
	size_t num=0;
	istringstream(t.text.c_str()) >> num;
	if (attrName == "MESSAGES")
		callback1.messageCount=num;
	else if (attrName == "UNSEEN")
		callback1.unreadCount=num;
	next_func= &mail::imapSTATUS::get_attr_name;
}
 |