/* 
 *   Copyright (c) 1990 Rodney Volz
 *   ALL RIGHTS RESERVED 
 * 
 *   THIS SOFTWARE PRODUCT CONTAINS THE
 *   UNPUBLISHED SOURCE CODE OF RODNEY VOLZ
 *
 *   The copyright notices above do not evidence
 *   intended publication of such source code.
 *
 */

/*
 * MERCURY UUCP SOURCECODE [pnews.c]
 */

#define _PNEWS

#ifndef MWC
#include "uulib.c"
#include "header.c"
overlay "main"
#else
#include "define.h"
#endif

static char tmpdir[LLEN];


main(argc,argv)
int argc;
char *argv[];
{
	static char s[LLEN],s2[LLEN],s3[LLEN],s4[LLEN];
	static char remote,rfile[LLEN],lg[LLEN];
	static char mqueue[LLEN],myname[LLEN],orgline[LLEN];
	static char user[LLEN],usr_fullname[LLEN],tmp[LLEN];
	static char seq[LLEN],subject[LLEN],group[LLEN];
	static char xout[LLEN],dout[LLEN],cout[LLEN];
	static char dist[LLEN],usr_home[LLEN],spoolhost[LLEN];
	static char nsys[LLEN],hosts[LLEN],domain[LLEN];
	static char *dot[400],*pchar;
	char c,last;
	struct header *hd,*head;
	
	int inh,outh,i,n,a,lines,local;
	long len;
	long l;

	hd = (struct header *)malloc((long)sizeof(struct header));
	bzero(hd,(long)sizeof(struct header));
	
	getconf(4,domain);
	getconf(13,nsys);
	getconf(14,mqueue);
	getconf(15,hosts);
	sys_impdirn(mqueue);
	getconf(21,tmpdir);
	sys_impdirn(tmpdir);
	remote = FALSE;		/* shows if pnews was invoked by rn... */
	getconf(27,spoolhost);
	getconf(2,myname);
	getconf(3,orgline);
	getprof(1,user);
	getprof(2,usr_home);
	sys_impdirn(usr_home);
	getprof(3,usr_fullname);

	local = FALSE;
	
	strcpy(tmpdir,tmp);
	mktmp(tmp);
	strcpys(tmp,seq,rinstr(tmp,'\\') + 1,strlen(tmp));
	
	remote = FALSE;
	if (argc == 3)
	{
		strcpy((char *)argv[1],s);

		if (strcmp(s,"-followup") != TRUE)
		{
			send("Illegally supplied argument(s).");
			cr(1);
			rec(s);
			return(-1);
		}
		
		strcpy((char *)argv[2],rfile);
		++remote;
	}
	
	strcpy(mqueue,xout);
	strcpy(mqueue,dout);
	
	stradd(seq,xout);
	stradd(seq,dout);
	
	stradd(".x",xout);
	stradd(".d",dout);
	stradd(".c",cout);
	
	send("Postnews - ");
	send(VERSION);
	cr(1);

	if (remote) /* Are we doing a followup? */
	{
		head = (struct header *)gethead(rfile);
	}
	else
	{
		head = (struct header *)malloc((long)sizeof(struct header));
		bzero(head,(long)sizeof(struct header));
	}

	/* Eventually get their subject */
	trim(head->subject);
	if (*head->subject)
	{
		strcpys(head->subject,s,0,2);
		if (strcmp(s,"Re:") == FALSE)
			strcpy("Re: ",hd->subject);
		else
			strcpy("",hd->subject);
		stradd(head->subject,hd->subject);
	}

	/* Get their newsgroup */
	if (*head->followto)
	{
		strcpy(head->followto,s);
#if 0
		stradd(",",s);
		strcpys(s,s2,0,linstr(s,',') - 1);
#else
		strcpy(s,s2);
#endif
		if (strcmp(s2,"junk") == TRUE)
		{
			pen(ROT);
			send("Warning: ");
			send(head->from);
			send(" is trying to fool us!");
			cr(1);
			send("He or she's trying to make us post to \"junk\".");
			cr(1);
			pen(WEISS);
		}
		strcpy(head->followto,hd->nbuf);
	}
	else
	{
		strcpy(head->nbuf,hd->nbuf);
	}

	/* Get their references. */
	if (*head->id)
	{
		strcpy(head->refer,s);
		trim(s);
		if (*s)
			stradd(" ",s);
		stradd(head->id,s);
		trim(s);
		strcpy(s,hd->refer);
	}


	/* Get their distribution */
	strcpy(head->distribution,hd->distribution);


	if (!*hd->subject)
	{
		send("Subject: ");
		rec(hd->subject);
		cr(1);
		trim(hd->subject);
		if (!*hd->subject)
		{
			send("Postings must have a subject.");
			cr(1);
			return(-1);
		}
	}

	if (!*hd->nbuf)
	{
		send("Newsgroup: ");
		rec(hd->nbuf);
		cr(1);
		trim(hd->nbuf);
		if (!*hd->nbuf)
		{
			send("You must specify a newsgroup.");
			cr(1);
			return(-1);
		}
	}

	if (!*hd->distribution)
	{
		send("Distribution: ");
		rec(hd->distribution);
		cr(1);
		if (!*hd->distribution)
		{
			send("You must specify a distribution.");
			cr(1);
			return(-1);
		}
	}

#if 1	/********************************* No summary / keywords?? */
	send("Reply-To (optional): ");
	rec(s);
	cr(1);
	trim(s);
	if (*s)
		adrparse(s,hd->replyto,hd->replytofull);

#if 0
	send("Followup-To (optional): ");
	rec(s);
	cr(1);
	trim(s);
	if (*s)
		strcpy(s,hd->followto);
#endif

	send("Keywords (optional): ");
	rec(s);
	cr(1);
	trim(s);
	if (*s)
		strcpy(s,hd->keywords);


	send("Summary (optional): ");
	rec(s);
	cr(1);
	trim(s);
	if (*s)
		strcpy(s,hd->summary);

#if 0
	send("Sender (optional): ");
	rec(s);
	cr(1);
	trim(s);
	if (*s)
		adrparse(s,hd->sender,hd->senderfull);
#endif

#if 0
	send("Approved (optional): ");
	rec(s);
	cr(1);
	trim(s);
	if (*s)
		adrparse(s,hd->approved,hd->approvedfull);
#endif

		
#endif /****************** SUMMARY etc */

	
	if (remote)
	{
		inh = open(rfile,2);
		if (inh < 0)
		{
			send("Couldn't open ");
			send(rfile);
			cr(1);
			return(-1);
		}

		outh = open(tmp,1);
		if (outh < 0)
		{
			send("Couldn't open ");
			send(tmp);
			send(" as output.");
			cr(1);
			return(-1);
		}

		strcpy("In article ",s);
		stradd(head->id,s);
		stradd(" ",s);
		
		stradd(head->from,s);

		if ((24 + strlen(head->id)
		+ strlen(head->from)
		+ strlen(head->fromfull)) < 78)
		{
			stradd(" ",s);
		}
		else
		{
			putline(outh,s);
			strcpy("",s);
		}
		
		if (*head->fromfull)
		{
			stradd("(",s);
			stradd(head->fromfull,s);
			stradd(")",s);
		}

		stradd(" writes:",s);
		putline(outh,s);

		if (*head->sender && (strcmp(head->sender,head->from) == FALSE))
		{
			strcpy("(Sender was: ",s);
			stradd(head->sender,s);
			stradd(")",s);
			putline(outh,s);
		}

		seek(inh,0l);

		while(getline(inh,s) != -1)
		{
			strcpy("> ",s2);
			stradd(s,s2);
			putline(outh,s2);
		}

		close(inh);
		close(outh);

	} /* remote quoting */
		
	pnews_edit:

	edi(tmp);

	pnews_whatagain:
	n = whatnow("What now? Edit, Send, Cancel, Rot-13, Options? ","escro");
	switch(n)
	{
		case 1:
			goto pnews_edit;
		case 2:
			break;
		case 3:
		{
			send("No posting sent.");
			cr(1);
			goto pnews_abort;
		}
		case 4:
		{
			dorot13(tmp);
			goto pnews_whatagain;
		}
		case 5:
		{
			p_options(hd);
			goto pnews_whatagain;
		}
	}
		
	
	send("Posting article... ");
	
	strcpy("Pnews: ",lg);
	
	
	inh = open(tmp,2);
	n = 0;
	while (0 == 0)
	{
		if (getline(inh,s) == -1)
			break;
		++n;
	}
	lines = n;
	close(inh);

	/* Count length of signature */
	strcpy(usr_home,s);
	stradd("signatur.net",s);
	inh = open(s,2);
	if (inh > 0)
	{
		n = 0;
		++lines;
		while (getline(inh,s) != -1)
		{
			++n;
			++lines;
		}

		if (n > 5)
		{
			send("Very long signature. Please shorten down");
			cr(1);
			send("to less than 6 lines for not wasting bandwith.");
			cr(1);
		}

		close(inh);
	}

		
	outh = open(dout,1);
	if (outh <= 0)
	{
		send("Error opening ");
		send(dout);
		send(" as output.");
		cr(1);
		exit(2);
	}

#if 0	/* Braindamaged. What have I been up to when I came up with this?! */
	uu_putline(outh,"#! rnews XXXXXXX");	/* 17 Bytes, muessen von der
						File-Laenge abgezogen werden! */
#endif


	strcpy("Path: ",s);
#if 0
	stradd(myname,s);
	stradd("!",s);
#endif
	stradd(user,s);
	uu_putline(outh,s);

	/* loginfo */
	stradd("usr:",lg);
	stradd(user,lg);

	strcpy("From: ",s);
	stradd(user,s);
	stradd("@",s);
	stradd(myname,s);
	stradd(domain,s);
	stradd(" (",s);
	stradd(usr_fullname,s);
	stradd(")",s);
	uu_putline(outh,s);
	
	strcpy("Subject: ",s);
	stradd(hd->subject,s);
	uu_putline(outh,s);

	if (*hd->keywords)
	{
		strcpy("Keywords: ",s);
		stradd(hd->keywords,s);
		uu_putline(outh,s);
	}

	if (*hd->summary)
	{
		strcpy("Summary: ",s);
		stradd(hd->summary,s);
		uu_putline(outh,s);
	}
	



	/* Fuer's Logfile */
	stradd(" ng:",lg);
	stradd(hd->nbuf,lg);
	/* Oki doki. */
	
	strcpy("Newsgroups: ",s);
	stradd(hd->nbuf,s);
	uu_putline(outh,s);

	if (*hd->followto)
	{
		strcpy("Followup-To: ",s);
		stradd(hd->followto,s);
		uu_putline(outh,s);
	}
	
	if (*hd->replyto)
	{
		strcpy("Reply-To: ",s);
		stradd(hd->replyto,s);
		if (*hd->replytofull)
		{
			stradd(" (",s);
			stradd(hd->replytofull,s);
			stradd(")",s);
		}
		uu_putline(outh,s);
	}
	
	stradd(" subj:",lg);
	stradd(hd->subject,lg);

	strcpy("Message-ID: <",s);
	

	pchar = seq;
	++pchar;

	
	stradd(" id:",lg);
	stradd(pchar,lg);
	stradd("@",lg);
	stradd(myname,lg);
	stradd(domain,lg);

	log(lg,"pnews");
	
	stradd(pchar,s);
	stradd("@",s);
	stradd(myname,s);
	stradd(domain,s);
	stradd(">",s);
	uu_putline(outh,s);

	if (*hd->refer)
	{
		strcpy("References: ",s);
		stradd(hd->refer,s);
		uu_putline(outh,s);
	}

	if (*hd->control)
	{
		strcpy("Control: ",s);
		stradd(hd->control,s);
		uu_putline(outh,s);
	}
	
	strcpy("Date: ",s);
#if 0
	sys_dayname(s2);
	stradd(s2,s);	/* Monday */
	stradd(", ",s);
#endif
	str(sys_dayofmonth(),s2);
	stradd(s2,s);	/* 22 */
	stradd(" ",s);
	sys_monthname(s2);
	stradd(s2,s);	/* Oct */
	stradd(" ",s);
	str(sys_year(),s2);
	strcpys(s2,s3,2,4);
	strcpy(s3,s2);
	stradd(s2,s);	/* 89 */
	stradd(" ",s);
	sys_time(s2);
	stradd(s2,s);
	stradd(" GMT",s);
	uu_putline(outh,s);
	
	strcpy("Distribution: ",s);
	stradd(hd->distribution,s);
	uu_putline(outh,s);
	
	strcpy("Organization: ",s);
	stradd(orgline,s);
	uu_putline(outh,s);

	
	if (*hd->sender)
	{
		strcpy("Sender: ",s);
		stradd(hd->sender,s);
		if (*hd->senderfull)
		{
			stradd(" (",s);
			stradd(hd->senderfull,s);
			stradd(")",s);
		}
		uu_putline(outh,s);
	}
	
	if (*hd->approved)
	{
		strcpy("Approved: ",s);
		stradd(hd->approved,s);
		if (*hd->approvedfull)
		{
			stradd(" (",s);
			stradd(hd->approvedfull,s);
			stradd(")",s);
		}
		uu_putline(outh,s);
	}

	strcpy("X-Version: ",s);
	stradd(VERSION,s);
	uu_putline(outh,s);
	
	
	strcpy("Lines: ",s);
	str(lines,s2);
	stradd(s2,s);
	uu_putline(outh,s);
	uu_putline(outh,"");
	
	inh = open(tmp,2);
	if (inh < 0)
	{
		cr(1);
		send("Couldn't open ");
		send(tmp);
		send(" as input");
		cr(1);
		return(-1);
	}
	
	while (0 == 0)
	{
		if (getline(inh,s) == -1)
			break;

		filter(s);	/* ^M-Filter */

		uu_putline(outh,s);
	}
	close(inh);
	
	strcpy(usr_home,s);
	stradd("signatur.net",s);
	inh = open(s,2);
	if (inh <= 0)
	{
		send("Couldn't append ");
		send(s);
		send(" as signature.");
		cr(1);
	}
	else
	{
		uu_putline(outh,"--");
		while (getline(inh,s) != -1)
		{
			filter(s);	/* ^M's filtern */
			uu_putline(outh,s);
		}
		close(inh);
	}
		
	
#if 0	/* Wozu? (die 2.) */
	l = (long)gemdos(0x42,0l,outh,2);
	len = (long)(l - 17l);	/* "#! rnews xxxxx" abziehen... */
	gemdos(0x42,9l,outh,0);
	strcpy("0000000000",s);		/* BUG! */
	sprintf(s2,"%ld",len);
	stradd(s2,s);
	strcpys(s,s2,strlen(s) - 7,strlen(s));
	strcpy(s2,s);
	uu_putline(outh,s);
#endif

	close(outh);
	
	
	/*--------------------- Xout -------------------- */
	strcpy(xout,s);
	outh = open(s,1);
	if (outh <= 0)
	{
		send("Error opening output file!");
		cr(1);
		return(-1);
	}

	strcpy("U news ",s);
	stradd(myname,s);
	uu_putline(outh,s);
	
	strcpy("Z",s);
	uu_putline(outh,s);
	
	strcpy("R news",s);
	uu_putline(outh,s);

	strcpy("F D.000",s);
	stradd(seq,s);
	uu_putline(outh,s);

	strcpy("I D.000",s);
	stradd(seq,s);
	uu_putline(outh,s);
	
	strcpy("C rnews ",s);
	uu_putline(outh,s);
	close(outh);

	cr(1);
	send("Done.");
	cr(1);
	pnews_abort:
	mfree(hd);
	mfree(head);
}




