Commit 0908e210 0908e2108247ae700d37138d970eff061251408e by Sergey Poznyakoff

Auto-detection of MH format

1 parent 54759f99
...@@ -21,20 +21,113 @@ ...@@ -21,20 +21,113 @@
21 21
22 #ifdef ENABLE_MH 22 #ifdef ENABLE_MH
23 23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
24 #include <errno.h> 29 #include <errno.h>
30 #include <unistd.h>
31 #include <dirent.h>
25 32
33 #include <url0.h>
26 #include <folder0.h> 34 #include <folder0.h>
27 #include <registrar0.h> 35 #include <registrar0.h>
28 36
37 static int
38 _mh_folder_init (folder_t folder ARG_UNUSED)
39 {
40 return 0;
41 }
42
43 /* Check if NAME is a valid MH message name */
44 static int
45 mh_message_name_p (const char *name)
46 {
47 for ( ; *name; name++)
48 if (!isdigit (*name))
49 return 0;
50 return 1;
51 }
52
53 /* Check if directory NAME is a valid MH folder directory */
54 static int
55 mh_dir_p (const char *name)
56 {
57 DIR *dir;
58 struct dirent *entry;
59 int result = 0;
60
61 dir = opendir (name);
62 if (!dir)
63 return 1; /* Maybe yes */
64
65 while (!result && (entry = readdir (dir)))
66 {
67 switch (entry->d_name[0])
68 {
69 case '.':
70 result = strcmp (entry->d_name, ".mh_sequences") == 0;
71 break;
72
73 case ',':
74 result = mh_message_name_p (entry->d_name + 1);
75 break;
76
77 default:
78 result = mh_message_name_p (entry->d_name);
79 break;
80 }
81 }
82 closedir (dir);
83 return result;
84 }
85
86 static int
87 _mh_is_scheme (record_t record, const char *url)
88 {
89 if (!url || !record->scheme)
90 return 0;
91
92 if (strncmp (record->scheme, url, strlen (record->scheme)) == 0)
93 return 1;
94
95 if (strncmp (MU_PATH_SCHEME, url, MU_PATH_SCHEME_LEN) == 0)
96 {
97 /* Attemp auto-detection */
98 struct stat st;
99
100 if (stat (url, &st) < 0)
101 return 1; /* mailbox_open will complain */
102
103 if (!S_ISDIR (st.st_mode))
104 return 0;
105
106 return mh_dir_p (url);
107 }
108
109 return 0;
110 }
111
112 /*
113 MH url
114 mh:path
115 */
116 static int
117 _mh_url_init (url_t url)
118 {
119 return amd_url_init (url, MU_MH_SCHEME);
120 }
121
29 static struct _record _mh_record = 122 static struct _record _mh_record =
30 { 123 {
31 MU_MH_SCHEME, 124 MU_MH_SCHEME,
32 _url_mh_init, /* Url init. */ 125 _mh_url_init, /* Url init. */
33 _mailbox_mh_init, /* Mailbox init. */ 126 _mailbox_mh_init, /* Mailbox init. */
34 NULL, /* Mailer init. */ 127 NULL, /* Mailer init. */
35 _folder_mh_init, /* Folder init. */ 128 _mh_folder_init, /* Folder init. */
36 NULL, /* back pointer. */ 129 NULL, /* back pointer. */
37 NULL, /* _is_scheme method. */ 130 _mh_is_scheme, /* _is_scheme method. */
38 NULL, /* _get_url method. */ 131 NULL, /* _get_url method. */
39 NULL, /* _get_mailbox method. */ 132 NULL, /* _get_mailbox method. */
40 NULL, /* _get_mailer method. */ 133 NULL, /* _get_mailer method. */
...@@ -42,12 +135,6 @@ static struct _record _mh_record = ...@@ -42,12 +135,6 @@ static struct _record _mh_record =
42 }; 135 };
43 record_t mh_record = &_mh_record; 136 record_t mh_record = &_mh_record;
44 137
45 int
46 _folder_mh_init (folder_t folder ARG_UNUSED)
47 {
48 return 0;
49 }
50
51 #else 138 #else
52 #include <stdio.h> 139 #include <stdio.h>
53 #include <registrar0.h> 140 #include <registrar0.h>
......