/* This sample program can be used to create efficient robots and auto
responders for dnews news servers.


The articles are read from STDIN, Unlike other systems dnews keeps the program
running and sends it each item so it can process items much faster.
Articles are sent in the following way.

ARTICLE group:item_number <messageid>
(Actual article text)
.  (single full stop is end of article) 
ARTICLE group:item_number <messageid>
(Actual article text)
.  (single full stop is end of article) 
etc...

For this example error messages are printed to the log file along with 
the actual articles.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif



#define MAX_LINE 20000
void main(int argc, char *argv[])
{
	FILE *f;
	char bf[MAX_LINE];
	int inarticle= FALSE;
	f = fopen(argv[1],"a");
	if (f==NULL) {perror(argv[1]); return;}	

	for (;!feof(stdin);) {
  	  if (fgets(bf,MAX_LINE-1,stdin)==NULL) break;
	  if (!inarticle) {
	  	if (strncmp(bf,"ARTICLE",7)==0) {
			inarticle = TRUE;
			continue;
		} else if (strncmp(bf,"EXIT",4)==0) {
			fprintf(f,"--- DNEWS asked us to exit\n");
			break;
		} else {
			fprintf(f,"--- Unknown Command {%s} \n",bf);
			continue;
		} 
	  } else {
		if (strcmp(bf,".\n")==0) { /* single full stop is end of message */
			fprintf(f,"-------------END MESSAGE----\n");
			fflush(f); 
			inarticle = FALSE;
			continue;
		}
		if (strcmp(bf,"..\n")==0) strcpy(bf,".\n"); /* message contained a full stop so it was translated */
	 	fwrite(bf,1,strlen(bf),f); /* Write message to archive */
 	  }
	}
	fprintf(f,"----------Exiting\n");
	fclose(f);
}
