]> granicus.if.org Git - nethack/commitdiff
Add SIMPLE_MAIL compile-time option for public servers
authorPasi Kallinen <paxed@alt.org>
Mon, 4 Jan 2016 17:52:34 +0000 (19:52 +0200)
committerPasi Kallinen <paxed@alt.org>
Mon, 4 Jan 2016 17:54:36 +0000 (19:54 +0200)
include/unixconf.h
src/mail.c

index bc6984eec44a901e66e755b60493c4cc3472c0d6..751895ba97416fdacf8ebab44ec1c1f6a8c122af 100644 (file)
 #endif
 #endif
 
+/* If SIMPLE_MAIL is defined, the mail spool file format is
+   "sender:message", one mail per line, and mails are
+   read within game, from demon-delivered mail scrolls */
+/* #define SIMPLE_MAIL */
+
+#ifndef MAILCKFREQ
+/* How often mail spool file is checked for new messages, in turns */
 #define MAILCKFREQ 50
+#endif
+
 #endif /* MAIL */
 
 /*
index fb9c07c50c222fc7f08b5807a39316358685ab61..ec31393a42d6d445ceff7e4428569336f29879d2 100644 (file)
@@ -5,6 +5,10 @@
 #include "hack.h"
 
 #ifdef MAIL
+#ifdef SIMPLE_MAIL
+# include <fcntl.h>
+# include <errno.h>
+#endif /* SIMPLE_MAIL */
 #include "mail.h"
 
 /*
@@ -498,6 +502,64 @@ ckmailstatus()
     }
 }
 
+#ifdef SIMPLE_MAIL
+void
+read_simplemail()
+{
+    FILE* mb = fopen(mailbox, "r");
+    char curline[102], *msg;
+    boolean seen_one_already = FALSE;
+    struct flock fl = { 0 };
+
+    fl.l_type = F_RDLCK;
+    fl.l_whence = SEEK_SET;
+    fl.l_start = 0;
+    fl.l_len = 0;
+
+    if (!mb)
+        goto bail;
+
+    /* Allow this call to block. */
+    if (fcntl (fileno (mb), F_SETLKW, &fl) == -1)
+        goto bail;
+
+    errno = 0;
+    while (fgets(curline, 102, mb) != NULL) {
+        fl.l_type = F_UNLCK;
+        fcntl (fileno(mb), F_UNLCK, &fl);
+
+        pline("There is a%s message on this scroll.",
+              seen_one_already ? "nother" : "");
+
+        msg = strchr(curline, ':');
+
+        if (!msg)
+            goto bail;
+
+        *msg = '\0';
+        msg++;
+
+        pline("This message is from '%s'.", curline);
+
+        msg[strlen(msg) - 1] = '\0'; /* kill newline */
+        pline ("It reads: \"%s\".", msg);
+
+        seen_one_already = TRUE;
+        errno = 0;
+
+        fl.l_type = F_RDLCK;
+        fcntl(fileno(mb), F_SETLKW, &fl);
+    }
+
+    fl.l_type = F_UNLCK;
+    fcntl(fileno(mb), F_UNLCK, &fl);
+    fclose(mb);
+    unlink(mailbox);
+bail:
+    pline("It appears to be all gibberish."); /* bail out _professionally_ */
+}
+#endif /* SIMPLE_MAIL */
+
 /*ARGSUSED*/
 void
 readmail(otmp)
@@ -505,7 +567,12 @@ struct obj *otmp UNUSED;
 {
 #ifdef DEF_MAILREADER /* This implies that UNIX is defined */
     register const char *mr = 0;
-
+#endif /* DEF_MAILREADER */
+#ifdef SIMPLE_MAIL
+    read_simplemail();
+    return;
+#endif /* SIMPLE_MAIL */
+#ifdef DEF_MAILREADER /* This implies that UNIX is defined */
     display_nhwindow(WIN_MESSAGE, FALSE);
     if (!(mr = nh_getenv("MAILREADER")))
         mr = DEF_MAILREADER;