OSDN Git Service

ビホルダーの訳もtwelveを20と間違っていたので修正。
[hengband/hengband.git] / src / report.c
1 /* File: report.c */
2
3 #define _GNU_SOURCE
4 #include "angband.h"
5
6 #ifdef WORLD_SCORE
7
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <ctype.h>
11 #include <string.h>
12
13 #if defined(WINDOWS)
14 #include <winsock.h>
15 #elif defined(MACINTOSH)
16 #include <OpenTransport.h>
17 #include <OpenTptInternet.h>
18 #else
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netdb.h>
23 #include <sys/time.h>
24
25 #include <setjmp.h>
26 #include <signal.h>
27 #endif
28
29 #ifdef JP
30 #define SCORE_PATH "http://www.kmc.gr.jp/~habu/local/hengscore/score.cgi"
31 #else
32 #define SCORE_PATH "http://www.kmc.gr.jp/~habu/local/hengscore-en/score.cgi"
33 #endif
34
35 /* for debug */
36 /*#define SCORE_PATH "http://www.kmc.gr.jp/~habu/local/scoretest/score.cgi" */
37
38 /*
39  * simple buffer library
40  */
41
42 typedef struct {
43         size_t max_size;
44         size_t size;
45         char *data;
46 } BUF;
47
48 #define BUFSIZE (65536)
49
50 #ifndef HAVE_VASPRINTF
51 #define vasprintf       Vasprintf
52
53 static int Vasprintf(char **buf, const char *fmt, va_list ap)
54 {
55         int ret;
56
57         *buf = malloc(1024);
58
59 #if defined(HAVE_VSNPRINTF)
60         ret = vsnprintf(*buf, 1024, fmt, ap);
61 #else
62         ret = vsprintf(*buf, fmt, ap);
63 #endif
64         return ret;
65 }
66
67 #endif /* ifndef HAVE_VASPRINTF */ 
68
69 static BUF* buf_new(void)
70 {
71         BUF *p;
72
73         if ((p = malloc(sizeof(BUF))) == NULL)
74                 return NULL;
75
76         p->size = 0;
77         p->max_size = BUFSIZE;
78         if ((p->data = malloc(BUFSIZE)) == NULL)
79         {
80                 free(p);
81                 return NULL;
82         }
83         return p;
84 }
85
86 static void buf_delete(BUF *b)
87 {
88         free(b->data);
89         free(b);
90 }
91
92 static int buf_append(BUF *buf, const char *data, size_t size)
93 {
94         while (buf->size + size > buf->max_size)
95         {
96                 char *tmp;
97                 if ((tmp = malloc(buf->max_size * 2)) == NULL) return -1;
98
99                 memcpy(tmp, buf->data, buf->max_size);
100                 free(buf->data);
101
102                 buf->data = tmp;
103
104                 buf->max_size *= 2;
105         }
106         memcpy(buf->data + buf->size, data, size);
107         buf->size += size;
108
109         return buf->size;
110 }
111
112 static int buf_sprintf(BUF *buf, const char *fmt, ...)
113 {
114         int             ret;
115         char    *tmpbuf;
116         va_list ap;
117
118         va_start(ap, fmt);
119         vasprintf(&tmpbuf, fmt, ap);
120         va_end(ap);
121
122         if(!tmpbuf) return -1;
123
124         ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
125
126         free(tmpbuf);
127
128         return ret;
129 }
130
131 #if 0
132 static int buf_read(BUF *buf, int fd)
133 {
134         int len;
135 #ifndef MACINTOSH
136         char tmp[BUFSIZE];
137 #else
138         char *tmp;
139         
140         tmp = calloc( BUFSIZE , sizeof(char) );
141 #endif
142
143         while ((len = read(fd, tmp, BUFSIZE)) > 0)
144                 buf_append(buf, tmp, len);
145
146         return buf->size;
147 }
148 #endif
149
150 #if 0
151 static int buf_write(BUF *buf, int fd)
152 {
153         write(fd, buf->data, buf->size);
154
155         return buf->size;
156 }
157
158 static int buf_search(BUF *buf, const char *str)
159 {
160         char *ret;
161
162         ret = strstr(buf->data, str);
163
164         if (!ret) return -1;
165
166         return ret - buf->data;
167 }
168
169 static BUF * buf_subbuf(BUF *buf, int pos1, size_t sz)
170 {
171         BUF *ret;
172
173         if (pos1 < 0) return NULL;
174
175         ret = buf_new();
176
177         if (sz <= 0) sz = buf->size - pos1;
178
179         buf_append(ret, buf->data + pos1, sz);
180
181         return ret;
182 }
183 #endif
184
185 static void http_post(int sd, char *url, BUF *buf)
186 {
187         BUF *output;
188
189         output = buf_new();
190         buf_sprintf(output, "POST %s HTTP/1.0\r\n", url);
191         buf_sprintf(output, "User-Agent: Hengband %d.%d.%d\r\n",
192                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
193
194         buf_sprintf(output, "Content-Length: %d\r\n", buf->size);
195         buf_sprintf(output, "Content-Encoding: binary\r\n");
196         buf_sprintf(output, "Content-Type: application/octet-stream\r\n");
197         buf_sprintf(output, "\r\n");
198         buf_append(output, buf->data, buf->size);
199
200         soc_write(sd, output->data, output->size);
201 }
202
203
204 /* ¥­¥ã¥é¥¯¥¿¥À¥ó¥×¤òºî¤Ã¤Æ BUF¤ËÊݸ */
205 static errr make_dump(BUF* dumpbuf)
206 {
207         errr make_character_dump(FILE *fff);
208
209         char            buf[1024];
210         FILE *fff;
211         char file_name[1024];
212
213         /* Open a new file */
214         fff = my_fopen_temp(file_name, 1024);
215         if (!fff)
216         {
217 #ifdef JP
218                 msg_format("°ì»þ¥Õ¥¡¥¤¥ë %s ¤òºîÀ®¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£", file_name);
219 #else
220                 msg_format("Failed to create temporary file %s.", file_name);
221 #endif
222                 msg_print(NULL);
223                 return 1;
224         }
225
226         /* °ìö°ì»þ¥Õ¥¡¥¤¥ë¤òºî¤ë¡£Ä̾ï¤Î¥À¥ó¥×½ÐÎϤȶ¦Ä̲½¤¹¤ë¤¿¤á¡£ */
227         (void)make_character_dump(fff);
228
229         /* Close the file */
230         my_fclose(fff);
231
232         /* Open for read */
233         fff = my_fopen(file_name, "r");
234
235         while (fgets(buf, 1024, fff))
236         {
237                 (void)buf_append(dumpbuf, buf, strlen(buf));
238         }
239
240         /* Close the file */
241         my_fclose(fff);
242
243         /* Remove the file */
244         fd_kill(file_name);
245
246         /* Success */
247         return (0);
248 }
249
250 /*
251  * Make screen dump to buffer
252  */
253 cptr make_screen_dump(void)
254 {
255         BUF *screen_buf;
256         int y, x, i;
257         cptr ret;
258
259         byte a = 0, old_a = 0;
260         char c = ' ';
261
262         static char *html_head[] = {
263                 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
264                 "<pre>",
265                 0,
266         };
267         static char *html_foot[] = {
268                 "</pre>\n",
269                 "</body>\n</html>\n",
270                 0,
271         };
272
273         if (use_graphics)
274                 return NULL;
275
276         /* Alloc buffer */
277         screen_buf = buf_new();
278         if (screen_buf == NULL) return (NULL);
279
280         for (i = 0; html_head[i]; i++)
281                 buf_sprintf(screen_buf, html_head[i]);
282
283         /* Dump the screen */
284         for (y = 0; y < 24; y++)
285         {
286                 /* Start the row */
287                 if (y != 0)
288                         buf_sprintf(screen_buf, "\n");
289
290                 /* Dump each row */
291                 for (x = 0; x < 79; x++)
292                 {
293                         int rv, gv, bv;
294                         char *cc = NULL;
295                         /* Get the attr/char */
296                         (void)(Term_what(x, y, &a, &c));
297
298                         switch (c)
299                         {
300                         case '&': cc = "&amp;"; break;
301                         case '<': cc = "&lt;"; break;
302                         case '>': cc = "&gt;"; break;
303 #ifdef WINDOWS
304                         case 0x1f: c = '.'; break;
305                         case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
306 #endif
307                         }
308
309                         a = a & 0x0F;
310                         if ((y == 0 && x == 0) || a != old_a) {
311                                 rv = angband_color_table[a][1];
312                                 gv = angband_color_table[a][2];
313                                 bv = angband_color_table[a][3];
314                                 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">", 
315                                             ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
316                                 old_a = a;
317                         }
318                         if (cc)
319                                 buf_sprintf(screen_buf, "%s", cc);
320                         else
321                                 buf_sprintf(screen_buf, "%c", c);
322                 }
323         }
324         buf_sprintf(screen_buf, "</font>");
325
326         for (i = 0; html_foot[i]; i++)
327                 buf_sprintf(screen_buf, html_foot[i]);
328
329         /* Screen dump size is too big ? */
330         if (screen_buf->size + 1> SCREEN_BUF_SIZE)
331         {
332                 buf_delete(screen_buf);
333                 return (NULL);
334         }
335
336         /* Terminate string */
337         buf_append(screen_buf, "", 1);
338
339         ret = string_make(screen_buf->data);
340
341         /* Free buffer */
342         buf_delete(screen_buf);
343
344         return ret;
345 }
346
347
348 errr report_score(void)
349 {
350 #ifdef MACINTOSH
351         OSStatus err;
352 #else
353         errr err = 0;
354 #endif
355
356 #ifdef WINDOWS
357         WSADATA wsaData;
358         WORD wVersionRequested =(WORD) (( 1) |  ( 1 << 8));
359 #endif
360
361         BUF *score;
362         int sd;
363         char seikakutmp[128];
364
365         score = buf_new();
366
367 #ifdef JP
368         sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "¤Î" : ""));
369 #else
370         sprintf(seikakutmp, "%s ", ap_ptr->title);
371 #endif
372
373         buf_sprintf(score, "name: %s\n", player_name);
374 #ifdef JP
375         buf_sprintf(score, "version: ÊѶòÈÚÅÜ %d.%d.%d\n",
376                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
377 #else
378         buf_sprintf(score, "version: Hengband %d.%d.%d\n",
379                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
380 #endif
381         buf_sprintf(score, "score: %d\n", total_points());
382         buf_sprintf(score, "level: %d\n", p_ptr->lev);
383         buf_sprintf(score, "depth: %d\n", dun_level);
384         buf_sprintf(score, "maxlv: %d\n", p_ptr->max_plv);
385         buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
386         buf_sprintf(score, "au: %d\n", p_ptr->au);
387         buf_sprintf(score, "turns: %d\n", turn_real(turn));
388         buf_sprintf(score, "sex: %d\n", p_ptr->psex);
389         buf_sprintf(score, "race: %s\n", rp_ptr->title);
390         buf_sprintf(score, "class: %s\n", cp_ptr->title);
391         buf_sprintf(score, "seikaku: %s\n", seikakutmp);
392         buf_sprintf(score, "realm1: %s\n", realm_names[p_ptr->realm1]);
393         buf_sprintf(score, "realm2: %s\n", realm_names[p_ptr->realm2]);
394         buf_sprintf(score, "killer: %s\n", died_from);
395         buf_sprintf(score, "-----charcter dump-----\n");
396
397         make_dump(score);
398
399         if (screen_dump)
400         {
401                 buf_sprintf(score, "-----screen shot-----\n");
402                 buf_append(score, screen_dump, strlen(screen_dump));
403         }
404         
405 #ifdef WINDOWS
406         if (WSAStartup(wVersionRequested, &wsaData))
407         {
408                 msg_print("Report: WSAStartup failed.");
409                 goto report_end;
410         }
411 #endif
412
413 #ifdef MACINTOSH
414 #if TARGET_API_MAC_CARBON
415         err = InitOpenTransportInContext(kInitOTForApplicationMask, NULL);
416 #else
417         err = InitOpenTransport();
418 #endif
419         if (err != noErr)
420         {
421                 msg_print("Report: OpenTransport failed.");
422                 return 1;
423         }
424 #endif
425
426         Term_clear();
427
428         while (1)
429         {
430                 char buff[160];
431 #ifdef JP
432                 prt("ÀܳÃæ...", 0, 0);
433 #else
434                 prt("connecting...", 0, 0);
435 #endif
436                 Term_fresh();
437                 
438                 sd = connect_scoreserver();
439                 if (!(sd < 0)) break;
440 #ifdef JP
441                 sprintf(buff, "¥¹¥³¥¢¡¦¥µ¡¼¥Ð¤Ø¤ÎÀܳ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£(%s)", soc_err());
442 #else
443                 sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
444 #endif
445                 prt(buff, 0, 0);
446                 (void)inkey();
447                 
448 #ifdef JP
449                 if (!get_check("¤â¤¦°ìÅÙÀܳ¤ò»î¤ß¤Þ¤¹¤«? "))
450 #else
451                 if (!get_check("Try again? "))
452 #endif
453                 {
454                         err = 1;
455                         goto report_end;
456                 }
457         }
458 #ifdef JP
459         prt("¥¹¥³¥¢Á÷¿®Ãæ...", 0, 0);
460 #else
461         prt("Sending the score...", 0, 0);
462 #endif
463         Term_fresh();
464         http_post(sd, SCORE_PATH, score);
465
466         disconnect_server(sd);
467  report_end:
468 #ifdef WINDOWS
469         WSACleanup();
470 #endif
471
472 #ifdef MACINTOSH
473 #if TARGET_API_MAC_CARBON
474         CloseOpenTransportInContext(NULL);
475 #else
476         CloseOpenTransport();
477 #endif
478 #endif
479
480         return err;
481 }
482
483 #endif /* WORLD_SCORE */