]> git.pond.sub.org Git - empserver/blobdiff - src/lib/player/player.c
Factor out code to read mailboxes, and make read more robust
[empserver] / src / lib / player / player.c
index 7227115788c846e4130118a784f9a767fcc82996..3852c1161e3c74cc75a79f9c86081fa81b243e72 100644 (file)
@@ -53,6 +53,7 @@
 
 static int command(void);
 static int status(void);
+static int print_sink(char *, size_t, void *);
 
 struct player *player;
 
@@ -257,38 +258,40 @@ execute(void)
 int
 show_motd(void)
 {
-    FILE *motd_fp;
+    show_first_tel(motdfil);
+    return RET_OK;
+}
+
+/*
+ * Print first telegram in file FNAME.
+ */
+int
+show_first_tel(char *fname)
+{
+    FILE *fp;
     struct telstr tgm;
-    char buf[MAXTELSIZE + 1];  /* UTF-8 */
 
-    if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
+    if ((fp = fopen(fname, "rb")) == NULL) {
        if (errno == ENOENT)
-           return RET_OK;
+           return 0;
        else {
-           pr ("Could not open motd.\n");
-           logerror("Could not open motd (%s).\n", motdfil);
-           return RET_FAIL;
+           logerror("Could not open %s.\n", fname);
+           return -1;
        }
     }
-    if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
-       logerror("bad header on login message (motdfil)");
-       fclose(motd_fp);
-       return RET_FAIL;
-    }
-    if (tgm.tel_length >= (long)sizeof(buf)) {
-       logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
-       fclose(motd_fp);
-       return RET_FAIL;
-    }
-    if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
-       logerror("bad length %ld on login message", tgm.tel_length);
-       fclose(motd_fp);
-       return RET_FAIL;
-    }
-    buf[tgm.tel_length] = 0;
-    uprnf(buf);
-    fclose(motd_fp);
-    return RET_OK;
+    if (tel_read_header(fp, fname, &tgm) < 0)
+       return -1;
+    if (tel_read_body(fp, fname, &tgm, print_sink, NULL) < 0)
+       return -1;
+    fclose(fp);
+    return 0;
+}
+
+static int
+print_sink(char *chunk, size_t sz, void *arg)
+{
+    uprnf(chunk);
+    return 0;
 }
 
 int