]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/flash.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / commands / flash.c
index 46bd6f8ae6996d48861a28b583e0ca1ae1c81a0c..e0c415bf4577db6ad0e7a98b8ed37e0b512c0241 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
@@ -32,6 +32,8 @@
  *     Steve McClure, 1998
  */
 
+#include <config.h>
+
 #include <time.h>
 #include "misc.h"
 #include "player.h"
 #include "file.h"
 #include "commands.h"
 
-static int ufindbreak(char *message /* message is message text */,
-                     int num_chars);
-
 int
 flash(void)
 {
     struct natstr *us;
     struct natstr *to;
-    char buf[1024]; /* buf is message text */
+    char buf[1024];            /* UTF-8 */
     int tocn;
-    char *sp; /* sp is message text */
+    char *sp;                  /* points into player->combuf[], UTF-8 */
 
     us = getnatp(player->cnum);
     if ((tocn = natarg(player->argp[1], "to which country? ")) < 0)
        return RET_SYN;
-    if (!(to = getnatp((natid)tocn))) {
-       pr("Bad country number\n");
-       return RET_SYN;
-    }
+    to = getnatp(tocn);
 
-    if (us->nat_stat & STAT_GOD) {
+    if (us->nat_stat == STAT_GOD) {
        /* We are gods, we can flash anyone */
-    } else if (us->nat_stat == VIS) {
+    } else if (us->nat_stat == STAT_VIS) {
        /* We are a visitor.  We can only flash the gods. :) */
-       if (!(to->nat_stat & STAT_GOD)) {
+       if (to->nat_stat != STAT_GOD) {
            pr("Visitors can only flash the gods.\n");
            return RET_SYN;
        }
     } else {
        /* Ok, we are a normal country, can we flash them? */
-       if ((!(to->nat_stat & STAT_GOD)) &&
-           (getrel(to, player->cnum) < FRIENDLY)) {
+       if (to->nat_stat != STAT_GOD && getrel(to, player->cnum) < FRIENDLY) {
            pr("%s is not a deity or friendly with us.\n", to->nat_cnam);
            return RET_SYN;
        }
     }
 
     if (player->argp[2]) {
-       for (sp = &player->combuf[0]; *sp && *sp != ' '; ++sp) ;
+       for (sp = player->combuf; *sp && *sp != ' '; ++sp) ;
        for (++sp; *sp && *sp != ' '; ++sp) ;
-       sprintf(buf, ":%s", sp);
-       for(sp = buf; 0 != *sp; ++sp) {
-           if ((*sp >= 0x0 && *sp < 0x20  && *sp != '\t') ||
-               *sp == 0x7f)
-               *sp = '?';
-           else if (!(us->nat_flags & NF_UTF8) && (*sp & 0x80))
-               *sp = '?';
-       }
+       buf[0] = ':';
+       if (player->flags & PF_UTF8)
+           strcpy(buf+1, sp);
+       else
+           copy_utf8_to_ascii_no_funny(buf+1, sp);
        sendmessage(us, to, buf, 1);
     } else {
        sendmessage(us, to, "...", 1);
@@ -104,20 +96,17 @@ int
 wall(void)
 {
     struct natstr *us;
-    char buf[1024]; /* buf is message text */
-    char *sp; /* sp is message text */
+    char buf[1024];            /* UTF-8 */
+    char *sp;                  /* points into player->combuf[], UTF-8 */
 
     us = getnatp(player->cnum);
     if (player->argp[1]) {
-       for (sp = &player->combuf[0]; *sp && *sp != ' '; ++sp) ;
-       sprintf(buf, ":%s", sp);
-       for(sp = buf; 0 != *sp; ++sp) {
-           if ((*sp >= 0x0 && *sp < 0x20  && *sp != '\t') ||
-               *sp == 0x7f)
-               *sp = '?';
-           else if (!(us->nat_flags & NF_UTF8) && (*sp & 0x80))
-               *sp = '?';
-       }
+       for (sp = player->combuf; *sp && *sp != ' '; ++sp) ;
+       buf[0] = ':';
+       if (player->flags & PF_UTF8)
+           strcpy(buf+1, sp);
+       else
+           copy_utf8_to_ascii_no_funny(buf+1, sp);
        sendmessage(us, 0, buf, 1);
     } else {
        sendmessage(us, 0, "...", 1);
@@ -131,26 +120,33 @@ wall(void)
     return RET_OK;
 }
 
+/*
+ * Send flash message MESSAGE from US to TO.
+ * MESSAGE is UTF-8.  Long messages are broken into several parts.
+ * A header identifying US is prepended to each part.  The first
+ * header is more verbose if ONESHOT.
+ */
 int
-sendmessage(struct natstr *us, struct natstr *to, char *message
-           /* message is message text */, int oneshot)
+sendmessage(struct natstr *us, struct natstr *to, char *message, int oneshot)
 {
     struct player *other;
     struct tm *tm;
     time_t now;
     int sent = 0;
     struct natstr *wto;
-    char c; /* c is message text */
+    char c;
     int pos;
 
-    pos = ufindbreak(message, 60);
+    pos = ufindpfx(message, 60);
     c = message[pos];
     if (c)
         message[pos] = '\0';
-    
+
     time(&now);
     tm = localtime(&now);
     for (other = player_next(0); other != 0; other = player_next(other)) {
+       if (other->state != PS_PLAYING)
+           continue;
        if (to && other->cnum != to->nat_cnum)
            continue;
        if (!(wto = getnatp(other->cnum)))
@@ -204,22 +200,3 @@ sendmessage(struct natstr *us, struct natstr *to, char *message
     }
     return 0;
 }
-
-/*
- * Return byte-index of the N-th UTF-8 character in UTF-8 string S.
- * If S doesn't have that many characters, return its length instead.
- */
-int
-ufindbreak(char *s /* s is message text */, int n)
-{
-    int i = 0;
-
-    while (n && s[i])
-    {
-       if ((s[i++] & 0xc0) == 0xc0)
-            while ((s[i] & 0xc0) == 0x80)
-               i++;
-        --n;
-    }
-    return i;
-}