1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-nntp-utils.c : utilities used by the nntp code. */
5 * Author : Chris Toshok <toshok@helixcode.com>
7 * Copyright (C) 2000 Helix Code .
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 #include "camel-folder-summary.h"
26 #include "camel-nntp-folder.h"
27 #include "camel-nntp-store.h"
28 #include "camel-nntp-utils.h"
29 #include "camel-stream-buffer.h"
30 #include "camel-stream-mem.h"
36 get_XOVER_headers(CamelNNTPStore *nntp_store, CamelFolder *folder,
37 int first_message, int last_message, CamelException *ex)
40 CamelNNTPFolder *nntp_folder = CAMEL_NNTP_FOLDER (folder);
42 status = camel_nntp_command (nntp_store, NULL,
47 if (status == CAMEL_NNTP_OK) {
48 CamelStream *nntp_istream = nntp_store->istream;
49 gboolean done = FALSE;
54 line = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER ( nntp_istream ));
60 CamelMessageInfo *new_info = g_new0(CamelMessageInfo, 1);
61 char **split_line = g_strsplit (line, "\t", 7);
63 new_info->subject = g_strdup(split_line[1]);
64 new_info->from = g_strdup(split_line[2]);
65 new_info->to = g_strdup(nntp_folder->group_name);
66 new_info->date_sent = header_decode_date(split_line[3], NULL);
68 /* XXX do we need to fill in both dates? */
69 new_info->headers.date_received = g_strdup(split_line[3]);
71 new_info->size = atoi(split_line[5]);
72 new_info->uid = g_strdup(split_line[4]);
73 g_strfreev (split_line);
75 camel_folder_summary_add (nntp_folder->summary, new_info);
84 get_HEAD_headers(CamelNNTPStore *nntp_store, CamelFolder *folder,
85 int first_message, int last_message, CamelException *ex)
90 for (i = first_message; i < last_message; i ++) {
91 status = camel_nntp_command (nntp_store, NULL,
94 if (status == CAMEL_NNTP_OK) {
95 gboolean done = FALSE;
100 CamelStream *header_stream;
101 GArray *header_array;
102 CamelStream *nntp_istream;
103 CamelMessageInfo *new_info = g_new0(CamelMessageInfo, 1);
107 buf = malloc(buf_alloc);
112 nntp_istream = nntp_store->istream;
118 line = camel_stream_buffer_read_line (
119 CAMEL_STREAM_BUFFER ( nntp_istream ));
120 line_length = strlen ( line );
126 if (buf_len + line_length > buf_alloc) {
128 buf = realloc (buf, buf_alloc);
132 buf_len += strlen(line);
137 /* create a stream from which to parse the headers */
138 header_stream = camel_stream_mem_new_with_buffer(buf,
140 CAMEL_STREAM_MEM_READ);
142 header_array = get_header_array_from_stream (header_stream);
144 memset (&info, 0, sizeof(info));
146 for (h = 0; h < header_array->len; h ++) {
147 Rfc822Header *header = &((Rfc822Header*)header_array->data)[h];
148 if (!g_strcasecmp(header->name, "From"))
149 new_info->from = g_strdup(header->value);
150 else if (!g_strcasecmp(header->name, "To"))
151 new_info->to = g_strdup(header->value);
152 else if (!g_strcasecmp(header->name, "Subject"))
153 new_info->subject = g_strdup(header->value);
154 else if (!g_strcasecmp(header->name, "Message-ID"))
155 new_info->uid = g_strdup(header->value);
156 else if (!g_strcasecmp(header->name, "Date")) {
157 new_info->date_sent = header_decode_date (header->value);
159 new_info->date_sent = g_strdup(header->value);
160 new_info->date_received = g_strdup(header->value);
165 camel_folder_summary_add (nntp_folder->summary, new_info);
167 else if (status == CAMEL_NNTP_FAIL) {
168 /* nasty things are afoot */
169 g_warning ("failure doing HEAD\n");
177 camel_nntp_get_headers (CamelStore *store,
178 CamelNNTPFolder *nntp_folder,
181 CamelNNTPStore *nntp_store = CAMEL_NNTP_STORE (store);
182 CamelFolder *folder = CAMEL_FOLDER (nntp_folder);
184 int first_message, nb_message, last_message;
187 status = camel_nntp_command (nntp_store, &ret,
188 "GROUP %s", CAMEL_NNTP_FOLDER (folder)->group_name);
190 sscanf (ret, "%d %d %d", &nb_message, &first_message, &last_message);
193 if (status != CAMEL_NNTP_OK) {
194 /* XXX throw invalid group exception */
195 printf ("invalid group\n");
200 if (nntp_store->extensions & CAMEL_NNTP_EXT_XOVER) {
202 get_XOVER_headers (nntp_store, folder, first_message, last_message, ex);
206 get_HEAD_headers (nntp_store, folder, first_message, last_message, ex);