OSDN Git Service

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