Diffstat (limited to 'kmicromail/libetpan/generic/nntpstorage.c') (more/less context) (show whitespace changes)
-rw-r--r-- | kmicromail/libetpan/generic/nntpstorage.c | 267 |
1 files changed, 267 insertions, 0 deletions
diff --git a/kmicromail/libetpan/generic/nntpstorage.c b/kmicromail/libetpan/generic/nntpstorage.c new file mode 100644 index 0000000..5ba333b --- a/dev/null +++ b/kmicromail/libetpan/generic/nntpstorage.c | |||
@@ -0,0 +1,267 @@ | |||
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 "nntpstorage.h" | ||
37 | |||
38 | #include <stdlib.h> | ||
39 | #include <string.h> | ||
40 | |||
41 | #include "maildriver.h" | ||
42 | #include "nntpdriver.h" | ||
43 | #include "nntpdriver_cached.h" | ||
44 | #include "mailstorage_tools.h" | ||
45 | #include "mail.h" | ||
46 | |||
47 | /* nntp storage */ | ||
48 | |||
49 | #define NNTP_DEFAULT_PORT 119 | ||
50 | #define NNTPS_DEFAULT_PORT 563 | ||
51 | |||
52 | static int nntp_mailstorage_connect(struct mailstorage * storage); | ||
53 | static int nntp_mailstorage_get_folder_session(struct mailstorage * storage, | ||
54 | char * pathname, mailsession ** result); | ||
55 | static void nntp_mailstorage_uninitialize(struct mailstorage * storage); | ||
56 | |||
57 | static mailstorage_driver nntp_mailstorage_driver = { | ||
58 | .sto_name = "nntp", | ||
59 | .sto_connect = nntp_mailstorage_connect, | ||
60 | .sto_get_folder_session = nntp_mailstorage_get_folder_session, | ||
61 | .sto_uninitialize = nntp_mailstorage_uninitialize, | ||
62 | }; | ||
63 | |||
64 | int nntp_mailstorage_init(struct mailstorage * storage, | ||
65 | char * nn_servername, uint16_t nn_port, | ||
66 | char * nn_command, | ||
67 | int nn_connection_type, int nn_auth_type, | ||
68 | char * nn_login, char * nn_password, | ||
69 | int nn_cached, char * nn_cache_directory, char * nn_flags_directory) | ||
70 | { | ||
71 | struct nntp_mailstorage * nntp_storage; | ||
72 | int res; | ||
73 | |||
74 | nntp_storage = malloc(sizeof(struct nntp_mailstorage)); | ||
75 | if (nntp_storage == NULL) { | ||
76 | res = MAIL_ERROR_MEMORY; | ||
77 | goto err; | ||
78 | } | ||
79 | |||
80 | nntp_storage->nntp_servername = strdup(nn_servername); | ||
81 | if (nntp_storage->nntp_servername == NULL) { | ||
82 | res = MAIL_ERROR_MEMORY; | ||
83 | goto free; | ||
84 | } | ||
85 | |||
86 | nntp_storage->nntp_connection_type = nn_connection_type; | ||
87 | |||
88 | if (nn_port == 0) { | ||
89 | switch (nn_connection_type) { | ||
90 | case CONNECTION_TYPE_PLAIN: | ||
91 | case CONNECTION_TYPE_COMMAND: | ||
92 | nn_port = NNTP_DEFAULT_PORT; | ||
93 | break; | ||
94 | |||
95 | case CONNECTION_TYPE_TLS: | ||
96 | case CONNECTION_TYPE_COMMAND_TLS: | ||
97 | nn_port = NNTPS_DEFAULT_PORT; | ||
98 | break; | ||
99 | |||
100 | default: | ||
101 | res = MAIL_ERROR_INVAL; | ||
102 | goto free_servername; | ||
103 | } | ||
104 | } | ||
105 | |||
106 | nntp_storage->nntp_port = nn_port; | ||
107 | |||
108 | if (nn_command != NULL) { | ||
109 | nntp_storage->nntp_command = strdup(nn_command); | ||
110 | if (nntp_storage->nntp_command == NULL) { | ||
111 | res = MAIL_ERROR_MEMORY; | ||
112 | goto free_servername; | ||
113 | } | ||
114 | } | ||
115 | else | ||
116 | nntp_storage->nntp_command = NULL; | ||
117 | |||
118 | nntp_storage->nntp_auth_type = nn_auth_type; | ||
119 | |||
120 | if (nn_login != NULL) { | ||
121 | nntp_storage->nntp_login = strdup(nn_login); | ||
122 | if (nntp_storage->nntp_login == NULL) { | ||
123 | res = MAIL_ERROR_MEMORY; | ||
124 | goto free_command; | ||
125 | } | ||
126 | } | ||
127 | else | ||
128 | nntp_storage->nntp_login = NULL; | ||
129 | |||
130 | if (nn_password != NULL) { | ||
131 | nntp_storage->nntp_password = strdup(nn_password); | ||
132 | if (nntp_storage->nntp_password == NULL) { | ||
133 | res = MAIL_ERROR_MEMORY; | ||
134 | goto free_login; | ||
135 | } | ||
136 | } | ||
137 | else | ||
138 | nntp_storage->nntp_password = NULL; | ||
139 | |||
140 | nntp_storage->nntp_cached = nn_cached; | ||
141 | |||
142 | if (nn_cached && (nn_cache_directory != NULL) && | ||
143 | (nn_flags_directory != NULL)) { | ||
144 | nntp_storage->nntp_cache_directory = strdup(nn_cache_directory); | ||
145 | if (nntp_storage->nntp_cache_directory == NULL) { | ||
146 | res = MAIL_ERROR_MEMORY; | ||
147 | goto free_password; | ||
148 | } | ||
149 | nntp_storage->nntp_flags_directory = strdup(nn_flags_directory); | ||
150 | if (nntp_storage->nntp_flags_directory == NULL) { | ||
151 | res = MAIL_ERROR_MEMORY; | ||
152 | goto free_cache_directory; | ||
153 | } | ||
154 | } | ||
155 | else { | ||
156 | nntp_storage->nntp_cached = FALSE; | ||
157 | nntp_storage->nntp_cache_directory = NULL; | ||
158 | nntp_storage->nntp_flags_directory = NULL; | ||
159 | } | ||
160 | |||
161 | storage->sto_data = nntp_storage; | ||
162 | storage->sto_driver = &nntp_mailstorage_driver; | ||
163 | |||
164 | return MAIL_NO_ERROR; | ||
165 | |||
166 | free_cache_directory: | ||
167 | free(nntp_storage->nntp_cache_directory); | ||
168 | free_password: | ||
169 | free(nntp_storage->nntp_password); | ||
170 | free_login: | ||
171 | free(nntp_storage->nntp_login); | ||
172 | free_command: | ||
173 | free(nn_command); | ||
174 | free_servername: | ||
175 | free(nntp_storage->nntp_servername); | ||
176 | free: | ||
177 | free(nntp_storage); | ||
178 | err: | ||
179 | return res; | ||
180 | } | ||
181 | |||
182 | static void nntp_mailstorage_uninitialize(struct mailstorage * storage) | ||
183 | { | ||
184 | struct nntp_mailstorage * nntp_storage; | ||
185 | |||
186 | nntp_storage = storage->sto_data; | ||
187 | |||
188 | if (nntp_storage->nntp_flags_directory != NULL) | ||
189 | free(nntp_storage->nntp_flags_directory); | ||
190 | if (nntp_storage->nntp_cache_directory != NULL) | ||
191 | free(nntp_storage->nntp_cache_directory); | ||
192 | if (nntp_storage->nntp_password != NULL) | ||
193 | free(nntp_storage->nntp_password); | ||
194 | if (nntp_storage->nntp_login != NULL) | ||
195 | free(nntp_storage->nntp_login); | ||
196 | if (nntp_storage->nntp_command != NULL) | ||
197 | free(nntp_storage->nntp_command); | ||
198 | free(nntp_storage->nntp_servername); | ||
199 | free(nntp_storage); | ||
200 | |||
201 | storage->sto_data = NULL; | ||
202 | } | ||
203 | |||
204 | static int nntp_mailstorage_connect(struct mailstorage * storage) | ||
205 | { | ||
206 | struct nntp_mailstorage * nntp_storage; | ||
207 | mailsession_driver * driver; | ||
208 | int r; | ||
209 | int res; | ||
210 | mailsession * session; | ||
211 | |||
212 | nntp_storage = storage->sto_data; | ||
213 | |||
214 | if (nntp_storage->nntp_cached) | ||
215 | driver = nntp_cached_session_driver; | ||
216 | else | ||
217 | driver = nntp_session_driver; | ||
218 | |||
219 | r = mailstorage_generic_connect(driver, | ||
220 | nntp_storage->nntp_servername, | ||
221 | nntp_storage->nntp_port, nntp_storage->nntp_command, | ||
222 | nntp_storage->nntp_connection_type, | ||
223 | NNTPDRIVER_CACHED_SET_CACHE_DIRECTORY, | ||
224 | nntp_storage->nntp_cache_directory, | ||
225 | NNTPDRIVER_CACHED_SET_FLAGS_DIRECTORY, | ||
226 | nntp_storage->nntp_flags_directory, | ||
227 | &session); | ||
228 | switch (r) { | ||
229 | case MAIL_NO_ERROR_NON_AUTHENTICATED: | ||
230 | case MAIL_NO_ERROR_AUTHENTICATED: | ||
231 | case MAIL_NO_ERROR: | ||
232 | break; | ||
233 | default: | ||
234 | res = r; | ||
235 | goto err; | ||
236 | } | ||
237 | |||
238 | r = mailstorage_generic_auth(session, r, | ||
239 | nntp_storage->nntp_connection_type, | ||
240 | nntp_storage->nntp_login, | ||
241 | nntp_storage->nntp_password); | ||
242 | if (r != MAIL_NO_ERROR) { | ||
243 | res = r; | ||
244 | goto free; | ||
245 | } | ||
246 | |||
247 | storage->sto_session = session; | ||
248 | |||
249 | return MAIL_NO_ERROR; | ||
250 | |||
251 | free: | ||
252 | mailsession_free(session); | ||
253 | err: | ||
254 | return res; | ||
255 | } | ||
256 | |||
257 | static int nntp_mailstorage_get_folder_session(struct mailstorage * storage, | ||
258 | char * pathname, mailsession ** result) | ||
259 | { | ||
260 | int r; | ||
261 | |||
262 | r = mailsession_select_folder(storage->sto_session, pathname); | ||
263 | |||
264 | * result = storage->sto_session; | ||
265 | |||
266 | return MAIL_NO_ERROR; | ||
267 | } | ||