author | zautrix <zautrix> | 2005-03-18 20:17:03 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2005-03-18 20:17:03 (UTC) |
commit | 9e549686b23b6dffdcbd09c9b10dc2cb795fbcdf (patch) (unidiff) | |
tree | 2528e6cc740225ca0f47d5ac8ff70f7d3bb10621 /libetpan/src/driver/implementation/mh/mhstorage.c | |
parent | 9319998f20f03dcc217fbb39656755dc65226276 (diff) | |
download | kdepimpi-9e549686b23b6dffdcbd09c9b10dc2cb795fbcdf.zip kdepimpi-9e549686b23b6dffdcbd09c9b10dc2cb795fbcdf.tar.gz kdepimpi-9e549686b23b6dffdcbd09c9b10dc2cb795fbcdf.tar.bz2 |
Initial revision
Diffstat (limited to 'libetpan/src/driver/implementation/mh/mhstorage.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libetpan/src/driver/implementation/mh/mhstorage.c | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/libetpan/src/driver/implementation/mh/mhstorage.c b/libetpan/src/driver/implementation/mh/mhstorage.c new file mode 100644 index 0000000..e7fc2f0 --- a/dev/null +++ b/libetpan/src/driver/implementation/mh/mhstorage.c | |||
@@ -0,0 +1,192 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 "mhstorage.h" | ||
37 | |||
38 | #include "mhdriver.h" | ||
39 | #include "mhdriver_cached.h" | ||
40 | #include "mail.h" | ||
41 | |||
42 | #include <stdlib.h> | ||
43 | #include <string.h> | ||
44 | |||
45 | /* mh storage */ | ||
46 | |||
47 | static int mh_mailstorage_connect(struct mailstorage * storage); | ||
48 | static int mh_mailstorage_get_folder_session(struct mailstorage * storage, | ||
49 | char * pathname, mailsession ** result); | ||
50 | static void mh_mailstorage_uninitialize(struct mailstorage * storage); | ||
51 | |||
52 | static mailstorage_driver mh_mailstorage_driver = { | ||
53 | .sto_name = "mh", | ||
54 | .sto_connect = mh_mailstorage_connect, | ||
55 | .sto_get_folder_session = mh_mailstorage_get_folder_session, | ||
56 | .sto_uninitialize = mh_mailstorage_uninitialize, | ||
57 | }; | ||
58 | |||
59 | int mh_mailstorage_init(struct mailstorage * storage, | ||
60 | char * mh_pathname, int mh_cached, | ||
61 | char * mh_cache_directory, char * mh_flags_directory) | ||
62 | { | ||
63 | struct mh_mailstorage * mh_storage; | ||
64 | |||
65 | mh_storage = malloc(sizeof(* mh_storage)); | ||
66 | if (mh_storage == NULL) | ||
67 | goto err; | ||
68 | |||
69 | mh_storage->mh_pathname = strdup(mh_pathname); | ||
70 | if (mh_storage->mh_pathname == NULL) | ||
71 | goto free; | ||
72 | |||
73 | mh_storage->mh_cached = mh_cached; | ||
74 | |||
75 | if (mh_cached && (mh_cache_directory != NULL) && | ||
76 | (mh_flags_directory != NULL)) { | ||
77 | mh_storage->mh_cache_directory = strdup(mh_cache_directory); | ||
78 | if (mh_storage->mh_cache_directory == NULL) | ||
79 | goto free_pathname; | ||
80 | mh_storage->mh_flags_directory = strdup(mh_flags_directory); | ||
81 | if (mh_storage->mh_flags_directory == NULL) | ||
82 | goto free_cache_directory; | ||
83 | } | ||
84 | else { | ||
85 | mh_storage->mh_cached = FALSE; | ||
86 | mh_storage->mh_cache_directory = NULL; | ||
87 | mh_storage->mh_flags_directory = NULL; | ||
88 | } | ||
89 | |||
90 | storage->sto_data = mh_storage; | ||
91 | storage->sto_driver = &mh_mailstorage_driver; | ||
92 | |||
93 | return MAIL_NO_ERROR; | ||
94 | |||
95 | free_cache_directory: | ||
96 | free(mh_storage->mh_cache_directory); | ||
97 | free_pathname: | ||
98 | free(mh_storage->mh_pathname); | ||
99 | free: | ||
100 | free(mh_storage); | ||
101 | err: | ||
102 | return MAIL_ERROR_MEMORY; | ||
103 | } | ||
104 | |||
105 | static void mh_mailstorage_uninitialize(struct mailstorage * storage) | ||
106 | { | ||
107 | struct mh_mailstorage * mh_storage; | ||
108 | |||
109 | mh_storage = storage->sto_data; | ||
110 | if (mh_storage->mh_flags_directory != NULL) | ||
111 | free(mh_storage->mh_flags_directory); | ||
112 | if (mh_storage->mh_cache_directory != NULL) | ||
113 | free(mh_storage->mh_cache_directory); | ||
114 | free(mh_storage->mh_pathname); | ||
115 | free(mh_storage); | ||
116 | |||
117 | storage->sto_data = NULL; | ||
118 | } | ||
119 | |||
120 | static int mh_mailstorage_connect(struct mailstorage * storage) | ||
121 | { | ||
122 | struct mh_mailstorage * mh_storage; | ||
123 | mailsession_driver * driver; | ||
124 | int r; | ||
125 | int res; | ||
126 | mailsession * session; | ||
127 | |||
128 | mh_storage = storage->sto_data; | ||
129 | |||
130 | if (mh_storage->mh_cached) | ||
131 | driver = mh_cached_session_driver; | ||
132 | else | ||
133 | driver = mh_session_driver; | ||
134 | |||
135 | session = mailsession_new(driver); | ||
136 | if (session == NULL) { | ||
137 | res = MAIL_ERROR_MEMORY; | ||
138 | goto err; | ||
139 | } | ||
140 | |||
141 | if (mh_storage->mh_cached) { | ||
142 | r = mailsession_parameters(session, | ||
143 | MHDRIVER_CACHED_SET_CACHE_DIRECTORY, | ||
144 | mh_storage->mh_cache_directory); | ||
145 | if (r != MAIL_NO_ERROR) { | ||
146 | res = r; | ||
147 | goto free; | ||
148 | } | ||
149 | |||
150 | r = mailsession_parameters(session, | ||
151 | MHDRIVER_CACHED_SET_FLAGS_DIRECTORY, | ||
152 | mh_storage->mh_flags_directory); | ||
153 | if (r != MAIL_NO_ERROR) { | ||
154 | res = r; | ||
155 | goto free; | ||
156 | } | ||
157 | } | ||
158 | |||
159 | r = mailsession_connect_path(session, mh_storage->mh_pathname); | ||
160 | switch (r) { | ||
161 | case MAIL_NO_ERROR_NON_AUTHENTICATED: | ||
162 | case MAIL_NO_ERROR_AUTHENTICATED: | ||
163 | case MAIL_NO_ERROR: | ||
164 | break; | ||
165 | default: | ||
166 | res = r; | ||
167 | goto free; | ||
168 | } | ||
169 | |||
170 | storage->sto_session = session; | ||
171 | |||
172 | return MAIL_NO_ERROR; | ||
173 | |||
174 | free: | ||
175 | mailsession_free(session); | ||
176 | err: | ||
177 | return res; | ||
178 | } | ||
179 | |||
180 | static int mh_mailstorage_get_folder_session(struct mailstorage * storage, | ||
181 | char * pathname, mailsession ** result) | ||
182 | { | ||
183 | int r; | ||
184 | |||
185 | r = mailsession_select_folder(storage->sto_session, pathname); | ||
186 | if (r != MAIL_NO_ERROR) | ||
187 | return r; | ||
188 | |||
189 | * result = storage->sto_session; | ||
190 | |||
191 | return MAIL_NO_ERROR; | ||
192 | } | ||