edi(file)	/* Editor aufrufen */
char file[];
{
	char s[LLEN];
	getconf(20,s);
	noenv_exec(s,file);
	curs();
	return(0);
}




dorot13(s)
char *s;
{
	int inh,outh;
	static char s2[LLEN],s3[LLEN];

	strcpy(tmpdir,s2);
	mktmp(s2);

	send("Rotating... ");
	
	outh = open(s2,1);
	if (outh < 0)
	{	
		cr(1);
		send("Couldn't open ");
		send(s2);
		send(" as output");
		cr(1);
		return(-1);
	}

	inh = open(s,2);
	if (inh < 0)
	{
		cr(1);
		send("Couldn't open ");
		send(s);
		send(" as input");
		cr(1);
		return(-1);
	}

	while (getline(inh,s3) != -1)
	{
		drot13(s3);
		putline(outh,s3);
	}
	close(inh);
	close(outh);

	sys_remove(s);
	sys_fcopy(s2,s);
	send("Done.");
	cr(1);
}


p_options(hd)
struct header *hd;
{
	char s[LLEN];
	int n;

	send("\033E");
	cr(2);
	send("	- New [S]ubject");
	cr(1);
	send("	- specify [F]ollowup-To");
	cr(1);
	send("	- Add [R]eply-To");
	cr(1);
	send("	- specify S[e]nder");
	cr(1);
	send("	- Add [A]pproved");
	cr(1);
	send("	- Add [C]ontrol");
	cr(1);
	send("	- Insert [K]eywords");
	cr(1);
	send("	- Insert S[u]ummary");
	cr(1);
	send("	- Change [D]istribution");
	cr(1);
	send("	- Change [N]ewsgroup(s)");
	cr(1);

