OSDN Git Service

スクリーンダンプの末尾に'\0'を付けてきちんと終端させるようにした。
[hengband/hengband.git] / src / report.c
1 /* File: report.c */
2
3 #define _GNU_SOURCE
4 #include "angband.h"
5
6 #ifdef WORLD_SCORE
7
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <ctype.h>
11 #include <string.h>
12
13 #if defined(WINDOWS)
14 #include <winsock.h>
15 #elif defined(MACINTOSH)
16 #include <OpenTransport.h>
17 #include <OpenTptInternet.h>
18 #else
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netdb.h>
23 #include <sys/time.h>
24
25 #include <setjmp.h>
26 #include <signal.h>
27 #endif
28
29 #ifdef JP
30 #define SCORE_PATH "http://www.kmc.gr.jp/~habu/local/hengscore/score.cgi"
31 #else
32 #define SCORE_PATH "http://www.kmc.gr.jp/~habu/local/hengscore-en/score.cgi"
33 #endif
34
35 /* for debug */
36 /*#define SCORE_PATH "http://www.kmc.gr.jp/~habu/local/scoretest/score.cgi" */
37
38 /*
39  * simple buffer library
40  */
41
42 typedef struct {
43         size_t max_size;
44         size_t size;
45         char *data;
46 } BUF;
47
48 #define BUFSIZE (65536)
49
50 #ifndef HAVE_VASPRINTF
51 #define vasprintf       Vasprintf
52
53 static int Vasprintf(char **buf, const char *fmt, va_list ap)
54 {
55         int ret;
56
57         *buf = malloc(1024);
58
59 #if defined(HAVE_VSNPRINTF)
60         ret = vsnprintf(*buf, 1024, fmt, ap);
61 #else
62         ret = vsprintf(*buf, fmt, ap);
63 #endif
64         return ret;
65 }
66
67 #endif /* ifndef HAVE_VASPRINTF */ 
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 static void buf_delete(BUF *b)
87 {
88         free(b->data);
89         free(b);
90 }
91
92 static int buf_append(BUF *buf, const char *data, size_t size)
93 {
94         while (buf->size + size > buf->max_size)
95         {
96                 char *tmp;
97                 if ((tmp = malloc(buf->max_size * 2)) == NULL) return -1;
98
99                 memcpy(tmp, buf->data, buf->max_size);
100                 free(buf->data);
101
102                 buf->data = tmp;
103
104                 buf->max_size *= 2;
105         }
106         memcpy(buf->data + buf->size, data, size);
107         buf->size += size;
108
109         return buf->size;
110 }
111
112 static int buf_sprintf(BUF *buf, const char *fmt, ...)
113 {
114         int             ret;
115         char    *tmpbuf;
116         va_list ap;
117
118         va_start(ap, fmt);
119         vasprintf(&tmpbuf, fmt, ap);
120         va_end(ap);
121
122         if(!tmpbuf) return -1;
123
124         ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
125
126         free(tmpbuf);
127
128         return ret;
129 }
130
131 #if 0
132 static int buf_read(BUF *buf, int fd)
133 {
134         int len;
135 #ifndef MACINTOSH
136         char tmp[BUFSIZE];
137 #else
138         char *tmp;
139         
140         tmp = calloc( BUFSIZE , sizeof(char) );
141 #endif
142
143         while ((len = read(fd, tmp, BUFSIZE)) > 0)
144                 buf_append(buf, tmp, len);
145
146         return buf->size;
147 }
148 #endif
149
150 #if 0
151 static int buf_write(BUF *buf, int fd)
152 {
153         write(fd, buf->data, buf->size);
154
155         return buf->size;
156 }
157
158 static int buf_search(BUF *buf, const char *str)
159 {
160         char *ret;
161
162         ret = strstr(buf->data, str);
163
164         if (!ret) return -1;
165
166         return ret - buf->data;
167 }
168
169 static BUF * buf_subbuf(BUF *buf, int pos1, size_t sz)
170 {
171         BUF *ret;
172
173         if (pos1 < 0) return NULL;
174
175         ret = buf_new();
176
177         if (sz <= 0) sz = buf->size - pos1;
178
179         buf_append(ret, buf->data + pos1, sz);
180
181         return ret;
182 }
183 #endif
184
185 static void http_post(int sd, char *url, BUF *buf)
186 {
187         BUF *output;
188
189         output = buf_new();
190         buf_sprintf(output, "POST %s HTTP/1.0\r\n", url);
191         buf_sprintf(output, "User-Agent: Hengband %d.%d.%d\r\n",
192                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
193
194         buf_sprintf(output, "Content-Length: %d\r\n", buf->size);
195         buf_sprintf(output, "Content-Encoding: binary\r\n");
196         buf_sprintf(output, "Content-Type: application/octet-stream\r\n");
197         buf_sprintf(output, "\r\n");
198         buf_append(output, buf->data, buf->size);
199
200         soc_write(sd, output->data, output->size);
201 }
202
203
204 /* ¥­¥ã¥é¥¯¥¿¥À¥ó¥×¤òºî¤Ã¤Æ BUF¤ËÊݸ */
205 static errr make_dump(BUF* dumpbuf)
206 {
207         errr make_character_dump(FILE *fff);
208
209         char            buf[1024];
210         FILE *fff;
211         char file_name[1024];
212
213         /* Open a new file */
214         fff = my_fopen_temp(file_name, 1024);
215         if (!fff)
216         {
217 #ifdef JP
218                 msg_format("°ì»þ¥Õ¥¡¥¤¥ë %s ¤òºîÀ®¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£", file_name);
219 #else
220                 msg_format("Failed to create temporary file %s.", file_name);
221 #endif
222                 msg_print(NULL);
223                 return 1;
224         }
225
226         /* °ìö°ì»þ¥Õ¥¡¥¤¥ë¤òºî¤ë¡£Ä̾ï¤Î¥À¥ó¥×½ÐÎϤȶ¦Ä̲½¤¹¤ë¤¿¤á¡£ */
227         (void)make_character_dump(fff);
228
229         /* Close the file */
230         my_fclose(fff);
231
232         /* Open for read */
233         fff = my_fopen(file_name, "r");
234
235         while (fgets(buf, 1024, fff))
236         {
237                 (void)buf_append(dumpbuf, buf, strlen(buf));
238         }
239
240         /* Close the file */
241         my_fclose(fff);
242
243         /* Remove the file */
244         fd_kill(file_name);
245
246         /* Success */
247         return (0);
248 }
249
250 /*
251  * Make screen dump to buffer
252  */
253 cptr make_screen_dump(void)
254 {
255         BUF *screen_buf;
256         int y, x, i;
257         cptr ret;
258
259         byte a = 0, old_a = 0;
260         char c = ' ';
261
262         char *html_head[] = {
263                 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
264                 "<pre>",
265                 0,
266         };
267         char *html_foot[] = {
268                 "</pre>\n",
269                 "</body>\n</html>\n",
270                 0,
271         };
272
273         /* Alloc buffer */
274         screen_buf = buf_new();
275         if (screen_buf == NULL) return (NULL);
276
277         for (i = 0; html_head[i]; i++)
278                 buf_sprintf(screen_buf, html_head[i]);
279
280         /* Dump the screen */
281         for (y = 0; y < 24; y++)
282         {
283                 /* Start the row */
284                 if (y != 0)
285                         buf_sprintf(screen_buf, "\n");
286
287                 /* Dump each row */
288                 for (x = 0; x < 79; x++)
289                 {
290                         int rv, gv, bv;
291                         char *cc = NULL;
292                         /* Get the attr/char */
293                         (void)(Term_what(x, y, &a, &c));
294
295                         switch (c)
296                         {
297                         case '&': cc = "&amp;"; break;
298                         case '<': cc = "&lt;"; break;
299                         case '>': cc = "&gt;"; break;
300 #ifdef WINDOWS
301                         case 0x1f: c = '.'; break;
302                         case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
303 #endif
304                         }
305
306                         a = a & 0x0F;
307                         if ((y == 0 && x == 0) || a != old_a) {
308                                 rv = angband_color_table[a][1];
309                                 gv = angband_color_table[a][2];
310                                 bv = angband_color_table[a][3];
311                                 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">", 
312                                             ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
313                                 old_a = a;
314                         }
315                         if (cc)
316                                 buf_sprintf(screen_buf, "%s", cc);
317                         else
318                                 buf_sprintf(screen_buf, "%c", c);
319                 }
320         }
321         buf_sprintf(screen_buf, "</font>");
322
323         for (i = 0; html_foot[i]; i++)
324                 buf_sprintf(screen_buf, html_foot[i]);
325
326         /* Screen dump size is too big ? */
327         if (screen_buf->size + 1> SCREEN_BUF_SIZE)
328         {
329                 buf_delete(screen_buf);
330                 return (NULL);
331         }
332
333         /* Terminate string */
334         screen_buf->data[screen_buf->size] = '\0';
335
336         ret = string_make(screen_buf->data);
337
338         /* Free buffer */
339         buf_delete(screen_buf);
340
341         return ret;
342 }
343
344
345 errr report_score(void)
346 {
347 #ifdef MACINTOSH
348         OSStatus err;
349 #else
350         errr err = 0;
351 #endif
352
353 #ifdef WINDOWS
354         WSADATA wsaData;
355         WORD wVersionRequested =(WORD) (( 1) |  ( 1 << 8));
356 #endif
357
358         BUF *score;
359         int sd;
360         char seikakutmp[128];
361
362         score = buf_new();
363
364 #ifdef JP
365         sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "¤Î" : ""));
366 #else
367         sprintf(seikakutmp, "%s ", ap_ptr->title);
368 #endif
369
370         buf_sprintf(score, "name: %s\n", player_name);
371 #ifdef JP
372         buf_sprintf(score, "version: ÊѶòÈÚÅÜ %d.%d.%d\n",
373                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
374 #else
375         buf_sprintf(score, "version: Hengband %d.%d.%d\n",
376                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
377 #endif
378         buf_sprintf(score, "score: %d\n", total_points());
379         buf_sprintf(score, "level: %d\n", p_ptr->lev);
380         buf_sprintf(score, "depth: %d\n", dun_level);
381         buf_sprintf(score, "maxlv: %d\n", p_ptr->max_plv);
382         buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
383         buf_sprintf(score, "au: %d\n", p_ptr->au);
384         buf_sprintf(score, "turns: %d\n", turn_real(turn));
385         buf_sprintf(score, "sex: %d\n", p_ptr->psex);
386         buf_sprintf(score, "race: %s\n", rp_ptr->title);
387         buf_sprintf(score, "class: %s\n", cp_ptr->title);
388         buf_sprintf(score, "seikaku: %s\n", seikakutmp);
389         buf_sprintf(score, "realm1: %s\n", realm_names[p_ptr->realm1]);
390         buf_sprintf(score, "realm2: %s\n", realm_names[p_ptr->realm2]);
391         buf_sprintf(score, "killer: %s\n", died_from);
392         buf_sprintf(score, "-----charcter dump-----\n");
393
394         make_dump(score);
395
396         if (screen_dump)
397         {
398                 buf_sprintf(score, "-----screen shot-----\n");
399                 buf_sprintf(score, screen_dump);
400         }
401
402 #ifdef WINDOWS
403         if (WSAStartup(wVersionRequested, &wsaData))
404         {
405                 msg_print("Report: WSAStartup failed.");
406                 goto report_end;
407         }
408 #endif
409
410 #ifdef MACINTOSH
411 #if TARGET_API_MAC_CARBON
412         err = InitOpenTransportInContext(kInitOTForApplicationMask, NULL);
413 #else
414         err = InitOpenTransport();
415 #endif
416         if (err != noErr)
417         {
418                 msg_print("Report: OpenTransport failed.");
419                 return 1;
420         }
421 #endif
422
423         Term_clear();
424
425         while (1)
426         {
427                 char buff[160];
428 #ifdef JP
429                 prt("ÀܳÃæ...", 0, 0);
430 #else
431                 prt("connecting...", 0, 0);
432 #endif
433                 Term_fresh();
434                 
435                 sd = connect_scoreserver();
436                 if (!(sd < 0)) break;
437 #ifdef JP
438                 sprintf(buff, "¥¹¥³¥¢¡¦¥µ¡¼¥Ð¤Ø¤ÎÀܳ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£(%s)", soc_err());
439 #else
440                 sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
441 #endif
442                 prt(buff, 0, 0);
443                 (void)inkey();
444                 
445 #ifdef JP
446                 if (!get_check("¤â¤¦°ìÅÙÀܳ¤ò»î¤ß¤Þ¤¹¤«? "))
447 #else
448                 if (!get_check("Try again? "))
449 #endif
450                 {
451                         err = 1;
452                         goto report_end;
453                 }
454         }
455 #ifdef JP
456         prt("¥¹¥³¥¢Á÷¿®Ãæ...", 0, 0);
457 #else
458         prt("Sending the score...", 0, 0);
459 #endif
460         Term_fresh();
461         http_post(sd, SCORE_PATH, score);
462
463         disconnect_server(sd);
464  report_end:
465 #ifdef WINDOWS
466         WSACleanup();
467 #endif
468
469 #ifdef MACINTOSH
470 #if TARGET_API_MAC_CARBON
471         CloseOpenTransportInContext(NULL);
472 #else
473         CloseOpenTransport();
474 #endif
475 #endif
476
477         return err;
478 }
479
480 #endif /* WORLD_SCORE */