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

  1. /*
  2.  *      $Filename: subscribe.c $
  3.  *      $Revision: 2.4 $
  4.  *      $Date: 1994/01/17 16:02:18 $
  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: subscribe.c,v 2.4 1994/01/17 16:02:18 simons Exp simons $
  23.  *
  24.  */
  25.  
  26.  
  27. /************************************* includes ***********/
  28. #include "listserv.h"
  29. #include "protos.h"
  30.  
  31. /************************************* global variables ***/
  32. static char rcsid[] = "$Id: subscribe.c,v 2.4 1994/01/17 16:02:18 simons Exp simons $";
  33.  
  34. /************************************* program ************/
  35. int subscription(char *from, char *command, int add)
  36. {
  37.         FILE *list;
  38.         FILE *listtmp;
  39.         char grp[256], adr[256], tmp[256], tmp2[256], buf[BUFSIZ];
  40.         int del = 0, found = 0, i;
  41.  
  42.         printf("subscription %s %s %d\n", from, command, add);
  43.  
  44.         i = sscanf(command,"%s%s%s", tmp, adr, grp);
  45.         if ((i <= 1 && !def_listname) || i > 3) {
  46.                 sendhelp(from, command);
  47.                 return -1;
  48.         }
  49.         else if (i == 2)
  50.                 if (def_listname && (stristr(adr, "!") || stristr(adr, "@")))
  51.                         strcpy(grp, def_listname);
  52.                 else {
  53.                         strcpy(grp, adr);
  54.                         strcpy(adr, from);
  55.                 }
  56.         else if (i == 1) {
  57.                 strcpy(grp, def_listname);
  58.                 strcpy(adr, from);
  59.         }
  60.  
  61.         if (!stricmp(grp, adr))
  62.                 {
  63.                 callmailer(LISTSERVMANAGER, from, command);
  64.                 fprintf(mailer,"Subscription address loop: %s\n", adr);
  65.                 pclose(mailer);
  66.                 return(-1);
  67.                 }
  68.  
  69.         sprintf(tmp,"%s%s/List", LISTDIR, grp);
  70.         LockFile(tmp);
  71.         if (access(tmp,R_OK))
  72.                 {
  73.                 UnLockFile(tmp);
  74.                 callmailer("", from, command);
  75.                 fprintf(mailer,"The mailing list \"%s\" could not be found.\n",
  76.                         grp);
  77.                 fprintf(mailer,"You may use the INDEX command to get a listing\n");
  78.                 fprintf(mailer,"of available mailing lists.\n");
  79.                 pclose(mailer);
  80.                 return(-1);
  81.                 }
  82.  
  83.         if (add)
  84.                 {
  85.                 if (!(list = fopen(tmp, "r+")))
  86.                         {
  87.                         UnLockFile(tmp);
  88.                         callmailer(LISTSERVMANAGER, from, command);
  89.                         fprintf(mailer, FATALERROR_MSG, 1);
  90.                         pclose(mailer);
  91.                         return(-1);
  92.                         }
  93.  
  94.                 /* check if address is already subscribed */
  95.                 while (fgets(buf, sizeof(buf), list)) {
  96.                         buf[strlen(buf)-1] = '\0';
  97.                         if (!stricmp(buf, adr))
  98.                                 found++;
  99.                 }
  100.                 if (!found)
  101.                         if (fseek(list, 0L, SEEK_END) != -1)
  102.                                 fprintf(list, "%s\n", adr);
  103.                 fclose(list);
  104.                 UnLockFile(tmp);
  105.                 }
  106.         else
  107.                 {
  108.                 del = 0;
  109.                 if (!(list = fopen(tmp, "r")))
  110.                         {
  111.                         UnLockFile(tmp);
  112.                         callmailer(LISTSERVMANAGER, from, command);
  113.                         fprintf(mailer, FATALERROR_MSG, 2);
  114.                         pclose(mailer);
  115.                         return(-1);
  116.                         }
  117.  
  118.                 sprintf(tmp2, "%s.tmp", tmp);
  119.                 LockFile(tmp2);
  120.                 if (!(listtmp = fopen(tmp2, "w")))
  121.                         {
  122.                         callmailer(LISTSERVMANAGER, from, command);
  123.                         fprintf(mailer, FATALERROR_MSG, 3);
  124.                         pclose(mailer);
  125.                         return(-1);
  126.                         }
  127.                 /* copy the list, omitting the one address */
  128.                 while (fgets(buf, sizeof(buf), list))
  129.                         {
  130.                         buf[strlen(buf)-1] = '\0';
  131.                         if (stricmp(buf, adr))
  132.                                 {
  133.                                 fputs(buf, listtmp);
  134.                                 fputs("\n", listtmp);
  135.                                 }
  136.                         else
  137.                                 del++;
  138.                         }
  139.                 fclose(listtmp);
  140.                 fclose(list);
  141.                 /* replace the old list with the shortened one */
  142.  
  143.                 remove(tmp);            /* delete original file */
  144.                 rename(tmp2, tmp);      /* put updated one in place */
  145.                 UnLockFile(tmp);
  146.                 UnLockFile(tmp2);
  147.                 }
  148.  
  149.  
  150.         /* Mail subscription confirmation and introduction files.*/
  151.  
  152.         sprintf(tmp, "%s-Admin", grp);
  153.         if (stricmp(from, adr)) {
  154.                 callmailer(tmp, adr, command);
  155.                 fprintf(mailer, "Per request by %s\n", from);
  156.         }
  157.         else {
  158.                 callmailer(tmp, from, command);
  159.                 fprintf(mailer,"Per your request\n");
  160.         }
  161.  
  162.         fprintf(mailer,"\t\"%s\"\n", command);
  163.  
  164.         if (add)
  165.                 if (!found) {
  166.                         fprintf(mailer,"'%s' was ADDED to the '%s' mailing list.\n", adr, grp);
  167.  
  168.                         sprintf(buf, "%s%s/Introduction", LISTDIR, grp);
  169.                         LockFile(buf);
  170.                         if (!access(buf,R_OK)) {
  171.                                 UnLockFile(buf);
  172.                                 fprintf(mailer, "\n\n------------------------ List introduction ---------------------------\n");
  173.                                 mailcat(buf, "");
  174.                                 fprintf(mailer, "----------------------------------------------------------------------\n");
  175.                         }
  176.                         else
  177.                                 UnLockFile(buf);
  178.                 }
  179.                 else
  180.                         fprintf(mailer,"'%s' is already subscribed to the '%s' mailing list.\n", adr, grp);
  181.         else
  182.                 if (del) {
  183.                         fprintf(mailer, "'%s' was DELETED from the '%s' mailing list.\n\n", adr, grp);
  184.                         fprintf(mailer, DELETED_MSG);
  185.                 }
  186.                 else
  187.                         fprintf(mailer, "'%s' was NOT FOUND on the '%s' mailing list.\n", adr, grp);
  188.  
  189.         pclose(mailer);
  190. }
  191.