Diffstat (limited to 'kmicromail/libetpan/generic/maildirstorage.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kmicromail/libetpan/generic/maildirstorage.c | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/kmicromail/libetpan/generic/maildirstorage.c b/kmicromail/libetpan/generic/maildirstorage.c new file mode 100644 index 0000000..7e6b461 --- a/dev/null +++ b/kmicromail/libetpan/generic/maildirstorage.c | |||
@@ -0,0 +1,192 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "mailstorage.h" | ||
37 | |||
38 | #include "mail.h" | ||
39 | #include "mailmessage.h" | ||
40 | #include "maildirdriver.h" | ||
41 | #include "maildirdriver_cached.h" | ||
42 | #include "maildriver.h" | ||
43 | |||
44 | #include <stdlib.h> | ||
45 | #include <string.h> | ||
46 | |||
47 | /* maildir storage */ | ||
48 | |||
49 | static int maildir_mailstorage_connect(struct mailstorage * storage); | ||
50 | static int | ||
51 | maildir_mailstorage_get_folder_session(struct mailstorage * storage, | ||
52 | char * pathname, mailsession ** result); | ||
53 | static void maildir_mailstorage_uninitialize(struct mailstorage * storage); | ||
54 | |||
55 | static mailstorage_driver maildir_mailstorage_driver = { | ||
56 | .sto_name = "maildir", | ||
57 | .sto_connect = maildir_mailstorage_connect, | ||
58 | .sto_get_folder_session = maildir_mailstorage_get_folder_session, | ||
59 | .sto_uninitialize = maildir_mailstorage_uninitialize, | ||
60 | }; | ||
61 | |||
62 | int maildir_mailstorage_init(struct mailstorage * storage, | ||
63 | char * md_pathname, int md_cached, | ||
64 | char * md_cache_directory, char * md_flags_directory) | ||
65 | { | ||
66 | struct maildir_mailstorage * maildir_storage; | ||
67 | |||
68 | maildir_storage = malloc(sizeof(struct maildir_mailstorage)); | ||
69 | if (maildir_storage == NULL) | ||
70 | goto err; | ||
71 | |||
72 | maildir_storage->md_pathname = strdup(md_pathname); | ||
73 | if (maildir_storage->md_pathname == NULL) | ||
74 | goto free; | ||
75 | |||
76 | maildir_storage->md_cached = md_cached; | ||
77 | |||
78 | if (md_cached && (md_cache_directory != NULL) && | ||
79 | (md_flags_directory != NULL)) { | ||
80 | maildir_storage->md_cache_directory = strdup(md_cache_directory); | ||
81 | if (maildir_storage->md_cache_directory == NULL) | ||
82 | goto free_pathname; | ||
83 | |||
84 | maildir_storage->md_flags_directory = strdup(md_flags_directory); | ||
85 | if (maildir_storage->md_flags_directory == NULL) | ||
86 | goto free_cache_directory; | ||
87 | } | ||
88 | else { | ||
89 | maildir_storage->md_cached = FALSE; | ||
90 | maildir_storage->md_cache_directory = NULL; | ||
91 | maildir_storage->md_flags_directory = NULL; | ||
92 | } | ||
93 | |||
94 | storage->sto_data = maildir_storage; | ||
95 | storage->sto_driver = &maildir_mailstorage_driver; | ||
96 | |||
97 | return MAIL_NO_ERROR; | ||
98 | |||
99 | free_cache_directory: | ||
100 | free(maildir_storage->md_cache_directory); | ||
101 | free_pathname: | ||
102 | free(maildir_storage->md_pathname); | ||
103 | free: | ||
104 | free(maildir_storage); | ||
105 | err: | ||
106 | return MAIL_ERROR_MEMORY; | ||
107 | } | ||
108 | |||
109 | static void maildir_mailstorage_uninitialize(struct mailstorage * storage) | ||
110 | { | ||
111 | struct maildir_mailstorage * maildir_storage; | ||
112 | |||
113 | maildir_storage = storage->sto_data; | ||
114 | if (maildir_storage->md_flags_directory != NULL) | ||
115 | free(maildir_storage->md_flags_directory); | ||
116 | if (maildir_storage->md_cache_directory != NULL) | ||
117 | free(maildir_storage->md_cache_directory); | ||
118 | free(maildir_storage->md_pathname); | ||
119 | free(maildir_storage); | ||
120 | |||
121 | storage->sto_data = NULL; | ||
122 | } | ||
123 | |||
124 | static int maildir_mailstorage_connect(struct mailstorage * storage) | ||
125 | { | ||
126 | struct maildir_mailstorage * maildir_storage; | ||
127 | mailsession_driver * driver; | ||
128 | int r; | ||
129 | int res; | ||
130 | mailsession * session; | ||
131 | |||
132 | maildir_storage = storage->sto_data; | ||
133 | |||
134 | if (maildir_storage->md_cached) | ||
135 | driver = maildir_cached_session_driver; | ||
136 | else | ||
137 | driver = maildir_session_driver; | ||
138 | |||
139 | session = mailsession_new(driver); | ||
140 | if (session == NULL) { | ||
141 | res = MAIL_ERROR_MEMORY; | ||
142 | goto err; | ||
143 | } | ||
144 | |||
145 | if (maildir_storage->md_cached) { | ||
146 | r = mailsession_parameters(session, | ||
147 | MAILDIRDRIVER_CACHED_SET_CACHE_DIRECTORY, | ||
148 | maildir_storage->md_cache_directory); | ||
149 | if (r != MAIL_NO_ERROR) { | ||
150 | res = r; | ||
151 | goto free; | ||
152 | } | ||
153 | |||
154 | r = mailsession_parameters(session, | ||
155 | MAILDIRDRIVER_CACHED_SET_FLAGS_DIRECTORY, | ||
156 | maildir_storage->md_flags_directory); | ||
157 | if (r != MAIL_NO_ERROR) { | ||
158 | res = r; | ||
159 | goto free; | ||
160 | } | ||
161 | } | ||
162 | |||
163 | r = mailsession_connect_path(session, maildir_storage->md_pathname); | ||
164 | switch (r) { | ||
165 | case MAIL_NO_ERROR_NON_AUTHENTICATED: | ||
166 | case MAIL_NO_ERROR_AUTHENTICATED: | ||
167 | case MAIL_NO_ERROR: | ||
168 | break; | ||
169 | default: | ||
170 | res = r; | ||
171 | goto free; | ||
172 | } | ||
173 | |||
174 | storage->sto_session = session; | ||
175 | |||
176 | return MAIL_NO_ERROR; | ||
177 | |||
178 | free: | ||
179 | mailsession_free(session); | ||
180 | err: | ||
181 | return res; | ||
182 | } | ||
183 | |||
184 | static int | ||
185 | maildir_mailstorage_get_folder_session(struct mailstorage * storage, | ||
186 | char * pathname, mailsession ** result) | ||
187 | { | ||
188 | * result = storage->sto_session; | ||
189 | |||
190 | return MAIL_NO_ERROR; | ||
191 | } | ||
192 | |||