]> code.citadel.org Git - citadel.git/blob - rss2ctdl/io-internal.c
7d9cb21a9e791b3bc1ad9e968ee34b6d7b55bd31
[citadel.git] / rss2ctdl / io-internal.c
1 /*
2  * $Id$
3  * 
4  * Copyright 2003-2004 Oliver Feiler <kiza@kcore.de>
5  *
6  * io-internal.c
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22  
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <time.h>
31 #include <signal.h>
32
33 #include "config.h"
34
35 #include "main.h"
36 #include "conversions.h"
37 #include "netio.h"
38 #include "xmlparse.h"
39 #include "io-internal.h"
40
41 extern char *browser;
42
43 void GetHTTPErrorString (char * errorstring, int size, int httpstatus) {
44         switch (httpstatus) {
45                 case 400:
46                         snprintf(errorstring, size, "Bad request");
47                         break;
48                 case 402:
49                         snprintf(errorstring, size, "Payment required");
50                         break;
51                 case 403:
52                         snprintf(errorstring, size, "Access denied");
53                         break;
54                 case 500:
55                         snprintf(errorstring, size, "Internal server error");
56                         break;
57                 case 501:
58                         snprintf(errorstring, size, "Not implemented");
59                         break;
60                 case 502:
61                 case 503:
62                         snprintf(errorstring, size, "Service unavailable");
63                         break;
64                 default:
65                         sprintf(errorstring, "HTTP %d!", httpstatus);
66         }
67 }
68
69 void PrintUpdateError (int suppressoutput, struct feed * cur_ptr) {
70         netio_error_type err;
71         char errstr[256];
72         char httperrstr[64];
73
74         err = cur_ptr->netio_error;
75         
76         if (!suppressoutput) {
77                 switch (err) {
78                         case NET_ERR_OK:
79                                 break;
80                         case NET_ERR_URL_INVALID:
81                                 fprintf(stderr, "%s: Invalid URL!\n", cur_ptr->title);
82                                 break;
83                         case NET_ERR_SOCK_ERR:
84                                 fprintf(stderr, "%s: Couldn't create network socket!\n", cur_ptr->title);
85                                 break;
86                         case NET_ERR_HOST_NOT_FOUND:
87                                 fprintf(stderr, "%s: Can't resolve host!\n", cur_ptr->title);
88                                 break;
89                         case NET_ERR_CONN_REFUSED:
90                                 fprintf(stderr, "%s: Connection refused!\n", cur_ptr->title);
91                                 break;
92                         case NET_ERR_CONN_FAILED:
93                                 fprintf(stderr, "%s: Couldn't connect to server: %s\n",
94                                         cur_ptr->title,
95                                         (strerror(cur_ptr->connectresult) ? strerror(cur_ptr->connectresult) : "(null)"));
96                                 break;
97                         case NET_ERR_TIMEOUT:
98                                 fprintf(stderr, "%s: Connection timed out.\n", cur_ptr->title);
99                                 break;
100                         case NET_ERR_UNKNOWN:
101                                 break;
102                         case NET_ERR_REDIRECT_COUNT_ERR:
103                                 fprintf(stderr, "%s: Too many HTTP redirects encountered! Giving up.\n", cur_ptr->title);
104                                 break;
105                         case NET_ERR_REDIRECT_ERR:
106                                 fprintf(stderr, "%s: Server sent an invalid redirect!\n", cur_ptr->title);
107                                 break;
108                         case NET_ERR_HTTP_410:
109                         case NET_ERR_HTTP_404:
110                                 fprintf(stderr, "%s: This feed no longer exists. Please unsubscribe!\n", cur_ptr->title);
111                                 break;
112                         case NET_ERR_HTTP_NON_200:
113                                 GetHTTPErrorString(httperrstr, sizeof(httperrstr), cur_ptr->lasthttpstatus);
114                                 fprintf(stderr, "%s: Could not download feed: %s\n", cur_ptr->title, httperrstr);
115                                 break;
116                         case NET_ERR_HTTP_PROTO_ERR:
117                                 fprintf(stderr, "%s: Error in server reply.\n", cur_ptr->title);
118                                 break;
119                         case NET_ERR_AUTH_FAILED:
120                                 fprintf(stderr, "%s: Authentication failed!\n", cur_ptr->title);
121                                 break;
122                         case NET_ERR_AUTH_NO_AUTHINFO:
123                                 fprintf(stderr, "%s: URL does not contain authentication information!\n", cur_ptr->title);
124                                 break;
125                         case NET_ERR_AUTH_GEN_AUTH_ERR:
126                                 fprintf(stderr, "%s: Could not generate authentication information!\n", cur_ptr->title);
127                                 break;
128                         case NET_ERR_AUTH_UNSUPPORTED:
129                                 fprintf(stderr, "%s: Unsupported authentication method requested by server!\n", cur_ptr->title);
130                                 break;
131                         case NET_ERR_GZIP_ERR:
132                                 fprintf(stderr, "%s: Error decompressing server reply!\n", cur_ptr->title);
133                                 break;
134                         default:
135                                 break;
136                 }
137                 /* Must be inside if(!suppressoutput) statement! */
138         }
139 }
140
141
142 /* Update given feed from server.
143  * Reload XML document and replace in memory cur_ptr->feed with it.
144  */
145 int UpdateFeed (struct feed * cur_ptr) {
146         char *tmpname;
147         char *freeme;
148
149         if (cur_ptr == NULL) {
150                 return 1;
151         }
152         
153         /* Need to work on a copy of ->feedurl, because DownloadFeed() changes the pointer. */
154         tmpname = strdup (cur_ptr->feedurl);
155         freeme = tmpname;       /* Need to make a copy, otherwise we cannot free all RAM. */
156         free (cur_ptr->feed);
157
158         cur_ptr->feed = DownloadFeed (tmpname, cur_ptr, 0);
159         free (freeme);
160
161         /* Set title and link structure to something.
162          * To the feedurl in this case so the program show something
163          * as placeholder instead of crash. */
164         if (cur_ptr->title == NULL)
165                 cur_ptr->title = strdup (cur_ptr->feedurl);
166         if (cur_ptr->link == NULL)
167                 cur_ptr->link = strdup (cur_ptr->feedurl);
168
169         /* If the download function returns a NULL pointer return from here. */
170         if (cur_ptr->feed == NULL) {
171         if (cur_ptr->problem == 1)
172                         PrintUpdateError (0, cur_ptr);
173                 return 1;
174         }
175         
176         /* If there is no feed, return. */
177         if (cur_ptr->feed == NULL)
178                 return 1;
179         
180         if ((DeXML (cur_ptr)) != 0) {
181                 fprintf(stderr, "Invalid XML! Cannot parse this feed!\n");
182
183                 /* Activate feed problem flag. */
184                 cur_ptr->problem = 1;
185                 return 1;
186         }
187         
188         /* We don't need these anymore. Free the raw XML to save some memory. */
189         free (cur_ptr->feed);
190         cur_ptr->feed = NULL;
191                 
192         return 0;
193 }
194
195