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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-nntp-folder.c : Abstract class for an email folder */
/*
* Author : Chris Toshok <toshok@helixcode.com>
*
* Copyright (C) 2000 Helix Code .
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* 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-nntp-store.h"
#include "camel-nntp-folder.h"
#include "camel-nntp-store.h"
#include "camel-nntp-utils.h"
#include "camel-nntp-summary.h"
#include "string-utils.h"
#include "camel-stream-mem.h"
#include "camel-stream-buffer.h"
#include "gmime-utils.h"
#include "camel-data-wrapper.h"
#include "camel-mime-message.h"
#include "camel-exception.h"
static CamelFolderClass *parent_class=NULL;
/* Returns the class for a CamelNNTPFolder */
#define CNNTPF_CLASS(so) CAMEL_NNTP_FOLDER_CLASS (GTK_OBJECT(so)->klass)
#define CF_CLASS(so) CAMEL_FOLDER_CLASS (GTK_OBJECT(so)->klass)
#define CNNTPS_CLASS(so) CAMEL_STORE_CLASS (GTK_OBJECT(so)->klass)
static void _init (CamelFolder *folder, CamelStore *parent_store,
CamelFolder *parent_folder, const gchar *name,
gchar separator, CamelException *ex);
static void _set_name(CamelFolder *folder, const gchar *name, CamelException *ex);
static void _open (CamelFolder *folder, CamelFolderOpenMode mode, CamelException *ex);
static void _close (CamelFolder *folder, gboolean expunge, CamelException *ex);
static gboolean _exists (CamelFolder *folder, CamelException *ex);
static gboolean _create(CamelFolder *folder, CamelException *ex);
static gboolean _delete (CamelFolder *folder, gboolean recurse, CamelException *ex);
static gboolean _delete_messages (CamelFolder *folder, CamelException *ex);
static GList *_list_subfolders (CamelFolder *folder, CamelException *ex);
static CamelMimeMessage *_get_message_by_number (CamelFolder *folder, gint number, CamelException *ex);
static gint _get_message_count (CamelFolder *folder, CamelException *ex);
static void _append_message (CamelFolder *folder, CamelMimeMessage *message, CamelException *ex);
static GList *_get_uid_list (CamelFolder *folder, CamelException *ex);
static CamelMimeMessage *_get_message_by_uid (CamelFolder *folder, const gchar *uid, CamelException *ex);
#if 0
static void _expunge (CamelFolder *folder, CamelException *ex);
static void _copy_message_to (CamelFolder *folder, CamelMimeMessage *message, CamelFolder *dest_folder, CamelException *ex);
#endif
static const gchar *_get_message_uid (CamelFolder *folder, CamelMimeMessage *message, CamelException *ex);
static void _finalize (GtkObject *object);
static void
camel_nntp_folder_class_init (CamelNNTPFolderClass *camel_nntp_folder_class)
{
CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS (camel_nntp_folder_class);
GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (camel_folder_class);
parent_class = gtk_type_class (camel_folder_get_type ());
/* virtual method definition */
/* virtual method overload */
camel_folder_class->init = _init;
camel_folder_class->set_name = _set_name;
camel_folder_class->open = _open;
camel_folder_class->close = _close;
camel_folder_class->exists = _exists;
camel_folder_class->create = _create;
camel_folder_class->delete = _delete;
camel_folder_class->delete_messages = _delete_messages;
camel_folder_class->list_subfolders = _list_subfolders;
camel_folder_class->get_message_by_number = _get_message_by_number;
camel_folder_class->get_message_count = _get_message_count;
camel_folder_class->get_uid_list = _get_uid_list;
camel_folder_class->get_message_by_uid = _get_message_by_uid;
#if 0
camel_folder_class->append_message = _append_message;
camel_folder_class->expunge = _expunge;
camel_folder_class->copy_message_to = _copy_message_to;
camel_folder_class->get_message_uid = _get_message_uid;
#endif
gtk_object_class->finalize = _finalize;
}
static void
_finalize (GtkObject *object)
{
CamelNNTPFolder *nntp_folder = CAMEL_NNTP_FOLDER (object);
g_free (nntp_folder->summary_file_path);
GTK_OBJECT_CLASS (parent_class)->finalize (object);
}
GtkType
camel_nntp_folder_get_type (void)
{
static GtkType camel_nntp_folder_type = 0;
if (!camel_nntp_folder_type) {
GtkTypeInfo camel_nntp_folder_info =
{
"CamelNNTPFolder",
sizeof (CamelNNTPFolder),
sizeof (CamelNNTPFolderClass),
(GtkClassInitFunc) camel_nntp_folder_class_init,
(GtkObjectInitFunc) NULL,
/* reserved_1 */ NULL,
/* reserved_2 */ NULL,
(GtkClassInitFunc) NULL,
};
camel_nntp_folder_type = gtk_type_unique (CAMEL_FOLDER_TYPE, &camel_nntp_folder_info);
}
return camel_nntp_folder_type;
}
static void
_init (CamelFolder *folder, CamelStore *parent_store,
CamelFolder *parent_folder, const gchar *name, gchar separator,
CamelException *ex)
{
/* call parent method */
parent_class->init (folder, parent_store, parent_folder,
name, separator, ex);
if (camel_exception_get_id (ex)) return;
/* we assume that the parent init
method checks for the existance of @folder */
if (!strcmp(name, "/"))
{
folder->has_summary_capability = FALSE;
folder->can_hold_messages = FALSE;
folder->can_hold_folders = TRUE;
}
else
{
folder->has_summary_capability = TRUE;
folder->can_hold_messages = TRUE;
folder->can_hold_folders = TRUE;
}
folder->has_uid_capability = TRUE;
folder->has_search_capability = FALSE;
folder->summary = NULL;
}
/* internal method used to :
- test for the existence of a summary file
- test the sync between the summary and the newsgroup
- load the summary or create it if necessary
*/
static void
_check_get_or_maybe_generate_summary_file (CamelNNTPFolder *nntp_folder,
CamelException *ex)
{
CamelFolder *folder = CAMEL_FOLDER (nntp_folder);
CamelNNTPSummary *summ = NULL;
GArray *message_info_array;
folder->summary = NULL;
/* Test for the existence and up-to-dateness of the summary file. */
if (access (nntp_folder->summary_file_path, F_OK) == 0) {
summ = camel_nntp_summary_load (CAMEL_FOLDER(nntp_folder)->name,
nntp_folder->summary_file_path, ex);
if (summ) {
folder->summary = CAMEL_FOLDER_SUMMARY (summ);
} else {
/* Bad summary file */
if (camel_exception_get_id (ex) !=
CAMEL_EXCEPTION_FOLDER_SUMMARY_INVALID)
return;
camel_exception_clear (ex);
}
}
/* In the case where the summary does not exist (or was the
* wrong version), or is not in sync with the nntp file,
* regenerate it.
*/
if (folder->summary == NULL) {
message_info_array =
camel_nntp_get_headers (CAMEL_FOLDER( folder )->parent_store, nntp_folder, ex);
if (camel_exception_get_id (ex))
return;
summ = CAMEL_NNTP_SUMMARY (gtk_object_new (camel_nntp_summary_get_type (), NULL));
summ->message_info = message_info_array;
summ->nb_message = summ->message_info->len;
folder->summary = CAMEL_FOLDER_SUMMARY (summ);
camel_nntp_summary_save (summ,
nntp_folder->summary_file_path, ex);
}
}
static void
_open (CamelFolder *folder, CamelFolderOpenMode mode, CamelException *ex)
{
CamelNNTPFolder *nntp_folder = CAMEL_NNTP_FOLDER (folder);
/* call parent class */
parent_class->open (folder, mode, ex);
if (camel_exception_get_id(ex))
return;
#if 0
/* get (or create) uid list */
if (!(nntp_load_uid_list (nntp_folder) > 0))
nntp_generate_uid_list (nntp_folder);
#endif
_check_get_or_maybe_generate_summary_file (nntp_folder, ex);
}
static void
_close (CamelFolder *folder, gboolean expunge, CamelException *ex)
{
CamelNNTPFolder *nntp_folder = CAMEL_NNTP_FOLDER (folder);
CamelNNTPSummary *nntp_summary = CAMEL_NNTP_SUMMARY (folder->summary);
/* call parent implementation */
parent_class->close (folder, expunge, ex);
camel_nntp_summary_save (nntp_summary,
nntp_folder->summary_file_path, ex);
}
static void
_set_name (CamelFolder *folder, const gchar *name, CamelException *ex)
{
CamelNNTPFolder *nntp_folder = CAMEL_NNTP_FOLDER (folder);
const gchar *root_dir_path;
/* call default implementation */
parent_class->set_name (folder, name, ex);
if (camel_exception_get_id (ex)) return;
root_dir_path = camel_nntp_store_get_toplevel_dir (CAMEL_NNTP_STORE(folder->parent_store));
nntp_folder->summary_file_path = g_strdup_printf ("%s/%s-ev-summary", root_dir_path, folder->name);
}
static gboolean
_exists (CamelFolder *folder, CamelException *ex)
{
#if 0
CamelNNTPFolder *nntp_folder;
struct stat stat_buf;
gint stat_error;
gboolean exists;
g_assert(folder != NULL);
nntp_folder = CAMEL_NNTP_FOLDER (folder);
/* check if the nntp summary path is determined */
if (!nntp_folder->summary_file_path) {
camel_exception_set (ex,
CAMEL_EXCEPTION_FOLDER_INVALID,
"undetermined folder summary path. Maybe use set_name ?");
return FALSE;
}
/* check if the nntp file exists */
stat_error = stat (nntp_folder->summary_file_path, &stat_buf);
if (stat_error == -1)
return FALSE;
exists = S_ISREG (stat_buf.st_mode);
/* we should check the rights here */
return exists;
#endif
return TRUE;
}
static gboolean
_create (CamelFolder *folder, CamelException *ex)
{
#if 0
CamelNNTPSummary *summary;
g_assert(folder != NULL);
/* call default implementation */
parent_class->create (folder, ex);
/* create the summary object */
summary = CAMEL_NNTP_SUMMARY (gtk_object_new (camel_nntp_summary_get_type (), NULL));
summary->nb_message = 0;
summary->next_uid = 1;
summary->nntp_file_size = 0;
summary->message_info = g_array_new (FALSE, FALSE, sizeof (CamelNNTPSummaryInformation));
#endif
return TRUE;
}
static gboolean
_delete (CamelFolder *folder, gboolean recurse, CamelException *ex)
{
#if 0
gboolean folder_already_exists;
g_assert(folder != NULL);
/* check if the folder object exists */
/* in the case where the folder does not exist,
return immediatly */
folder_already_exists = camel_folder_exists (folder, ex);
if (camel_exception_get_id (ex))
return FALSE;
if (!folder_already_exists)
return TRUE;
/* call default implementation.
It should delete the messages in the folder
and recurse the operation to subfolders */
parent_class->delete (folder, recurse, ex);
#endif
return TRUE;
}
gboolean
_delete_messages (CamelFolder *folder, CamelException *ex)
{
gboolean folder_already_exists;
g_assert(folder!=NULL);
/* in the case where the folder does not exist,
return immediatly */
folder_already_exists = camel_folder_exists (folder, ex);
if (camel_exception_get_id (ex)) return FALSE;
if (!folder_already_exists) return TRUE;
return TRUE;
}
static GList *
_list_subfolders (CamelFolder *folder, CamelException *ex)
{
/* newsgroups don't have subfolders */
return NULL;
}
static gint
_get_message_count (CamelFolder *folder, CamelException *ex)
{
gint message_count = 0;
g_assert (folder);
g_assert (folder->summary);
message_count = CAMEL_NNTP_SUMMARY (folder->summary)->nb_message;
return message_count;
}
#if 0
static void
_append_message (CamelFolder *folder, CamelMimeMessage *message, CamelException *ex)
{
CamelNNTPFolder *nntp_folder = CAMEL_NNTP_FOLDER (folder);
#if 0
CamelNNTPSummary *summary = CAMEL_NNTP_SUMMARY (folder->summary);
#endif
CamelStream *output_stream;
guint32 tmp_file_size;
guint32 next_uid;
gint tmp_file_fd;
GArray *message_info_array;
#if 0
GArray *nntp_summary_info;
#endif
gchar *tmp_message_filename;
gint fd1, fd2;
int i;
/* write the message itself */
output_stream = camel_stream_fs_new_with_name (tmp_message_filename,
CAMEL_STREAM_FS_WRITE);
if (output_stream != NULL) {
camel_stream_write_string (output_stream, "From - \n");
camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (message), output_stream);
}
camel_stream_close (output_stream);
gtk_object_unref (GTK_OBJECT (output_stream));
/* at this point we have saved the message to a
temporary file, now, we have to add the x-evolution
field and also update the main summary */
/*
First : parse the nntp file, but only from the
position where the message has been added,
wich happens to be the last postion in the
nntp file before we added the message.
This position is still stored in the summary
for the moment
*/
next_uid = summary->next_uid;
tmp_file_fd = open (tmp_message_filename, O_RDONLY);
message_info_array =
camel_nntp_parse_file (tmp_file_fd, "From - ", 0,
&tmp_file_size, &next_uid, TRUE,
NULL, 0, ex);
close (tmp_file_fd);
/* get the value of the last available UID
as saved in the summary file, again */
next_uid = summary->next_uid;
/* make sure all our of message info's have 0 uid - ignore any
set elsewhere */
for (i=0;i<message_info_array->len;i++) {
g_array_index(message_info_array, CamelNNTPParserMessageInfo, i).uid = 0;
}
/*
OK, this is not very efficient, we should not use the same
method as for parsing an entire mail file,
but I have no time to write a simpler parser
*/
#if 0
next_uid = camel_nntp_write_xev (nntp_folder, tmp_message_filename,
message_info_array, &tmp_file_size, next_uid, ex);
#endif
if (camel_exception_get_id (ex)) {
/* ** FIXME : free the preparsed information */
return;
}
#if 0
nntp_summary_info =
parsed_information_to_nntp_summary (message_info_array);
#endif
/* store the number of messages as well as the summary array */
summary->nb_message += 1;
summary->next_uid = next_uid;
((CamelNNTPSummaryInformation *)(nntp_summary_info->data))->position +=
summary->nntp_file_size;
summary->nntp_file_size += tmp_file_size;
camel_nntp_summary_append_entries (summary, nntp_summary_info);
g_array_free (nntp_summary_info, TRUE);
/* append the temporary file message to the nntp file */
fd1 = open (tmp_message_filename, O_RDONLY);
fd2 = open (nntp_folder->folder_file_path,
O_WRONLY | O_CREAT | O_APPEND,
0600);
if (fd2 == -1) {
camel_exception_setv (ex,
CAMEL_EXCEPTION_FOLDER_INSUFFICIENT_PERMISSION,
"could not open the nntp folder file for appending the message\n"
"\t%s\n"
"Full error is : %s\n",
nntp_folder->folder_file_path,
strerror (errno));
return;
}
camel_nntp_copy_file_chunk (fd1,
fd2,
tmp_file_size,
ex);
close (fd1);
close (fd2);
/* remove the temporary file */
unlink (tmp_message_filename);
g_free (tmp_message_filename);
}
#endif
static GList *
_get_uid_list (CamelFolder *folder, CamelException *ex)
{
GArray *message_info_array;
CamelNNTPSummaryInformation *message_info;
GList *uid_list = NULL;
int i;
message_info_array =
CAMEL_NNTP_SUMMARY (folder->summary)->message_info;
for (i=0; i<message_info_array->len; i++) {
message_info = (CamelNNTPSummaryInformation *)(message_info_array->data) + i;
uid_list = g_list_prepend (uid_list, g_strdup (message_info->headers.uid));
}
return uid_list;
}
static CamelMimeMessage *
_get_message_by_number (CamelFolder *folder, gint number, CamelException *ex)
{
GArray *message_info_array;
CamelNNTPSummaryInformation *message_info;
message_info_array =
CAMEL_NNTP_SUMMARY (folder->summary)->message_info;
if (number > message_info_array->len) {
camel_exception_setv (ex, CAMEL_EXCEPTION_FOLDER_INVALID,
"No such message %d in folder `%s'.",
number, folder->name);
return NULL;
}
message_info =
(CamelNNTPSummaryInformation *)(message_info_array->data) +
(number - 1);
return _get_message_by_uid (folder, message_info->headers.uid, ex);
}
static CamelMimeMessage *
_get_message_by_uid (CamelFolder *folder, const gchar *uid, CamelException *ex)
{
CamelStream *nntp_istream;
CamelStream *message_stream;
CamelMimeMessage *message = NULL;
CamelStore *parent_store;
char *buf;
int buf_len;
int buf_alloc;
int status;
gboolean done;
/* get the parent store */
parent_store = camel_folder_get_parent_store (folder, ex);
if (camel_exception_get_id (ex)) {
return NULL;
}
status = camel_nntp_command (CAMEL_NNTP_STORE( parent_store ), NULL, "ARTICLE %s", uid);
nntp_istream = CAMEL_NNTP_STORE (parent_store)->istream;
/* if the uid was not found, raise an exception and return */
if (status != CAMEL_NNTP_OK) {
camel_exception_setv (ex,
CAMEL_EXCEPTION_FOLDER_INVALID_UID,
"message %s not found.",
uid);
return NULL;
}
/* XXX ick ick ick. read the entire message into a buffer and
then create a stream_mem for it. */
buf_alloc = 2048;
buf_len = 0;
buf = malloc(buf_alloc);
done = FALSE;
buf[0] = 0;
while (!done) {
char *line = camel_stream_buffer_read_line ( CAMEL_STREAM_BUFFER ( nntp_istream ));
int line_length = strlen ( line );
if (!strcmp(line, ".")) {
done = TRUE;
g_free (line);
}
else {
if (buf_len + line_length > buf_alloc) {
buf_alloc *= 2;
buf = realloc (buf, buf_alloc);
}
strcat(buf, line);
strcat(buf, "\n");
buf_len += strlen(line) + 1;
g_free (line);
}
}
/* create a stream bound to the message */
message_stream = camel_stream_mem_new_with_buffer(buf,
buf_len,
CAMEL_STREAM_MEM_READ);
message = camel_mime_message_new ();
camel_data_wrapper_set_input_stream (CAMEL_DATA_WRAPPER (message), message_stream);
return message;
}
|