00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #include <config.h>
00053
00054 #include <stdarg.h>
00055 #include <stdlib.h>
00056 #include "com.h"
00057 #include "empio.h"
00058 #include "file.h"
00059 #include "misc.h"
00060 #include "nat.h"
00061 #include "player.h"
00062 #include "proto.h"
00063 #include "prototypes.h"
00064 #include "server.h"
00065 #include "tel.h"
00066
00067 static void pr_player(struct player *pl, int id, char *buf);
00068 static void upr_player(struct player *pl, int id, char *buf);
00069 static void outid(struct player *pl, int n);
00070
00071
00072
00073
00074
00075
00076
00077 void
00078 pr(char *format, ...)
00079 {
00080 char buf[4096];
00081 va_list ap;
00082
00083 va_start(ap, format);
00084 (void)vsprintf(buf, format, ap);
00085 va_end(ap);
00086 if (player->flags & PF_UTF8)
00087
00088 upr_player(player, C_DATA, buf);
00089 else
00090
00091 pr_player(player, C_DATA, buf);
00092 }
00093
00094
00095
00096
00097 void
00098 uprnf(char *buf)
00099 {
00100 char *p;
00101
00102 if (!(player->flags & PF_UTF8)) {
00103 p = malloc(strlen(buf) + 1);
00104 copy_utf8_to_ascii_no_funny(p, buf);
00105 pr_player(player, C_DATA, p);
00106 free(p);
00107 } else
00108 pr_player(player, C_DATA, buf);
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 void
00120 pr_id(struct player *p, int id, char *format, ...)
00121 {
00122 char buf[4096];
00123 va_list ap;
00124
00125 if (p->curid >= 0) {
00126 io_puts(p->iop, "\n");
00127 p->curid = -1;
00128 }
00129 va_start(ap, format);
00130 (void)vsprintf(buf, format, ap);
00131 va_end(ap);
00132 pr_player(p, id, buf);
00133 }
00134
00135
00136
00137
00138
00139
00140
00141 void
00142 pr_flash(struct player *pl, char *format, ...)
00143 {
00144 char buf[4096];
00145 va_list ap;
00146
00147 if (pl->state != PS_PLAYING)
00148 return;
00149 va_start(ap, format);
00150 (void)vsprintf(buf, format, ap);
00151 va_end(ap);
00152 if (!(pl->flags & PF_UTF8))
00153 copy_utf8_to_ascii_no_funny(buf, buf);
00154 pr_player(pl, C_FLASH, buf);
00155 io_output(pl->iop, IO_NOWAIT);
00156 }
00157
00158
00159
00160
00161
00162
00163
00164 void
00165 pr_inform(struct player *pl, char *format, ...)
00166 {
00167 char buf[4096];
00168 va_list ap;
00169
00170 if (pl->state != PS_PLAYING)
00171 return;
00172 va_start(ap, format);
00173 (void)vsprintf(buf, format, ap);
00174 va_end(ap);
00175 pr_player(pl, C_INFORM, buf);
00176 io_output(pl->iop, IO_NOWAIT);
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186 void
00187 pr_wall(char *format, ...)
00188 {
00189 time_t now;
00190 struct tm *tm;
00191 char buf[4096];
00192 int n;
00193 struct player *p;
00194 va_list ap;
00195
00196 time(&now);
00197 tm = localtime(&now);
00198 n = sprintf(buf, "BROADCAST from %s @ %02d:%02d: ",
00199 getnatp(0)->nat_cnam, tm->tm_hour, tm->tm_min);
00200
00201 va_start(ap, format);
00202 (void)vsprintf(buf + n, format, ap);
00203 va_end(ap);
00204 for (p = player_next(0); p; p = player_next(p)) {
00205 if (p->state != PS_PLAYING)
00206 continue;
00207 pr_player(p, C_FLASH, buf);
00208 io_output(p->iop, IO_NOWAIT);
00209 }
00210 }
00211
00212
00213
00214
00215
00216
00217
00218 static void
00219 pr_player(struct player *pl, int id, char *buf)
00220 {
00221 char *p;
00222 char *bp;
00223 int len;
00224
00225 bp = buf;
00226 while (*bp != '\0') {
00227 if (pl->curid != -1 && pl->curid != id) {
00228 io_puts(pl->iop, "\n");
00229 pl->curid = -1;
00230 }
00231 if (pl->curid == -1)
00232 outid(pl, id);
00233 p = strchr(bp, '\n');
00234 if (p != NULL) {
00235 len = (p - bp) + 1;
00236 if ((pl->command && (pl->command->c_flags & C_MOD)) ||
00237 (player != pl))
00238 io_write(pl->iop, bp, len, IO_NOWAIT);
00239 else
00240 io_write(pl->iop, bp, len, IO_WAIT);
00241 bp += len;
00242 pl->curid = -1;
00243 } else {
00244 len = io_puts(pl->iop, bp);
00245 bp += len;
00246 }
00247 }
00248 }
00249
00250
00251
00252
00253
00254
00255
00256 static void
00257 upr_player(struct player *pl, int id, char *buf)
00258 {
00259 char *bp;
00260 int standout = 0;
00261 char printbuf[2];
00262 char ch;
00263
00264 printbuf[0] = '\0';
00265 printbuf[1] = '\0';
00266
00267 bp = buf;
00268 while ((ch = *bp++)) {
00269 if (pl->curid != -1 && pl->curid != id) {
00270 io_puts(pl->iop, "\n");
00271 pl->curid = -1;
00272 }
00273 if (pl->curid == -1)
00274 outid(pl, id);
00275
00276 if (ch & 0x80) {
00277 if (standout == 0) {
00278 printbuf[0] = 0x0e;
00279 io_puts(pl->iop, printbuf);
00280 standout = 1;
00281 }
00282 ch &= 0x7f;
00283 } else {
00284 if (standout == 1) {
00285 printbuf[0] = 0x0f;
00286 io_puts(pl->iop, printbuf);
00287 standout = 0;
00288 }
00289 }
00290 if (ch == '\n') {
00291 if ((pl->command && (pl->command->c_flags & C_MOD)) ||
00292 (player != pl))
00293 io_write(pl->iop, &ch, 1, IO_NOWAIT);
00294 else
00295 io_write(pl->iop, &ch, 1, IO_WAIT);
00296 pl->curid = -1;
00297 } else {
00298 printbuf[0] = ch;
00299 io_puts(pl->iop, printbuf);
00300 }
00301 }
00302 }
00303
00304
00305
00306
00307
00308 static void
00309 outid(struct player *pl, int n)
00310 {
00311 char buf[3];
00312
00313 if (CANT_HAPPEN(n > C_LAST))
00314 n = C_DATA;
00315
00316 if (n >= 10)
00317 buf[0] = 'a' - 10 + n;
00318 else
00319 buf[0] = '0' + n;
00320 buf[1] = ' ';
00321 buf[2] = '\0';
00322 io_puts(pl->iop, buf);
00323 pl->curid = n;
00324 }
00325
00326
00327
00328
00329
00330
00331 void
00332 prredir(char *redir)
00333 {
00334 pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
00335 }
00336
00337
00338
00339
00340
00341
00342 void
00343 prexec(char *file)
00344 {
00345 pr_id(player, C_EXECUTE, "%s\n", file);
00346 }
00347
00348
00349
00350
00351 void
00352 prprompt(int min, int btu)
00353 {
00354 pr_id(player, C_PROMPT, "%d %d\n", min, btu);
00355 }
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 int
00367 prmptrd(char *prompt, char *buf, int size)
00368 {
00369 int r;
00370
00371
00372
00373
00374
00375 CANT_HAPPEN(player->aborted);
00376
00377 if (CANT_HAPPEN(!prompt))
00378 prompt = "? ";
00379
00380 pr_id(player, C_FLUSH, "%s\n", prompt);
00381 if ((r = recvclient(buf, size)) < 0)
00382 return r;
00383 time(&player->curup);
00384 if (*buf == 0)
00385 return 1;
00386 if (player->flags & PF_UTF8)
00387 return copy_utf8_to_ascii_no_funny(buf, buf);
00388 return copy_ascii_no_funny(buf, buf);
00389 }
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 int
00402 uprmptrd(char *prompt, char *buf, int size)
00403 {
00404 int r;
00405
00406
00407 CANT_HAPPEN(player->aborted);
00408
00409 if (CANT_HAPPEN(!prompt))
00410 prompt = "? ";
00411
00412 pr_id(player, C_FLUSH, "%s\n", prompt);
00413 if ((r = recvclient(buf, size)) < 0)
00414 return r;
00415 time(&player->curup);
00416 if (*buf == 0)
00417 return 1;
00418 if (player->flags & PF_UTF8)
00419 return copy_utf8_no_funny(buf, buf);
00420 return copy_ascii_no_funny(buf, buf);
00421 }
00422
00423
00424
00425
00426 void
00427 prdate(void)
00428 {
00429 time_t now;
00430
00431 (void)time(&now);
00432 pr(ctime(&now));
00433 }
00434
00435
00436
00437
00438
00439
00440 void
00441 prxy(char *format, coord x, coord y, natid country)
00442 {
00443 struct natstr *np;
00444
00445 np = getnatp(country);
00446 pr(format, xrel(np, x), yrel(np, y));
00447 }
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458 void
00459 PR(int cn, char *format, ...)
00460 {
00461
00462 static char longline[MAXNOC][512];
00463 int newline;
00464 va_list ap;
00465 char buf[1024];
00466
00467 va_start(ap, format);
00468 (void)vsprintf(buf, format, ap);
00469 va_end(ap);
00470 newline = strrchr(buf, '\n') ? 1 : 0;
00471 strcat(longline[cn], buf);
00472 if (newline) {
00473 if (update_running || (cn && cn != player->cnum))
00474 typed_wu(0, cn, longline[cn], TEL_BULLETIN);
00475 else
00476 pr_player(player, C_DATA, longline[cn]);
00477 longline[cn][0] = '\0';
00478 }
00479 }
00480
00481
00482
00483
00484
00485
00486 void
00487 PRdate(natid cn)
00488 {
00489 time_t now;
00490
00491 (void)time(&now);
00492 PR(cn, ctime(&now));
00493 }
00494
00495
00496
00497
00498 void
00499 pr_beep(void)
00500 {
00501 struct natstr *np = getnatp(player->cnum);
00502
00503 if (np->nat_flags & NF_BEEP)
00504 pr("\07");
00505 }
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515 void
00516 mpr(int cn, char *format, ...)
00517 {
00518 char buf[4096];
00519 va_list ap;
00520
00521 va_start(ap, format);
00522 (void)vsprintf(buf, format, ap);
00523 va_end(ap);
00524 if (cn) {
00525 if (update_running || cn != player->cnum)
00526 typed_wu(0, cn, buf, TEL_BULLETIN);
00527 else
00528 pr_player(player, C_DATA, buf);
00529 }
00530 }
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540 size_t
00541 copy_ascii_no_funny(char *dst, char *src)
00542 {
00543 char *p;
00544 unsigned char ch;
00545
00546 p = dst;
00547 while ((ch = *src++)) {
00548 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
00549 ;
00550 else if (ch > 0x7f)
00551 *p++ = '?';
00552 else
00553 *p++ = ch;
00554 }
00555 *p = 0;
00556
00557 return p - dst;
00558 }
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568 size_t
00569 copy_utf8_no_funny(char *dst, char *src)
00570 {
00571 char *p;
00572 unsigned char ch;
00573
00574 p = dst;
00575 while ((ch = *src++)) {
00576
00577 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
00578 ;
00579 else
00580 *p++ = ch;
00581 }
00582 *p = 0;
00583
00584 return p - dst;
00585 }
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595 size_t
00596 copy_utf8_to_ascii_no_funny(char *dst, char *src)
00597 {
00598 char *p;
00599 unsigned char ch;
00600
00601 p = dst;
00602 while ((ch = *src++)) {
00603
00604 if ((ch < 0x20 && ch != '\t' && ch != '\n') || ch == 0x7f)
00605 ;
00606 else if (ch > 0x7f) {
00607 *p++ = '?';
00608 while ((*src++ & 0xc0) == 0x80) ;
00609 } else
00610 *p++ = ch;
00611 }
00612 *p = 0;
00613
00614 return p - dst;
00615 }
00616
00617
00618
00619
00620
00621 int
00622 ufindpfx(char *s, int n)
00623 {
00624 int i = 0;
00625
00626 while (n && s[i])
00627 {
00628 if ((s[i++] & 0xc0) == 0xc0)
00629 while ((s[i] & 0xc0) == 0x80)
00630 i++;
00631 --n;
00632 }
00633 return i;
00634 }