2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, 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
41 #include "prototypes.h"
44 mailbox(char *buf, natid cn)
46 sprintf(buf, "%s/tel%d", teldir, cn);
51 * Create an empty telegram file named MBOX.
54 mailbox_create(char *mbox)
56 close(creat(mbox, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
61 * Read telegram header from FP into TEL.
62 * MBOX is the file name, it is used for logging errors.
63 * Return 1 on success, 0 on EOF, -1 on error.
66 tel_read_header(FILE *fp, char *mbox, struct telstr *tel)
70 n = fread(tel, 1, sizeof(*tel), fp);
71 if (n == 0 && feof(fp))
74 || tel->tel_type > TEL_LAST || tel->tel_from > MAXNOC) {
75 logerror("Mailbox %s corrupt: bad header", mbox);
82 * Read telegram body from FP.
83 * MBOX is the file name, it is used for logging errors.
85 * Unless SINK is null, it is called like SINK(CHUNK, SZ, ARG) to
86 * consume the body, chunk by chunk. The chunks are UTF-8, and
87 * CHUNK[SZ} is 0. Reading fails when SINK() returns a negative
89 * Return 0 on success, -1 on failure.
92 tel_read_body(FILE *fp, char *mbox, struct telstr *tel,
93 int (*sink)(char *, size_t, void *),
99 left = tel->tel_length;
101 sz = MIN(left, sizeof(buf) - 1);
102 if (fread(buf, 1, sz, fp) != sz) {
103 logerror("Mailbox %s corrupt: can't read body", mbox);
107 if (sink && sink(buf, sz, arg) < 0)