11 #include <sys/types.h>
14 #include "md5_utils.h"
15 #include "string_utils.h"
16 #include "file_utils.h"
22 static struct mail_list
*mlist
;
24 void list_add_from_file(const char *filename
)
31 char buf
[POP3_BUFSIZE
];
32 char from
[POP3_BUFSIZE
];
33 char subject
[POP3_BUFSIZE
]; /* 998 + CR + LF */
35 /* Check if is a regular file */
36 if (file_type(filename
)) {
40 fh
= fopen(filename
, "r");
44 while (fgets(buf
, sizeof(buf
), fh
) && done
!= 3) {
45 if (!strncmp(buf
, "From:", 5)) {
46 rfc5322_extract_address(from
, buf
, sizeof(from
));
48 } else if (!strncmp(buf
, "Subject:", 5)) {
49 rfc5322_extract_subject(subject
, buf
, sizeof(subject
));
53 printf("[SKIP] ------- %s", buf
);
60 if (mlist
->nfree
< MESSAGE_LIMIT
) {
63 fprintf(stderr
, "%s from file %s to slot %lu\n", __func__
, filename
, (unsigned long) n
);
64 strncpy(mlist
->list
[n
].name
, "name", 1024);
65 strncpy(mlist
->list
[n
].addr
, from
, 1024);
66 strncpy(mlist
->list
[n
].subj
, subject
, 1024);
67 md5_calc_sum(from
, mlist
->list
[n
].addr_md5
);
70 fprintf(stderr
, "%s - No free slot for %s\n", __func__
, filename
);
75 size_t dir_entries(const char *path
)
86 fprintf(stderr
, "%s - %s: %s\n", __func__
, path
, strerror(errno
));
93 if (file_type(de
->d_name
) == 0)
98 /* de is null on error or EOD */
100 fprintf(stderr
, "%s - %s!\n", __func__
, strerror(errno
));
110 int main(__attribute__((unused
)) int argc
,
111 __attribute__((unused
)) char **argv
)
116 char old_path
[PATH_MAX
];
120 struct pop3_mailbox mb
;
123 mb
.host
.name
= DEFAULT_SERVER
;
124 mb
.host
.port
= DEFAULT_PORT
;
125 mb
.auth
.user
= DEFAULT_USER
;
126 mb
.auth
.pass
= DEFAULT_PASS
;
128 fprintf(stderr
, "Connecting to POP3 server %s...\n", mb
.host
.name
);
129 if (pop3_connect(&mb
) < 0)
132 if (pop3_authenticate(&mb
) < 0) {
133 fprintf(stderr
, "POP3 Authentication failed\n");
137 /* Retrieve the number of messages present on the mailbox */
138 if (pop3_stat(&mb
) < 0)
139 fprintf(stderr
, "Error fetch messages\n");
141 fprintf(stderr
, "%u Message in maildrop\n", mb
.messages
);
143 if (mb
.messages
== 0) {
148 mlist
= list_create(mb
.messages
, MESSAGE_LIMIT
);
154 fprintf(stderr
, "Try to process %lu messages\n", (unsigned long) mlist
->n
);
156 if (mb
.messages
> 0) {
157 for (n
= 1; n
<= mb
.messages
; n
++) {
158 rc
= pop3_top(&mb
, n
);
160 fprintf(stderr
, "%s - TOP err n=%u\n", __func__
, n
);
165 fprintf(stderr
, "%s - mbox.messages = %u ", __func__
, mb
.messages
);
169 rc
= scan_dir(path
, list_add_from_file
);
176 pop3_disconnect(&mb
);