summaryrefslogtreecommitdiffstats
path: root/libmail/imaphandler.C
blob: b1639174b4e11e50ede6544bf693c939cfa2a52b (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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
** Copyright 2002, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "imap.H"
#include "imaphandler.H"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

using namespace std;

//////////////////////////////////////////////////////////////////////////
//
// Tokenize server response
//

int mail::imapHandlerStructured::process(mail::imap &imapAccount, string &buffer)
{
	string::iterator b=buffer.begin();
	string::iterator e=buffer.end();
	int cnt=0;

	errbuf=buffer;

	if (stringmode)	// Collecting a string
	{
		string s="";

		while (b != e)
		{
			if (*b == '"')
			{
				stringmode=0;
				largestringsize=s.size();
				process(imapAccount, Token(STRING, s));
				return cnt+1;
			}

			if (*b == '\\')
			{
				b++;
				cnt++;
				if (b == e)
					break;
			}
			if (*b == '\n')
			{
				stringmode=0;
				process(imapAccount, Token(EOL));
				imapAccount.uninstallHandler(this);
				return cnt+1;
			}

			if (s.length() < 10000)
				s.append(&*b, 1);
			++b;
			++cnt;
		}
		return 0;
	}

	if (largestringleft > 0)  // Collecting {nnn}\r\n strings
	{
		size_t n=buffer.length();

		if (n > largestringleft)
			n=largestringleft;

		string buffer_cpy=buffer;

		buffer_cpy.erase(n);

		largestringleft -= n;

		setTimeout(); // Don't time me out just yet...

		if (wantLargeString())
		{
			setTimeout();

			if (!errormode)
			{
				if (largestringleft == 0)
					process(imapAccount, Token(STRING,
							    buffer_cpy));
				else	// More later...
					process(imapAccount, Token(LARGESTRING,
							    buffer_cpy));
			}
			return n;
		}

		// Subclass is not prepared to handle {nnn}\r\n strings.
		// For simplicity, they are converted to regular strings,
		// but to guard against hostile servers truncate large
		// strings at 8K

		stringbuffer += buffer_cpy;

		if (stringbuffer.length() > 8192)
			stringbuffer.erase(8192);

		setTimeout();
		if (largestringleft == 0 && !errormode)
			process(imapAccount, Token(STRING, stringbuffer));
		return n;
	}

	while (b != e)
	{
		if (*b == '\n')
		{
			setTimeout();
			if (!donemode && !errormode)
				process(imapAccount, Token(EOL));
			imapAccount.uninstallHandler(this);
			return (cnt+1);
		}

		if (unicode_isspace((unsigned char)*b))
		{
			b++;
			cnt++;
			continue;
		}

		if (*b == '{')	// Possibly a large string?
		{
			size_t n=0;

			b++;
			cnt++;

			for (;;)
			{
				if (b == e)
					return 0;

				if (*b == '}')
				{
					b++;
					cnt++;
					if (b == e)
						return 0;

					if (*b == '\r')
					{
						b++;
						cnt++;
						if (b == e)
							return 0;
					}

					if (*b != '\n')
						break;

					b++;
					cnt++;

					largestringsize=n;
					largestringleft=n;
					stringbuffer="";

					if (donemode && !errormode)
					{
						error(imapAccount);
						return cnt;
					}

					setTimeout();
					if (!errormode && n == 0)
						// Make sure we recognize a
						// 0-length string
					{
						process(imapAccount,
							Token(STRING,
							      stringbuffer));
					}
					return cnt;
				}

				if (!isdigit((int)(unsigned char)*b))
					break;

				n=n * 10 + (*b-'0');
				b++;
				cnt++;
			}

			if (!errormode)
				error(imapAccount);
			return cnt;
		}
		if (errormode)
			// Recovering from an error, just eat input until \n.
		{
			b++;
			cnt++;
			continue;
		}


		if (donemode)
		{
			error(imapAccount);
			continue;
		}

		if (strchr("*()", *b) != NULL)
		{
			setTimeout();
			process(imapAccount, Token((int)(char)*b));
			return cnt+1;
		}

		if (*b == '"')
		{
			stringmode=1;
			return cnt+1;
		}

#define ATOMCHAR(c) ATOMCHAR2((int)(unsigned char)(c))
#define ATOMCHAR2(c) ( (c) > ' ' && (c) != '"' && \
			(c) != '(' && (c) != ')' && \
			(c) != '{' && (c) != '%' && (c) != '*')

		if (ATOMCHAR(*b))	// ATOM
		{
			string s="";

			s.append(&*b, 1);
			b++;
			cnt++;

			// Swallow up BODY[HEADER.FIELDS (list)]

			unsigned bracket_cnt=0;

			while (b != e)
			{
				if (bracket_cnt > 0)
				{
					if ( *b == '\n' || *b == '\r')
					{
						process(imapAccount,
							Token(ATOM, s));
						return cnt;
					}
					
					if (s.length() < 10000)
						s.append(&*b, 1);

					if ( *b == ']' )
					{
						if (--bracket_cnt == 0)
						{
							process(imapAccount,
								Token(ATOM, s)
								);
							return cnt+1;
						}
					}
					++b;
					++cnt;

					continue;
				}

				if (!ATOMCHAR(*b))
				{
					setTimeout();

					if (strcasecmp(s.c_str(), "NIL") == 0)
						process(imapAccount, Token(NIL));
					else
						process(imapAccount, Token(ATOM, s));
					return cnt;
				}

				if (*b == '[')
					++bracket_cnt;

				if (s.length() < 10000)
					s.append(&*b, 1);
				++b;
				++cnt;
			}
			return (0);
		}

		error(imapAccount);
		return cnt+1;
	}

	return cnt;
}

bool mail::imapHandlerStructured::wantLargeString()
{
	return false; // The default, only some subclasses have large string
	// handler code
}

void mail::imapHandlerStructured::done()
{
	donemode=1;
}

//
// Go into error recovery mode, eating input until next endofline

void mail::imapHandlerStructured::error(mail::imap &imapAccount)
{
	if (errbuf.length() > 32)
	{
		errbuf.erase(32);
		errbuf.append("...");
	}

	size_t p=errbuf.find('\n');

	if (p < std::string::npos)
		errbuf.erase(p);

	imapAccount.fatalError("IMAP reply parse failure: ... " + errbuf);
	errormode=1;
}


mail::imapHandlerStructured::Token::operator string()
{
	switch ((enum TokenType)token) {
	case EOL:
		return "EOL";
	case ATOM:
		return text;
	case STRING:
	case LARGESTRING:
		return ( "STRING[" + text + "]");
	case NIL:
		return ( "NIL" );
	}

	char c[4];

	c[0]='\'';
	c[1]=(char)token;
	c[2]='\'';
	c[3]=0;

	return c;
}