Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / providers / nntp / camel-nntp-utils.c
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. */
3
4 /* 
5  * Author : Chris Toshok <toshok@ximian.com> 
6  *
7  * Copyright (C) 2000 Ximian .
8  *
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.
12  *
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.
17  *
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
21  * USA
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <libedataserver/md5-utils.h>
28
29 #include "camel-folder-summary.h"
30 #include "camel-stream-mem.h"
31 #include "camel-exception.h"
32
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"
37
38 static void
39 get_XOVER_headers(CamelNNTPStore *nntp_store, CamelFolder *folder,
40                   int first_message, int last_message, CamelException *ex)
41 {
42         int status;
43         CamelNNTPFolder *nntp_folder = CAMEL_NNTP_FOLDER (folder);
44         char digest[16];
45
46         status = camel_nntp_command (nntp_store, ex, NULL,
47                                      "XOVER %d-%d",
48                                      first_message,
49                                      last_message);
50
51         if (status == NNTP_DATA_FOLLOWS) {
52                 gboolean done = FALSE;
53
54                 while (!done) {
55                         char *line;
56
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");
59                                 break;
60                         }
61
62                         if (*line == '.') {
63                                 done = TRUE;
64                                 g_print ("done\n");
65                         }
66                         else {
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;
70                                 char *uid;
71
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];
77                                 
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:");
91
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);
97
98                                 new_info->date_sent = camel_header_decode_date(date, NULL);
99 #if 0
100                                 /* XXX do we need to fill in both dates? */
101                                 new_info->headers.date_received = g_strdup(date);
102 #endif
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));
106
107                                 if (camel_nntp_newsrc_article_is_read (nntp_store->newsrc,
108                                                                        folder->name,
109                                                                        atoi (split_line[0])))
110                                     new_info->flags |= CAMEL_MESSAGE_SEEN;
111
112                                 camel_folder_summary_add (folder->summary, new_info);
113                                 g_strfreev (split_line);
114                         }
115                         g_free (line);
116                 }
117         }
118         else {
119                 /* XXX */
120                 g_warning ("weird nntp response for XOVER: %d\n", status);
121         }
122 }
123
124 #if 0
125 static GArray*
126 get_HEAD_headers(CamelNNTPStore *nntp_store, CamelFolder *folder,
127                  int first_message, int last_message, CamelException *ex)
128 {
129         int i;
130         int status;
131
132         for (i = first_message; i < last_message; i ++) {
133                 status = camel_nntp_command (nntp_store, ex, NULL,
134                                              "HEAD %d", i);
135
136                 if (status == NNTP_HEAD_FOLLOWS) {
137                         gboolean done = FALSE;
138                         char *buf;
139                         int buf_len;
140                         int buf_alloc;
141                         int h;
142                         CamelStream *header_stream;
143                         GArray *header_array;
144                         CamelStream *nntp_istream;
145                         CamelMessageInfo *new_info = g_new0(CamelMessageInfo, 1);
146
147                         buf_alloc = 2048;
148                         buf_len = 0;
149                         buf = g_malloc(buf_alloc);
150                         done = FALSE;
151
152                         buf[0] = 0;
153
154                         nntp_istream = nntp_store->istream;
155
156                         while (!done) {
157                                 char *line;
158                                 int line_length;
159
160                                 line = camel_stream_buffer_read_line ( 
161                                                       CAMEL_STREAM_BUFFER ( nntp_istream ));
162                                 line_length = strlen ( line );
163
164                                 if (*line == '.') {
165                                         done = TRUE;
166                                 }
167                                 else {
168                                         if (buf_len + line_length > buf_alloc) {
169                                                 buf_alloc *= 2;
170                                                 buf = g_realloc (buf, buf_alloc);
171                                         }
172                                         strcat(buf, line);
173                                         strcat(buf, "\n");
174                                         buf_len += strlen(line);
175                                         g_free (line);
176                                 }
177                         }
178
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);
182
183                         header_array = get_header_array_from_stream (header_stream);
184
185                         memset (&info, 0, sizeof(info));
186
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);
198                                 }
199                                 else if (!g_ascii_strcasecmp(header->name, "Date")) {
200                                         new_info->date_sent = camel_header_decode_date (header->value);
201 #if 0
202                                         new_info->date_sent = g_strdup(header->value);
203                                         new_info->date_received = g_strdup(header->value);
204 #endif
205                                 }
206                         }
207
208                         camel_folder_summary_add (nntp_folder->summary, new_info);
209                 }
210                 else if (status == CAMEL_NNTP_FAIL) {
211                         /* nasty things are afoot */
212                         g_warning ("failure doing HEAD\n");
213                         break;
214                 }
215         }
216 }
217 #endif
218
219 static inline int
220 uid_num (CamelFolderSummary *summary, int index)
221 {
222         char *tmp;
223         char *brk;
224         CamelMessageInfo *minfo;
225         int ret;
226         
227         minfo = camel_folder_summary_index(summary, index);
228         if(minfo == NULL)
229                 return 0;
230
231         tmp = g_strdup(camel_message_info_uid(minfo));
232         camel_message_info_free(minfo);
233         
234         if((brk = strchr(tmp, ',')) == NULL)
235                 ret = 0;
236         else {
237                 *brk = 0;
238                 ret = atoi(tmp);
239         }
240         
241         g_free(tmp);
242         
243         return ret;
244 }
245
246 void
247 camel_nntp_get_headers (CamelStore *store,
248                         CamelNNTPFolder *nntp_folder,
249                         CamelException *ex)
250 {
251         CamelNNTPStore *nntp_store = CAMEL_NNTP_STORE (store);
252         CamelFolder *folder = CAMEL_FOLDER (nntp_folder);
253         char *ret;
254         int first_message, nb_message, last_message, last_summary;
255         int status;
256         int i;
257
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);
261         g_free (ret);
262
263         i = camel_folder_summary_count(folder->summary);
264         if(i != 0) {
265                 last_summary = uid_num(folder->summary, i-1);
266
267                 if(last_summary < first_message)
268                         camel_folder_summary_clear(folder->summary);
269                 else {
270                         while(uid_num(folder->summary, 0) < first_message) 
271                                 camel_folder_summary_remove_index(folder->summary, 0);
272                         
273                         if(last_summary >= last_message)
274                                 return;
275                         
276                         first_message = last_summary;
277                 }
278         }
279                         
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",
285                                       folder->name);
286                 return;
287         }
288
289
290         if (nntp_store->extensions & CAMEL_NNTP_EXT_OVER) {
291                 get_XOVER_headers (nntp_store, folder, first_message, last_message, ex);
292         }
293         else {
294                 g_warning ("need to fix get_HEAD_headers\n");
295 #if 0
296                 get_HEAD_headers (nntp_store, folder, first_message, last_message, ex);
297 #endif
298         }               
299
300 }