ignore test-newsrc
[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@helixcode.com> 
6  *
7  * Copyright (C) 2000 Helix Code .
8  *
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.
13  *
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.
18  *
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
22  * USA
23  */
24
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"
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 static void
36 get_XOVER_headers(CamelNNTPStore *nntp_store, CamelFolder *folder,
37                   int first_message, int last_message, CamelException *ex)
38 {
39         int status;
40         CamelNNTPFolder *nntp_folder = CAMEL_NNTP_FOLDER (folder);
41
42         status = camel_nntp_command (nntp_store, NULL,
43                                      "XOVER %d-%d",
44                                      first_message,
45                                      last_message);
46                 
47         if (status == CAMEL_NNTP_OK) {
48                 CamelStream *nntp_istream = nntp_store->istream;
49                 gboolean done = FALSE;
50
51                 while (!done) {
52                         char *line;
53
54                         line = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER ( nntp_istream ));
55
56                         if (*line == '.') {
57                                 done = TRUE;
58                         }
59                         else {
60                                 CamelMessageInfo *new_info = g_new0(CamelMessageInfo, 1);
61                                 char **split_line = g_strsplit (line, "\t", 7);
62
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);
67 #if 0
68                                 /* XXX do we need to fill in both dates? */
69                                 new_info->headers.date_received = g_strdup(split_line[3]);
70 #endif
71                                 new_info->size = atoi(split_line[5]);
72                                 new_info->uid = g_strdup(split_line[4]);
73                                 g_strfreev (split_line);
74
75                                 camel_folder_summary_add (nntp_folder->summary, new_info);
76                         }
77                         g_free (line);
78                 }
79         }
80 }
81
82 #if 0
83 static GArray*
84 get_HEAD_headers(CamelNNTPStore *nntp_store, CamelFolder *folder,
85                  int first_message, int last_message, CamelException *ex)
86 {
87         int i;
88         int status;
89
90         for (i = first_message; i < last_message; i ++) {
91                 status = camel_nntp_command (nntp_store, NULL,
92                                              "HEAD %d", i);
93
94                 if (status == CAMEL_NNTP_OK) {
95                         gboolean done = FALSE;
96                         char *buf;
97                         int buf_len;
98                         int buf_alloc;
99                         int h;
100                         CamelStream *header_stream;
101                         GArray *header_array;
102                         CamelStream *nntp_istream;
103                         CamelMessageInfo *new_info = g_new0(CamelMessageInfo, 1);
104
105                         buf_alloc = 2048;
106                         buf_len = 0;
107                         buf = malloc(buf_alloc);
108                         done = FALSE;
109
110                         buf[0] = 0;
111
112                         nntp_istream = nntp_store->istream;
113
114                         while (!done) {
115                                 char *line;
116                                 int line_length;
117
118                                 line = camel_stream_buffer_read_line ( 
119                                                       CAMEL_STREAM_BUFFER ( nntp_istream ));
120                                 line_length = strlen ( line );
121
122                                 if (*line == '.') {
123                                         done = TRUE;
124                                 }
125                                 else {
126                                         if (buf_len + line_length > buf_alloc) {
127                                                 buf_alloc *= 2;
128                                                 buf = realloc (buf, buf_alloc);
129                                         }
130                                         strcat(buf, line);
131                                         strcat(buf, "\n");
132                                         buf_len += strlen(line);
133                                         g_free (line);
134                                 }
135                         }
136
137                         /* create a stream from which to parse the headers */
138                         header_stream = camel_stream_mem_new_with_buffer(buf,
139                                                                  buf_len,
140                                                                  CAMEL_STREAM_MEM_READ);
141
142                         header_array = get_header_array_from_stream (header_stream);
143
144                         memset (&info, 0, sizeof(info));
145
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);
158 #if 0
159                                         new_info->date_sent = g_strdup(header->value);
160                                         new_info->date_received = g_strdup(header->value);
161 #endif
162                                 }
163                         }
164
165                         camel_folder_summary_add (nntp_folder->summary, new_info);
166                 }
167                 else if (status == CAMEL_NNTP_FAIL) {
168                         /* nasty things are afoot */
169                         g_warning ("failure doing HEAD\n");
170                         break;
171                 }
172         }
173 }
174 #endif
175
176 void
177 camel_nntp_get_headers (CamelStore *store,
178                         CamelNNTPFolder *nntp_folder,
179                         CamelException *ex)
180 {
181         CamelNNTPStore *nntp_store = CAMEL_NNTP_STORE (store);
182         CamelFolder *folder = CAMEL_FOLDER (nntp_folder);
183         char *ret;
184         int first_message, nb_message, last_message;
185         int status;
186
187         status = camel_nntp_command (nntp_store, &ret,
188                                      "GROUP %s", CAMEL_NNTP_FOLDER (folder)->group_name);
189
190         sscanf (ret, "%d %d %d", &nb_message, &first_message, &last_message);
191         g_free (ret);
192
193         if (status != CAMEL_NNTP_OK) {
194                 /* XXX throw invalid group exception */
195                 printf ("invalid group\n");
196                 return;
197         }
198
199 #if 0
200         if (nntp_store->extensions & CAMEL_NNTP_EXT_XOVER) {
201 #endif
202                 get_XOVER_headers (nntp_store, folder, first_message, last_message, ex);
203 #if 0
204         }
205         else {
206                 get_HEAD_headers (nntp_store, folder, first_message, last_message, ex);
207         }
208 #endif
209 }
210