/[pkg-mixmaster]/branches/Mix-stats/Src/menustats.c
ViewVC logotype

Contents of /branches/Mix-stats/Src/menustats.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 814 - (show annotations) (download)
Tue May 4 22:42:08 2004 UTC (9 years ago) by weasel
File MIME type: text/plain
File size: 6653 byte(s)
Apply Colin's patch for downloading files
1 #if 0
2 void errlog(int type, char *format, ...) { };
3 char MIXDIR[512];
4 #include "util.c"
5 #include "buffers.c"
6 int menu_getuserpass(BUFFER *p, int mode) { return 0; };
7 #define ALLPINGERSFILE "allpingers.txt"
8 #endif
9
10 #include <string.h>
11 #include <assert.h>
12 #include <stdlib.h>
13 #include <sys/wait.h>
14
15 #include "menu.h"
16 #ifdef WIN32
17 #include <urlmon.h>
18 #pragma comment(lib,"urlmon.lib")
19 #endif /* WIN32 */
20
21
22 int url_download(char *url, char *dest) {
23 int err;
24 #ifdef WIN32
25 err = URLDownloadToFile(NULL, url, dest, BINDF_GETNEWESTVERSION, NULL);
26
27 if (err != S_OK)
28 return -1;
29 else
30 return 0;
31 #else
32 char s[PATHMAX];
33 snprintf(s, PATHMAX, "wget -nv %s -O %s", url, dest);
34 err = system(s);
35
36 if (err != -1) {
37 if (WIFEXITED(err) == 0)
38 return -1; /* abnormal child exit */
39 else
40 return 0;
41 }
42 else
43 return -1;
44 #endif /* WIN32 */
45 }
46
47 /** read the allpingers file into the buffer */
48 void read_allpingers(BUFFER *allpingers) {
49 FILE *f;
50
51 f = mix_openfile(ALLPINGERSFILE, "r");
52 if (f != NULL) {
53 buf_clear(allpingers);
54 buf_read(allpingers, f);
55 fclose(f);
56 }
57 }
58
59 /* Get all sections from inifile.
60 *
61 * They are put into the sections buffer, separated by newlines
62 */
63 void get_sections(BUFFER *inifile, BUFFER *sections) {
64 BUFFER *line;
65 int err;
66
67 line = buf_new();
68
69 buf_rewind(inifile);
70 buf_reset(sections);
71
72 while ((err = buf_getline(inifile, line)) != -1) {
73 if (bufileft (line, "[") &&
74 bufiright(line, "]")) {
75 line->data[line->length-1] = '\0';
76 buf_appends(sections, line->data+1);
77 buf_nl(sections);
78 };
79 }
80 buf_free (line);
81 }
82
83 /* Get value of an attribute
84 *
85 * returns -1 if it isn't found.
86 */
87 int get_attribute(BUFFER *inifile, char *section, char *attribute, BUFFER *value) {
88 BUFFER *line;
89 int err = -1;
90 int insection = 0;
91
92 line = buf_new();
93
94 buf_rewind(inifile);
95 buf_reset(value);
96
97 while ((err = buf_getline(inifile, line)) != -1) {
98 if (bufileft (line, "[") &&
99 bufiright(line, "]")) {
100 if (insection)
101 break;
102
103 line->data[line->length-1] = '\0';
104 if (strcasecmp(section, line->data+1) == 0) {
105 insection = 1;
106 }
107 } else if (insection && bufileft(line, attribute)) {
108 /* we are in the right section and this attribute name
109 * at least starts with what we want */
110 char *ptr = line->data + strlen(attribute);
111 /* eat up whitespace */
112 while ((*ptr == ' ') || (*ptr == '\t'))
113 ptr++;
114 if (*ptr != '=')
115 continue;
116 ptr++;
117 while ((*ptr == ' ') || (*ptr == '\t'))
118 ptr++;
119 buf_appends(value, ptr);
120 err = 0;
121 break;
122 }
123 }
124 buf_free (line);
125 return (err);
126 }
127
128
129
130
131
132 static const char *files[]={"mlist","rlist","mixring","pgpring","type2list"};
133 #define NUMFILES sizeof(files)/sizeof(*files)
134
135 /* Download all the needed files from the specified source */
136 /* returns -1 on error */
137 static int download (BUFFER *allpingers, char *sourcename) {
138 const char *localfiles[]={"mlist.txt","rlist.txt","pubring.mix","pgpring.asc","type2.list"};
139 char path[PATHMAX];
140 BUFFER *value;
141 int ret = 0;
142 int err;
143 int i;
144
145 value = buf_new();
146
147 clear();
148 standout();
149
150 err = get_attribute(allpingers, sourcename, "base", value);
151 if (err == 0)
152 printw("%s",value->data);
153 standend();
154
155 for (i=0; i<NUMFILES; i++) {
156 err = get_attribute(allpingers, sourcename, files[i], value);
157 if (err < 0) {
158 /* the attribute vanished under us */
159 ret = -1;
160 break;
161 }
162 mixfile(path, localfiles[i]);
163 mvprintw(i+3, 0, "downloading %s...", localfiles[i]);
164 refresh();
165 err = url_download(value->data,path);
166 if (err < 0) {
167 printw("failed to download.\n\rTry using another stats source.");
168 ret = -1;
169 break;
170 }
171 printw("done");
172 }
173
174 printw("\n\n\rPress any key to continue");
175 getch();
176 clear();
177 buf_free(value);
178 return ret;
179 }
180 /* Checks whether the stats source has all the required files.
181 *
182 * 1 if it has,
183 * 0 otherwise
184 */
185 static int good_source (BUFFER *allpingers, char *sourcename) {
186 BUFFER *value;
187 int i;
188 int res = 1;
189 int err;
190
191 value = buf_new();
192
193 for (i = 0; i < NUMFILES; i++) {
194 err = get_attribute(allpingers, sourcename, files[i], value);
195 if (err < 0) {
196 res = 0;
197 break;
198 }
199 }
200
201 buf_free (value);
202
203 return res;
204 }
205 /* Download allpingers.txt */
206 /* -1 on error */
207 static int download_list() {
208 char path[PATHMAX];
209
210 mixfile(path, ALLPINGERSFILE);
211
212 clear();
213 standout();
214 printw(ALLPINGERSURL);
215 standend();
216
217 mvprintw(3,0,"downloading %s...", ALLPINGERSURL);
218 refresh();
219 if (url_download(ALLPINGERSURL, path) < 0) {
220 printw("failed to download.\n\rTry again later.");
221 printw("\n\n\rPress any key to continue");
222 getch();
223 return -1;
224 }
225 return 0;
226 }
227
228 /* Displays the choice of stats sources */
229 #define MAXPING (26 * 2)
230 void update_stats() {
231 char c;
232 BUFFER *inifile;
233 BUFFER *pingernames;
234 BUFFER *goodpingers;
235 BUFFER *line;
236 int num;
237 int err;
238 int x, y;
239 int i;
240
241 inifile = buf_new();
242 pingernames = buf_new();
243 goodpingers = buf_new();
244 line = buf_new();
245
246 while (1) {
247 clear();
248 standout();
249 printw("Select stats source:\n\n");
250 standend();
251
252 read_allpingers(inifile);
253 get_sections (inifile, pingernames);
254
255 num = 0;
256 buf_reset(goodpingers);
257 buf_rewind(pingernames);
258 while ((buf_getline(pingernames, line) != -1) && num < MAXPING) {
259 if (good_source (inifile, line->data)) {
260 buf_cat(goodpingers, line);
261 buf_nl(goodpingers);
262 num++;
263 }
264 }
265
266 x = 0;
267 buf_rewind(goodpingers);
268 for (i=0; i<num; i++) {
269 err = buf_getline(goodpingers, line);
270 assert (err != -1);
271 y = i;
272 if (y >= LINES - 6)
273 y -= LINES - 6, x = 40;
274 mvprintw(y + 2, x, "%c", i < 26 ? i + 'a' : i - 26 + 'A');
275 mvprintw(y + 2, x + 2, "%s", line->data);
276 }
277 y = i + 3;
278 if (y > LINES - 4)
279 y = LINES - 4;
280 mvprintw(y, 0, "* update list of pingers");
281 c = getch();
282 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
283 if (c >= 'a')
284 c -= 'a';
285 else
286 c = c - 'A' + 26;
287 if (c < num) {
288 buf_rewind(goodpingers);
289 while (c >= 0) {
290 err = buf_getline(goodpingers, line);
291 assert (err != -1);
292 c--;
293 }
294 if (download(inifile, line->data) < 0)
295 break;
296 }
297 }
298 else if (c == '*') {
299 download_list();
300 }
301 else break;
302 }
303 clear();
304
305 buf_free(inifile);
306 buf_free(line);
307 buf_free(pingernames);
308 buf_free(goodpingers);
309 }
310
311 #if 0
312 int main() {
313 strcpy(MIXDIR,"./");
314
315 BUFFER *allpingers;
316 BUFFER *pingernames;
317 BUFFER *value;
318
319 allpingers = buf_new();
320 pingernames = buf_new();
321 value = buf_new();
322
323 read_allpingers (allpingers);
324 get_sections (allpingers, pingernames);
325
326 printf("%s", pingernames->data);
327
328 get_attribute (allpingers, "noreply", "rlist", value);
329 printf("%s\n", value->data);
330
331
332 exit(0);
333 }
334
335 #endif

Properties

Name Value
svn:keywords Id

  ViewVC Help
Powered by ViewVC 1.1.5