Integrate test_header in test_view_mail and various coding style modifications
[rmail.git] / src / utils / test_pop3_list.c
blob21a97ad936a00311b516158abc5300b986c29158
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"
20 #include "pop3.h"
22 static struct mail_list *mlist;
24 void list_add_from_file(const char *filename)
26 FILE *fh;
27 int done = 0;
29 size_t n = 0;
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)) {
37 return;
40 fh = fopen(filename, "r");
41 if (!fh)
42 return;
44 while (fgets(buf, sizeof(buf), fh) && done != 3) {
45 if (!strncmp(buf, "From:", 5)) {
46 rfc5322_extract_address(from, buf, sizeof(from));
47 done |= 1;
48 } else if (!strncmp(buf, "Subject:", 5)) {
49 rfc5322_extract_subject(subject, buf, sizeof(subject));
50 done |= 2;
51 } else {
52 #if 0
53 printf("[SKIP] ------- %s", buf);
54 #endif
57 fclose(fh);
59 if (done && mlist) {
60 if (mlist->nfree < MESSAGE_LIMIT) {
61 n = mlist->nfree;
62 /* Update List */
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);
68 mlist->nfree += 1;
69 } else {
70 fprintf(stderr, "%s - No free slot for %s\n", __func__, filename);
75 size_t dir_entries(const char *path)
77 DIR *dir;
78 struct dirent *de;
79 size_t count = 0;
81 if (!path)
82 return 0;
84 dir = opendir(path);
85 if (!dir) {
86 fprintf(stderr, "%s - %s: %s\n", __func__, path, strerror(errno));
87 return 0;
90 do {
91 de = readdir(dir);
92 if (de) {
93 if (file_type(de->d_name) == 0)
94 count++;
95 continue;
98 /* de is null on error or EOD */
99 if (errno) {
100 fprintf(stderr, "%s - %s!\n", __func__, strerror(errno));
101 break;
103 } while (de);
105 closedir(dir);
107 return count;
110 int main(__attribute__((unused)) int argc,
111 __attribute__((unused)) char **argv)
113 int rc;
115 #if 0
116 char old_path[PATH_MAX];
117 const char *path;
118 #endif
120 struct pop3_mailbox mb;
121 unsigned int n;
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)
130 return -1;
132 if (pop3_authenticate(&mb) < 0) {
133 fprintf(stderr, "POP3 Authentication failed\n");
134 goto app_out;
137 /* Retrieve the number of messages present on the mailbox */
138 if (pop3_stat(&mb) < 0)
139 fprintf(stderr, "Error fetch messages\n");
140 else
141 fprintf(stderr, "%u Message in maildrop\n", mb.messages);
143 if (mb.messages == 0) {
144 return 0;
147 /* Init mail list */
148 mlist = list_create(mb.messages, MESSAGE_LIMIT);
149 if (!mlist) {
150 rc = -1;
151 goto app_out;
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);
159 if (rc < 0) {
160 fprintf(stderr, "%s - TOP err n=%u\n", __func__, n);
161 break;
164 } else {
165 fprintf(stderr, "%s - mbox.messages = %u ", __func__, mb.messages);
168 #if 0
169 rc = scan_dir(path, list_add_from_file);
170 #endif
172 list_print(mlist);
173 list_free(mlist);
175 app_out:
176 pop3_disconnect(&mb);
177 return rc;