OSDN Git Service

[Refactor] #37353 player-race.c/h を作成して関連構造体と変数を移動.
[hengband/hengband.git] / src / report.c
1 /*!
2  * @file report.c
3  * @brief スコアサーバ転送機能の実装
4  * @date 2014/07/14
5  * @author Hengband Team
6  */
7
8 #define _GNU_SOURCE /*!< 未使用*/
9 #include "angband.h"
10 #include "util.h"
11
12 #include "floor.h"
13 #include "player-status.h"
14 #include "player-class.h"
15 #include "player-race.h"
16 #include "files.h"
17 #include "world.h"
18
19 #ifdef WORLD_SCORE
20
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <ctype.h>
24 #include <string.h>
25
26 #if defined(WINDOWS)
27 #include <winsock.h>
28 #elif defined(MACINTOSH)
29 #include <OpenTransport.h>
30 #include <OpenTptInternet.h>
31 #else
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netdb.h>
36 #include <sys/time.h>
37
38 #include <setjmp.h>
39 #include <signal.h>
40 #endif
41
42 /*
43  * internet resource value
44  */
45 #define HTTP_PROXY ""                   /*!< デフォルトのプロキシURL / Default proxy url */
46 #define HTTP_PROXY_PORT 0               /*!< デフォルトのプロキシポート / Default proxy port */
47 #define HTTP_TIMEOUT    20              /*!< デフォルトのタイムアウト時間(秒) / Timeout length (second) */
48 #define SCORE_SERVER "hengband.osdn.jp" /*!< デフォルトのスコアサーバURL / Default score server url */
49 #define SCORE_PORT 80                   /*!< デフォルトのスコアサーバポート / Default score server port */
50
51 #ifdef JP
52 #define SCORE_PATH "http://hengband.osdn.jp/score/register_score.php" /*!< スコア開示URL */
53 #else
54 #define SCORE_PATH "http://moon.kmc.gr.jp/hengband/hengscore-en/score.cgi" /*!< スコア開示URL */
55 #endif
56
57 /* for debug */
58 #if 0
59 #define SCORE_PATH "http://moon.kmc.gr.jp/hengband/scoretest/score.cgi" /*!< スコア開示URL */
60 #endif
61
62 /*
63  * simple buffer library
64  */
65 typedef struct {
66         size_t max_size;
67         size_t size;
68         char *data;
69 } BUF;
70
71 #define BUFSIZE (65536) /*!< スコアサーバ転送バッファサイズ */
72
73 /*!
74  * @brief 転送用バッファの確保
75  * @return 確保したバッファの参照ポインタ
76  */
77 static BUF* buf_new(void)
78 {
79         BUF *p;
80
81         if ((p = malloc(sizeof(BUF))) == NULL)
82                 return NULL;
83
84         p->size = 0;
85         p->max_size = BUFSIZE;
86         if ((p->data = malloc(BUFSIZE)) == NULL)
87         {
88                 free(p);
89                 return NULL;
90         }
91         return p;
92 }
93
94 /*!
95  * @brief 転送用バッファの解放
96  * @param b 解放するバッファの参照ポインタ
97  */
98 static void buf_delete(BUF *b)
99 {
100         free(b->data);
101         free(b);
102 }
103
104 /*!
105  * @brief 転送用バッファにデータを追加する
106  * @param buf 追加先バッファの参照ポインタ
107  * @param data 追加元データ
108  * @param size 追加サイズ
109  * @return 追加後のバッファ容量
110  */
111 static int buf_append(BUF *buf, concptr data, size_t size)
112 {
113         while (buf->size + size > buf->max_size)
114         {
115                 char *tmp;
116                 if ((tmp = malloc(buf->max_size * 2)) == NULL) return -1;
117
118                 memcpy(tmp, buf->data, buf->max_size);
119                 free(buf->data);
120
121                 buf->data = tmp;
122
123                 buf->max_size *= 2;
124         }
125         memcpy(buf->data + buf->size, data, size);
126         buf->size += size;
127
128         return buf->size;
129 }
130
131 /*!
132  * @brief 転送用バッファにフォーマット指定した文字列データを追加する
133  * @param buf 追加先バッファの参照ポインタ
134  * @param fmt 文字列フォーマット
135  * @return 追加後のバッファ容量
136  */
137 static int buf_sprintf(BUF *buf, concptr fmt, ...)
138 {
139         int             ret;
140         char    tmpbuf[8192];
141         va_list ap;
142
143         va_start(ap, fmt);
144 #if defined(HAVE_VSNPRINTF)
145         ret = vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
146 #else
147         ret = vsprintf(tmpbuf, fmt, ap);
148 #endif
149         va_end(ap);
150
151         if (ret < 0) return -1;
152
153 #if ('\r' == 0x0a && '\n' == 0x0d)
154         {
155                 /*
156                  * Originally '\r'= CR (= 0x0d) and '\n'= LF (= 0x0a)
157                  * But for MPW (Macintosh Programers Workbench), these
158                  * are reversed so that '\r'=LF and '\n'=CR unless the
159                  * -noMapCR option is not defined.
160                  *
161                  * We need to swap back these here since the score
162                  * dump text should be written using LF as the end of
163                  * line.
164                  */
165                 char *ptr;
166                 for (ptr = tmpbuf; *ptr; ptr++)
167                 {
168                         if (0x0d == *ptr) *ptr = 0x0a;
169                 }
170         }
171 #endif
172
173         ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
174
175         return ret;
176 }
177
178 #if 0
179 static int buf_read(BUF *buf, int fd)
180 {
181         int len;
182 #ifndef MACINTOSH
183         char tmp[BUFSIZE];
184 #else
185         char *tmp;
186         
187         tmp = calloc( BUFSIZE , sizeof(char) );
188 #endif
189
190         while ((len = read(fd, tmp, BUFSIZE)) > 0)
191                 buf_append(buf, tmp, len);
192
193         return buf->size;
194 }
195 #endif
196
197 #if 0
198 static int buf_write(BUF *buf, int fd)
199 {
200         write(fd, buf->data, buf->size);
201
202         return buf->size;
203 }
204
205 static int buf_search(BUF *buf, concptr str)
206 {
207         char *ret;
208
209         ret = my_strstr(buf->data, str);
210
211         if (!ret) return -1;
212
213         return ret - buf->data;
214 }
215
216 static BUF * buf_subbuf(BUF *buf, int pos1, size_t sz)
217 {
218         BUF *ret;
219
220         if (pos1 < 0) return NULL;
221
222         ret = buf_new();
223
224         if (sz <= 0) sz = buf->size - pos1;
225
226         buf_append(ret, buf->data + pos1, sz);
227
228         return ret;
229 }
230 #endif
231
232 /*!
233  * @brief HTTPによるダンプ内容伝送
234  * @param sd ソケットID
235  * @param url 伝送先URL
236  * @param buf 伝送内容バッファ
237  * @return なし
238  */
239 static bool http_post(int sd, concptr url, BUF *buf)
240 {
241         BUF *output;
242         char response_buf[1024] = "";
243         concptr HTTP_RESPONSE_CODE_OK = "HTTP/1.1 200 OK";
244
245         output = buf_new();
246         buf_sprintf(output, "POST %s HTTP/1.0\r\n", url);
247         buf_sprintf(output, "User-Agent: Hengband %d.%d.%d\r\n",
248                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
249
250         buf_sprintf(output, "Content-Length: %d\r\n", buf->size);
251         buf_sprintf(output, "Content-Encoding: binary\r\n");
252 #ifdef JP
253 #ifdef SJIS
254         buf_sprintf(output, "Content-Type: text/plain; charset=SHIFT_JIS\r\n");
255 #endif
256 #ifdef EUC
257         buf_sprintf(output, "Content-Type: text/plain; charset=EUC-JP\r\n");
258 #endif
259 #else
260         buf_sprintf(output, "Content-Type: text/plain; charset=ASCII\r\n");
261 #endif
262         buf_sprintf(output, "\r\n");
263         buf_append(output, buf->data, buf->size);
264
265         soc_write(sd, output->data, output->size);
266
267         soc_read(sd, response_buf, sizeof(response_buf));
268
269         return strncmp(response_buf, HTTP_RESPONSE_CODE_OK, strlen(HTTP_RESPONSE_CODE_OK)) == 0;
270 }
271
272 /*!
273  * @brief キャラクタダンプを作って BUFに保存
274  * @param dumpbuf 伝送内容バッファ
275  * @return エラーコード
276  */
277 static errr make_dump(BUF* dumpbuf)
278 {
279         char            buf[1024];
280         FILE *fff;
281         GAME_TEXT file_name[1024];
282
283         /* Open a new file */
284         fff = my_fopen_temp(file_name, 1024);
285         if (!fff)
286         {
287 #ifdef JP
288                 msg_format("一時ファイル %s を作成できませんでした。", file_name);
289 #else
290                 msg_format("Failed to create temporary file %s.", file_name);
291 #endif
292                 msg_print(NULL);
293                 return 1;
294         }
295
296         /* 一旦一時ファイルを作る。通常のダンプ出力と共通化するため。 */
297         (void)make_character_dump(fff);
298         my_fclose(fff);
299
300         /* Open for read */
301         fff = my_fopen(file_name, "r");
302
303         while (fgets(buf, 1024, fff))
304         {
305                 (void)buf_sprintf(dumpbuf, "%s", buf);
306         }
307         my_fclose(fff);
308         fd_kill(file_name);
309
310         /* Success */
311         return (0);
312 }
313
314 /*!
315  * @brief スクリーンダンプを作成する/ Make screen dump to buffer
316  * @return 作成したスクリーンダンプの参照ポインタ
317  */
318 concptr make_screen_dump(void)
319 {
320         BUF *screen_buf;
321         int y, x, i;
322         concptr ret;
323
324         TERM_COLOR a = 0, old_a = 0;
325         SYMBOL_CODE c = ' ';
326
327         static concptr html_head[] = {
328                 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
329                 "<pre>",
330                 0,
331         };
332         static concptr html_foot[] = {
333                 "</pre>\n",
334                 "</body>\n</html>\n",
335                 0,
336         };
337
338         bool old_use_graphics = use_graphics;
339
340         int wid, hgt;
341
342         Term_get_size(&wid, &hgt);
343
344         /* Alloc buffer */
345         screen_buf = buf_new();
346         if (screen_buf == NULL) return (NULL);
347
348         if (old_use_graphics)
349         {
350                 /* Clear -more- prompt first */
351                 msg_print(NULL);
352
353                 use_graphics = FALSE;
354                 reset_visuals();
355
356                 p_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
357                 handle_stuff();
358         }
359
360         for (i = 0; html_head[i]; i++)
361                 buf_sprintf(screen_buf, html_head[i]);
362
363         /* Dump the screen */
364         for (y = 0; y < hgt; y++)
365         {
366                 /* Start the row */
367                 if (y != 0)
368                         buf_sprintf(screen_buf, "\n");
369
370                 /* Dump each row */
371                 for (x = 0; x < wid - 1; x++)
372                 {
373                         int rv, gv, bv;
374                         concptr cc = NULL;
375                         /* Get the attr/char */
376                         (void)(Term_what(x, y, &a, &c));
377
378                         switch (c)
379                         {
380                         case '&': cc = "&amp;"; break;
381                         case '<': cc = "&lt;"; break;
382                         case '>': cc = "&gt;"; break;
383                         case '"': cc = "&quot;"; break;
384                         case '\'': cc = "&#39;"; break;
385 #ifdef WINDOWS
386                         case 0x1f: c = '.'; break;
387                         case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
388 #endif
389                         }
390
391                         a = a & 0x0F;
392                         if ((y == 0 && x == 0) || a != old_a) {
393                                 rv = angband_color_table[a][1];
394                                 gv = angband_color_table[a][2];
395                                 bv = angband_color_table[a][3];
396                                 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">", 
397                                             ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
398                                 old_a = a;
399                         }
400                         if (cc)
401                                 buf_sprintf(screen_buf, "%s", cc);
402                         else
403                                 buf_sprintf(screen_buf, "%c", c);
404                 }
405         }
406         buf_sprintf(screen_buf, "</font>");
407
408         for (i = 0; html_foot[i]; i++)
409                 buf_sprintf(screen_buf, html_foot[i]);
410
411         /* Screen dump size is too big ? */
412         if (screen_buf->size + 1> SCREEN_BUF_MAX_SIZE)
413         {
414                 ret = NULL;
415         }
416         else
417         {
418                 /* Terminate string */
419                 buf_append(screen_buf, "", 1);
420
421                 ret = string_make(screen_buf->data);
422         }
423
424         /* Free buffer */
425         buf_delete(screen_buf);
426
427         if (old_use_graphics)
428         {
429                 use_graphics = TRUE;
430                 reset_visuals();
431
432                 p_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
433                 handle_stuff();
434         }
435
436         return ret;
437 }
438
439 /*!
440  * @brief スコア転送処理のメインルーチン
441  * @return エラーコード
442  */
443 errr report_score(void)
444 {
445 #ifdef MACINTOSH
446         OSStatus err;
447 #else
448         errr err = 0;
449 #endif
450
451 #ifdef WINDOWS
452         WSADATA wsaData;
453         WORD wVersionRequested =(WORD) (( 1) |  ( 1 << 8));
454 #endif
455
456         BUF *score;
457         int sd;
458         char seikakutmp[128];
459
460         score = buf_new();
461
462 #ifdef JP
463         sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "の" : ""));
464 #else
465         sprintf(seikakutmp, "%s ", ap_ptr->title);
466 #endif
467
468         buf_sprintf(score, "name: %s\n", p_ptr->name);
469 #ifdef JP
470         buf_sprintf(score, "version: 変愚蛮怒 %d.%d.%d\n",
471                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
472 #else
473         buf_sprintf(score, "version: Hengband %d.%d.%d\n",
474                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
475 #endif
476         buf_sprintf(score, "score: %d\n", calc_score());
477         buf_sprintf(score, "level: %d\n", p_ptr->lev);
478         buf_sprintf(score, "depth: %d\n", current_floor_ptr->dun_level);
479         buf_sprintf(score, "maxlv: %d\n", p_ptr->max_plv);
480         buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
481         buf_sprintf(score, "au: %d\n", p_ptr->au);
482         buf_sprintf(score, "turns: %d\n", turn_real(current_world_ptr->game_turn));
483         buf_sprintf(score, "sex: %d\n", p_ptr->psex);
484         buf_sprintf(score, "race: %s\n", rp_ptr->title);
485         buf_sprintf(score, "class: %s\n", cp_ptr->title);
486         buf_sprintf(score, "seikaku: %s\n", seikakutmp);
487         buf_sprintf(score, "realm1: %s\n", realm_names[p_ptr->realm1]);
488         buf_sprintf(score, "realm2: %s\n", realm_names[p_ptr->realm2]);
489         buf_sprintf(score, "killer: %s\n", p_ptr->died_from);
490         buf_sprintf(score, "-----charcter dump-----\n");
491
492         make_dump(score);
493
494         if (screen_dump)
495         {
496                 buf_sprintf(score, "-----screen shot-----\n");
497                 buf_append(score, screen_dump, strlen(screen_dump));
498         }
499         
500 #ifdef WINDOWS
501         if (WSAStartup(wVersionRequested, &wsaData))
502         {
503                 msg_print("Report: WSAStartup failed.");
504                 goto report_end;
505         }
506 #endif
507
508 #ifdef MACINTOSH
509 #if TARGET_API_MAC_CARBON
510         err = InitOpenTransportInContext(kInitOTForApplicationMask, NULL);
511 #else
512         err = InitOpenTransport();
513 #endif
514         if (err != noErr)
515         {
516                 msg_print("Report: OpenTransport failed.");
517                 return 1;
518         }
519 #endif
520
521         Term_clear();
522
523         while (1)
524         {
525                 char buff[160];
526 #ifdef JP
527                 prt("接続中...", 0, 0);
528 #else
529                 prt("connecting...", 0, 0);
530 #endif
531                 Term_fresh();
532                 
533                 /* プロキシを設定する */
534                 set_proxy(HTTP_PROXY, HTTP_PROXY_PORT);
535
536                 /* Connect to the score server */
537                 sd = connect_server(HTTP_TIMEOUT, SCORE_SERVER, SCORE_PORT);
538
539
540                 if (sd < 0) {
541 #ifdef JP
542                         sprintf(buff, "スコア・サーバへの接続に失敗しました。(%s)", soc_err());
543 #else
544                         sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
545 #endif
546                         prt(buff, 0, 0);
547                         (void)inkey();
548
549 #ifdef JP
550                         if (!get_check_strict("もう一度接続を試みますか? ", CHECK_NO_HISTORY))
551 #else
552                         if (!get_check_strict("Try again? ", CHECK_NO_HISTORY))
553 #endif
554                         {
555                                 err = 1;
556                                 goto report_end;
557                         }
558
559                         continue;
560                 }
561
562 #ifdef JP
563                 prt("スコア送信中...", 0, 0);
564 #else
565                 prt("Sending the score...", 0, 0);
566 #endif
567                 Term_fresh();
568
569                 if (!http_post(sd, SCORE_PATH, score)) {
570                         disconnect_server(sd);
571 #ifdef JP
572                         sprintf(buff, "スコア・サーバへの送信に失敗しました。");
573 #else
574                         sprintf(buff, "Failed to send to the score server.");
575 #endif
576                         prt(buff, 0, 0);
577                         (void)inkey();
578
579 #ifdef JP
580                         if (!get_check_strict("もう一度接続を試みますか? ", CHECK_NO_HISTORY))
581 #else
582                         if (!get_check_strict("Try again? ", CHECK_NO_HISTORY))
583 #endif
584                         {
585                                 err = 1;
586                                 goto report_end;
587                         }
588
589                         continue;
590                 }
591
592                 disconnect_server(sd);
593                 break;
594         }
595
596  report_end:
597 #ifdef WINDOWS
598         WSACleanup();
599 #endif
600
601 #ifdef MACINTOSH
602 #if TARGET_API_MAC_CARBON
603         CloseOpenTransportInContext(NULL);
604 #else
605         CloseOpenTransport();
606 #endif
607 #endif
608
609         return err;
610 }
611
612 #endif /* WORLD_SCORE */