/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
- * Ken Stevens, Steve McClure
+ * Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Ken Stevens, Steve McClure, Markus Armbruster
*
- * This program is free software; you can redistribute it and/or modify
+ * Empire is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ---
*
- * 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.
*
* ---
*
* flash.c: Flash a message to another player
- *
+ *
* Known contributors to this file:
* Ken Stevens, 1995
* Steve McClure, 1998
+ * Ron Koenderink, 2005
+ * Markus Armbruster, 2004-2013
*/
-#include <time.h>
-#include "misc.h"
-#include "player.h"
-#include "nat.h"
-#include "file.h"
+#include <config.h>
+
#include "commands.h"
-static int ufindbreak(char *message /* message is message text */,
- int num_chars);
+static int chat(struct natstr *, char *);
+static int sendmessage(struct natstr *, char *, int);
int
-flash(void)
+c_flash(void)
{
struct natstr *us;
struct natstr *to;
- char buf[1024]; /* buf is message text */
int tocn;
- char *sp; /* sp is message text */
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
+ && relations_with(tocn, 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; *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 (!(player->flags & PF_UTF8) && (*sp & 0x80))
- *sp = '?';
- }
- sendmessage(us, to, buf, 1);
- } else {
- sendmessage(us, to, "...", 1);
- while (ugetstring("> ", buf)) {
- if (*buf == '.')
- break;
- sendmessage(us, to, buf, 0);
- }
- sendmessage(us, to, "<EOT>", 0);
- }
- return RET_OK;
+ return chat(to, player->comtail[2]);
}
int
-wall(void)
+c_wall(void)
{
- struct natstr *us;
- char buf[1024]; /* buf is message text */
- char *sp; /* sp is message text */
+ return chat(NULL, player->comtail[1]);
+}
- 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 (!(player->flags & PF_UTF8) && (*sp & 0x80))
- *sp = '?';
- }
- sendmessage(us, 0, buf, 1);
+/*
+ * Send flash message(s) to @to.
+ * Null @to broadcasts to all.
+ * @message is UTF-8. If it is null, prompt for messages interactively.
+ * Return RET_OK.
+ */
+static int
+chat(struct natstr *to, char *message)
+{
+ char buf[1024]; /* UTF-8 */
+
+ if (message) {
+ buf[0] = ':';
+ buf[1] = ' ';
+ strcpy(buf+2, message);
+ sendmessage(to, buf, 1);
} else {
- sendmessage(us, 0, "...", 1);
+ sendmessage(to, "...", 1);
while (ugetstring("> ", buf)) {
if (*buf == '.')
break;
- sendmessage(us, 0, buf, 0);
+ sendmessage(to, buf, 0);
}
- sendmessage(us, 0, "<EOT>", 0);
+ sendmessage(to, "<EOT>", 0);
}
return RET_OK;
}
-int
-sendmessage(struct natstr *us, struct natstr *to, char *message
- /* message is message text */, int oneshot)
+/*
+ * Send flash message @message to @to.
+ * @message is UTF-8.
+ * Null @to broadcasts to all.
+ * A header identifying the player is prepended to the message. It is
+ * more verbose if @verbose.
+ */
+static int
+sendmessage(struct natstr *to, char *message, int verbose)
{
struct player *other;
struct tm *tm;
time_t now;
- int sent = 0;
+ int sent = 0, rejected = 0;
struct natstr *wto;
- char c; /* c is message text */
- int pos;
- pos = ufindbreak(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 (to && other->cnum != to->nat_cnum)
- continue;
- if (!(wto = getnatp(other->cnum)))
- continue;
- if (!to && !player->god && getrel(wto, player->cnum) != ALLIED)
+ for (other = player_next(NULL); other; other = player_next(other)) {
+ if (other->state != PS_PLAYING)
continue;
- if (!player->god && !(wto->nat_flags & NF_FLASH))
- continue;
- if (player == other)
+ if (to) {
+ /* flash */
+ if (other->cnum != to->nat_cnum)
+ continue;
+ wto = to;
+ } else {
+ /* wall */
+ if (player == other)
+ continue;
+ wto = getnatp(other->cnum);
+ if (CANT_HAPPEN(!wto))
+ continue;
+ if (!player->god
+ && relations_with(other->cnum, player->cnum) != ALLIED)
+ continue;
+ }
+ if (!player->god && !(wto->nat_flags & NF_FLASH)) {
+ rejected++;
continue;
- if (oneshot)
+ }
+
+ if (verbose)
if (to)
- pr_flash(other, "FLASH from %s (#%d) @ %02d:%02d%s\n",
- us->nat_cnam, us->nat_cnum, tm->tm_hour,
- tm->tm_min, message);
+ pr_flash(other, "FLASH from %s @ %02d:%02d%s\n",
+ prnatid(player->cnum),
+ tm->tm_hour, tm->tm_min, message);
else
- pr_flash(other, "BROADCAST from %s (#%d) @ %02d:%02d%s\n",
- us->nat_cnam, us->nat_cnum, tm->tm_hour,
- tm->tm_min, message);
+ pr_flash(other, "BROADCAST from %s @ %02d:%02d%s\n",
+ prnatid(player->cnum),
+ tm->tm_hour, tm->tm_min, message);
else
- pr_flash(other, "%s (#%d): %s\n",
- us->nat_cnam, us->nat_cnum, message);
- player_wakeup(other);
+ pr_flash(other, "%s: %s\n",
+ prnatid(player->cnum), message);
sent++;
}
- if (player->god) {
- if (to)
- if (sent)
- pr("Flash sent to %s\n", to->nat_cnam);
- else
- pr("%s is not logged on\n", to->nat_cnam);
- else if (sent)
- pr("Broadcast sent to %d players\n", sent);
- else
- pr("No-one is logged in\n");
- }
- if (to && !player->god) {
- /* If they are allied with us, we would normally see that
- * they are logged in anyway, so just tell us */
- if ((getrel(to, player->cnum) == ALLIED) && !sent) {
- if (to->nat_flags & NF_FLASH)
+
+ if (to) {
+ /* flash */
+ if (player->god
+ || relations_with(to->nat_cnum, player->cnum) == ALLIED) {
+ /* Can see TO logged in anyway, so it's okay to tell */
+ if (rejected)
+ pr("%s is not accepting flashes\n", to->nat_cnam);
+ else if (!sent) {
pr("%s is not logged on\n", to->nat_cnam);
+ }
+ }
+ } else {
+ /* wall */
+ if (player->god) {
+ if (sent)
+ pr("Broadcast sent to %d players\n", sent);
else
- pr("%s is not accepting flashes\n", to->nat_cnam);
+ pr("No-one is logged in\n");
}
}
- if (c) {
- message[pos] = c;
- sendmessage(us, to, &message[pos], 0);
- }
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;
-}