OSDN Git Service

[Implement] 一週間以上前のデバッグログの自動削除
[hengband/hengband.git] / src / io / report.c
1 /*!
2  * @file report.c
3  * @brief スコアサーバ転送機能の実装
4  * @date 2014/07/14
5  * @author Hengband Team
6  */
7
8 #include "io/report.h"
9 #include "core/asking-player.h"
10 #include "core/player-redraw-types.h"
11 #include "core/stuff-handler.h"
12 #include "core/turn-compensator.h"
13 #include "core/visuals-reseter.h"
14 #include "dungeon/dungeon.h"
15 #include "game-option/special-options.h"
16 #include "io-dump/character-dump.h"
17 #include "io/inet.h"
18 #include "io/input-key-acceptor.h"
19 #include "player/player-class.h"
20 #include "player/player-personality.h"
21 #include "player/player-race.h"
22 #include "realm/realm-names-table.h"
23 #include "system/angband-version.h"
24 #include "system/floor-type-definition.h"
25 #include "system/system-variables.h"
26 #include "term/gameterm.h"
27 #include "term/screen-processor.h"
28 #include "util/angband-files.h"
29 #include "view/display-messages.h"
30 #include "world/world.h"
31
32 #ifdef WORLD_SCORE
33 #ifdef WINDOWS
34 #include <winsock.h>
35 #else
36 #include <netdb.h>
37 #include <netinet/in.h>
38 #include <sys/socket.h>
39 #include <sys/time.h>
40 #include <sys/types.h>
41
42 #include <setjmp.h>
43 #include <signal.h>
44 #endif
45
46 concptr screen_dump = NULL;
47
48 /*
49  * internet resource value
50  */
51 #define HTTP_PROXY "" /*!< デフォルトのプロキシURL / Default proxy url */
52 #define HTTP_PROXY_PORT 0 /*!< デフォルトのプロキシポート / Default proxy port */
53 #define HTTP_TIMEOUT 20 /*!< デフォルトのタイムアウト時間(秒) / Timeout length (second) */
54 #define SCORE_SERVER "hengband.osdn.jp" /*!< デフォルトのスコアサーバURL / Default score server url */
55 #define SCORE_PORT 80 /*!< デフォルトのスコアサーバポート / Default score server port */
56
57 #ifdef JP
58 #define SCORE_PATH "http://hengband.osdn.jp/score/register_score.php" /*!< スコア開示URL */
59 #else
60 #define SCORE_PATH "http://moon.kmc.gr.jp/hengband/hengscore-en/score.cgi" /*!< スコア開示URL */
61 #endif
62
63 /*
64  * simple buffer library
65  */
66 typedef struct {
67     size_t max_size;
68     size_t size;
69     char *data;
70 } BUF;
71
72 #define BUFSIZE (65536) /*!< スコアサーバ転送バッファサイズ */
73
74 /*!
75  * @brief 転送用バッファの確保
76  * @return 確保したバッファの参照ポインタ
77  */
78 static BUF *buf_new(void)
79 {
80     BUF *p;
81     p = malloc(sizeof(BUF));
82     if (!p)
83         return NULL;
84
85     p->size = 0;
86     p->max_size = BUFSIZE;
87     p->data = malloc(BUFSIZE);
88     if (!p->data) {
89         free(p);
90         return NULL;
91     }
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         char *tmp;
117         if ((tmp = malloc(buf->max_size * 2)) == NULL)
118             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)
154         return -1;
155
156     ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
157     return ret;
158 }
159
160 /*!
161  * @brief HTTPによるダンプ内容伝送
162  * @param sd ソケットID
163  * @param url 伝送先URL
164  * @param buf 伝送内容バッファ
165  * @return なし
166  */
167 static bool http_post(int sd, concptr url, BUF *buf)
168 {
169     BUF *output;
170     char response_buf[1024] = "";
171     concptr HTTP_RESPONSE_CODE_OK = "HTTP/1.1 200 OK";
172
173     output = buf_new();
174     buf_sprintf(output, "POST %s HTTP/1.0\r\n", url);
175     buf_sprintf(output, "User-Agent: Hengband %d.%d.%d.%d\r\n", FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH, FAKE_VER_EXTRA);
176
177     buf_sprintf(output, "Content-Length: %d\r\n", buf->size);
178     buf_sprintf(output, "Content-Encoding: binary\r\n");
179 #ifdef JP
180 #ifdef SJIS
181     buf_sprintf(output, "Content-Type: text/plain; charset=SHIFT_JIS\r\n");
182 #endif
183 #ifdef EUC
184     buf_sprintf(output, "Content-Type: text/plain; charset=EUC-JP\r\n");
185 #endif
186 #else
187     buf_sprintf(output, "Content-Type: text/plain; charset=ASCII\r\n");
188 #endif
189     buf_sprintf(output, "\r\n");
190     buf_append(output, buf->data, buf->size);
191
192     soc_write(sd, output->data, output->size);
193
194     soc_read(sd, response_buf, sizeof(response_buf));
195
196     return strncmp(response_buf, HTTP_RESPONSE_CODE_OK, strlen(HTTP_RESPONSE_CODE_OK)) == 0;
197 }
198
199 /*!
200  * @brief キャラクタダンプを作って BUFに保存
201  * @param creature_ptr プレーヤーへの参照ポインタ
202  * @param dumpbuf 伝送内容バッファ
203  * @return エラーコード
204  */
205 static errr make_dump(player_type *creature_ptr, BUF *dumpbuf, void (*update_playtime)(void), display_player_pf display_player)
206 {
207     char buf[1024];
208     FILE *fff;
209     GAME_TEXT file_name[1024];
210
211     /* Open a new file */
212     fff = angband_fopen_temp(file_name, 1024);
213     if (!fff) {
214 #ifdef JP
215         msg_format("一時ファイル %s を作成できませんでした。", file_name);
216 #else
217         msg_format("Failed to create temporary file %s.", file_name);
218 #endif
219         msg_print(NULL);
220         return 1;
221     }
222
223     /* 一旦一時ファイルを作る。通常のダンプ出力と共通化するため。 */
224     make_character_dump(creature_ptr, fff, update_playtime, display_player);
225     angband_fclose(fff);
226
227     /* Open for read */
228     fff = angband_fopen(file_name, "r");
229
230     while (fgets(buf, 1024, fff)) {
231         (void)buf_sprintf(dumpbuf, "%s", buf);
232     }
233     angband_fclose(fff);
234     fd_kill(file_name);
235
236     /* Success */
237     return 0;
238 }
239
240 /*!
241  * @brief スクリーンダンプを作成する/ Make screen dump to buffer
242  * @return 作成したスクリーンダンプの参照ポインタ
243  */
244 concptr make_screen_dump(player_type *creature_ptr, void (*process_autopick_file_command)(char *))
245 {
246     static concptr html_head[] = {
247         "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
248         "<pre>",
249         0,
250     };
251     static concptr html_foot[] = {
252         "</pre>\n",
253         "</body>\n</html>\n",
254         0,
255     };
256
257     int wid, hgt;
258     term_get_size(&wid, &hgt);
259
260     /* Alloc buffer */
261     BUF *screen_buf;
262     screen_buf = buf_new();
263     if (screen_buf == NULL)
264         return (NULL);
265
266     bool old_use_graphics = use_graphics;
267     if (old_use_graphics) {
268         /* Clear -more- prompt first */
269         msg_print(NULL);
270
271         use_graphics = FALSE;
272         reset_visuals(creature_ptr, process_autopick_file_command);
273
274         creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
275         handle_stuff(creature_ptr);
276     }
277
278     for (int i = 0; html_head[i]; i++)
279         buf_sprintf(screen_buf, html_head[i]);
280
281     /* Dump the screen */
282     for (int y = 0; y < hgt; y++) {
283         /* Start the row */
284         if (y != 0)
285             buf_sprintf(screen_buf, "\n");
286
287         /* Dump each row */
288         TERM_COLOR a = 0, old_a = 0;
289         SYMBOL_CODE c = ' ';
290         for (int x = 0; x < wid - 1; x++) {
291             int rv, gv, bv;
292             concptr cc = NULL;
293             /* Get the attr/char */
294             (void)(term_what(x, y, &a, &c));
295
296             switch (c) {
297             case '&':
298                 cc = "&amp;";
299                 break;
300             case '<':
301                 cc = "&lt;";
302                 break;
303             case '>':
304                 cc = "&gt;";
305                 break;
306             case '"':
307                 cc = "&quot;";
308                 break;
309             case '\'':
310                 cc = "&#39;";
311                 break;
312 #ifdef WINDOWS
313             case 0x1f:
314                 c = '.';
315                 break;
316             case 0x7f:
317                 c = (a == 0x09) ? '%' : '#';
318                 break;
319 #endif
320             }
321
322             a = a & 0x0F;
323             if ((y == 0 && x == 0) || a != old_a) {
324                 rv = angband_color_table[a][1];
325                 gv = angband_color_table[a][2];
326                 bv = angband_color_table[a][3];
327                 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">", ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
328                 old_a = a;
329             }
330
331             if (cc)
332                 buf_sprintf(screen_buf, "%s", cc);
333             else
334                 buf_sprintf(screen_buf, "%c", c);
335         }
336     }
337
338     buf_sprintf(screen_buf, "</font>");
339
340     for (int i = 0; html_foot[i]; i++)
341         buf_sprintf(screen_buf, html_foot[i]);
342
343     /* Screen dump size is too big ? */
344     concptr ret;
345     if (screen_buf->size + 1 > SCREEN_BUF_MAX_SIZE) {
346         ret = NULL;
347     } else {
348         /* Terminate string */
349         buf_append(screen_buf, "", 1);
350
351         ret = string_make(screen_buf->data);
352     }
353
354     /* Free buffer */
355     buf_delete(screen_buf);
356
357     if (!old_use_graphics)
358         return ret;
359
360     use_graphics = TRUE;
361     reset_visuals(creature_ptr, process_autopick_file_command);
362
363     creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
364     handle_stuff(creature_ptr);
365     return ret;
366 }
367
368 /*!
369  * todo メッセージは言語選択の関数マクロで何とかならんか?
370  * @brief スコア転送処理のメインルーチン
371  * @param creature_ptr プレーヤーへの参照ポインタ
372  * @return 正常終了の時0、異常があったら1
373  */
374 errr report_score(player_type *creature_ptr, void (*update_playtime)(void), display_player_pf display_player)
375 {
376 #ifdef WINDOWS
377     WSADATA wsaData;
378     WORD wVersionRequested = (WORD)((1) | (1 << 8));
379 #endif
380
381     BUF *score;
382     score = buf_new();
383
384     char seikakutmp[128];
385     char title[128];
386     put_version(title);
387 #ifdef JP
388     sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "の" : ""));
389 #else
390     sprintf(seikakutmp, "%s ", ap_ptr->title);
391 #endif
392
393     buf_sprintf(score, "name: %s\n", creature_ptr->name);
394     buf_sprintf(score, "version: %s\n", title);
395     buf_sprintf(score, "score: %d\n", calc_score(creature_ptr));
396     buf_sprintf(score, "level: %d\n", creature_ptr->lev);
397     buf_sprintf(score, "depth: %d\n", creature_ptr->current_floor_ptr->dun_level);
398     buf_sprintf(score, "maxlv: %d\n", creature_ptr->max_plv);
399     buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
400     buf_sprintf(score, "au: %d\n", creature_ptr->au);
401     buf_sprintf(score, "turns: %d\n", turn_real(creature_ptr, current_world_ptr->game_turn));
402     buf_sprintf(score, "sex: %d\n", creature_ptr->psex);
403     buf_sprintf(score, "race: %s\n", rp_ptr->title);
404     buf_sprintf(score, "class: %s\n", cp_ptr->title);
405     buf_sprintf(score, "seikaku: %s\n", seikakutmp);
406     buf_sprintf(score, "realm1: %s\n", realm_names[creature_ptr->realm1]);
407     buf_sprintf(score, "realm2: %s\n", realm_names[creature_ptr->realm2]);
408     buf_sprintf(score, "killer: %s\n", creature_ptr->died_from);
409     buf_sprintf(score, "-----charcter dump-----\n");
410
411     make_dump(creature_ptr, score, update_playtime, display_player);
412
413     if (screen_dump) {
414         buf_sprintf(score, "-----screen shot-----\n");
415         buf_append(score, screen_dump, strlen(screen_dump));
416     }
417
418 #ifdef WINDOWS
419     if (WSAStartup(wVersionRequested, &wsaData)) {
420         msg_print("Report: WSAStartup failed.");
421 #ifdef WINDOWS
422         WSACleanup();
423 #endif
424         return 1;
425     }
426 #endif
427
428     term_clear();
429
430     int sd;
431     while (TRUE) {
432         char buff[160];
433 #ifdef JP
434         prt("接続中...", 0, 0);
435 #else
436         prt("connecting...", 0, 0);
437 #endif
438         term_fresh();
439
440         /* プロキシを設定する */
441         set_proxy(HTTP_PROXY, HTTP_PROXY_PORT);
442
443         /* Connect to the score server */
444         sd = connect_server(HTTP_TIMEOUT, SCORE_SERVER, SCORE_PORT);
445
446         if (sd < 0) {
447 #ifdef JP
448             sprintf(buff, "スコア・サーバへの接続に失敗しました。(%s)", soc_err());
449 #else
450             sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
451 #endif
452             prt(buff, 0, 0);
453             (void)inkey();
454
455 #ifdef JP
456             if (!get_check_strict(creature_ptr, "もう一度接続を試みますか? ", CHECK_NO_HISTORY))
457 #else
458             if (!get_check_strict(creature_ptr, "Try again? ", CHECK_NO_HISTORY))
459 #endif
460             {
461 #ifdef WINDOWS
462                 WSACleanup();
463 #endif
464                 return 1;
465             }
466
467             continue;
468         }
469
470 #ifdef JP
471         prt("スコア送信中...", 0, 0);
472 #else
473         prt("Sending the score...", 0, 0);
474 #endif
475         term_fresh();
476
477         if (!http_post(sd, SCORE_PATH, score)) {
478             disconnect_server(sd);
479 #ifdef JP
480             sprintf(buff, "スコア・サーバへの送信に失敗しました。");
481 #else
482             sprintf(buff, "Failed to send to the score server.");
483 #endif
484             prt(buff, 0, 0);
485             (void)inkey();
486
487 #ifdef JP
488             if (!get_check_strict(creature_ptr, "もう一度接続を試みますか? ", CHECK_NO_HISTORY))
489 #else
490             if (!get_check_strict(creature_ptr, "Try again? ", CHECK_NO_HISTORY))
491 #endif
492             {
493 #ifdef WINDOWS
494                 WSACleanup();
495 #endif
496                 return 1;
497             }
498
499             continue;
500         }
501
502         disconnect_server(sd);
503         break;
504     }
505
506 #ifdef WINDOWS
507     WSACleanup();
508 #endif
509
510     return 0;
511 }
512 #endif /* WORLD_SCORE */