OSDN Git Service

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