Init mime struct
[rmail.git] / src / utils / test_list.c
blobed2ef7717b33beea52e0c753faf18150f46e83f8
1 #define _BSD_SOURCE
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <dirent.h>
8 #include <errno.h>
9 #include <fcntl.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
14 #include "md5_utils.h"
15 #include "string_utils.h"
16 #include "file_utils.h"
17 #include "rfc5322.h"
19 #include "list.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)
29 FILE *fh;
30 int done = 0;
31 int rc;
33 char buf[1024];
35 struct mail_entry entry;
37 /* Check if is a regular file */
38 if (file_type(filename)) {
39 return;
42 fh = fopen(filename, "r");
43 if (!fh)
44 return;
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)
53 continue;
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;
61 } else {
62 #if 0
63 printf("[SKIP] ------- %s", buf);
64 #endif
67 /* Exits loop if all fields are valid */
68 if (((done & FROM_ADDRESS_VALID) || (done & REPLY_TO_VALID)) &&
69 (done & SUBJECT_VALID)) {
70 break;
73 fclose(fh);
75 if (done && mlist) {
76 strncpy(entry.name, "name", 1024);
77 md5_calc_sum(entry.addr, entry.addr_md5);
79 rc = list_add_entry(mlist, &entry);
81 if (rc)
82 fprintf(stderr, "%s - No free slot for %s\n", __func__, filename);
83 else
84 fprintf(stderr, "%s - File %s added to list\n", __func__, filename);
89 int main(int argc, char **argv)
91 int rc;
92 char old_path[PATH_MAX];
94 const char *path;
96 size_t n;
98 if (argc < 2) {
99 path = "/home/roberto/Maildir/cur/";
100 } else {
101 path = argv[1];
104 if (!getcwd(old_path, PATH_MAX)) {
105 fprintf(stderr, "%s - GETCWD: %s\n", __func__, strerror(errno));
106 return -1;
109 if (chdir(path) < 0) {
110 fprintf(stderr, "%s - CHDIR: %s\n", __func__, strerror(errno));
111 return -1;
114 n = dir_entries(path);
115 if (n < 1) {
116 fprintf(stderr, "%s is empty!\n", path);
117 rc = -1;
118 goto out_1;
121 /* Init mail list */
122 mlist = list_create(n, MESSAGE_LIMIT);
123 if (!mlist) {
124 rc = -1;
125 goto out_1;
128 fprintf(stderr, "Try to process %lu files\n", (unsigned long) mlist->n);
131 rc = scan_dir(path, list_add_from_file);
133 list_print(mlist);
134 list_free(mlist);
136 out_1:
137 if (chdir(old_path) < 0) {
138 fprintf(stderr, "%s - Cannot restore path %s!\n", __func__, strerror(errno));
141 return rc;