OSDN Git Service

スコアサーバーのURL等を定義するマクロをdefines.hからreport.cに移動。
[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 /*
30  * internet resource value
31  */
32 #define HTTP_PROXY ""                   /* Default proxy url */
33 #define HTTP_PROXY_PORT 0               /* Default proxy port */
34 #define HTTP_TIMEOUT    20              /* Timeout length (second) */
35 #define SCORE_SERVER "www.kmc.gr.jp"    /* Default score server url */
36 #define SCORE_PORT 80                   /* Default score server port */
37
38 #ifdef JP
39 #define SCORE_PATH "http://www.kmc.gr.jp/~habu/local/hengscore/score.cgi"
40 #else
41 #define SCORE_PATH "http://www.kmc.gr.jp/~habu/local/hengscore-en/score.cgi"
42 #endif
43
44 /* for debug */
45 #if 0
46 #define SCORE_PATH "http://www.kmc.gr.jp/~habu/local/scoretest/score.cgi"
47 #endif
48
49 /*
50  * simple buffer library
51  */
52
53 typedef struct {
54         size_t max_size;
55         size_t size;
56         char *data;
57 } BUF;
58
59 #define BUFSIZE (65536)
60
61 #ifndef HAVE_VASPRINTF
62 #define vasprintf       Vasprintf
63
64 static int Vasprintf(char **buf, const char *fmt, va_list ap)
65 {
66         int ret;
67
68         *buf = malloc(1024);
69
70 #if defined(HAVE_VSNPRINTF)
71         ret = vsnprintf(*buf, 1024, fmt, ap);
72 #else
73         ret = vsprintf(*buf, fmt, ap);
74 #endif
75         return ret;
76 }
77
78 #endif /* ifndef HAVE_VASPRINTF */ 
79
80 static BUF* buf_new(void)
81 {
82         BUF *p;
83
84         if ((p = malloc(sizeof(BUF))) == NULL)
85                 return NULL;
86
87         p->size = 0;
88         p->max_size = BUFSIZE;
89         if ((p->data = malloc(BUFSIZE)) == NULL)
90         {
91                 free(p);
92                 return NULL;
93         }
94         return p;
95 }
96
97 static void buf_delete(BUF *b)
98 {
99         free(b->data);
100         free(b);
101 }
102
103 static int buf_append(BUF *buf, const char *data, size_t size)
104 {
105         while (buf->size + size > buf->max_size)
106         {
107                 char *tmp;
108                 if ((tmp = malloc(buf->max_size * 2)) == NULL) return -1;
109
110                 memcpy(tmp, buf->data, buf->max_size);
111                 free(buf->data);
112
113                 buf->data = tmp;
114
115                 buf->max_size *= 2;
116         }
117         memcpy(buf->data + buf->size, data, size);
118         buf->size += size;
119
120         return buf->size;
121 }
122
123 static int buf_sprintf(BUF *buf, const char *fmt, ...)
124 {
125         int             ret;
126         char    *tmpbuf;
127         va_list ap;
128
129         va_start(ap, fmt);
130         vasprintf(&tmpbuf, fmt, ap);
131         va_end(ap);
132
133         if(!tmpbuf) return -1;
134
135 #if ('\r' == 0x0a && '\n' == 0x0d)
136         {
137                 /*
138                  * Originally '\r'= CR (= 0x0d) and '\n'= LF (= 0x0a)
139                  * But for MPW (Macintosh Programers Workbench), these
140                  * are reversed so that '\r'=LF and '\n'=CR unless the
141                  * -noMapCR option is not defined.
142                  *
143                  * We need to swap back these here since the score
144                  * dump text should be written using LF as the end of
145                  * line.
146                  */
147                 char *ptr;
148                 for (ptr = tmpbuf; *ptr; ptr++)
149                 {
150                         if (0x0d == *ptr) *ptr = 0x0a;
151                 }
152         }
153 #endif
154
155         ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
156
157         free(tmpbuf);
158
159         return ret;
160 }
161
162 #if 0
163 static int buf_read(BUF *buf, int fd)
164 {
165         int len;
166 #ifndef MACINTOSH
167         char tmp[BUFSIZE];
168 #else
169         char *tmp;
170         
171         tmp = calloc( BUFSIZE , sizeof(char) );
172 #endif
173
174         while ((len = read(fd, tmp, BUFSIZE)) > 0)
175                 buf_append(buf, tmp, len);
176
177         return buf->size;
178 }
179 #endif
180
181 #if 0
182 static int buf_write(BUF *buf, int fd)
183 {
184         write(fd, buf->data, buf->size);
185
186         return buf->size;
187 }
188
189 static int buf_search(BUF *buf, const char *str)
190 {
191         char *ret;
192
193         ret = my_strstr(buf->data, str);
194
195         if (!ret) return -1;
196
197         return ret - buf->data;
198 }
199
200 static BUF * buf_subbuf(BUF *buf, int pos1, size_t sz)
201 {
202         BUF *ret;
203
204         if (pos1 < 0) return NULL;
205
206         ret = buf_new();
207
208         if (sz <= 0) sz = buf->size - pos1;
209
210         buf_append(ret, buf->data + pos1, sz);
211
212         return ret;
213 }
214 #endif
215
216 static void http_post(int sd, cptr url, BUF *buf)
217 {
218         BUF *output;
219
220         output = buf_new();
221         buf_sprintf(output, "POST %s HTTP/1.0\n", url);
222         buf_sprintf(output, "User-Agent: Hengband %d.%d.%d\n",
223                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
224
225         buf_sprintf(output, "Content-Length: %d\n", buf->size);
226         buf_sprintf(output, "Content-Encoding: binary\n");
227         buf_sprintf(output, "Content-Type: application/octet-stream\n");
228         buf_sprintf(output, "\n");
229         buf_append(output, buf->data, buf->size);
230
231         soc_write(sd, output->data, output->size);
232 }
233
234
235 /* ¥­¥ã¥é¥¯¥¿¥À¥ó¥×¤òºî¤Ã¤Æ BUF¤ËÊݸ */
236 static errr make_dump(BUF* dumpbuf)
237 {
238         char            buf[1024];
239         FILE *fff;
240         char file_name[1024];
241
242         /* Open a new file */
243         fff = my_fopen_temp(file_name, 1024);
244         if (!fff)
245         {
246 #ifdef JP
247                 msg_format("°ì»þ¥Õ¥¡¥¤¥ë %s ¤òºîÀ®¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£", file_name);
248 #else
249                 msg_format("Failed to create temporary file %s.", file_name);
250 #endif
251                 msg_print(NULL);
252                 return 1;
253         }
254
255         /* °ìö°ì»þ¥Õ¥¡¥¤¥ë¤òºî¤ë¡£Ä̾ï¤Î¥À¥ó¥×½ÐÎϤȶ¦Ä̲½¤¹¤ë¤¿¤á¡£ */
256         (void)make_character_dump(fff);
257
258         /* Close the file */
259         my_fclose(fff);
260
261         /* Open for read */
262         fff = my_fopen(file_name, "r");
263
264         while (fgets(buf, 1024, fff))
265         {
266                 (void)buf_sprintf(dumpbuf, "%s", buf);
267         }
268
269         /* Close the file */
270         my_fclose(fff);
271
272         /* Remove the file */
273         fd_kill(file_name);
274
275         /* Success */
276         return (0);
277 }
278
279 /*
280  * Make screen dump to buffer
281  */
282 cptr make_screen_dump(void)
283 {
284         BUF *screen_buf;
285         int y, x, i;
286         cptr ret;
287
288         byte a = 0, old_a = 0;
289         char c = ' ';
290
291         static cptr html_head[] = {
292                 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
293                 "<pre>",
294                 0,
295         };
296         static cptr html_foot[] = {
297                 "</pre>\n",
298                 "</body>\n</html>\n",
299                 0,
300         };
301
302         bool old_use_graphics = use_graphics;
303
304         int wid, hgt;
305
306         Term_get_size(&wid, &hgt);
307
308         /* Alloc buffer */
309         screen_buf = buf_new();
310         if (screen_buf == NULL) return (NULL);
311
312         if (old_use_graphics)
313         {
314                 /* Clear -more- prompt first */
315                 msg_print(NULL);
316
317                 use_graphics = FALSE;
318                 reset_visuals();
319
320                 /* Redraw everything */
321                 p_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
322
323                 /* Hack -- update */
324                 handle_stuff();
325         }
326
327         for (i = 0; html_head[i]; i++)
328                 buf_sprintf(screen_buf, html_head[i]);
329
330         /* Dump the screen */
331         for (y = 0; y < hgt; y++)
332         {
333                 /* Start the row */
334                 if (y != 0)
335                         buf_sprintf(screen_buf, "\n");
336
337                 /* Dump each row */
338                 for (x = 0; x < wid - 1; x++)
339                 {
340                         int rv, gv, bv;
341                         cptr cc = NULL;
342                         /* Get the attr/char */
343                         (void)(Term_what(x, y, &a, &c));
344
345                         switch (c)
346                         {
347                         case '&': cc = "&amp;"; break;
348                         case '<': cc = "&lt;"; break;
349                         case '>': cc = "&gt;"; break;
350 #ifdef WINDOWS
351                         case 0x1f: c = '.'; break;
352                         case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
353 #endif
354                         }
355
356                         a = a & 0x0F;
357                         if ((y == 0 && x == 0) || a != old_a) {
358                                 rv = angband_color_table[a][1];
359                                 gv = angband_color_table[a][2];
360                                 bv = angband_color_table[a][3];
361                                 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">", 
362                                             ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
363                                 old_a = a;
364                         }
365                         if (cc)
366                                 buf_sprintf(screen_buf, "%s", cc);
367                         else
368                                 buf_sprintf(screen_buf, "%c", c);
369                 }
370         }
371         buf_sprintf(screen_buf, "</font>");
372
373         for (i = 0; html_foot[i]; i++)
374                 buf_sprintf(screen_buf, html_foot[i]);
375
376         /* Screen dump size is too big ? */
377         if (screen_buf->size + 1> SCREEN_BUF_SIZE)
378         {
379                 ret = NULL;
380         }
381         else
382         {
383                 /* Terminate string */
384                 buf_append(screen_buf, "", 1);
385
386                 ret = string_make(screen_buf->data);
387         }
388
389         /* Free buffer */
390         buf_delete(screen_buf);
391
392         if (old_use_graphics)
393         {
394                 use_graphics = TRUE;
395                 reset_visuals();
396
397                 /* Redraw everything */
398                 p_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
399
400                 /* Hack -- update */
401                 handle_stuff();
402         }
403
404         return ret;
405 }
406
407
408 errr report_score(void)
409 {
410 #ifdef MACINTOSH
411         OSStatus err;
412 #else
413         errr err = 0;
414 #endif
415
416 #ifdef WINDOWS
417         WSADATA wsaData;
418         WORD wVersionRequested =(WORD) (( 1) |  ( 1 << 8));
419 #endif
420
421         BUF *score;
422         int sd;
423         char seikakutmp[128];
424
425         score = buf_new();
426
427 #ifdef JP
428         sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "¤Î" : ""));
429 #else
430         sprintf(seikakutmp, "%s ", ap_ptr->title);
431 #endif
432
433         buf_sprintf(score, "name: %s\n", player_name);
434 #ifdef JP
435         buf_sprintf(score, "version: ÊѶòÈÚÅÜ %d.%d.%d\n",
436                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
437 #else
438         buf_sprintf(score, "version: Hengband %d.%d.%d\n",
439                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
440 #endif
441         buf_sprintf(score, "score: %d\n", total_points());
442         buf_sprintf(score, "level: %d\n", p_ptr->lev);
443         buf_sprintf(score, "depth: %d\n", dun_level);
444         buf_sprintf(score, "maxlv: %d\n", p_ptr->max_plv);
445         buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
446         buf_sprintf(score, "au: %d\n", p_ptr->au);
447         buf_sprintf(score, "turns: %d\n", turn_real(turn));
448         buf_sprintf(score, "sex: %d\n", p_ptr->psex);
449         buf_sprintf(score, "race: %s\n", rp_ptr->title);
450         buf_sprintf(score, "class: %s\n", cp_ptr->title);
451         buf_sprintf(score, "seikaku: %s\n", seikakutmp);
452         buf_sprintf(score, "realm1: %s\n", realm_names[p_ptr->realm1]);
453         buf_sprintf(score, "realm2: %s\n", realm_names[p_ptr->realm2]);
454         buf_sprintf(score, "killer: %s\n", p_ptr->died_from);
455         buf_sprintf(score, "-----charcter dump-----\n");
456
457         make_dump(score);
458
459         if (screen_dump)
460         {
461                 buf_sprintf(score, "-----screen shot-----\n");
462                 buf_append(score, screen_dump, strlen(screen_dump));
463         }
464         
465 #ifdef WINDOWS
466         if (WSAStartup(wVersionRequested, &wsaData))
467         {
468                 msg_print("Report: WSAStartup failed.");
469                 goto report_end;
470         }
471 #endif
472
473 #ifdef MACINTOSH
474 #if TARGET_API_MAC_CARBON
475         err = InitOpenTransportInContext(kInitOTForApplicationMask, NULL);
476 #else
477         err = InitOpenTransport();
478 #endif
479         if (err != noErr)
480         {
481                 msg_print("Report: OpenTransport failed.");
482                 return 1;
483         }
484 #endif
485
486         Term_clear();
487
488         while (1)
489         {
490                 char buff[160];
491 #ifdef JP
492                 prt("ÀܳÃæ...", 0, 0);
493 #else
494                 prt("connecting...", 0, 0);
495 #endif
496                 Term_fresh();
497                 
498                 /* ¥×¥í¥­¥·¤òÀßÄꤹ¤ë */
499                 set_proxy(HTTP_PROXY, HTTP_PROXY_PORT);
500
501                 /* Connect to the score server */
502                 sd = connect_server(HTTP_TIMEOUT, SCORE_SERVER, SCORE_PORT);
503
504
505                 if (!(sd < 0)) break;
506 #ifdef JP
507                 sprintf(buff, "¥¹¥³¥¢¡¦¥µ¡¼¥Ð¤Ø¤ÎÀܳ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£(%s)", soc_err());
508 #else
509                 sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
510 #endif
511                 prt(buff, 0, 0);
512                 (void)inkey();
513                 
514 #ifdef JP
515                 if (!get_check_strict("¤â¤¦°ìÅÙÀܳ¤ò»î¤ß¤Þ¤¹¤«? ", CHECK_NO_HISTORY))
516 #else
517                 if (!get_check_strict("Try again? ", CHECK_NO_HISTORY))
518 #endif
519                 {
520                         err = 1;
521                         goto report_end;
522                 }
523         }
524 #ifdef JP
525         prt("¥¹¥³¥¢Á÷¿®Ãæ...", 0, 0);
526 #else
527         prt("Sending the score...", 0, 0);
528 #endif
529         Term_fresh();
530         http_post(sd, SCORE_PATH, score);
531
532         disconnect_server(sd);
533  report_end:
534 #ifdef WINDOWS
535         WSACleanup();
536 #endif
537
538 #ifdef MACINTOSH
539 #if TARGET_API_MAC_CARBON
540         CloseOpenTransportInContext(NULL);
541 #else
542         CloseOpenTransport();
543 #endif
544 #endif
545
546         return err;
547 }
548
549 #endif /* WORLD_SCORE */