2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2012, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * mailbox.c: Mailbox file access
29 * Known contributors to this file:
30 * Markus Armbruster, 2009-2011
42 #include "prototypes.h"
45 mailbox(char *buf, natid cn)
47 sprintf(buf, "%s/tel%d", teldir, cn);
52 * Create an empty telegram file named MBOX.
53 * Return 0 on success, -1 on failure.
56 mailbox_create(char *mbox)
60 fd = creat(mbox, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
61 if (fd < 0 || close(fd) < 0) {
62 logerror("Can't create mailbox %s (%s)", mbox, strerror(errno));
69 * Read telegram header from FP into TEL.
70 * MBOX is the file name, it is used for logging errors.
71 * Return 1 on success, 0 on EOF, -1 on error.
74 tel_read_header(FILE *fp, char *mbox, struct telstr *tel)
78 n = fread(tel, 1, sizeof(*tel), fp);
79 if (n == 0 && feof(fp))
82 || tel->tel_type > TEL_LAST || tel->tel_from > MAXNOC) {
83 logerror("Mailbox %s corrupt: bad header", mbox);
90 * Read telegram body from FP.
91 * MBOX is the file name, it is used for logging errors.
93 * Unless SINK is null, it is called like SINK(CHUNK, SZ, ARG) to
94 * consume the body, chunk by chunk. The chunks are UTF-8, and
95 * CHUNK[SZ} is 0. Reading fails when SINK() returns a negative
97 * Return 0 on success, -1 on failure.
100 tel_read_body(FILE *fp, char *mbox, struct telstr *tel,
101 int (*sink)(char *, size_t, void *),
107 left = tel->tel_length;
109 sz = MIN(left, sizeof(buf) - 1);
110 if (fread(buf, 1, sz, fp) != sz) {
111 logerror("Mailbox %s corrupt: can't read body", mbox);
115 if (sink && sink(buf, sz, arg) < 0)