From 919432ff6be3ebe0ee697642f0cdcccd528a1004 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Wed, 14 Nov 2012 20:30:22 -0700 Subject: [PATCH] Handle msgdir (timeouts still blow) --- bot.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/bot.c b/bot.c index fc062b6..ea67d9e 100644 --- a/bot.c +++ b/bot.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include "dispatch.h" @@ -309,6 +310,22 @@ handle_input() call_with_lines(inbuf, &inbuflen, dispatch); } +void +handle_file(FILE *f, void (*func) (const char *, size_t)) +{ + char line[2048]; + size_t linelen; + + // Read a line. If we didn't have enough space, pretend it was a line + // anyway. + fgets(line, sizeof line, f); + linelen = strlen(line); + if (line[linelen-1] != '\n') { + line[linelen++] = '\n'; + } + func(line, linelen); +} + void handle_subproc(struct subproc *s) { @@ -351,6 +368,36 @@ loop() int nfds = 0; fd_set rfds; + if (msgdir) { + DIR *d = opendir(msgdir); + + while (d) { + struct dirent *ent = readdir(d); + + if (! ent) { + break; + } + if (ent->d_type == DT_REG) { + char fn[PATH_MAX]; + FILE *f; + + snprintf(fn, sizeof fn, "%s/%s", msgdir, ent->d_name); + f = fopen(fn, "r"); + if (f) { + while (! feof(f)) { + handle_file(f, output); + } + fclose(f); + } + remove(fn); + } + } + + if (d) { + closedir(d); + } + } + FD_ZERO(&rfds); FD_SET(0, &rfds); for (i = 0; i < MAX_SUBPROCS; i += 1) {