	send("	- E[x]it this menu");
	cr(1);

	cr(1);
	n = whatnow("Your choice:","sfreakudncx");

	cr(1);
	switch(n)
	{
		case 1:
		{
			if (valid(hd->subject))
			{
				send("Subject (old): ");
				send(hd->subject);
				cr(1);
				send("Subject (new): ");
			}
			else
				send("Subject: ");
			rec(s);
			cr(1);
			trim(s);
			if (!valid(s))
			{
				send("Subject-line left unchanged.");
				cr(1);
			}
			else
				strcpy(s,hd->subject);
			break;
		}
		
		case 2:
		{
			if (valid(hd->followto))
			{
				send("Followup-To (old): ");
				send(hd->followto);
				cr(1);
				send("Followup-To (new): ");
			}
			else
				send("Followup-To: ");
			
			rec(s);
			cr(1);
			trim(s);
			if (valid(s))
			{
				strcpy(s,hd->followto);
			}
			else
			{
				send("\"Followup-To\"-line left unchanged.");
				cr(1);
			}
			break;
		}

		case 3:
		{
			if (valid(hd->replyto))
			{
				send("Reply-To (old): ");
				send(hd->replyto);
				if (*hd->replytofull)
				{
					send(" (");
					send(hd->replytofull);
					send(")");
				}
				cr(1);
				send("Reply-To (new): ");
			}
			else
				send("Reply-To: ");
				
			
			rec(s);
			cr(1);
			trim(s);
			if (valid(s))
			{
				adrparse(s,hd->replyto,hd->replytofull);
			}
			else
			{
				send("\"Reply-To:\"-line left unchanged.");
				cr(1);
			}
			break;
		}
			
		case 4:
		{
			if (valid(hd->sender))
			{
				send("Sender (old): ");
				send(hd->sender);
				if (*hd->senderfull)
				{
					send(" (");
					send(hd->senderfull);
					send(")");
				}
				cr(1);
				send("Sender (new): ");
			}
			else
				send("Sender: ");
			
			rec(s);
			cr(1);
			trim(s);
			if (valid(s))
			{
				adrparse(s,hd->sender,hd->senderfull);
			}
			else
			{
				send("No \"Sender:\"-line set.");
				cr(1);
			}
			break;
		}

		case 5:
		{
			if (*hd->approved)
			{
				send("Approved (old): ");
				send(hd->approved);
				if (*hd->approvedfull)
				{
					send(" (");
					send(hd->approvedfull);
					send(")");
				}
				cr(1);
				send("Approved (new): ");
			}
			else
				send("Approved: ");
			
			rec(s);
			cr(1);
			trim(s);
			if (s[0])
			{
				adrparse(s,hd->approved,hd->approvedfull);
			}
			else
			{
				send("\"Approved:\"-line left unchanged.");
				cr(1);
			}
			break;
		}

		case 6:
		{
			if (*hd->keywords)
			{
				send("Keywords (old): ");
				send(hd->keywords);
				cr(1);
				send("Keywords (new): ");
			}
			else
				send("Keywords: ");
			
			rec(s);
			cr(1);
			trim(s);
			if (*s)
			{
				strcpy(s,hd->keywords);
			}
			else
			{
				send("\"Keywords:\"-line left unchanged.");
				cr(1);
			}
			break;
		}

		case 7:
		{
			if (*hd->summary)
			{
				send("Summary (old): ");
				send(hd->summary);
				cr(1);
				send("Summary (new): ");
			}
			else
				send("Summary: ");
			
			rec(s);
			cr(1);
			trim(s);
			if (*s)
			{
				strcpy(s,hd->summary);
			}
			else
			{
				send("\"Summary:\"-line left unchanged.");
				cr(1);
			}
			break;
		}


		case 8:
		{
			if (*hd->distribution)
			{
				send("Distribution (old): ");
				send(hd->distribution);
				cr(1);
				send("Distribution (new): ");
			}
			else
				send("Distribution: ");
			
			rec(s);
			cr(1);
			trim(s);
			if (*s)
			{
				strcpy(s,hd->distribution);
			}
			else
			{
				send("Distribution left unchanged.");
				cr(1);
			}
			break;
		}


		case 9:
		{
			if (*hd->nbuf)
			{
				send("Newsgroups (old): ");
				send(hd->nbuf);
				cr(1);
				send("Newsgroups (new): ");
			}
			else
				send("Newsgroups: ");
		
			rec(s);
			cr(1);
			trim(s);
			if (*s)
			{
				strcpy(s,hd->nbuf);
			}
			else
			{
				send("Nothing changed.");
				cr(1);
			}
			break;
		}

		case 10:
		{
			if (*hd->control)
			{
				send("Control (old): ");
				send(hd->control);
				cr(1);
				send("Control (new): ");
			}
			else
				send("Control: ");
			rec(s);
			cr(1);
			trim(s);
			strcpy(s,hd->control);
			break;
		}
	
	}
}

