summaryrefslogtreecommitdiffstats
path: root/imap/fetchinfo.c
blob: 96bd1e43c57b487492dd4a8b9f02922fb53ac2a8 (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 1998 - 1999 Double Precision, Inc.
** See COPYING for distribution information.
*/

#ifndef	HAVE_CONFIG_H
#include	"config.h"
#endif

#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>
#include    <ctype.h>
#include    <sys/types.h>

#include	"imaptoken.h"
#include	"imapwrite.h"
#include	"fetchinfo.h"


/* This file contains functions to parse a FETCH attribute list */

static struct fetchinfo *alloc_headerlist(int);
static char *good_section(char *);

struct fetchinfo *fetchinfo_alloc(int oneonly)
{
struct fetchinfo *list, **listtail, *p;
struct imaptoken *tok;

	list=0;
	listtail= &list;

	while ((tok=currenttoken())->tokentype == IT_ATOM)
	{
		if (oneonly && list)	break;
		*listtail=p=(struct fetchinfo *)malloc(sizeof(*list));
		if (!p)	write_error_exit(0);
		p->next=0;
		p->name=my_strdup(tok->tokenbuf);
		p->bodysection=0;
		p->bodysublist=0;
		p->ispartial=0;
		listtail= &p->next;

		if (strcmp(p->name, "ALL") == 0 ||
			strcmp(p->name, "BODYSTRUCTURE") == 0 ||
			strcmp(p->name, "ENVELOPE") == 0 ||
			strcmp(p->name, "FLAGS") == 0 ||
			strcmp(p->name, "FAST") == 0 ||
			strcmp(p->name, "FULL") == 0 ||
			strcmp(p->name, "INTERNALDATE") == 0 ||
			strcmp(p->name, "RFC822") == 0 ||
			strcmp(p->name, "RFC822.HEADER") == 0 ||
			strcmp(p->name, "RFC822.SIZE") == 0 ||
			strcmp(p->name, "RFC822.TEXT") == 0 ||
			strcmp(p->name, "UID") == 0)
		{
			nexttoken();
			continue;
		}
		if (strcmp(p->name, "BODY") && strcmp(p->name, "BODY.PEEK"))
			break;
		if (nexttoken()->tokentype != IT_LBRACKET)	continue;

		/* Parse BODY[ ... ] */

		if ((tok=nexttoken())->tokentype != IT_RBRACKET)
		{
		char	*s;

			if ( (tok->tokentype != IT_ATOM &&
				tok->tokentype != IT_NUMBER) ||
				!(s=good_section(tok->tokenbuf)))
			{
				fetchinfo_free(list);
				return (0);
			}
			p->bodysection=my_strdup(tok->tokenbuf);

			if (strcmp(s, "HEADER.FIELDS") == 0 ||
				strcmp(s, "HEADER.FIELDS.NOT") == 0)
			{
				/* Must be followed by header list */

				if ((tok=nexttoken_nouc())->tokentype
						!= IT_LPAREN)
				{
					p->bodysublist=alloc_headerlist(1);
					if (p->bodysublist == 0)
					{
						fetchinfo_free(list);
						return (0);
					}
				}
				else
				{
					nexttoken_nouc();
					p->bodysublist=alloc_headerlist(0);
					if ( currenttoken()->tokentype
						!= IT_RPAREN)
					{
						fetchinfo_free(list);
						return (0);
					}
				}
			}
			tok=nexttoken();
			
		}
		else p->bodysection=my_strdup("");

		if (tok->tokentype != IT_RBRACKET)
		{
			fetchinfo_free(list);
			return (0);
		}
		tok=nexttoken();
		if (tok->tokentype == IT_ATOM && tok->tokenbuf[0] == '<' &&
			tok->tokenbuf[strlen(tok->tokenbuf)-1] == '>' &&
			(p->ispartial=sscanf(tok->tokenbuf+1, "%lu.%lu",
				&p->partialstart, &p->partialend)) > 0)
			nexttoken();
	}
	return (list);
}

/* Just validate that the syntax of the attribute is correct */

static char *good_section(char *p)
{
int	has_mime=0;

	while (isdigit((int)(unsigned char)*p))
	{
		if (*p == '0')	return (0);
		has_mime=1;
		while (isdigit((int)(unsigned char)*p))	++p;
		if (*p == '\0')
			return (p);

		if (*p != '.')	return (0);
		++p;
	}

	if (strcmp(p, "HEADER") == 0 ||
		strcmp(p, "HEADER.FIELDS") == 0 ||
		strcmp(p, "HEADER.FIELDS.NOT") == 0 ||
		strcmp(p, "TEXT") == 0)
		return (p);

	if (strcmp(p, "MIME") == 0 && has_mime)	return (p);
	return (0);
}

/* Header list looks like atoms to me */

static struct fetchinfo *alloc_headerlist(int oneonly)
{
struct fetchinfo *list, **listtail, *p;
struct imaptoken *tok;

	list=0;
	listtail= &list;

	while ((tok=currenttoken())->tokentype == IT_ATOM ||
	       tok->tokentype == IT_QUOTED_STRING ||
	       tok->tokentype == IT_NUMBER)
	{
		*listtail=p=(struct fetchinfo *)malloc(sizeof(*list));
		if (!p)	write_error_exit(0);
		p->next=0;
		p->name=my_strdup(tok->tokenbuf);
		p->bodysublist=0;
		p->bodysection=0;
		listtail= &p->next;
		if (oneonly)
			break;
		nexttoken_nouc();
	}
	return (list);
}

void fetchinfo_free(struct fetchinfo *p)
{
struct fetchinfo *q;

	while (p)
	{
		if (p->bodysublist)	fetchinfo_free(p->bodysublist);
		q=p->next;
		if (p->name)	free(p->name);
		if (p->bodysection) free(p->bodysection);
		free(p);
		p=q;
	}
}