-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
2131 lines (1930 loc) · 61.1 KB
/
main.cpp
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
#define _LARGEFILE64_SOURCE
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/mman.h>
#ifdef __APPLE__
#include <sys/syslimits.h>
#endif
using namespace std;
#define MAX_TOCC_INDEX 128
//#define _DEBUG
#ifdef _DEBUG
#define DEBUG(x) \
{ \
x \
fflush(stdout); \
} while(0);
#else
#define DEBUG(x)
#endif
extern "C" {
#include "io.h"
#include "mem.h"
#include "val.h"
}
#include "array.h"
#include "br.h"
#include "funcs.h"
#include "main.h"
void show_usage(void)
{
printf("Usage: mboxstats -i inputfile -o outputfile [-a] [-w] [-m] [-h]\n");
printf("-a show all data (not just the top 10)\n");
printf("-n x show top 'x' (default is 10)\n");
printf("-l lock mailbox\n");
printf("-x XML output\n");
printf("-z omit XML header\n");
printf("-y omit empty entries\n");
printf("-s x hide e-mail addresses [1=if name is know, 2=always]\n");
printf("-c also calculate number of bits information per byte\n");
printf("-m parameter for -i is a maildir\n");
printf("-p per-user statistics\n");
printf("-k abbreviate byte-amounts\n");
printf("-w count word-frequency (slows the program considerably down!)\n");
printf("-Q only for XML output! adds an url for from, to, cc and subject\n");
printf(" add '__REPLACE__' in the string which will get the from/etc.\n");
printf("-V show version\n");
printf("-h this message\n");
}
char * to_xml_replace(char *in)
{
static char dummy[4096]; /* yes, I know */
int len = strlen(in);
int index, oindex=0;
for(index=0; index<len; index++)
{
if (in[index] == '<' ||
in[index] == '>' ||
in[index] == '&' ||
in[index] == ' ')
{
sprintf(&dummy[oindex], "&#%d;", in[index]);
oindex = strlen(dummy);
}
else
dummy[oindex++] = in[index];
}
dummy[oindex] = 0x00;
return dummy;
}
char * hide_email_address(char *in, char hide)
{
/* really kids: you should not return pointers to static
* buffers
*/
static char dummy[4096];
if (hide == 0)
{
return in;
}
if (in[0] == '"')
strcpy(dummy, in + 1);
else
strcpy(dummy, in);
char *lt = strchr(dummy, '<');
/* this will break on systems where NULL != 0
* and believe: those systems exist
*/
while(lt > dummy && (*lt == '<' || *lt == ' ' || *lt == '"'))
{
*lt = 0x00;
--lt;
}
if ((hide == 1 && lt != NULL) || hide == 2)
{
char *at = strchr(dummy, '@');
if (at)
{
strcpy(at + 1, "xxx");
}
}
return dummy;
}
char * to_xml_tag(char *in)
{
static char dummy[4096];
int len = strlen(in);
int index, oindex=0;
if (isdigit(in[0]))
{
strcpy(dummy, "address-");
oindex = strlen(dummy);
}
for(index=0; index<len; index++)
{
if (isalnum(in[index]) || in[index] == '-' || in[index] == '_' || in[index] == '.')
dummy[oindex++] = in[index];
else if (in[index] == ' ')
dummy[oindex++] = '-';
}
dummy[oindex] = 0x00;
return dummy;
}
char *url_escape(char *in)
{
int len = strlen(in);
int loop, index = 0;
char *out = (char *)mymalloc(len * 3 + 1, "url escape");
for(loop=0; loop<len; loop++)
{
if (isalnum(in[loop]))
out[index++] = in[loop];
else
{
sprintf(&out[index], "%%%02x", in[loop]);
index += 3;
}
}
out[index] = 0x00;
return out;
}
char * emit_url(char *searcher_in, char *string)
{
char *buffer, *repl, *searcher;
if (!searcher_in)
return mystrdup("");
buffer = (char *)mymalloc(strlen(searcher_in) + 128 + strlen(string) * 5 + 1, "url");
searcher = mystrdup(searcher_in);
repl = strstr(searcher, "__REPLACE__");
*repl = 0x00;
sprintf(buffer, "<url>%s%s%s</url>\n", searcher, url_escape(string), &repl[11]);
free(searcher);
return buffer;
}
char *b2kb(long unsigned int bytes, char abb)
{
/* FIXME: check for NULL and free() it after use (down there) */
char *buffer = (char *)mymalloc(64, "b2kb output");
if (!buffer)
{
fprintf(stderr, "out of memory\n");
exit(1);
}
if (abb)
{
if (bytes > (1024 * 1024 * 1024))
sprintf(buffer, "%ldGB", (bytes -1 + (1024 * 1024 * 1024)) / (1024 * 1024 * 1024));
else if (bytes > (1024 * 1024))
sprintf(buffer, "%ldMB", (bytes -1 + (1024 * 1024)) / (1024 * 1024));
else if (bytes > 1024)
sprintf(buffer, "%ldKB", (bytes -1 + 1024) / 1024);
else
sprintf(buffer, "%ld", bytes);
}
else
{
sprintf(buffer, "%ld", bytes);
}
return buffer;
}
int main(int argc, char *argv[])
{
/* statistics variables */
long unsigned int Cyear[3000]; // in the year 3000 this program will fail
long unsigned int Cyearbytes[3000];
long unsigned int Cmonth[12+1], Cday[31+1], Cwday[7+1], Chour[24], Ctotal=0, total=0;
long unsigned int Cmonthbytes[12+1], Cdaybytes[31+1], Cwdaybytes[7+1], Chourbytes[24];
long unsigned int importance_low = 0, importance_normal = 0, importance_high = 0;
long unsigned int total_lines = 0, total_header = 0;
long unsigned int total_bytes = 0, total_header_bytes = 0;
long unsigned int total_line_length = 0;
char **MessageIDs = NULL;
time_t *MessageIDst = NULL;
int nMessageIDs = 0;
struct tm first_tm;
memset(&first_tm, 0x00, sizeof(first_tm));
first_tm.tm_sec = 59;
first_tm.tm_min = 59;
first_tm.tm_hour = 23;
first_tm.tm_mday = 31;
first_tm.tm_mon = 12 - 1;
first_tm.tm_year = 2035 - 1900;
time_t first_ts = mktime(&first_tm);
time_t last_ts = 0;
double total_bits = 0.0;
char xml=0, xml_header=1;
char he=0;
char omit_empty=0;
char abbr=0;
int top_n = 10;
double avg_spam_score = 0.0;
int n_avg_spam_score = 0;
char bits_per_byte = 0;
char lock_file = 0;
char calc_quote = 0;
/* misc variables */
DIR *dir = NULL;
FILE *fh;
char empty_line = 0;
int c;
char *input = NULL, *output = NULL;
char all = 0, cnt_words = 0;
char peruser = 0;
char mbox = 0; /* input is a mailbox-folder */
int lockfd = -1;
time_t start, now;
double fsize=0.0; /* double to prevent overflows */
/* msg-storage array */
char **msg = NULL;
int msg_n = 0, msg_len = 0;
char *searcher = NULL;
memset(Cyear, 0x00, sizeof(Cyear));
memset(Cmonth, 0x00, sizeof(Cmonth));
memset(Cday, 0x00, sizeof(Cday));
memset(Cwday, 0x00, sizeof(Cwday));
memset(Chour, 0x00, sizeof(Chour));
memset(Cyearbytes, 0x00, sizeof(Cyearbytes));
memset(Cmonthbytes, 0x00, sizeof(Cmonthbytes));
memset(Cdaybytes, 0x00, sizeof(Cdaybytes));
memset(Cwdaybytes, 0x00, sizeof(Cwdaybytes));
memset(Chourbytes, 0x00, sizeof(Chourbytes));
if (argc == 1)
{
show_usage();
return 1;
}
/* parse parameters */
while((c = getopt(argc, argv, "i:o:l:awmphxzs:ykn:cqQ:")) != -1)
{
switch(c)
{
case 'i':
input = optarg;
break;
case 'o':
output = optarg;
break;
case 'a':
all = 1;
break;
case 'q':
calc_quote = 1;
break;
case 'l':
lock_file = 1;
break;
case 'c':
bits_per_byte = 1;
break;
case 'w':
cnt_words = 1;
break;
case 'm':
mbox = 1;
break;
case 'k':
abbr = 1;
break;
case 'p':
peruser = 1;
break;
case 'x':
xml=1;
break;
case 'z':
xml_header=0;
break;
case 's':
he = atoi(optarg);
if (he < 0 || he > 2)
{
fprintf(stderr, "-s parameter expects either 0, 1 or 2\n");
return 1;
}
break;
case 'y':
omit_empty=1;
break;
case 'n':
top_n = atoi(optarg);
if (top_n < 1)
{
fprintf(stderr, "number of elements to show must be at least 1 (and not %d)\n", top_n);
return 1;
}
break;
case 'Q':
searcher = optarg;
if (strstr(searcher, "__REPLACE__") == NULL)
{
fprintf(stderr, "Search-engine replace string misses '__REPLACE__'\n");
return 1;
}
break;
case 'V':
printf("mboxstats v" VERSION ", (C) 2003-2005 by [email protected]\n");
break;
case 'h':
show_usage();
exit(0);
default:
show_usage();
exit(0);
}
}
/* lock file */
if (mbox == 0 && input != NULL && lock_file == 1)
{
lockfd = lockfile(input);
if (lockfd == -1)
{
fprintf(stderr, "Cannot create lockfile!\n");
return 1;
}
}
array from(13, 4); /* 0=hits, 1=bytes, 2=time, 3=n_lines/msg, 4=wday, 5=dmonth, 6=month, 7=linelength, 8=spam score, 9=# spam */
/* 10=n_replies_to + 11=replytime, 12=quote percentage */
/* subarray: 0 = useragent, 1=from (recv most msgs
* from), 2=to (sent most msgs to), 3=subject */
array subject(5); /* 0=hits, 1=bytes, 2=time, 3=firstmsg, 4=lastmsg */
array to(3); /* 0=hits, 1=bytes, 2=time */
array cc(3); /* 0=hits, 1=bytes, 2=time */
array words(1);
array tld(2); /* top level domains (0=hits, 1=bytes) */
array org(2); /* organization (0=hits, 1=bytes) */
array useragent(2); /* user-agent (0=hits, 1=bytes) */
array tz(2); /* timezone 0=hits, 1=bytes */
array bd(2); /* bussiest day 0=hits, 1=bytes */
array urls(1);
int n_is_reply = 0;
long int n_resp_time = 0;
int global_is_reply = 0;
int pgp_signed = 0;
long long int total_att_size = 0;
int n_att = 0;
time(&start);
for(;;)
{
int fd;
char *file;
struct dirent *de = NULL;
char path[PATH_MAX];
DEBUG(printf("start\n");)
/* not a mailbox-dir? then treat as file */
if (mbox == 0)
{
file = input;
}
else
{
/* mailbox-dir */
/* open directory */
if (!dir)
{
/* create path */
if (mbox == 1)
sprintf(path, "%s/cur", input);
else
sprintf(path, "%s/new", input);
if (output) printf("Processing path: %s\n", path);
/* open directory */
dir = opendir(path);
if (!dir)
{
fprintf(stderr, "Could not open directory %s!\n", path);
if (mbox == 1)
{
mbox = 2;
continue;
}
else
{
break;
}
}
}
/* try to fetch a file */
de = readdir(dir);
if (!de) /* last file in dir, close dir */
{
if (closedir(dir) == -1)
{
fprintf(stderr, "error closing director!\n");
break;
}
dir = NULL;
/* both boxes done? exit */
if (mbox == 2)
break;
/* just did the cur-box, continue with the new-box */
mbox = 2;
/* restart the loop */
continue;
}
/* create path */
if (mbox == 1)
sprintf(path, "%s/cur/%s", input, de -> d_name);
else
sprintf(path, "%s/new/%s", input, de -> d_name);
file = path;
/* check for directories (skip them) */
struct stat64 st;
if (stat64(file, &st) == -1) /* file probably went away */
{
if (errno == ENOENT)
continue;
fprintf(stderr, "problem fetching parameters on file %s!\n", file);
return 1;
}
/* is dir? then skip */
if (S_ISDIR(st.st_mode))
continue;
}
DEBUG(printf("open file %s\n", file);)
/* open the file */
if (file == NULL)
fd = 0;
else
fd = open64(file, O_RDONLY);
if (fd == -1)
{
if (errno == ENOENT) /* file deleted? */
{
fprintf(stderr, "file %s is not there!\n", file);
break; /* continue */
}
/* otherwhise: error situation */
fprintf(stderr, "error opening file %s\n", input);
return 1;
}
/* get filesize */
if (fd != 0)
{
struct stat64 buf;
if (fstat64(fd, &buf) == -1)
{
fprintf(stderr, "error getting filesize: %s\n", strerror(errno));
return 1;
}
fsize = (double)buf.st_size;
}
/* start up buffered reader */
buffered_reader bf(fd);
for(;;)
{
char *line;
DEBUG(printf("read_line\n");)
line = bf.read_line();
/* grow array if neccessary */
DEBUG(printf("resize\n");)
if (resize((void **)&msg, msg_n, &msg_len, sizeof(char *)) == -1)
{
fprintf(stderr, "Memory allocation problem!\n");
break;
}
if (line != NULL && strlen(line) == 0) /* ignore empty lines */
{
DEBUG(printf("ignore empty line\n");)
empty_line = 1;
msg[msg_n++] = NULL;
free(line);
}
else if (line == NULL || (strncmp(line, "From ", 5) == 0 && empty_line == 1)) /* finished reading a message? */
{
int year=0, month=0, day=0, wday=0, hour=0, minute=0, second=0;
int from_index = -1, to_index[MAX_TOCC_INDEX], n_to_index=0, subject_index = -1;
char *to_str[MAX_TOCC_INDEX];
int cc_index[MAX_TOCC_INDEX], n_cc_index=0;
int loop, header_len = 0;
unsigned long int msg_bytes = 0;
char header = 1;
char *fromfld = NULL;
char *ua = NULL;
char *subj = NULL;
int add_org = -1;
double spam_score = 0.0;
char is_spam = 0;
int cur_tz = -1;
int cur_tld = -1;
int cur_bd = -1;
int cur_line_length = 0;
time_t ts_t = (time_t)0;
char *cur_msgid = NULL;
char *in_reply_to = NULL;
char *timezone = NULL;
char *boundary = NULL;
char msg_state = MS_TEXT;
char interesting_attachment = 0;
int quoted_lines = 0, original_lines = 0;
char signature = 0;
free(line);
empty_line = 0;
for(loop=0; loop<128; loop++)
to_index[loop] = -1;
/* keep track of total number of messages */
total++;
/* end of message, process */
DEBUG(printf("end of msg, process\n");)
for(loop=0; loop<msg_n; loop++)
{
if (likely(msg[loop]))
{
msg_bytes += strlen(msg[loop]);
if (bits_per_byte)
total_bits += calc_nbits_in_data((unsigned char *)msg[loop], strlen(msg[loop]));
}
if (msg[loop] == NULL && header) /* end of header */
{
/* keep track of total of lines */
total_lines += (msg_n - header_len);
total_header += header_len;
header = 0;
}
else if (header) /* header */
{
char *bound_dum;
header_len++; /* length of header */
total_header_bytes += strlen(msg[loop]);
DEBUG(printf("do header\n");)
if (strncmp(msg[loop], "From:", 5) == 0)
{
fromfld = stripstring(&msg[loop][6]);
char *dummy = mystrdup(fromfld);
from_index = from.addstring(dummy, 1);
char *dot = strrchr(dummy, '.');
if (dot)
{
char *end = strchr(dot, '>');
if (end)
*end = 0x00;
cur_tld = tld.addstring(dot + 1);
}
free(dummy);
}
else if (strncmp(msg[loop], "Message-ID:", 11) == 0)
{
char *dummy = strchr(&msg[loop][12], '<');
if (dummy)
{
cur_msgid = mystrdup(dummy);
dummy = strchr(cur_msgid, '>');
if (dummy) *dummy = 0x00;
}
}
else if (strncmp(msg[loop], "In-Reply-To:", 12) == 0)
{
char *dummy = strchr(&msg[loop][12], '<');
if (dummy)
{
in_reply_to = mystrdup(dummy);
dummy = strchr(in_reply_to, '>');
if (dummy) *dummy = 0x00;
}
global_is_reply++;
}
else if (strncmp(msg[loop], "Subject:", 8) == 0)
{
subj = stripstring(&msg[loop][9]);
subject_index = subject.addstring(subj);
}
else if (strncmp(msg[loop], "To:", 3) == 0)
{
char *dummy = stripstring(&msg[loop][4]);
char *komma = strchr(dummy, ',');
char *pnt = dummy;
while (komma)
{
*komma = 0x00;
to_index[n_to_index] = to.addstring(pnt, 1);
to_str[n_to_index++] = mystrdup(pnt);
pnt = komma + 1;
while(*pnt == ' ') pnt++;
komma = strchr(pnt, ',');
}
if (strlen(pnt) > 0)
{
to_index[n_to_index] = to.addstring(pnt, 1);
to_str[n_to_index++] = mystrdup(pnt);
}
free(dummy);
}
else if (strncasecmp(msg[loop], "CC:", 3) == 0)
{
char *dummy = stripstring(&msg[loop][4]);
char *komma = strchr(dummy, ',');
char *pnt = dummy;
while (komma)
{
*komma = 0x00;
cc_index[n_cc_index++] = cc.addstring(pnt, 1);
pnt = komma + 1;
while(*pnt == ' ') pnt++;
komma = strchr(pnt, ',');
}
if (strlen(pnt) > 0)
cc_index[n_cc_index++] = cc.addstring(pnt, 1);
free(dummy);
}
else if (strncmp(msg[loop], "Importance:", 11) == 0)
{
char *dummy = stripstring(&msg[loop][12]);
if (strcasecmp(dummy, "Low") == 0)
importance_low++;
else if (strcasecmp(dummy, "Normal") == 0)
importance_normal++;
else if (strcasecmp(dummy, "High") == 0)
importance_high++;
free(dummy);
}
else if (strncmp(msg[loop], "Organization:", 13) == 0)
{
char *dummy = stripstring(&msg[loop][14]);
add_org = org.addstring(dummy, 0);
free(dummy);
}
else if (strncmp(msg[loop], "User-Agent:", 11) == 0)
{
char *dummy = stripstring(&msg[loop][12]);
ua = mystrdup(&msg[loop][12]);
free(dummy);
}
else if (strncmp(msg[loop], "X-Mailer:", 9) == 0)
{
char *dummy = stripstring(&msg[loop][10]);
ua = mystrdup(&msg[loop][10]);
free(dummy);
}
else if (strncmp(msg[loop], "X-Spam-Status:", 14) == 0)
{
/* X-Spam-Status: No, score=0.0 required=5.0 tests=HTML_MESSAGE autolearn=failed */
char *dummy = strstr(&msg[loop][14], "score=");
if (dummy)
{
spam_score = atof(dummy + 6);
avg_spam_score += spam_score;
n_avg_spam_score++;
char *req = strstr(&msg[loop][14], "required=");
if (req)
{
if (spam_score >= atof(req + 9))
is_spam = 1;
}
}
}
else if ((bound_dum = strstr(msg[loop], " boundary=")) != NULL)
{
char *dummy = strchr(bound_dum, '"');
if (dummy)
{
int len = strlen(dummy + 1);
boundary = (char *)mymalloc(len + 1, "boundary");
memcpy(boundary, dummy + 1, len);
boundary[len] = 0x00;
dummy = strchr(boundary, '"');
if (dummy)
*dummy = 0x00;
}
else
{
bound_dum += 10;
int len = strlen(bound_dum);
boundary = (char *)mymalloc(len + 1, "boundary");
memcpy(boundary, bound_dum, len);
boundary[len] = 0x00;
dummy = strchr(boundary, ' ');
if (dummy) *dummy = 0x00;
dummy = strchr(boundary, '\t');
if (dummy) *dummy = 0x00;
dummy = strchr(boundary, ';');
if (dummy) *dummy = 0x00;
}
}
else if (strncmp(msg[loop], "Date:", 5) == 0)
{
char *dummy = stripstring(&msg[loop][6]);
if (datestringtofields(dummy, year, month, day, wday, hour, minute, second, &timezone))
{
if (month > 12 || day > 31 || wday > 7 || hour > 23 || year < 0)
{
if (output) printf("Invalid date-line: %s\n", &msg[loop][6]);
month = day = wday = hour = 0;
}
else
{
struct tm ts;
Cyear[year]++;
Cmonth[month]++;
Cday[day]++;
Cwday[wday]++;
Chour[hour]++;
Ctotal++;
memset(&ts, 0x00, sizeof(ts));
ts.tm_sec = second;
ts.tm_min = minute;
ts.tm_hour = hour;
ts.tm_mday = day;
ts.tm_mon = month - 1;
ts.tm_year = year - 1900;
ts_t = mktime(&ts);
if (ts_t != (time_t)-1 && ts_t < first_ts)
first_ts = ts_t;
if (ts_t != (time_t)-1 && ts_t > last_ts)
last_ts = ts_t;
if (ts_t == (time_t)-1)
ts_t = 0;
/* bussiest day */
char buffer[128];
sprintf(buffer, "%04d-%02d-%02d", year, month, day);
cur_bd = bd.addstring(buffer);
}
DEBUG(printf("add tz\n");)
if (timezone)
cur_tz = tz.addstring(timezone, 1);
DEBUG(printf("tz done\n");)
}
free(dummy);
}
}
else if (!signature)
{
if (msg[loop] != NULL) /* message-text */
{
int len = strlen(msg[loop]);
DEBUG(printf("do msg-text\n");)
char *pnt;
cur_line_length += len;
pnt = msg[loop];
while((pnt = strstr(pnt, "http://")))
{
char *end = strchr(pnt, ' ');
if (end)
*end = 0x00;
else
end = &pnt[strlen(pnt)];
urls.addstring(pnt);
pnt = end + 1;
}
pnt = msg[loop];
while((pnt = strstr(pnt, "ftp://")))
{
char *end = strchr(pnt, ' ');
if (end)
*end = 0x00;
else
end = &pnt[strlen(pnt)];
urls.addstring(pnt);
pnt = end + 1;
}
if (strcmp(msg[loop], "--") == 0 || strcmp(msg[loop], "-- ") == 0)
{
signature = 1;
}
if (strncmp(msg[loop], "-----BEGIN PGP SIGNED MESSAGE-----", 34) == 0 ||
strncmp(msg[loop], "-----BEGIN PGP SIGNATURE-----", 29) == 0)
{
pgp_signed++;
}
/* determine if its quoted text or not */
pnt = msg[loop];
while(*pnt == ' ' || *pnt == '\t') pnt++;
if (*pnt == '>')
quoted_lines++;
else
original_lines++;
/* start of mime thing? */
if (boundary != NULL && msg[loop][0] == '-' && msg[loop][1] == '-' && strcmp(&msg[loop][2], boundary) == 0)
{
if (msg_state == MS_DATA_LOOK)
{
// do anything?
}
msg_state = MS_HEADER;
interesting_attachment = 0;
}
else if (msg_state == MS_HEADER)
{
// is a file-attachment?
if (strstr(msg[loop], "filename="))
{
interesting_attachment = 1;
n_att++;
}
}
else if (msg_state == MS_DATA_LOOK)
{
total_att_size += len;
}
if (cnt_words)
{
char *dummy = msg[loop];
for(;;)
{
char *end, stop=0;
while(toupper(*dummy) < 'A' && toupper(*dummy) > 'Z' && *dummy != 0x00)
dummy++;
if (!*dummy)
break;
end = dummy;
while(toupper(*end) >= 'A' && toupper(*end) <= 'Z' && *end != 0x00)
{
*end = tolower(*end);
end++;
}
if (*end == 0x00)
stop = 1;
else
*end = 0x00;
if ((end-dummy) > 1 && (end-dummy) < 11)
words.addstring(dummy);
if (stop)
break;
dummy = end + 1;
}
}
}
else
{
if (interesting_attachment)
msg_state = MS_DATA_LOOK;
else
msg_state = MS_DATA;
}
}
else /* signature */
{
}
}
DEBUG(printf("msg processed, overall stats\n");)
total_bytes += msg_bytes;
if (year >= 0 && year < 3000)
Cyearbytes[year] += msg_bytes;
Cmonthbytes[month] += msg_bytes;
Cdaybytes[day] += msg_bytes;
Cwdaybytes[wday] += msg_bytes;
Chourbytes[hour] += msg_bytes;
/* keep track of compose-time of each message */
if (ts_t != (time_t)0 && cur_msgid != NULL)
{
MessageIDs = (char **)myrealloc(MessageIDs, (nMessageIDs + 1) * sizeof(char *), "MessageIDs");
MessageIDs[nMessageIDs] = cur_msgid;
MessageIDst = (time_t *)myrealloc(MessageIDst, (nMessageIDs + 1) * sizeof(time_t), "MessageIDs time_t");
if (timezone)
MessageIDst[nMessageIDs++] = ts_t - (86400 * atoi(timezone) / 100);
else
MessageIDst[nMessageIDs++] = ts_t ;
}
if (add_org != -1)
org.addcounter(add_org, 1, msg_bytes);
if (cur_tz != -1)
tz.addcounter(cur_tz, 1, msg_bytes);
if (cur_tld != -1)
tld.addcounter(cur_tld, 1, msg_bytes);