Commit 5a2f11bb 5a2f11bb9703297dc790c61d63407e587551fd05 by Sean 'Shaleh' Perry

added beginnings of MH handler

1 parent 72318c65
1 2000-01-19 Sean 'Shaleh' Perry
2
3 * mailbox/mbx_mh.c: new file
4 no header, no error checking, no hable englais
5
1 2000-01-17 Sean 'Shaleh' Perry 6 2000-01-17 Sean 'Shaleh' Perry
2 7
3 * examples/from.c: remove extraneous copyright notice 8 * examples/from.c: remove extraneous copyright notice
......
1 /* GNU mailutils - a suite of utilities for electronic mail
2 * Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Library Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <dirent.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 typedef int _url_mh_type;
31
32 struct mailbox_type _mailbox_mh_type =
33 {
34 "MH"
35 (int)&_url_mh_type, &_url_mh_type,
36 mailbox_mh_init, mailbox_mh_destroy
37 };
38
39 typedef struct _mh_data
40 {
41 time_t mtime; /* used for checking if mailbox was updated */
42 } mh_data;
43
44 static int mh_sequence(const char *name);
45
46 int
47 mailbox_mh_init (mailbox_t *pmbox, const char *name)
48 {
49 mailbox_t mbox;
50 mh_data *data;
51
52 mbox = malloc(sizeof(*mbox));
53 data = malloc(sizeof(mh_data));
54 mbox->name = malloc(strlen(name) + 1);
55 strcpy(mbox->name, name);
56 mbox->data = data;
57 *pmbox = mbox;
58
59 return 0;
60 }
61
62 void
63 mailbox_mh_destroy (mailbox_t *pmbox)
64 {
65 free((*pmbox)->data);
66 free((*pmbox)->name);
67 free(*pmbox);
68 pmbox = NULL;
69 }
70
71 /* FIXME: handle create once the design is ready */
72
73 /* mh_scan actually does all the dirty work */
74 static int
75 mh_open (mailbox_t mbox, int flags)
76 {
77 struct stat st;
78 mh_data *data;
79
80 if (stat(mbox->name, &st) == -1)
81 return errno;
82
83 if (! S_ISDIR(st.st_mode))
84 return EINVAL; /* mailbox is not a directory, thus it is also not MH */
85
86 data = mbox->data;
87 /* FIXME: does mtime change when the dir has a new file added? */
88 data->mtime = st.st_mtime;
89
90 return 0; /* everything is fine */
91 }
92
93 static int
94 mh_close (mailbox_t mbox)
95 {
96 mh_data *data;
97
98 data = mbox->data;
99
100 /* FIXME: implementation */
101 return 0;
102 }
103
104 static int
105 mh_scan (mailbox_t mbox, size_t *msgs)
106 {
107 struct stat st;
108 DIR *maildir;
109 struct dirent *entry;
110 mh_data *data;
111 unsigned int count = 0;
112
113 data = mbox->data;
114
115 maildir = opendir(mbox->name);
116
117 while((entry = readdir(maildir)) != NULL) {
118 if(strcmp(".", entry->d_name) == 0 ||
119 strcmp("..", entry->d_name) == 0)
120 continue;
121 /* FIXME: handle this MH extension */
122 if(entry->d_name[0] == '+')
123 continue;
124 /* FIXME: decide what to do with messages marked for removal */
125 if(entry->d_name[0] == ',')
126 continue;
127 if(entry->d_name[0] == '.') {
128 if(strcmp(".mh_sequences", entry->d_name))
129 continue; /* spurious file beginning w/ '.' */
130 else { /* MH info in .mh_sequences */
131 /* FIXME: parse this file */
132 }
133 }
134 if(mh_sequence(entry->d_name)) {
135 /* FIXME: parse file */
136 count++;
137 }
138 }
139 closedir(maildir);
140
141 stat(mbox->name, &st);
142 data->mtime = st.st_mtime;
143
144 if(msgs)
145 *msgs = count;
146 return 0;
147 }
148
149 /*
150 * Local atoi()
151 * created this to guarantee that name is only digits, normal atoi allows
152 * whitespace
153 */
154 static int
155 mh_sequence(const char *name)
156 {
157 char *sequence;
158 int i;
159
160 for(i = 0, sequence = name; *sequence; sequence++) {
161 if(!isdigit(*sequence))
162 return 0;
163 i *= 10;
164 i += (*sequence - '0');
165 }
166 return i;
167 }
...@@ -245,6 +245,7 @@ mailbox_unix_init (mailbox_t *pmbox, const char *name) ...@@ -245,6 +245,7 @@ mailbox_unix_init (mailbox_t *pmbox, const char *name)
245 return ENOMEM; /* errno set by calloc() */ 245 return ENOMEM; /* errno set by calloc() */
246 } 246 }
247 247
248 /* FIXME: what does the next comment mean? */
248 /* binary copy of the function */ 249 /* binary copy of the function */
249 *mbox = unixmbox; 250 *mbox = unixmbox;
250 251
...@@ -381,7 +382,7 @@ mailbox_unix_open (mailbox_t mbox, int flags) ...@@ -381,7 +382,7 @@ mailbox_unix_open (mailbox_t mbox, int flags)
381 /* handle CREAT with care, not to follow symlinks */ 382 /* handle CREAT with care, not to follow symlinks */
382 if (flags & MU_MB_CREAT) 383 if (flags & MU_MB_CREAT)
383 { 384 {
384 /* firts see if the file already exists */ 385 /* first see if the file already exists */
385 fd = open(mbox->name, flg); 386 fd = open(mbox->name, flg);
386 if (fd == -1) 387 if (fd == -1)
387 { 388 {
...@@ -527,6 +528,7 @@ mailbox_unix_get_name (mailbox_t mbox, int *id, char *name, ...@@ -527,6 +528,7 @@ mailbox_unix_get_name (mailbox_t mbox, int *id, char *name,
527 /* passwd */ 528 /* passwd */
528 /* We don't care */ 529 /* We don't care */
529 530
531 /* FIXME: hmmm, all of the files i check lack a timezone field */
530 /* FIXME: a little weak, we should do full reconnaissance of the 532 /* FIXME: a little weak, we should do full reconnaissance of the
531 the "From " header : 533 the "From " header :
532 From email weekday month day time timezone year 534 From email weekday month day time timezone year
...@@ -603,6 +605,7 @@ mailbox_unix_scan (mailbox_t mbox, size_t *msgs) ...@@ -603,6 +605,7 @@ mailbox_unix_scan (mailbox_t mbox, size_t *msgs)
603 struct stat st; 605 struct stat st;
604 606
605 mailbox_unix_ilock (mbox, MU_MB_WRLOCK); 607 mailbox_unix_ilock (mbox, MU_MB_WRLOCK);
608 /* FIXME: please clarify next comment */
606 /* FIXME: I should also block signals since 609 /* FIXME: I should also block signals since
607 We can affor to be intr */ 610 We can affor to be intr */
608 flockfile (mud->file); 611 flockfile (mud->file);
...@@ -622,7 +625,7 @@ mailbox_unix_scan (mailbox_t mbox, size_t *msgs) ...@@ -622,7 +625,7 @@ mailbox_unix_scan (mailbox_t mbox, size_t *msgs)
622 /* header */ 625 /* header */
623 if ((header && mailbox_unix_is_from (buf))) 626 if ((header && mailbox_unix_is_from (buf)))
624 { 627 {
625 /* FIXME: What happen if some mailer violates the rfc8222 and the 628 /* FIXME: What happen if some mailer violates the rfc822 and the
626 "From " field contains a NULL byte */ 629 "From " field contains a NULL byte */
627 int over = strlen (buf); 630 int over = strlen (buf);
628 count++; 631 count++;
......