Repository navigation
Expand file tree
/
Copy pathparse.c
More file actions
6399 lines (5698 loc) · 212 KB
/
Copy pathparse.c
File metadata and controls
6399 lines (5698 loc) · 212 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Copyright (C) 1994, 1995 Enterprise Integration Technologies Corp.
** VeriFone Inc./Hewlett-Packard. All Rights Reserved.
** Kevin Hughes, kev@kevcom.com 3/11/94
** Kent Landfield, kent@landfield.com 4/6/97
** Hypermail Project 1998-2023
**
** This program and library is free software; you can redistribute it and/or
** modify it under the terms of the GNU (Library) General Public License
** as published by the Free Software Foundation; either version 3
** of the License, or any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU (Library) General Public License for more details.
**
** You should have received a copy of the GNU (Library) General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <fcntl.h>
#include "hypermail.h"
#include "setup.h"
#include "struct.h"
#include "uudecode.h"
#include "base64.h"
#include "search.h"
#include "getname.h"
#include "parse.h"
#include "print.h"
#ifdef GDBM
#include "gdbm.h"
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_DIRENT_H
#ifdef __LCC__
#include "../lcc/dirent.h"
#include <direct.h>
#else
#include <dirent.h>
#endif
#else
#include <sys/dir.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
/*
** LCC doesn't have lstat() defined in sys/stat.h. We'll define it
** in lcc_extras.h, but really it just calls stat().
*/
#ifdef __LCC__
#include <sys/stat.h>
#include "../lcc/lcc_extras.h"
#endif
#define NEW_PARSER 1
typedef enum {
ENCODE_NORMAL,
ENCODE_QP, /* quoted printable */
ENCODE_MULTILINED, /* this is not a real type, but just a separator showing
that the types below are encoded in a way that makes
one line in the indata may become one or more lines
in the outdata */
ENCODE_BASE64, /* base64 */
ENCODE_UUENCODE, /* well, it seems there exist some kind of semi-standard
for uu-encoded attachments. */
ENCODE_UNKNOWN /* must be the last one */
} EncodeType;
typedef enum {
CONTENT_TEXT, /* normal mails are text based default */
CONTENT_BINARY, /* this kind we store separately and href to */
CONTENT_HTML, /* this is html formated text */
CONTENT_IGNORE, /* don't care about this content */
CONTENT_UNKNOWN /* must be the last one */
} ContentType;
typedef enum {
NO_FILE,
MAKE_FILE,
MADE_FILE
} FileStatus; /* for attachments */
int ignorecontent(char *type)
{
if (inlist(set_ignore_types, "$NONPLAIN") && !textcontent(type)
&& strncasecmp(type, "multipart/", 10))
return 1;
if (inlist(set_ignore_types, "$BINARY") && !textcontent(type)
&& strcasecmp(type, "text/html") && strncasecmp(type, "multipart/", 10))
return 1;
return (inlist(set_ignore_types, type));
}
int inlinecontent(char *type)
{
return (inlist(set_inline_types, type));
}
int preferedcontent(int *current_weight, char *type, int decode)
{
int weight = -1;
int status;
status = 0;
if (set_save_alts == 1)
return 1;
/* We let plain text remain PREFERED at all times */
if (!strcasecmp("text/plain", type)) {
if (*current_weight != 0) {
/* to avoid having two text/plain alternatives */
*current_weight = 0;
status = 1;
}
}
/* find the weight of the type arg. If the weight is
inferior to the current_weight, we make it the
prefered content */
else {
if (set_prefered_types)
weight = inlist_pos(set_prefered_types, type);
if (weight == -1) { /* not known to be good, use weaker evidence */
if (!strncasecmp("text/", type, 5))
weight = 1000;
else weight = 2000 + decode;
}
if (weight != -1) {
/* +1 so that weight 0 is reserved for text/plain */
weight++;
if (*current_weight == -1) {
*current_weight = weight;
status = 1;
}
else if (*current_weight > weight) {
*current_weight = weight;
status = 1;
}
}
}
return status;
}
int textcontent(char *type)
{
/* We let text/plain remain text at all times. Appearantly, older mailers
* can still use just "text" as content-type, and we better treat that as
* text/plain to make all those users happy. */
if (!strcasecmp("text/plain", type) || !strcasecmp("text", type))
return 1;
if (set_text_types) {
return (inlist(set_text_types, type));
}
return 0;
}
static int is_applemail_ua(char *ua_string)
{
/* returns TRUE if the ua_string is one of the declared applemail
* clients */
int res = FALSE;
if (ua_string && *ua_string != '\0') {
char *buff;
char *ptr;
buff = strsav(ua_string);
ptr = strcasestr(buff, " Mail (");
if (ptr) {
*ptr = '\0';
res = inlist(set_applemail_ua_value, buff);
}
free(buff);
}
return res;
}
/*
* Should return TRUE if the input is a Re: start. The end pointer should
* then point on the first character after the Re:
*
* Identifies "Re:", "Fw:" as well as "Re[<number>]:" strings.
*/
int isre(char *re, char **end)
{
char *endp = NULL;
if (!strncasecmp("Re:", re, 3)) {
endp = re + 3;
}
else if (!strncasecmp("Fw:", re, 3)) {
endp = re + 3;
}
else if (!strncasecmp("Re[", re, 3)) {
re += 3;
strtol(re, &re, 10); /* eat the number */
if (!strncmp("]:", re, 2)) {
/* we have an end "]:" and therefore it qualifies as a Re */
endp = re + 2;
}
}
if (endp) {
if (end)
*end = endp;
return TRUE;
}
return FALSE;
}
/*
* Find the first re-substring in the input and return the position
* where it is. The 'end' parameter will be filled in the first position
* *after* the re.
*/
char *findre(char *in, char **end)
{
while (*in) {
if (isre(in, end))
return in;
if (isspace(*in)) {
in++;
} else {
break;
}
}
return NULL;
}
void print_progress(int num, char *msg, char *filename)
{
char bufstr[256];
register int i;
static int lastlen = 0;
static int longest = 0;
int len = 0;
int newline = 0;
newline = 0;
if (msg != NULL) {
if (filename != NULL) {
trio_snprintf(bufstr, sizeof(bufstr), "%4d %-s %-s", num, msg, filename);
if (set_showprogress > 1)
newline = 1;
}
else {
trio_snprintf(bufstr, sizeof(bufstr), "%4d %-s.", num, msg);
newline = 1;
}
}
else
sprintf(bufstr, "%4d", num);
for (i = 0; i < lastlen; i++) /* Back up to the beginning of line */
fputc('\b', stdout);
fputs(bufstr, stdout); /* put out the string */
len = strlen(bufstr); /* get length of new string */
/*
* If there is a new message then erase
* the trailing info from the enw string
*/
if (msg != NULL) {
for (i = len; i <= longest; i++)
fputc(' ', stdout);
for (i = len; i <= longest; i++)
fputc('\b', stdout);
}
lastlen = len;
if (lastlen > longest)
longest = lastlen;
if (newline)
fputc('\n', stdout);
fflush(stdout);
}
char *safe_filename(char *name)
{
register char *sp;
register char *np;
np = name;
if (!np || *np == '\0') {
return NULL;
}
/* skip leading spaces in the filename */
while (*np && (*np == ' ' || *np == '\t'))
np++;
if (!*np || !(*np == '\n') || *np == '\r') {
/* filename is made of only spaces; replace them with
REPLACEMENT_CHAR */
np = name;
}
for (sp = name, np = name; *np && *np != '\n' && *np != '\r';) {
/* if valid character then store it */
if (((*np >= 'a' && *np <= 'z') || (*np >= '0' && *np <= '9') ||
(*np >= 'A' && *np <= 'Z') || (*np == '-') || (*np == '.') ||
(*np == ':') || (*np == '_'))
&& !(set_unsafe_chars && strchr(set_unsafe_chars, *np))) {
*sp = *np;
}
else /* Need to replace the character with a safe one */
*sp = REPLACEMENT_CHAR;
sp++;
np++;
}
*sp = '\0';
if (sp >= name + 6 && !strcmp(sp - 6, ".shtml"))
strcpy(sp - 6, ".html");
return name;
}
static void
create_attachname(char *attachname, int max_len)
{
int i, max_i;
char suffix[8];
max_i = strlen(attachname);
if(max_i >= max_len)
max_i = max_len - 1;
i = max_i;
while( i >= 0 && i > (max_i - (int) sizeof(suffix)) && attachname[i] != '.' )
--i;
if(i >= 0 && attachname[i] == '.')
strncpy(suffix, attachname + i, sizeof(suffix) - 1);
else
suffix[0] = 0;
strncpy(attachname, set_filename_base, max_len - 1);
/* make sure it is a NULL terminated string */
attachname[max_len - 1] = '\0';
strncat(attachname, suffix, max_len - strlen(attachname) - 1);
safe_filename(attachname);
}
/*
** Cross-indexes - adds to a list of replies. If a message is a reply to
** another, the number of the message it's replying to is added to the list.
** This list is searched upon printing.
*/
void crossindex(void)
{
int num, status, maybereply;
struct emailinfo *email;
num = 0;
if(!set_linkquotes)
replylist = NULL;
while (num <= max_msgnum) {
if (!hashnumlookup(num, &email)) {
++num;
continue;
}
status = hashreplynumlookup(email->msgnum,
email->inreplyto, email->subject,
&maybereply);
if (status != -1) {
struct emailinfo *email2;
if (!hashnumlookup(status, &email2)) {
++num;
continue;
}
/* make sure there is no recursion between the message
and reply lookup if a message and its reply-to were
archived in reverse, both messages share the same
subject (regardless of Re), and the message itself was
a reply to a non-archived message. */
if (maybereply && !strcmp (email2->inreplyto, email->msgid)) {
++num;
continue;
}
if (set_linkquotes) {
struct reply *rp;
int found_num = 0;
for (rp = replylist; rp != NULL; rp = rp->next)
if(rp->msgnum == status && rp->frommsgnum == num) {
found_num = 1;
break;
}
if (!found_num && !(maybereply || num <= status))
#ifdef FASTREPLYCODE
replylist = addreply2(replylist, email2, email, maybereply,
&replylist_end);
#else
replylist = addreply(replylist, status, email, maybereply,
&replylist_end);
#endif
}
else {
#ifdef FASTREPLYCODE
replylist = addreply2(replylist, email2, email, maybereply,
&replylist_end);
#else
replylist = addreply(replylist, status, email, maybereply,
&replylist_end);
#endif
}
}
num++;
}
#if DEBUG_THREAD
{
struct reply *r;
r = replylist;
fprintf(stderr, "START of replylist after crossindex\n");
fprintf(stderr, "- msgnum frommsgnum maybereply msgid\n");
while (r != NULL) {
fprintf(stderr, "- %d %d %d '%s'\n",
r->data->msgnum,
r->frommsgnum, r->maybereply, r->data->msgid);
r = r->next;
}
fprintf(stderr, "END of replylist after crossindex\n");
}
#endif
}
/*
** Recursively checks for replies to replies to a message, etc.
** Replies are added to the thread list.
*/
#ifdef FASTREPLYCODE
void crossindexthread2(int num)
{
struct reply *rp;
struct emailinfo *ep;
if(!hashnumlookup(num, &ep)) {
trio_snprintf(errmsg, sizeof(errmsg),
"internal error crossindexthread2 %d", num);
progerr(errmsg);
}
for (rp = ep->replylist; rp != NULL; rp = rp->next) {
if (!(rp->data->flags & USED_THREAD)) {
rp->data->flags |= USED_THREAD;
if (0) fprintf(stderr, "add thread.b %d %d %d\n", num, rp->data->msgnum, rp->msgnum);
threadlist = addreply(threadlist, num, rp->data, 0,
&threadlist_end);
#ifdef FIX_OR_DELETE_ME
/* JK: 2023-05-17: this seems to have been a longtime typo,
it produces memory leaks and didn't have any use in the
thread code. Tentatively correcting it to printthreadlist
and checking for side effects */
printedlist = markasprinted(printedthreadlist, rp->msgnum);
#else
printedthreadlist = markasprinted(printedthreadlist, rp->msgnum);
#endif
crossindexthread2(rp->msgnum);
}
}
}
#else
void crossindexthread2(int num)
{
struct reply *rp;
for (rp = replylist; rp != NULL; rp = rp->next) {
if (!(rp->data->flags & USED_THREAD) && (rp->frommsgnum == num)) {
rp->data->flags |= USED_THREAD;
threadlist = addreply(threadlist, num, rp->data, 0,
&threadlist_end);
#ifdef FIX_OR_DELETE_ME
/* JK: 2023-05-17: this seems to have been a longtime typo,
it produces memory leaks and didn't have any use in the
thread code. Tentatively correcting it to printthreadlist
and checking for side effects */
printedlist = markasprinted(printedthreadlist, rp->msgnum);
#endif
printedthreadlist = markasprinted(printedthreadlist, rp->msgnum);
crossindexthread2(rp->msgnum);
}
}
}
#endif
/*
** First, print out the threads in order by date...
** Each message number is appended to a thread list. Threads and individual
** messages are separated by a -1.
*/
void crossindexthread1(struct header *hp)
{
int isreply;
#ifndef FASTREPLYCODE
struct reply *rp;
#endif
if (hp) {
crossindexthread1(hp->left);
#ifdef FASTREPLYCODE
isreply = hp->data->isreply;
#else
for (isreply = 0, rp = replylist; rp != NULL; rp = rp->next) {
if (rp->msgnum == hp->data->msgnum) {
isreply = 1;
break;
}
}
#endif
/* If this message is not a reply to any other messages then it
* is the first message in a thread. If it hasn't already
* been dealt with, then add it to the thread list, followed by
* any descendants and then the end of thread marker.
*/
if (!isreply && !wasprinted(printedthreadlist, hp->data->msgnum) &&
!(hp->data->flags & USED_THREAD)) {
hp->data->flags |= USED_THREAD;
threadlist = addreply(threadlist, hp->data->msgnum, hp->data,
0, &threadlist_end);
crossindexthread2(hp->data->msgnum);
threadlist = addreply(threadlist, -1, NULL, 0, &threadlist_end);
}
crossindexthread1(hp->right);
}
}
/*
** Grabs the date string from a Date: header. (Y2K OK)
*/
char *getmaildate(char *line)
{
int i;
int len;
char *c;
struct Push buff;
INIT_PUSH(buff);
c = strchr(line, ':');
if (!*(c + 1)
|| ((*(c + 1) == '\n')
|| (*(c + 1) == '\r'))) {
PushString(&buff, NODATE);
RETURN_PUSH(buff);
}
c += 2;
while (*c == ' ' || *c == '\t')
c++;
for (i = 0, len = DATESTRLEN - 1; *c && *c != '\n' && *c != '\r' && i < len; c++)
PushByte(&buff, *c);
RETURN_PUSH(buff);
}
/*
** Grabs the date string from a From article separator. (Y2K OK)
*/
char *getfromdate(char *line)
{
static char tmpdate[DATESTRLEN];
int i;
int len;
char *c = NULL;
for (i = 0; days[i] != NULL &&
((c = strstr(line, days[i])) == NULL); i++);
if (days[i] == NULL)
tmpdate[0] = '\0';
else {
for (i = 0, len = DATESTRLEN - 1; *c && *c != '\n' && *c != '\r' && i < len; c++)
tmpdate[i++] = *c;
tmpdate[i] = '\0';
}
return tmpdate;
}
/*
** Grabs the message ID, like <...> from the Message-ID: header.
*/
char *getid(char *line)
{
int i;
char *c;
struct Push buff;
INIT_PUSH(buff);
if (strrchr(line, '<') == NULL) {
/*
* bozo alert!
* msg-id = "<" addr-spec ">"
* try to recover as best we can
*/
c = strchr(line, ':') + 1; /* we know this exists! */
/* skip spaces before message ID */
while (*c && (*c == ' ' || *c == '\t'))
c++;
}
else
c = strrchr(line, '<') + 1;
for (i = 0; *c && *c != '>' && *c != '\n' && *c != '\r'; c++) {
if (*c == '\\')
continue;
PushByte(&buff, *c);
i++;
}
if (i == 0)
PushString(&buff, "BOZO");
RETURN_PUSH(buff);
}
/*
** Grabs the subject from the Subject: header.
**
** Need to add a table of Re: equivalents (different languages, MUA, etc...)
**
** Returns ALLOCATED string.
*/
char *getsubject(char *line)
{
int i;
int len;
char *c;
char *startp;
char *strip_subject = NULL;
char *postre = NULL;
struct Push buff;
INIT_PUSH(buff);
c = strchr(line, ':');
if (!c)
return NULL;
c += 2;
if (set_stripsubject) {
/* compute a new subject */
strip_subject = replace(c, set_stripsubject, "");
/* point to it */
c = strip_subject;
}
while (isspace(*c))
c++;
startp = c;
for (i = len = 0; c && *c && (*c != '\n') && (*c != '\r'); c++) {
i++;
/* keep track of the max length without trailing white spaces: */
if (!isspace(*c))
len = i;
}
if (isre(startp, &postre)) {
if (!*postre || (*postre == '\n') || (*postre == '\r'))
len = 0;
}
if (!len)
PushString(&buff, NOSUBJECT);
else
PushNString(&buff, startp, len);
if (set_stripsubject && (strip_subject != NULL))
free(strip_subject);
RETURN_PUSH(buff);
}
/*
** Grabs the annotation values given in the annotation user-defined header
**
** annotation_content is set to the value of the content annotation
** annotation_robot is set to the values of the robot annotations
** Returns TRUE if an annotation was found, FALSE otherwise.
*/
static bool
getannotation(char *line, annotation_content_t *annotation_content,
annotation_robot_t *annotation_robot)
{
char *c;
*annotation_content = ANNOTATION_CONTENT_NONE;;
*annotation_robot = ANNOTATION_ROBOT_NONE;
c = strchr(line, ':');
if (!c)
return FALSE;
c++;
while (*c != '\n') {
int len;
char *startp;
while (isspace(*c))
c++;
startp = c;
while (!isspace (*c) && *c != '\n' && *c != '\r' && *c != ',') {
c++;
}
len = (int) (c-startp);
if (len > 0) {
if (!strncasecmp (startp, "deleted", len)) {
*annotation_content = ANNOTATION_CONTENT_DELETED_OTHER;
break;
}
else if (!strncasecmp (startp, "spam", len)) {
*annotation_content = ANNOTATION_CONTENT_DELETED_SPAM;
break;
}
else if (!strncasecmp (startp, "edited", len))
*annotation_content = ANNOTATION_CONTENT_EDITED;
else if (!strncasecmp (startp, "noindex", len))
*annotation_robot |= ANNOTATION_ROBOT_NO_INDEX;
else if (!strncasecmp (startp, "nofollow", len))
*annotation_robot |= ANNOTATION_ROBOT_NO_FOLLOW;
}
if (*c == ',')
c++;
}
/* only return true if at least a valid annotation was found */
return (*annotation_content != ANNOTATION_CONTENT_NONE
|| *annotation_robot != ANNOTATION_ROBOT_NONE);
}
/*
** Grabs the message ID, or date, from the In-reply-to: header.
**
** Maybe I'm confused but....
** What either ? Should it not be consistent and choose to return
** one (the msgid) as the default and fall back to date when a
** msgid cannot be found ?
**
** Who knows what other formats are out there...
**
** In-Reply-To: <1DD9B854E27@everett.pitt.cc.nc.us>
** In-Reply-To: <199709181645.MAA02097@mail.clark.net> from "Marcus J. Ranum" at Sep 18, 97 12:41:40 pm
** In-Reply-To: <199709181645.MAA02097@mail.clark.net> from
** In-Reply-To: "L. Detweiler"'s message of Fri, 04 Feb 94 22:51:22 -0700 <199402050551.WAA16189@longs.lance.colostate.edu>
**
** The message id should always be returned for threading purposes. Mixing
** message-ids and dates just does not allow for proper threading lookups.
**
** Returns ALLOCATED string. */
char *getreply(char *line)
{
char *c;
char *m;
struct Push buff;
INIT_PUSH(buff);
/* Check for blank line */
/*
* Check for line with " from " and " at ". Format of the line is
* <msgid> from "quoted user name" at date-string
*/
if (strstr(line, " from ") != NULL) {
if ((strstr(line, " at ")) != NULL) {
if ((m = strchr(line, '<')) != NULL) {
for (m++; *m && *m != '>' && *m != '\n' && *m != '\r'; m++) {
PushByte(&buff, *m);
}
RETURN_PUSH(buff);
}
}
/*
* If no 'at' the line may be a continued line or a truncated line.
* Both will be picked up later.
*/
}
/*
* Check for line with " message of ". Format of the line is
* "quoted user name"'s message of date-string <msgid>
*/
if ((c = strstr(line, "message of ")) != NULL) {
/*
* Check to see if there is a message ID on the line.
* If not this is a continued line and when you add a readline()
* function that concatenates continuation lines collapsing
* white space, you might want to revisit this...
*/
if ((m = strchr(line, '<')) != NULL) {
for (m++; *m && *m != '>' && *m != '\n' && *m != '\r'; m++) {
PushByte(&buff, *m);
}
RETURN_PUSH(buff);
}
/* Nope... Go for the Date info... Bug... */
c += 11;
while (isspace(*c))
c++;
if (*c == '"')
c++;
for (; *c && *c != '.' && *c != '\n' && *c != '\r'; c++) {
PushByte(&buff, *c);
}
RETURN_PUSH(buff);
}
if ((c = strstr(line, "dated: ")) != NULL) {
c += 7;
for (; *c && *c != '.' && *c != '\n' && *c != '\r'; c++) {
PushByte(&buff, *c);
}
RETURN_PUSH(buff);
}
if ((c = strstr(line, "dated ")) != NULL) {
c += 6;
for (; *c && *c != '.' && *c != '\n' && *c != '\r'; c++) {
PushByte(&buff, *c);
}
RETURN_PUSH(buff);
}
if ((c = strchr(line, '<')) != NULL) {
c++;
for (; *c && *c != '>' && *c != '\n' && *c != '\r'; c++) {
if (*c == '\\')
continue;
PushByte(&buff, *c);
}
RETURN_PUSH(buff);
}
if ((c = strstr(line, "sage of ")) != NULL) {
c += 8;
if (*c == '\"')
c++;
for (; *c && *c != '.' && *c != '\n' && *c != '\r' && *c != 'f'; c++) {
PushByte(&buff, *c);
}
RETURN_PUSH(buff);
}
PushByte(&buff, '\0');
RETURN_PUSH(buff);
}
/* Controls that a header has a space between the colon after the
** header name and the header value and adds it if needed;
** Headers that don't have any value are left as is
**
** Input:
** string: RFC-822 header line to convert
** Output
** converted line (must be freed by caller)
**
** Examples:
** string = "Message-ID:foo"
** will return "Message-ID: foo"
** string = "X-Foo:"
** won't be changed.
*/
static char *
_header_normalize_colon_space (char *string)
{
char *col_ptr;
/* search the first colon in the string */
col_ptr = strchr (string, ':');
if (col_ptr && *col_ptr) {
char *ptr;
/* colon found, advance to next char */
ptr = col_ptr;
ptr++;
/* missing space, add it */
if (*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '\r' && *ptr != '\n') {
struct Push buff;
INIT_PUSH(buff);
PushNString(&buff, string, col_ptr - string);
PushString(&buff, ": ");
PushString(&buff, ptr);
free(string);
string = PUSH_STRING(buff);
}
}
return string;
}
/* Converts an RFC-822 header line to UTF-8. If there's no declared
** charset, it will try to use the Content-Type charset. If it fails,
** it will call the chardet library.
** If the function fails to detect the charset, it will return an
** "(invalid string)" string.
** Input:
** string: RFC-822 header line to convert
** hd_charset: the header that was found when decoding the header
** ct_charset: charset as declared in the Content-Type line
** charsetsave: the last detected charset
** Output
** converted line (must be freed by caller)
** charsetsave: If libchardet detects a chardet that is different from
** the current charsetsave for this message, charsetsave will be
** update to the newly detected charset.
*/
static char *
header_detect_charset_and_convert_to_utf8 (char *string, char *hd_charset, char *ct_charset, char *charsetsave)
{
if ( i18n_is_valid_us_ascii(string) ) {
/* nothing to do, passing thru */
}
/* RFC6532 allows for using UTF-8 as a header value; we make
sure that it is valid UTF-8 */
else if ( !i18n_is_valid_utf8(string) ) {
char header_name[129];
char *header_value;
struct Push pbuf;
/*
** save the header_name:\s
*/
INIT_PUSH(pbuf);
sscanf(string, "%127[^:]", header_name);
PushString(&pbuf, header_name);
header_value = string + strlen(header_name);
/* push the ':' char */
PushByte(&pbuf, *header_value);
header_value++;
/* push the next char after the ':' if it's the space before
* the header value */
if (*header_value == ' ') {
PushByte(&pbuf, *header_value);
header_value++;
}
#if defined HAVE_CHARDET && HAVE_ICONV
{