OSDN Git Service

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