[alt.sources] newsmail - mail news articles to users automagically

erc@khijol.UUCP (Edwin R. Carp) (01/13/90)

This is a handy little program I threw together on a boring Saturday
evening to send mail from certain selected groups to me at work.  It could
also be used to "distribute" news groups to users.  It, um, parses the Subject
and Newsgroups lines from the article, then calls mail to mail it off.  This
can be put in your sys file to mail stuff - see the source for details.

Since it consists of only one file, I didn't go to the trouble of shar'ing
it or anything.  It uses a standard "cc -s -o newsmail newsmail.c -O" to
compile, and shouldn't require anything fancy.

If you: like it/don't like it/have suggestions, please let me know.
--------------------------------- cut here -----------------------------------
/*
*
* newsmail - package stdin appropriate for mailing, then mail to args
*          puts newsgroup(if any), then original subject in -s line.
*          like: "Subject: newsgroup: Subject"
*
* Handy for remailing stuff from news to individual users:
*
*          fwd1:sci.space,world::newsmail user (in /usr/lib/news/sys file)
*
* Should work on just about any system (BSD, Sys V, XENIX)
*
* Compile like:  cc -s -o newsmail newsmail.c -O
*
* flow:
*
* read stdin, copying to /tmp file.
* strip off Subject, Newsgroups lines along the way.
* construct subject line.
* call mail to do the dirty work.
*
*/
#include <stdio.h>

#define SUBJECT "Subject: "
#define NEWSG "Newsgroups: "

/*
* this should be the only place you have to change anything.
*/

#ifdef BSD
#define MAILER "Mail"
#else
#define MAILER "mail"
#endif

main(argc, argv)
int argc;
char **argv;
{
   char Subject[512];
   char Newsgroup[512];
   char outfile[20];
   char line[512];
   FILE *out;
   int lsub, lnews, i;

   sprintf(outfile, "/tmp/newsmail.%d", getpid());
   if((out=fopen(outfile, "w")) == (FILE *)NULL)
   {
      perror(outfile);
      exit(1);
   }
   lsub = strlen(SUBJECT);
   lnews = strlen(NEWSG);
   while(gets(line) != (char *)NULL)
   {
      if(strncmp(line, SUBJECT, lsub) == 0) strcpy(Subject, line+lsub);
      if(strncmp(line, NEWSG, lnews) == 0) strcpy(Newsgroup, line+lnews);
      strcat(line, "\n");
      fputs(line, out);
   }
   fclose(out);
   if(*Subject == NULL) strcpy(Subject, "<none>");
   if(*Newsgroup == NULL) strcpy(Newsgroup, "<none>");
   for(i=1; i<argc; i++)
   {
      sprintf(line, "%s -s '%s: %s' %s < %s",
          MAILER, Newsgroup, Subject, argv[i], outfile);
      puts(line);
      system(line);
   }
   unlink(outfile);
}
-- 
Ed Carp			N7EKG/5 (28.3-28.5)	uunet!cs.utexas.edu!khijol!erc
Austin, Texas		(512) 832-5884		"Good tea.  Nice house." - Worf