11 #include <sys/types.h>
14 #include "md5_utils.h"
15 #include "string_utils.h"
16 #include "file_utils.h"
21 static struct mail_list
*mlist
;
23 #define FROM_ADDRESS_VALID (1 << 0)
24 #define REPLY_TO_VALID (1 << 1)
25 #define SUBJECT_VALID (1 << 2)
27 void list_add_from_file(const char *filename
)
35 struct mail_entry entry
;
37 /* Check if is a regular file */
38 if (file_type(filename
)) {
42 fh
= fopen(filename
, "r");
46 while (fgets(buf
, sizeof(buf
), fh
)) {
47 if (!strncmp(buf
, "From:", 5)) {
48 if (!rfc5322_extract_address(entry
.addr
, buf
, 1024)) {
49 done
|= FROM_ADDRESS_VALID
;
51 } else if (!strncmp(buf
, "Reply-to:", 9)) {
52 if (done
& FROM_ADDRESS_VALID
)
55 if (!rfc5322_extract_address(entry
.addr
, buf
, 1024)) {
56 done
|= REPLY_TO_VALID
;
58 } else if (!strncmp(buf
, "Subject:", 8)) {
59 rfc5322_extract_subject(entry
.subj
, buf
, 1024);
60 done
|= SUBJECT_VALID
;
63 printf("[SKIP] ------- %s", buf
);
67 /* Exits loop if all fields are valid */
68 if (((done
& FROM_ADDRESS_VALID
) || (done
& REPLY_TO_VALID
)) &&
69 (done
& SUBJECT_VALID
)) {
76 strncpy(entry
.name
, "name", 1024);
77 md5_calc_sum(entry
.addr
, entry
.addr_md5
);
79 rc
= list_add_entry(mlist
, &entry
);
82 fprintf(stderr
, "%s - No free slot for %s\n", __func__
, filename
);
84 fprintf(stderr
, "%s - File %s added to list\n", __func__
, filename
);
89 int main(int argc
, char **argv
)
92 char old_path
[PATH_MAX
];
99 path
= "/home/roberto/Maildir/cur/";
104 if (!getcwd(old_path
, PATH_MAX
)) {
105 fprintf(stderr
, "%s - GETCWD: %s\n", __func__
, strerror(errno
));
109 if (chdir(path
) < 0) {
110 fprintf(stderr
, "%s - CHDIR: %s\n", __func__
, strerror(errno
));
114 n
= dir_entries(path
);
116 fprintf(stderr
, "%s is empty!\n", path
);
122 mlist
= list_create(n
, MESSAGE_LIMIT
);
128 fprintf(stderr
, "Try to process %lu files\n", (unsigned long) mlist
->n
);
131 rc
= scan_dir(path
, list_add_from_file
);
137 if (chdir(old_path
) < 0) {
138 fprintf(stderr
, "%s - Cannot restore path %s!\n", __func__
, strerror(errno
));