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

  ViewVC Help
Powered by ViewVC 1.1.5