#ifndef lint
static char *RCSid = "$Header: /usr/brule/guest/empire/empire/emprcs/lib/update/wu.c,v 2.0 1995/02/07 05:43:48 empire Exp $";
#endif

/*
 * wu.c
 *
 * server in-memory version of wu.
 * has one long "production" telegram
 *
 * from PSL Empire, 1985
 */

#ifdef notdef
#include "misc.h"
#include "news.h"
#include "tel.h"
#include "nat.h"
#include "file.h"

#include <stdio.h>
#include <fcntl.h>

struct msg {
	struct msg *m_next;
	int m_len;
	s_char *m_text;
};

struct	msg *msgs[MAXNOC];
struct	msg *msgs_tail[MAXNOC];

/*
 * ignore "from" field
 * it is there for historical reasons
 */
/*ARGSUSED*/
wu(from, to, text)
	natid	from;
	natid	to;
	s_char	*text;
{
	struct	msg *mp;

	mp = (struct msg *) malloc(sizeof(struct msg));
	mp->m_next = 0;
	mp->m_len = strlen(text);
	mp->m_text = strcpy(malloc((unsigned)mp->m_len+1), text);
	if (msgs_tail[to] == 0)
		msgs[to] = mp;
	else
		msgs_tail[to]->m_next = mp;
	msgs_tail[to] = mp;
}

flushwu()
{
	struct natstr *np;
	register struct msg *mp;
	register struct msg *next;
	register int len;
	struct	telstr t;
	natid	i;
	FILE	*fp;
	long	now;
	s_char	buf[1024];

	(void) time(&now);
	for (i=0; np = getnatp(i); i++) {
		if (msgs[i] == 0)
			continue;
		t.tel_from = 0;
		t.tel_date = now;
		t.tel_type = TEL_UPDATE;
		t.tel_length = 0;
		len = 0;
		for (mp = msgs[i]; mp != 0; mp = mp->m_next)
			len += mp->m_len;
		t.tel_length = len;
		if (len <= 0)
			continue;
		if ((fp = fopen(mailbox(buf, i), "a+")) == 0) {
			logerror("cannot open mailbox %s", mailbox(buf, i));
			continue;
		}
		(void) fwrite((s_char *)&t, sizeof(t), 1, fp);
		for (mp = msgs[i]; mp != 0; mp = next) {
			next = mp->m_next;
			fputs(mp->m_text, fp);
			free((s_char *)mp->m_text);
			free((s_char *)mp);
		}
		(void) fflush(fp);
		(void) fclose(fp);
		msgs[i] = 0;
		np->nat_tgms++;
	}
}
#endif
