2 ** Copyright 2000 Double Precision, Inc.
3 ** See COPYING for distribution information.
7 ** $Id: imapsubj.c,v 1.10 2006/04/11 02:24:59 mrsam Exp $
19 #define strcasecmp stricmp
25 #define strncasecmp strnicmp
30 static char *skipblob(char *p
, char **save_blob_ptr
)
39 if (*q
== '[' || *q
== ']')
41 else if (strchr("0123456789", *q
) == NULL
)
48 while (isspace((int)(unsigned char)*p
))
53 if (save_blob_ptr
&& *save_blob_ptr
&& !isalldigits
)
56 *(*save_blob_ptr
)++=*orig_p
++;
65 static char *skipblobs(char *p
, char **save_blob_ptr
)
72 q
=skipblob(p
, save_blob_ptr
);
77 /* Remove artifacts from the subject header */
79 static void stripsubj(char *s
, int *hasrefwd
, char *save_blob_buf
)
87 if (!isspace((int)(unsigned char)*p
))
92 while (p
[1] && isspace((int)(unsigned char)p
[1]))
105 ** (2) Remove all trailing text of the subject that matches
106 ** the subj-trailer ABNF, repeat until no more matches are
109 ** subj-trailer = "(fwd)" / WSP
116 if ( isspace((int)(unsigned char)p
[-1]))
121 if (p
-s
>= 5 && strncasecmp(p
-5, "(FWD)", 5) == 0)
124 *hasrefwd
|= CORESUBJ_FWD
;
136 int flag
=CORESUBJ_FWD
;
140 ** (3) Remove all prefix text of the subject
141 ** that matches the subj-leader ABNF.
143 ** subj-leader = (*subj-blob subj-refwd) / WSP
145 ** subj-blob = "[" *BLOBCHAR "]" *WSP
147 ** subj-refwd = ("re" / ("fw" ["d"])) *WSP [subj-blob] ":"
149 ** BLOBCHAR = %x01-5a / %x5c / %x5e-7f
150 ** ; any CHAR except '[' and ']'
153 if (isspace((int)(unsigned char)*p
))
159 q
=skipblobs(p
, NULL
);
161 if (strncasecmp(q
, "RE", 2) == 0)
166 else if (strncasecmp(q
, "FWD", 3) == 0)
170 else if (strncasecmp(q
, "FW", 2) == 0)
178 orig_blob_ptr
=save_blob_buf
;
180 q
=skipblob(q
, &save_blob_buf
);
188 save_blob_buf
=orig_blob_ptr
;
193 ** (4) If there is prefix text of the subject
194 ** that matches the subj-blob ABNF, and
195 ** removing that prefix leaves a non-empty
196 ** subj-base, then remove the prefix text.
198 ** subj-base = NONWSP *([*WSP] NONWSP)
199 ** ; can be a subj-blob
202 orig_blob_ptr
=save_blob_buf
;
204 q
=skipblob(p
, &save_blob_buf
);
211 save_blob_buf
=orig_blob_ptr
;
217 ** (6) If the resulting text begins with the
218 ** subj-fwd-hdr ABNF and ends with the subj-fwd-trl
219 ** ABNF, remove the subj-fwd-hdr and subj-fwd-trl and
220 ** repeat from step (2).
222 ** subj-fwd-hdr = "[fwd:"
224 ** subj-fwd-trl = "]"
227 if (strncasecmp(p
, "[FWD:", 5) == 0)
234 *hasrefwd
|= CORESUBJ_FWD
;
236 for (q
=s
; (*q
++=*p
++) != 0; )
246 while ( (*q
++ = *p
++) != 0)
252 char *rfc822_coresubj(const char *s
, int *hasrefwd
)
254 char *q
=strdup(s
), *r
;
264 if ((*r
& 0x80) == 0) /* Just US-ASCII casing, thanks */
265 *r
=toupper((int)(unsigned char)*r
);
266 stripsubj(q
, hasrefwd
, 0);
270 char *rfc822_coresubj_nouc(const char *s
, int *hasrefwd
)
281 stripsubj(q
, hasrefwd
, 0);
285 char *rfc822_coresubj_keepblobs(const char *s
)
287 char *q
=strdup(s
), *r
;
299 stripsubj(q
, &dummy
, r
);