OSDN Git Service

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