]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/wu.c
Update copyright notice
[empserver] / src / lib / subs / wu.c
index 292717ad0c295fe6b871c4cadcd5220ab4834483..ee7b7407b9ca8ae376e86562d8bb8adacb239fbd 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *
  *  ---
  *
- *  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.
  *
  *  ---
  *
  *  wu.c: Write a telegram to a user from another
- * 
+ *
  *  Known contributors to this file:
  *     Steve McClure, 2000
- *     
  */
 
-#include <stdarg.h>
-#include "misc.h"
+#include <config.h>
+
 #include <fcntl.h>
-#if !defined(_WIN32)
+#include <stdarg.h>
 #include <sys/uio.h>
-#endif
-#include "nat.h"
-#include "tel.h"
+#include <unistd.h>
 #include "file.h"
-#include "player.h"
-#include "server.h"
+#include "misc.h"
+#include "nat.h"
 #include "optlist.h"
+#include "player.h"
 #include "prototypes.h"
+#include "server.h"
+#include "tel.h"
 
 static struct telstr last_tel[MAXNOC];
 
@@ -57,7 +57,7 @@ clear_telegram_is_new(natid to)
 }
 
 /*
- * telegram_is_new counts new telegrams the same as read_telegrams in 
+ * telegram_is_new counts new telegrams the same as read_telegrams in
  * lib/commands/mail.c and lib/commands/rea.c
  */
 
@@ -68,7 +68,7 @@ telegram_is_new(natid to, struct telstr *tel)
 
     is_new |= tel->tel_type != last_tel[to].tel_type;
     is_new |= tel->tel_from != last_tel[to].tel_from;
-    is_new |= !update_pending &&       /* sometimes updates take a long time */
+    is_new |= !update_running && /* updates can take a long time */
        abs(tel->tel_date - last_tel[to].tel_date) > TEL_SECONDS;
 
     last_tel[to].tel_type = tel->tel_type;
@@ -78,39 +78,50 @@ telegram_is_new(natid to, struct telstr *tel)
     return is_new;
 }
 
-/*VARARGS*/
+/*
+ * Send a telegram from FROM to TO.
+ * Format text to send using printf-style FORMAT and optional
+ * arguments.  It is plain ASCII.
+ * If running from the update, telegram type is TEL_UPDATE.
+ * Else if FROM is a deity, type is TEL_BULLETIN.
+ * Else it is TEL_NORM.
+ * Return 0 on success, -1 on error.
+ */
 int
-wu(natid from, natid to, s_char *format, ...)
+wu(natid from, natid to, char *format, ...)
 {
     struct natstr *np;
     va_list ap;
-    s_char buf[4096];
+    char buf[4096];
 
     va_start(ap, format);
     (void)vsprintf(buf, format, ap);
     va_end(ap);
     np = getnatp(from);
-    if (update_pending)
+    if (update_running)
        return typed_wu(from, to, buf, TEL_UPDATE);
-    else if (np->nat_stat & STAT_GOD)
+    else if (np->nat_stat == STAT_GOD)
        return typed_wu(from, to, buf, TEL_BULLETIN);
     else
        return typed_wu(from, to, buf, TEL_NORM);
 }
 
+/*
+ * Send a telegram from FROM to TO.
+ * MESSAGE is the text to send, in UTF-8.
+ * TYPE is the telegram type.
+ * Return 0 on success, -1 on error.
+ */
 int
-typed_wu(natid from, natid to, s_char *message, int type)
+typed_wu(natid from, natid to, char *message, int type)
 {
-    register s_char *bp;
     int len;
     struct telstr tel;
     struct natstr *np;
-#if !defined(_WIN32)
     struct iovec iov[2];
-#endif
     int fd;
-    s_char box[1024];
-    int notify = 0;
+    char box[1024];
+    int write_ok = 0;
     int new_tele = 0;
     struct player *other;
 
@@ -120,11 +131,8 @@ typed_wu(natid from, natid to, s_char *message, int type)
        mailbox(box, to);
 
     if (type != TEL_ANNOUNCE)
-       if ((np = getnatp(to)) == 0 ||
-           ((np->nat_stat & STAT_NORM) == 0 &&
-            (np->nat_stat & STAT_SANCT) == 0)) {
+       if ((np = getnatp(to)) == 0 || np->nat_stat < STAT_SANCT)
            return -1;
-       }
 #if !defined(_WIN32)
     if ((fd = open(box, O_WRONLY | O_APPEND, 0)) < 0) {
 #else
@@ -133,44 +141,39 @@ typed_wu(natid from, natid to, s_char *message, int type)
        logerror("telegram 'open' of %s (#%d) failed", box, to);
        return -1;
     }
+    memset(&tel, 0, sizeof(tel));
     tel.tel_from = from;
     (void)time(&tel.tel_date);
-    bp = message;
-    while (*bp++) ;
-    len = bp - message;
-    if (len >= MAXTELSIZE)
-       len = (MAXTELSIZE - 1);
-    message[len] = 0;
+    len = strlen(message);
+    if (CANT_HAPPEN(len > MAXTELSIZE)) {
+       len = MAXTELSIZE;
+       message[len] = 0;
+    }
     tel.tel_length = len;
     tel.tel_type = type;
-#if !defined(_WIN32)
     iov[0].iov_base = &tel;
     iov[0].iov_len = sizeof(tel);
     iov[1].iov_base = message;
     iov[1].iov_len = len;
     if (writev(fd, iov, 2) < (int)(iov[0].iov_len + iov[1].iov_len)) {
-#else
-    if ((write(fd, &tel, sizeof(tel)) != sizeof(tel)) ||
-       (write(fd, message, len) != len)) {
-#endif
        logerror("telegram 'write' to #%d failed", to);
-    } else if (type == TEL_ANNOUNCE) {
+    } else
+       write_ok = 1;
+
+    if (close(fd) == -1) {
+       logerror("telegram 'write' to #%d failed to close.", to);
+    } else if (write_ok && type == TEL_ANNOUNCE) {
        for (to = 0; NULL != (np = getnatp(to)); to++) {
-           if (!(np->nat_stat & STAT_NORM) &&
-               !(np->nat_stat & STAT_SANCT))
+           if (np->nat_stat < STAT_SANCT)
                continue;
            if (!player->god && (getrejects(from, np) & REJ_ANNO))
                continue;
-           notify = (np->nat_ann == 0);
            np->nat_ann++;
            putnat(np);
-           if (notify)
-               player_wakeup_all(to);
        }
-    } else {
-       notify = (np->nat_tgms == 0);
+    } else if (write_ok) {
        new_tele = telegram_is_new(to, &tel);
-       np->nat_tgms += new_tele || notify;
+       np->nat_tgms += new_tele || np->nat_tgms == 0;
        putnat(np);
 
        if (new_tele && np->nat_flags & NF_INFORM) {
@@ -179,12 +182,9 @@ typed_wu(natid from, natid to, s_char *message, int type)
                    pr_inform(other, "[new tele]\n");
                else
                    pr_inform(other, "[%d new teles]\n", np->nat_tgms);
-               player_wakeup_all(to);
            }
-       } else if (notify)
-           player_wakeup_all(to);
+       }
     }
 
-    close(fd);
     return 0;
 }