Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / providers / nntp / camel-nntp-grouplist.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-nntp-grouplist.c : getting/updating the list of newsgroups on the server. */
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 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <errno.h>
29 #include <string.h>
30
31 #include <glib.h>
32 #include <glib/gstdio.h>
33
34 #include "camel-exception.h"
35
36 #include "camel-nntp-grouplist.h"
37 #include "camel-nntp-resp-codes.h"
38
39 static CamelNNTPGroupList *
40 camel_nntp_get_grouplist_from_server (CamelNNTPStore *store, CamelException *ex)
41 {
42         int status;
43         gboolean done = FALSE;
44         CamelNNTPGroupList *list;
45
46         CAMEL_NNTP_STORE_LOCK(store);
47         status = camel_nntp_command (store, ex, NULL, &line, "LIST");
48
49         if (status != NNTP_LIST_FOLLOWS) {
50                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
51                                       _("Could not get group list from server."));
52                 return NULL;
53         }
54
55         list = g_new0 (CamelNNTPGroupList, 1);
56         list->time = time (NULL);
57
58         while (!done) {
59                 char *line;
60
61                 if (camel_remote_store_recv_line (CAMEL_REMOTE_STORE (store), &line, ex) < 0) {
62                         list->group_list = g_list_reverse(list->group_list);
63                         return list;
64                 }
65
66                 if (*line == '.') {
67                         done = TRUE;
68                 }
69                 else {
70                         CamelNNTPGroupListEntry *entry = g_new (CamelNNTPGroupListEntry, 1);
71                         char **split_line = g_strsplit (line, " ", 4);
72
73                         entry->group_name = g_strdup (split_line[0]);
74                         entry->high = atoi (split_line[1]);
75                         entry->low = atoi (split_line[2]);
76
77                         g_strfreev (split_line);
78                         
79                         list->group_list = g_list_prepend (list->group_list, entry);
80                 }
81         }
82         CAMEL_NNTP_STORE_UNLOCK(store);
83
84         list->group_list = g_list_reverse(list->group_list);
85         return list;
86 }
87
88 static CamelNNTPGroupList*
89 camel_nntp_get_grouplist_from_file (CamelNNTPStore *store, CamelException *ex)
90 {
91         gchar *root_dir = camel_nntp_store_get_toplevel_dir(CAMEL_NNTP_STORE(store));
92         gchar *grouplist_file = g_strdup_printf ("%s/grouplist", root_dir);
93         CamelNNTPGroupList *list;
94         FILE *fp;
95         char buf[300];
96         unsigned long time;
97
98         g_free (root_dir);
99         fp = g_fopen (grouplist_file, "r");
100         g_free (grouplist_file);
101
102         if (fp == NULL) {
103                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
104                                       _("Unable to load grouplist file for %s: %s"),
105                                       CAMEL_SERVICE(store)->url->host,
106                                       strerror(errno));
107                 return NULL;
108         }
109
110         /* read the time */
111         if (!fgets (buf, sizeof (buf), fp)) {
112                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
113                                       _("Unable to load grouplist file for %s: %s"),
114                                       CAMEL_SERVICE(store)->url->host,
115                                       strerror(errno));
116                 fclose (fp);
117                 return NULL;
118         }
119
120
121         list = g_new0 (CamelNNTPGroupList, 1);
122         list->store = store;
123         sscanf (buf, "%lu", &time);
124         list->time = time;
125
126         while (fgets (buf, sizeof (buf), fp)) {
127                 CamelNNTPGroupListEntry *entry = g_new (CamelNNTPGroupListEntry, 1);
128                 char **split_line = g_strsplit (buf, " ", 4);
129
130                 entry->group_name = g_strdup (split_line[0]);
131                 entry->high = atoi (split_line[1]);
132                 entry->low = atoi (split_line[2]);
133
134                 g_strfreev (split_line);
135
136                 list->group_list = g_list_prepend (list->group_list, entry);
137         }
138
139         fclose (fp);
140
141         list->group_list = g_list_reverse(list->group_list);
142         return list;
143 }
144
145 static void
146 save_entry (CamelNNTPGroupListEntry *entry, FILE *fp)
147 {
148         fprintf (fp, "%s %d %d\n", entry->group_name, entry->low, entry->high);
149 }
150
151 void
152 camel_nntp_grouplist_save (CamelNNTPGroupList *group_list, CamelException *ex)
153 {
154         FILE *fp;
155         gchar *root_dir = camel_nntp_store_get_toplevel_dir(CAMEL_NNTP_STORE(group_list->store));
156         gchar *grouplist_file = g_strdup_printf ("%s/grouplist", root_dir);
157
158         g_free (root_dir);
159         fp = g_fopen (grouplist_file, "w");
160         g_free (grouplist_file);
161
162         if (fp == NULL) {
163                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
164                                       _("Unable to save grouplist file for %s: %s"),
165                                       CAMEL_SERVICE(group_list->store)->url->host,
166                                       strerror(errno));
167                 return;
168         }
169
170         fprintf (fp, "%lu\n", (long)group_list->time);
171
172         g_list_foreach (group_list->group_list, (GFunc)save_entry, fp);
173
174         fclose (fp);
175 }
176
177 static void
178 free_entry (CamelNNTPGroupListEntry *entry, void *data)
179 {
180         g_free (entry->group_name);
181         g_free (entry);
182 }
183
184 void
185 camel_nntp_grouplist_free (CamelNNTPGroupList *group_list)
186 {
187         g_return_if_fail (group_list);
188
189         g_list_foreach (group_list->group_list, (GFunc)free_entry, NULL);
190
191         g_free (group_list);
192 }
193
194 CamelNNTPGroupList*
195 camel_nntp_grouplist_fetch (CamelNNTPStore *store, CamelException *ex)
196 {
197         CamelNNTPGroupList *list;
198
199         list = camel_nntp_get_grouplist_from_file (store, ex);
200
201         printf ("camel_nntp_get_grouplist_from_file returned %p\n", list);
202
203         if (!list) {
204                 camel_exception_clear (ex);
205
206                 list = camel_nntp_get_grouplist_from_server (store, ex);
207
208                 if (!list) {
209                         camel_nntp_grouplist_free (list);
210                 }
211                 else {
212                         list->store = store;
213                         camel_nntp_grouplist_save (list, ex);
214                         return list;
215                 }
216         }
217
218         return list;
219 }
220
221 gint
222 camel_nntp_grouplist_update (CamelNNTPGroupList *group_list, CamelException *ex)
223 {
224         return 0;
225 }