OSDN Git Service

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