OSDN Git Service

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