aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/nntp/camel-nntp-folder.c
blob: 7edc4a98fde9b770a9206f6f656639f8ec9e42b6 (plain) (blame)
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
/* -*- 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-folder-summary.h"
#include "camel-nntp-resp-codes.h"
#include "camel-nntp-store.h"
#include "camel-nntp-folder.h"
#include "camel-nntp-store.h"
#include "camel-nntp-utils.h"

#include "string-utils.h"
#include "camel-stream-mem.h"
#include "camel-data-wrapper.h"
#include "camel-mime-message.h"
#include "camel-folder-summary.h"

#include "camel-exception.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 *store;

    camel_folder_summary_save (folder->summary);

    store = CAMEL_NNTP_STORE (camel_folder_get_parent_store (folder));

    if (store->newsrc)
        camel_nntp_newsrc_write (store->newsrc);
}

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);

    if (flags & set & CAMEL_MESSAGE_SEEN) {
        int article_num;
        CamelNNTPStore *nntp_store = CAMEL_NNTP_STORE (camel_folder_get_parent_store (folder));

        sscanf (uid, "%d", &article_num);

        camel_nntp_newsrc_mark_article_read (nntp_store->newsrc,
                             folder->name,
                             article_num);
    }
}

static CamelMimeMessage *
nntp_folder_get_message (CamelFolder *folder, const gchar *uid, CamelException *ex)
{
    CamelStream *message_stream = NULL;
    CamelMimeMessage *message = NULL;
    CamelStore *parent_store;
    char *buf;
    int buf_len;
    int buf_alloc;
    int status;
    gboolean done;
    char *message_id;

    /* get the parent store */
    parent_store = camel_folder_get_parent_store (folder);

    message_id = strchr (uid, ',') + 1;
    status = camel_nntp_command (CAMEL_NNTP_STORE( parent_store ), ex, NULL, "ARTICLE %s", message_id);

    /* if the message_id was not found, raise an exception and return */
    if (status == NNTP_NO_SUCH_ARTICLE) {
        camel_exception_setv (ex, 
                     CAMEL_EXCEPTION_FOLDER_INVALID_UID,
                     _("Message %s not found."),
                      message_id);
        return NULL;
    }
    else if (status != NNTP_ARTICLE_FOLLOWS) {
        /* XXX */
        g_warning ("weird nntp error %d\n", status);
        return NULL;
    }

    /* this could probably done fairly easily with an nntp stream that
       returns eof after '.' */

    /* 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 = g_malloc(buf_alloc);
    done = FALSE;

    buf[0] = 0;

    while (!done) {
        int line_length;
        char *line;

        if (camel_remote_store_recv_line (CAMEL_REMOTE_STORE (parent_store), &line, ex) < 0) {
            g_warning ("recv_line failed while building message\n");
            break;
        }

        /* XXX check exception */

        line_length = strlen ( line );

        if (!strcmp(line, ".")) {
            done = TRUE;
            g_free (line);
        }
        else {
            if (buf_len + line_length > buf_alloc) {
                buf_alloc *= 2;
                buf = g_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);

    message = camel_mime_message_new ();
    camel_data_wrapper_construct_from_stream (CAMEL_DATA_WRAPPER(message), message_stream);

    camel_object_unref (CAMEL_OBJECT (message_stream));

#if 0
    gtk_signal_connect (CAMEL_OBJECT (message), "message_changed", message_changed, folder);
#endif

    g_free (buf);

    return message;
}

static GPtrArray*
nntp_folder_search_by_expression (CamelFolder *folder, const char *expression, CamelException *ex)
{
    g_assert (0);
    return NULL;
}

static void           
nntp_folder_finalize (CamelObject *object)
{
    CamelNNTPFolder *nntp_folder = CAMEL_NNTP_FOLDER (object);

    g_free (nntp_folder->summary_file_path);
}

static void
camel_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;
}

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) camel_nntp_folder_class_init,
                                  NULL,
                                  (CamelObjectInitFunc) NULL,
                                  (CamelObjectFinalizeFunc) nntp_folder_finalize);
    }
    
    return camel_nntp_folder_type;
}

CamelFolder *
camel_nntp_folder_new (CamelStore *parent, const char *folder_name, CamelException *ex)
{
    CamelFolder *folder = CAMEL_FOLDER (camel_object_new (CAMEL_NNTP_FOLDER_TYPE));
    CamelNNTPFolder *nntp_folder = CAMEL_NNTP_FOLDER (folder);
    const gchar *root_dir_path;

    camel_folder_construct (folder, parent, folder_name, folder_name);
    folder->has_summary_capability = TRUE;

    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);

    folder->summary = camel_folder_summary_new ();
    camel_folder_summary_set_filename (folder->summary,
                       nntp_folder->summary_file_path);

    if (-1 == camel_folder_summary_load (folder->summary)) {
        /* Bad or nonexistant summary file */
        camel_nntp_get_headers (CAMEL_FOLDER( folder )->parent_store,
                    nntp_folder, ex);
        if (camel_exception_get_id (ex)) {
            camel_object_unref (CAMEL_OBJECT (folder));
            return NULL;
        }

        /* XXX check return value */
        camel_folder_summary_save (folder->summary);
    }

    return folder;
}