home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / ListSERV2_3.lha / ListSERV / source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-17  |  10.8 KB  |  350 lines

  1. /*
  2.  *      $Filename: main.c $
  3.  *      $Revision: 2.8 $
  4.  *      $Date: 1994/01/17 15:05:04 $
  5.  *
  6.  *      Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
  7.  *
  8.  *      This program is free software; you can redistribute it and/or
  9.  *      modify it under the terms of the GNU General Public License as
  10.  *      published by the Free Software Foundation; either version 2 of
  11.  *      the License, or (at your option) any later version.
  12.  *
  13.  *      This program is distributed in the hope that it will be useful,
  14.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  *      General Public License for more details.
  17.  *
  18.  *      You should have received a copy of the GNU General Public License
  19.  *      along with this program; if not, write to the Free Software
  20.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  *      $Id: main.c,v 2.8 1994/01/17 15:05:04 simons Exp simons $
  23.  *
  24.  */
  25.  
  26.  
  27. /************************************* includes ***********/
  28. #include <exec/execbase.h>
  29. #include <proto/exec.h>
  30. #include <proto/dos.h>
  31. #include <dos.h>
  32.  
  33. #include "listserv.h"
  34. #include "protos.h"
  35. #include "ListSERV_rev.h"
  36.  
  37. /************************************* defines ************/
  38. #define STARTUP "Amiga " VERS " -- written by by Peter Simons <simons@peti.GUN.de>\n"
  39. #define USAGE   "USAGE: ListSERV <mailfile [default_list]\n"
  40.  
  41. /************************************* global variables ***/
  42. static char rcssid[] = "$Id: main.c,v 2.8 1994/01/17 15:05:04 simons Exp simons $";
  43. static char dosid[] = VERSTAG;
  44. struct NetSupportLibrary *NetSupportBase;
  45. FILE *mailer;
  46. char *def_listname;
  47. extern long __stack = 1024*5;    /* In case the stack is too small,
  48.                                   * allocate a new one. (SAS/C 6+)  */
  49.  
  50. /************************************* program ************/
  51. int main(int argc, char **argv)
  52. {
  53.         extern struct ExecBase *SysBase;
  54.         char *from = NULL;
  55.         char buf[BUFSIZ];
  56.         char grp[64];
  57.         int gotcommand = 0;
  58.         int garbage = 0;
  59.  
  60.         if (SysBase->LibNode.lib_Version < 37) {
  61.                 printf("Sorry, but %s requires at least OS 2.04!\n", VERS);
  62.                 return 10;
  63.         }
  64.         printf(STARTUP);
  65.         if (argc > 2 || (argc == 2 && (!strcmp(argv[1], "?") || !stricmp(argv[1], "-h")))) {
  66.                 printf(USAGE);
  67.                 return 0;
  68.         }
  69.         if (!(NetSupportBase = (struct NetSupportLibrary *) OpenLibrary(NETSUPPORTNAME, 0L)))
  70.                 return 10;
  71.  
  72.         if (atexit(CleanUp)) {
  73.                 CleanUp();
  74.                 return 10;
  75.         }
  76.  
  77.         /* init some variables */
  78.         if (argc == 2 && argv[1])
  79.                 def_listname = argv[1];
  80.  
  81.  
  82.         /* Parse header for "from". */
  83.  
  84.         while (fgets(buf, BUFSIZ, stdin) && buf[0] != '\n') {
  85.                 /* drop trailing newline */
  86.                 buf[strlen(buf)-1] = '\0';
  87.  
  88.                 /* get the From: line so we can return the answer */
  89.                 if (!strnicmp(buf, "From: ",6))
  90.                         if (from = strdup(&buf[6]))
  91.                                 ParseAddress(from);
  92.         }
  93.  
  94.  
  95.         /* Check "from" validity! */
  96.  
  97.         if (!from) {
  98.                 WriteLog("Received mail without 'From:'-line!");
  99.                 return 5;
  100.         }
  101.         if (!strnicmp(from, "listserv", 8)) {
  102.                 printf("I refuse to talk to myself.\n");
  103.                 return 5;
  104.         }
  105.         if (stristr(from, "-daemon")) {
  106.                 printf("We don't do business with your type.\n");
  107.                 return 5;
  108.         }
  109.  
  110.  
  111.         /* Process mail body. */
  112.  
  113.         while (fgets(buf, BUFSIZ, stdin)) {
  114.                 /* drop trailing newline */
  115.                 buf[strlen(buf)-1] = '\0';
  116.  
  117.                 /* log the request */
  118.                 {
  119.                         char *tmp;
  120.  
  121.                         if (tmp = malloc(BUFSIZ*2)) {
  122.                                 sprintf(tmp, "%s: %s", from, buf);
  123.                                 WriteLog(tmp);
  124.                                 free(tmp);
  125.                         }
  126.                 }
  127.  
  128.                 if (!strnicmp(buf, "list ",5)
  129.                 || !strnicmp(buf, "delete-all ",11)
  130.                 || !strnicmp(buf, "del-all ",8)
  131.                 || !strnicmp(buf, "unsubscribe-all ",16)
  132.                 || !stricmp(buf, "list")
  133.                 || !stricmp(buf, "delete-all")
  134.                 || !stricmp(buf, "del-all")
  135.                 || !stricmp(buf, "unsubscribe-all"))
  136.                         {
  137.                         gotcommand++;
  138.                         listsearch(from, buf);
  139.                         continue;
  140.                         }
  141.  
  142.                 if (!strnicmp(buf,"add", 3)
  143.                 || !strnicmp(buf,"subscribe", 9)
  144.                 || !strnicmp(buf,"sub", 3))
  145.                         {
  146.                         gotcommand++;
  147.                         subscription(from,buf,1);
  148.                         continue;
  149.                         }
  150.  
  151.                 if (!strnicmp(buf,"delete", 6)
  152.                 || !strnicmp(buf,"del", 3)
  153.                 || !strnicmp(buf,"unsubscribe", 11))
  154.                         {
  155.                         gotcommand++;
  156.                         subscription(from,buf,0);
  157.                         continue;
  158.                         }
  159.  
  160.                 if (!stricmp(buf,"index")
  161.                 || !stricmp(buf,"longindex"))
  162.                         {
  163.                         gotcommand++;
  164.                         sendindex(from,buf,!stricmp(buf,"longindex"));
  165.                         continue;
  166.                         }
  167.  
  168.                 if (!strnicmp(buf, "faq ", 4))
  169.                         {
  170.                         gotcommand++;
  171.                         sendfaq(from, buf);
  172.                         continue;
  173.                         }
  174.  
  175.                 if (!stricmp(buf, "faq"))
  176.                         {
  177.                         gotcommand++;
  178.                         faqindex(from, buf);
  179.                         continue;
  180.                         }
  181.  
  182.                 if (!stricmp(buf,"help"))
  183.                         {
  184.                         gotcommand++;
  185.                         sendhelp(from,buf);
  186.                         continue;
  187.                         }
  188.  
  189.                 if (!strnicmp(buf,"help ",5))
  190.                         {
  191.                         gotcommand++;
  192.                         sscanf(buf,"%*s%s", grp);
  193.                         listhelp(from, grp, buf);
  194.                         continue;
  195.                         }
  196.                 /* Flush message if > 5 lines of garbage */
  197.                 if (++garbage > 5)
  198.                         {
  199.                         printf("Garbled message, flushing...\n");
  200.                         return 5;
  201.                         }
  202.                 }
  203.         if (gotcommand == 0)
  204.                 sendhelp(from, "indecipherable");
  205.  
  206.         return 0;
  207. }
  208.  
  209. void callmailer(char *carboncopy, char *toaddr, char *request)
  210. {
  211.         char cbuf[BUFSIZ];
  212.  
  213.         printf("callmailer \"%s\",\"%s\",\"%s\"\n", carboncopy, toaddr, request);
  214.  
  215.         strcpy(cbuf, GetConfig(NULL, SENDMAIL, NULL, SENDMAIL));
  216.  
  217.         strcat(cbuf, " -f " LISTSERVADDR " -R \"Mailing List Processor\"");
  218.         if (carboncopy && *carboncopy) {
  219.                 strcat(cbuf, " -c ");
  220.                 strcat(cbuf, carboncopy);
  221.         }
  222.         printf("mailer popen '%s'\n", cbuf);
  223.         if (!(mailer = popen(cbuf,"w"))) {
  224.                 perror(cbuf);
  225.                 exit(10);
  226.         }
  227.         fprintf(mailer,"To: %s\n", toaddr);
  228.         fprintf(mailer,"Subject: Re: your ListSERV request \"%s\"\n", request);
  229.         fprintf(mailer,"X-ListSERV: Peti's Amiga " VERS "\n");
  230.         fprintf(mailer,"\n");
  231. }
  232.  
  233. void mailcat(char *mfname, char *prefix)
  234. {
  235.         FILE *mf;
  236.         char buf[BUFSIZ];
  237.  
  238.         printf("mailcat \"%s\", \"%s\"\n", mfname, prefix);
  239.  
  240.         LockFile(mfname);
  241.         mf = fopen(mfname,"r");
  242.         if (mf == NULL)
  243.                 {
  244.                 UnLockFile(mfname);
  245.                 perror(mfname);
  246.                 fprintf(mailer,"This should have been the contents of\n");
  247.                 fprintf(mailer,"file '%s' but it's missing!\n",mfname);
  248.                 return;
  249.                 }
  250.  
  251.         /* copy the message file into the mailer */
  252.         while (fgets(buf,BUFSIZ,mf) != NULL)
  253.                 {
  254.                 fputs(prefix,mailer);
  255.                 fputs(buf,mailer);
  256.                 }
  257.         fclose(mf);
  258.         UnLockFile(mfname);
  259.         return;
  260. }
  261.  
  262.  
  263. void ParseAddress(char *line)
  264. {
  265.         int brackets, i, z;
  266.  
  267.         for (i = 0, z = 0, brackets = 0; line[i] != '\0'; i++) {
  268.                 if (line[i] == '(')
  269.                         brackets++;
  270.                 else if (line[i] == ')')
  271.                         brackets--;
  272.                 else if (line[i] == '<' && brackets == 0) {
  273.                         i++;
  274.                         while (1) {             /* Forever! */
  275.                                 if (line[i] == '>' && (line[i+1] == '\0' || isspace(line[i+1])))
  276.                                         break;
  277.                                 line[z++] = line[i++];
  278.                         }
  279.                         line[z] = '\0';
  280.                         break;
  281.                 }
  282.         }
  283.         if (z == 0) {   /* No <address> occured. */
  284.                 i = 0;
  285.                 while (!isspace(line[i]))
  286.                         i++;
  287.                 line[i] = '\0';
  288.         }
  289. }
  290.  
  291.  
  292. void CleanUp(void)              /* is called by atexit() automatically */
  293. {
  294.         UnLockFiles();
  295.         CloseLibrary((struct Library *) NetSupportBase);
  296. }
  297.  
  298.  
  299.  /* Replace the UNIX function with an AmigaOS version. */
  300.  
  301. void perror(const char *dummy)
  302. {
  303.         extern int _OSERR;
  304.         PrintFault(_OSERR, "ListSERV");
  305. }
  306.  
  307.  
  308. void WriteLog(STRPTR logmessage)
  309. {
  310.         struct DateTime dt;
  311.         BPTR fh;
  312.         TEXT StrDate[LEN_DATSTRING], StrTime[LEN_DATSTRING];
  313.  
  314.         DateStamp((struct DateStamp *) &dt);
  315.         dt.dat_Format = FORMAT_DOS;
  316.         dt.dat_StrDay = NULL;
  317.         dt.dat_StrDate = StrDate;
  318.         dt.dat_StrTime = StrTime;
  319.  
  320.         if (DateToStr(&dt)) {
  321.                 CheckDateString(StrDate);       /* remove trailing blanks */
  322.                 CheckDateString(StrTime);
  323.                 LockFile(LOGFILE);
  324.                 if (fh = Open(LOGFILE, MODE_READWRITE)) {
  325.                         Seek(fh, 0L, OFFSET_END);
  326.                         FPrintf(fh, "(%s/%s) %s\n", StrDate, StrTime, logmessage);
  327.                         Printf("(%s/%s) %s\n", StrDate, StrTime, logmessage);
  328.                         Close(fh);
  329.                 }
  330.                 UnLockFile(LOGFILE);
  331.         }
  332. }
  333.  
  334.  
  335.         /* This routine removes trailing spaces from the generated date
  336.          * string. These blanks are added in order make all date strings
  337.          * the same length. However, this is not required for our
  338.          * application.
  339.          */
  340.  
  341. void CheckDateString(STRPTR datestring)
  342. {
  343.         while(*datestring)
  344.                 datestring++;
  345.  
  346.         while(*(datestring-1) == ' ')
  347.                 *(--datestring) = '\0';
  348. }
  349.  
  350.