X-Git-Url: http://git.pond.sub.org/?p=empserver;a=blobdiff_plain;f=src%2Flib%2Fcommon%2Fmailbox.c;h=f940cad01a64beb8a2b05c80b7d48f494873c32c;hp=b317a95e18947e08765d960a1ece1ad21cdfe290;hb=4238323d630a3c5eb69c7ac796ee3d823ee623e5;hpb=6f1e669bea96e19aa69925ae2017d0acc4f58d4d diff --git a/src/lib/common/mailbox.c b/src/lib/common/mailbox.c index b317a95e1..f940cad01 100644 --- a/src/lib/common/mailbox.c +++ b/src/lib/common/mailbox.c @@ -36,6 +36,7 @@ #include #include "optlist.h" #include "tel.h" +#include "prototypes.h" char * mailbox(char *buf, natid cn) @@ -43,3 +44,57 @@ mailbox(char *buf, natid cn) sprintf(buf, "%s/tel%d", teldir, cn); return buf; } + +/* + * Read telegram header from FP into TEL. + * MBOX is the file name, it is used for logging errors. + * Return 1 on success, 0 on EOF, -1 on error. + */ +int +tel_read_header(FILE *fp, char *mbox, struct telstr *tel) +{ + size_t n; + + n = fread(tel, 1, sizeof(*tel), fp); + if (n == 0 && feof(fp)) + return 0; + if (n != sizeof(*tel) + || tel->tel_type > TEL_LAST || tel->tel_from > MAXNOC) { + logerror("Mailbox %s corrupt: bad header", mbox); + return -1; + } + return 1; +} + +/* + * Read telegram body from FP. + * MBOX is the file name, it is used for logging errors. + * TEL is the header. + * Unless SINK is null, it is called like SINK(CHUNK, SZ, ARG) to + * consume the body, chunk by chunk. The chunks are UTF-8, and + * CHUNK[SZ} is 0. Reading fails when SINK() returns a negative + * value. + * Return 0 on success, -1 on failure. + */ +int +tel_read_body(FILE *fp, char *mbox, struct telstr *tel, + int (*sink)(char *, size_t, void *), + void *arg) +{ + char buf[4096]; + size_t left, sz; + + left = tel->tel_length; + while (left) { + sz = MIN(left, sizeof(buf) - 1); + if (fread(buf, 1, sz, fp) != sz) { + logerror("Mailbox %s corrupt: can't read body", mbox); + return -1; + } + buf[sz] = 0; + if (sink && sink(buf, sz, arg) < 0) + return -1; + left -= sz; + } + return 0; +}