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@ximian.com>
7 * Copyright (C) 2000 Ximian .
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU Lesser General Public
11 * License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
27 #include <libedataserver/md5-utils.h>
29 #include "camel-folder-summary.h"
30 #include "camel-stream-mem.h"
31 #include "camel-exception.h"
33 #include "camel-nntp-resp-codes.h"
34 #include "camel-nntp-folder.h"
35 #include "camel-nntp-store.h"
36 #include "camel-nntp-utils.h"
39 get_XOVER_headers(CamelNNTPStore *nntp_store, CamelFolder *folder,
40 int first_message, int last_message, CamelException *ex)
43 CamelNNTPFolder *nntp_folder = CAMEL_NNTP_FOLDER (folder);
46 status = camel_nntp_command (nntp_store, ex, NULL,
51 if (status == NNTP_DATA_FOLLOWS) {
52 gboolean done = FALSE;
57 if (camel_remote_store_recv_line (CAMEL_REMOTE_STORE (nntp_store), &line, ex) < 0) {
58 g_warning ("failed to recv_line while building OVER header list\n");
67 CamelMessageInfo *new_info = camel_folder_summary_info_new(folder->summary);
68 char **split_line = g_strsplit (line, "\t", 7);
69 char *subject, *from, *date, *message_id, *bytes;
72 subject = split_line [nntp_store->overview_field [CAMEL_NNTP_OVER_SUBJECT].index];
73 from = split_line [nntp_store->overview_field [CAMEL_NNTP_OVER_FROM].index];
74 date = split_line [nntp_store->overview_field [CAMEL_NNTP_OVER_DATE].index];
75 message_id = split_line [nntp_store->overview_field [CAMEL_NNTP_OVER_MESSAGE_ID].index];
76 bytes = split_line [nntp_store->overview_field [CAMEL_NNTP_OVER_BYTES].index];
78 /* if the overview format flagged this
79 field as "full", skip over the
80 preceding field name and colon */
81 if (nntp_store->overview_field [ CAMEL_NNTP_OVER_SUBJECT ].full)
82 subject += strlen ("Subject:");
83 if (nntp_store->overview_field [ CAMEL_NNTP_OVER_FROM ].full)
84 from += strlen ("From:");
85 if (nntp_store->overview_field [ CAMEL_NNTP_OVER_DATE ].full)
86 date += strlen ("Date:");
87 if (nntp_store->overview_field [ CAMEL_NNTP_OVER_MESSAGE_ID ].full)
88 message_id += strlen ("Message-ID:");
89 if (nntp_store->overview_field [ CAMEL_NNTP_OVER_BYTES ].full)
90 bytes += strlen ("Bytes:");
92 uid = g_strdup_printf ("%s,%s", split_line[0], message_id);
93 camel_message_info_set_subject(new_info, g_strdup(subject));
94 camel_message_info_set_from(new_info, g_strdup(from));
95 camel_message_info_set_to(new_info, g_strdup(folder->name));
96 camel_message_info_set_uid(new_info, uid);
98 new_info->date_sent = camel_header_decode_date(date, NULL);
100 /* XXX do we need to fill in both dates? */
101 new_info->headers.date_received = g_strdup(date);
103 new_info->size = atoi(bytes);
104 md5_get_digest(message_id, strlen(message_id), digest);
105 memcpy(new_info->message_id.id.hash, digest, sizeof(new_info->message_id.id.hash));
107 if (camel_nntp_newsrc_article_is_read (nntp_store->newsrc,
109 atoi (split_line[0])))
110 new_info->flags |= CAMEL_MESSAGE_SEEN;
112 camel_folder_summary_add (folder->summary, new_info);
113 g_strfreev (split_line);
120 g_warning ("weird nntp response for XOVER: %d\n", status);
126 get_HEAD_headers(CamelNNTPStore *nntp_store, CamelFolder *folder,
127 int first_message, int last_message, CamelException *ex)
132 for (i = first_message; i < last_message; i ++) {
133 status = camel_nntp_command (nntp_store, ex, NULL,
136 if (status == NNTP_HEAD_FOLLOWS) {
137 gboolean done = FALSE;
142 CamelStream *header_stream;
143 GArray *header_array;
144 CamelStream *nntp_istream;
145 CamelMessageInfo *new_info = g_new0(CamelMessageInfo, 1);
149 buf = g_malloc(buf_alloc);
154 nntp_istream = nntp_store->istream;
160 line = camel_stream_buffer_read_line (
161 CAMEL_STREAM_BUFFER ( nntp_istream ));
162 line_length = strlen ( line );
168 if (buf_len + line_length > buf_alloc) {
170 buf = g_realloc (buf, buf_alloc);
174 buf_len += strlen(line);
179 /* create a stream from which to parse the headers */
180 header_stream = camel_stream_mem_new_with_buffer (buf, buf_len,
181 CAMEL_STREAM_MEM_READ);
183 header_array = get_header_array_from_stream (header_stream);
185 memset (&info, 0, sizeof(info));
187 for (h = 0; h < header_array->len; h ++) {
188 Rfc822Header *header = &((Rfc822Header*)header_array->data)[h];
189 if (!g_ascii_strcasecmp(header->name, "From"))
190 new_info->from = g_strdup(header->value);
191 else if (!g_ascii_strcasecmp(header->name, "To"))
192 new_info->to = g_strdup(header->value);
193 else if (!g_ascii_strcasecmp(header->name, "Subject"))
194 new_info->subject = g_strdup(header->value);
195 else if (!g_ascii_strcasecmp(header->name, "Message-ID")) {
196 new_info->uid = g_strdup_printf("%d,%s", i, header->value);
197 new_info->message_id = g_strdup(header->value);
199 else if (!g_ascii_strcasecmp(header->name, "Date")) {
200 new_info->date_sent = camel_header_decode_date (header->value);
202 new_info->date_sent = g_strdup(header->value);
203 new_info->date_received = g_strdup(header->value);
208 camel_folder_summary_add (nntp_folder->summary, new_info);
210 else if (status == CAMEL_NNTP_FAIL) {
211 /* nasty things are afoot */
212 g_warning ("failure doing HEAD\n");
220 uid_num (CamelFolderSummary *summary, int index)
224 CamelMessageInfo *minfo;
227 minfo = camel_folder_summary_index(summary, index);
231 tmp = g_strdup(camel_message_info_uid(minfo));
232 camel_message_info_free(minfo);
234 if((brk = strchr(tmp, ',')) == NULL)
247 camel_nntp_get_headers (CamelStore *store,
248 CamelNNTPFolder *nntp_folder,
251 CamelNNTPStore *nntp_store = CAMEL_NNTP_STORE (store);
252 CamelFolder *folder = CAMEL_FOLDER (nntp_folder);
254 int first_message, nb_message, last_message, last_summary;
258 status = camel_nntp_command (nntp_store, ex, &ret,
259 "GROUP %s", folder->name);
260 sscanf (ret, "%d %d %d", &nb_message, &first_message, &last_message);
263 i = camel_folder_summary_count(folder->summary);
265 last_summary = uid_num(folder->summary, i-1);
267 if(last_summary < first_message)
268 camel_folder_summary_clear(folder->summary);
270 while(uid_num(folder->summary, 0) < first_message)
271 camel_folder_summary_remove_index(folder->summary, 0);
273 if(last_summary >= last_message)
276 first_message = last_summary;
280 if (status == NNTP_NO_SUCH_GROUP) {
281 /* XXX throw invalid group exception */
282 camel_exception_setv (ex,
283 CAMEL_EXCEPTION_FOLDER_INVALID,
284 "group %s not found on server",
290 if (nntp_store->extensions & CAMEL_NNTP_EXT_OVER) {
291 get_XOVER_headers (nntp_store, folder, first_message, last_message, ex);
294 g_warning ("need to fix get_HEAD_headers\n");
296 get_HEAD_headers (nntp_store, folder, first_message, last_message, ex);