File: imapsubj.c

package info (click to toggle)
courier 0.60.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 52,288 kB
  • ctags: 12,677
  • sloc: ansic: 165,348; cpp: 24,820; sh: 16,410; perl: 6,839; makefile: 3,621; yacc: 289; sed: 16
file content (303 lines) | stat: -rw-r--r-- 4,858 bytes parent folder | download | duplicates (10)
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
/*
** Copyright 2000 Double Precision, Inc.
** See COPYING for distribution information.
*/

/*
** $Id: imapsubj.c,v 1.10 2006/04/11 02:24:59 mrsam Exp $
*/
#include	<stdio.h>
#include	<ctype.h>
#include	<stdlib.h>
#include	<string.h>
#include	"rfc822.h"
#include	"config.h"

#if	HAVE_STRCASECMP

#else
#define	strcasecmp	stricmp
#endif

#if	HAVE_STRNCASECMP

#else
#define	strncasecmp	strnicmp
#endif

/* Skip over blobs */

static char *skipblob(char *p, char **save_blob_ptr)
{
	char *q;
	char *orig_p=p;
	int isalldigits=1;

	if (*p == '[')
	{
		for (q= p+1; *q; q++)
			if (*q == '[' || *q == ']')
				break;
			else if (strchr("0123456789", *q) == NULL)
				isalldigits=0;

		if (*q == ']')
		{
			p=q+1;

			while (isspace((int)(unsigned char)*p))
			{
				++p;
			}

			if (save_blob_ptr && *save_blob_ptr && !isalldigits)
			{
				while (orig_p != p)
					*(*save_blob_ptr)++=*orig_p++;
			}

			return (p);
		}
	}
	return (p);
}

static char *skipblobs(char *p, char **save_blob_ptr)
{
	char *q=p;

	do
	{
		p=q;
		q=skipblob(p, save_blob_ptr);
	} while (q != p);
	return (q);
}

/* Remove artifacts from the subject header */

static void stripsubj(char *s, int *hasrefwd, char *save_blob_buf)
{
	char	*p;
	char	*q;
	int doit;

	for (p=q=s; *p; p++)
	{
		if (!isspace((int)(unsigned char)*p))
		{
			*q++=*p;
			continue;
		}
		while (p[1] && isspace((int)(unsigned char)p[1]))
		{
			++p;
		}
		*q++=' ';
	}
	*q=0;

	do
	{
		doit=0;
		/*
		**
		** (2) Remove all trailing text of the subject that matches
		** the subj-trailer ABNF, repeat until no more matches are
		** possible.
		**
		**  subj-trailer    = "(fwd)" / WSP
		*/

		for (p=s; *p; p++)
			;
		while (p > s)
		{
			if ( isspace((int)(unsigned char)p[-1]))
			{
				--p;
				continue;
			}
			if (p-s >= 5 && strncasecmp(p-5, "(FWD)", 5) == 0)
			{
				p -= 5;
				*hasrefwd |= CORESUBJ_FWD;
				continue;
			}
			break;
		}
		*p=0;

		for (p=s; *p; )
		{
			for (;;)
			{
				char *orig_blob_ptr;
				int flag=CORESUBJ_FWD;

				/*
				**
				** (3) Remove all prefix text of the subject
				** that matches the subj-leader ABNF.
				**
				**   subj-leader     = (*subj-blob subj-refwd) / WSP
				**
				**   subj-blob       = "[" *BLOBCHAR "]" *WSP
				**
				**   subj-refwd      = ("re" / ("fw" ["d"])) *WSP [subj-blob] ":"
				**
				**   BLOBCHAR        = %x01-5a / %x5c / %x5e-7f
				**                   ; any CHAR except '[' and ']'
				*/

				if (isspace((int)(unsigned char)*p))
				{
					++p;
					continue;
				}

				q=skipblobs(p, NULL);

				if (strncasecmp(q, "RE", 2) == 0)
				{
					flag=CORESUBJ_RE;
					q += 2;
				}
				else if (strncasecmp(q, "FWD", 3) == 0)
				{
					q += 3;
				}
				else if (strncasecmp(q, "FW", 2) == 0)
				{
					q += 2;
				}
				else q=0;

				if (q)
				{
					orig_blob_ptr=save_blob_buf;

					q=skipblob(q, &save_blob_buf);
					if (*q == ':')
					{
						p=q+1;
						*hasrefwd |= flag;
						continue;
					}

					save_blob_buf=orig_blob_ptr;
				}


				/*
				** (4) If there is prefix text of the subject
				** that matches the subj-blob ABNF, and
				** removing that prefix leaves a non-empty
				** subj-base, then remove the prefix text.
				**
				**   subj-base       = NONWSP *([*WSP] NONWSP)
				**                   ; can be a subj-blob
				*/

				orig_blob_ptr=save_blob_buf;

				q=skipblob(p, &save_blob_buf);

				if (q != p && *q)
				{
					p=q;
					continue;
				}
				save_blob_buf=orig_blob_ptr;
				break;
			}

			/*
			**
			** (6) If the resulting text begins with the
			** subj-fwd-hdr ABNF and ends with the subj-fwd-trl
			** ABNF, remove the subj-fwd-hdr and subj-fwd-trl and
			** repeat from step (2).
			**
			**   subj-fwd-hdr    = "[fwd:"
			**
			**   subj-fwd-trl    = "]"
			*/

			if (strncasecmp(p, "[FWD:", 5) == 0)
			{
				q=strrchr(p, ']');
				if (q && q[1] == 0)
				{
					*q=0;
					p += 5;
					*hasrefwd |= CORESUBJ_FWD;

					for (q=s; (*q++=*p++) != 0; )
						;
					doit=1;
				}
			}
			break;
		}
	} while (doit);

	q=s;
	while ( (*q++ = *p++) != 0)
		;
	if (save_blob_buf)
		*save_blob_buf=0;
}

char *rfc822_coresubj(const char *s, int *hasrefwd)
{
	char *q=strdup(s), *r;
	int dummy;

	if (!hasrefwd)
		hasrefwd= &dummy;

	*hasrefwd=0;
	if (!q)	return (0);

	for (r=q; *r; r++)
		if ((*r & 0x80) == 0)	/* Just US-ASCII casing, thanks */
			*r=toupper((int)(unsigned char)*r);
	stripsubj(q, hasrefwd, 0);
	return (q);
}

char *rfc822_coresubj_nouc(const char *s, int *hasrefwd)
{
	char *q=strdup(s);
	int dummy;

	if (!hasrefwd)
		hasrefwd= &dummy;

	*hasrefwd=0;
	if (!q)	return (0);

	stripsubj(q, hasrefwd, 0);
	return (q);
}

char *rfc822_coresubj_keepblobs(const char *s)
{
	char *q=strdup(s), *r;
	int dummy;

	if (!q)	return (0);

	r=strdup(s);
	if (!r)
	{
		free(q);
		return (0);
	}

	stripsubj(q, &dummy, r);
	strcat(r, q);
	free(q);
	return (r);
}