1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-nntp-folder.c : Class for a news folder
*
* Authors : Chris Toshok <toshok@ximian.com>
* Michael Zucchi <notzed@ximian.com>
*
* Copyright (C) 2001 Ximian .
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include <config.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include "camel/string-utils.h"
#include "camel/camel-stream-mem.h"
#include "camel/camel-data-wrapper.h"
#include "camel/camel-mime-message.h"
#include "camel/camel-folder-search.h"
#include "camel/camel-exception.h"
#include "camel/camel-session.h"
#include "camel/camel-data-cache.h"
#include "camel-nntp-summary.h"
#include "camel-nntp-store.h"
#include "camel-nntp-folder.h"
#include "camel-nntp-store.h"
#include "camel-nntp-private.h"
static CamelFolderClass *parent_class=NULL;
/* Returns the class for a CamelNNTPFolder */
#define CNNTPF_CLASS(so) CAMEL_NNTP_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
#define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
#define CNNTPS_CLASS(so) CAMEL_STORE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
static void
nntp_folder_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
{
CamelNNTPStore *nntp_store;
CamelFolderChangeInfo *changes = NULL;
CamelNNTPFolder *nntp_folder;
nntp_store = (CamelNNTPStore *)folder->parent_store;
nntp_folder = (CamelNNTPFolder *)folder;
CAMEL_NNTP_STORE_LOCK(nntp_store, command_lock);
if (camel_nntp_summary_check((CamelNNTPSummary *)folder->summary, nntp_folder->changes, ex) != -1)
camel_folder_summary_save (folder->summary);
if (camel_folder_change_info_changed(nntp_folder->changes)) {
changes = nntp_folder->changes;
nntp_folder->changes = camel_folder_change_info_new();
}
CAMEL_NNTP_STORE_UNLOCK(nntp_store, command_lock);
if (changes) {
camel_object_trigger_event((CamelObject *)folder, "folder_changed", changes);
camel_folder_change_info_free(changes);
}
}
static void
nntp_folder_set_message_flags(CamelFolder *folder, const char *uid, guint32 flags, guint32 set)
{
((CamelFolderClass *)parent_class)->set_message_flags(folder, uid, flags, set);
}
static CamelMimeMessage *
nntp_folder_get_message (CamelFolder *folder, const char *uid, CamelException *ex)
{
CamelMimeMessage *message = NULL;
CamelNNTPStore *nntp_store;
CamelFolderChangeInfo *changes;
CamelNNTPFolder *nntp_folder;
CamelStream *stream = NULL;
int ret;
char *line;
const char *msgid;
nntp_store = (CamelNNTPStore *)folder->parent_store;
nntp_folder = (CamelNNTPFolder *)folder;
CAMEL_NNTP_STORE_LOCK(nntp_store, command_lock);
msgid = strchr(uid, ',');
if (msgid == 0) {
camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
_("Internal error: uid in invalid format: %s"), uid);
goto fail;
}
msgid++;
/* Lookup in cache, NEWS is global messageid's so use a global cache path */
stream = camel_data_cache_get(nntp_store->cache, "cache", msgid, NULL);
if (stream == NULL) {
/* Not in cache, retrieve and put in cache */
if (camel_nntp_store_set_folder(nntp_store, folder, nntp_folder->changes, ex) == -1)
goto fail;
ret = camel_nntp_command(nntp_store, &line, "article %s", msgid);
if (ret == -1)
goto error;
if (ret == 220) {
stream = camel_data_cache_add(nntp_store->cache, "cache", msgid, NULL);
if (stream) {
if (camel_stream_write_to_stream((CamelStream *)nntp_store->stream, stream) == -1)
goto error;
if (camel_stream_reset(stream) == -1)
goto error;
} else {
stream = (CamelStream *)nntp_store->stream;
camel_object_ref((CamelObject *)stream);
}
}
}
if (stream) {
message = camel_mime_message_new();
if (camel_data_wrapper_construct_from_stream((CamelDataWrapper *)message, stream) == -1)
goto error;
CAMEL_NNTP_STORE_UNLOCK(nntp_store, command_lock);
camel_object_unref((CamelObject *)stream);
return message;
}
camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM, _("Cannot get message %s: %s"), uid, line);
error:
if (errno == EINTR)
camel_exception_setv(ex, CAMEL_EXCEPTION_USER_CANCEL, _("User cancelled"));
else
camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM, _("Cannot get message %s: %s"), uid, strerror(errno));
fail:
if (message)
camel_object_unref((CamelObject *)message);
if (stream)
camel_object_unref((CamelObject *)stream);
if (camel_folder_change_info_changed(nntp_folder->changes)) {
changes = nntp_folder->changes;
nntp_folder->changes = camel_folder_change_info_new();
} else {
changes = NULL;
}
CAMEL_NNTP_STORE_UNLOCK(nntp_store, command_lock);
if (changes) {
camel_object_trigger_event((CamelObject *)folder, "folder_changed", changes);
camel_folder_change_info_free(changes);
}
return NULL;
}
static GPtrArray*
nntp_folder_search_by_expression (CamelFolder *folder, const char *expression, CamelException *ex)
{
CamelNNTPFolder *nntp_folder = CAMEL_NNTP_FOLDER (folder);
GPtrArray *matches, *summary;
CAMEL_NNTP_FOLDER_LOCK(nntp_folder, search_lock);
if(nntp_folder->search == NULL)
nntp_folder->search = camel_folder_search_new();
camel_folder_search_set_folder(nntp_folder->search, folder);
summary = camel_folder_get_summary(folder);
camel_folder_search_set_summary(nntp_folder->search, summary);
matches = camel_folder_search_execute_expression(nntp_folder->search, expression, ex);
CAMEL_NNTP_FOLDER_UNLOCK(nntp_folder, search_lock);
camel_folder_free_summary(folder, summary);
return matches;
}
static GPtrArray *
nntp_folder_search_by_uids(CamelFolder *folder, const char *expression, GPtrArray *uids, CamelException *ex)
{
CamelNNTPFolder *nntp_folder = CAMEL_NNTP_FOLDER(folder);
GPtrArray *summary, *matches;
int i;
/* NOTE: could get away without the search lock by creating a new
search object each time */
summary = g_ptr_array_new();
for (i=0;i<uids->len;i++) {
CamelMessageInfo *info;
info = camel_folder_get_message_info(folder, uids->pdata[i]);
if (info)
g_ptr_array_add(summary, info);
}
if (summary->len == 0)
return summary;
CAMEL_NNTP_FOLDER_LOCK(folder, search_lock);
if (nntp_folder->search == NULL)
nntp_folder->search = camel_folder_search_new();
camel_folder_search_set_folder(nntp_folder->search, folder);
camel_folder_search_set_summary(nntp_folder->search, summary);
matches = camel_folder_search_execute_expression(nntp_folder->search, expression, ex);
CAMEL_NNTP_FOLDER_UNLOCK(folder, search_lock);
for (i=0;i<summary->len;i++)
camel_folder_free_message_info(folder, summary->pdata[i]);
g_ptr_array_free(summary, TRUE);
return matches;
}
static void
nntp_folder_search_free(CamelFolder *folder, GPtrArray *result)
{
CamelNNTPFolder *nntp_folder = CAMEL_NNTP_FOLDER (folder);
camel_folder_search_free_result(nntp_folder->search, result);
}
static void
nntp_folder_init(CamelNNTPFolder *nntp_folder, CamelNNTPFolderClass *klass)
{
nntp_folder->changes = camel_folder_change_info_new();
}
static void
nntp_folder_finalise (CamelNNTPFolder *nntp_folder)
{
g_free(nntp_folder->storage_path);
}
static void
nntp_folder_class_init (CamelNNTPFolderClass *camel_nntp_folder_class)
{
CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS (camel_nntp_folder_class);
parent_class = CAMEL_FOLDER_CLASS (camel_type_get_global_classfuncs (camel_folder_get_type ()));
/* virtual method definition */
/* virtual method overload */
camel_folder_class->sync = nntp_folder_sync;
camel_folder_class->set_message_flags = nntp_folder_set_message_flags;
camel_folder_class->get_message = nntp_folder_get_message;
camel_folder_class->search_by_expression = nntp_folder_search_by_expression;
camel_folder_class->search_by_uids = nntp_folder_search_by_uids;
camel_folder_class->search_free = nntp_folder_search_free;
}
CamelType
camel_nntp_folder_get_type (void)
{
static CamelType camel_nntp_folder_type = CAMEL_INVALID_TYPE;
if (camel_nntp_folder_type == CAMEL_INVALID_TYPE) {
camel_nntp_folder_type = camel_type_register (CAMEL_FOLDER_TYPE, "CamelNNTPFolder",
sizeof (CamelNNTPFolder),
sizeof (CamelNNTPFolderClass),
(CamelObjectClassInitFunc) nntp_folder_class_init,
NULL,
(CamelObjectInitFunc) nntp_folder_init,
(CamelObjectFinalizeFunc) nntp_folder_finalise);
}
return camel_nntp_folder_type;
}
/* not yet */
/* Idea is we update in stages, but this requires a different xover command, etc */
#ifdef ASYNC_SUMMARY
struct _folder_check_msg {
CamelSessionThreadMsg msg;
CamelNNTPFolder *folder;
};
static void
folder_check(CamelSession *session, CamelSessionThreadMsg *msg)
{
struct _folder_check_msg *m = (struct _folder_check_msg *)msg;
CamelException *ex;
CamelNNTPStore *nntp_store;
nntp_store = (CamelNNTPStore *)m->folder->parent.parent_store;
CAMEL_NNTP_STORE_LOCK(nntp_store, command_lock);
ex = camel_exception_new();
camel_nntp_summary_check((CamelNNTPSummary *)m->folder->parent.summary, m->folder->changes, ex);
camel_exception_free(ex);
CAMEL_NNTP_STORE_UNLOCK(nntp_store, command_lock);
}
static void
folder_check_free(CamelSession *session, CamelSessionThreadMsg *msg)
{
struct _folder_check_msg *m = (struct _folder_check_msg *)msg;
camel_object_unref((CamelObject *)m->folder);
}
static CamelSessionThreadOps folder_check_ops = {
folder_check,
folder_check_free,
};
#endif
CamelFolder *
camel_nntp_folder_new (CamelStore *parent, const char *folder_name, CamelException *ex)
{
CamelFolder *folder;
CamelNNTPFolder *nntp_folder;
char *root;
CamelService *service;
#ifdef ASYNC_SUMMARY
struct _folder_check_msg *m;
#endif
service = (CamelService *)parent;
root = camel_session_get_storage_path(service->session, service, ex);
if (root == NULL)
return NULL;
/* If this doesn't work, stuff wont save, but let it continue anyway */
(void) camel_mkdir_hier(root, 0777);
folder = (CamelFolder *) camel_object_new (CAMEL_NNTP_FOLDER_TYPE);
nntp_folder = (CamelNNTPFolder *)folder;
camel_folder_construct (folder, parent, folder_name, folder_name);
folder->folder_flags |= CAMEL_FOLDER_HAS_SUMMARY_CAPABILITY|CAMEL_FOLDER_HAS_SEARCH_CAPABILITY;
nntp_folder->storage_path = g_strdup_printf("%s/%s", root, folder->full_name);
g_free(root);
folder->summary = (CamelFolderSummary *)camel_nntp_summary_new(nntp_folder);
camel_folder_summary_load (folder->summary);
#ifdef ASYNC_SUMMARY
m = camel_session_thread_msg_new(service->session, &folder_check_ops, sizeof(*m));
m->folder = nntp_folder;
camel_object_ref((CamelObject *)folder);
camel_session_thread_queue(service->session, &m->msg, 0);
#else
if (camel_nntp_summary_check((CamelNNTPSummary *)folder->summary, nntp_folder->changes, ex) == -1) {
camel_object_unref((CamelObject *)folder);
folder = NULL;
}
#endif
return folder;
}
|