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

  1. /*
  2.  *      $Filename: listsearch.c $
  3.  *      $Revision: 2.2 $
  4.  *      $Date: 1994/01/17 15:11:03 $
  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: listsearch.c,v 2.2 1994/01/17 15:11:03 simons Exp simons $
  23.  *
  24.  */
  25.  
  26.  
  27. /************************************* includes ***********/
  28. #include <functions.h>
  29.  
  30. #include "listserv.h"
  31. #include "protos.h"
  32.  
  33. /************************************* global variables ***/
  34. static char rcsid[] = "$Id: listsearch.c,v 2.2 1994/01/17 15:11:03 simons Exp simons $";
  35.  
  36. /************************************* program ************/
  37. int listsearch(char *from, char *command)
  38. {
  39.         FILE *list;
  40.         FILE *listtmp;
  41.         FILE *subslist;
  42.         DIR *listdir;
  43.         struct dirent *entry;
  44.         char adr[256], tmp[512], tmp2[512], request[256], buf[BUFSIZ];
  45.         char *template = tmpnam(NULL);
  46.         int i, found = 0;
  47.  
  48.         printf("listsearch %s %s \n", from, command);
  49.         i = sscanf(command,"%s%s", request, adr);
  50.         if ((i < 1) || (i > 2))
  51.                 sendhelp(from, command);
  52.         if (i == 1)
  53.                 strcpy(adr, from);
  54.  
  55.         listdir = opendir(LISTDIR);
  56.         if (listdir == NULL)
  57.                 {
  58.                 perror(LISTDIR);
  59.                 exit(1);
  60.                 }
  61.         if (!(subslist = fopen(template, "w+")))
  62.                 {
  63.                 callmailer(LISTSERVMANAGER, from, command);
  64.                 fprintf(mailer, FATALERROR_MSG, 5);
  65.                 pclose(mailer);
  66.                 return(-1);
  67.                 }
  68.         while (entry = readdir(listdir))
  69.                 {
  70.                 sprintf(tmp, "%s%s/List", LISTDIR, entry->d_name);
  71.                 LockFile(tmp);
  72.                 list = fopen(tmp, "r");
  73.                 if (list == NULL)
  74.                         {
  75.                         UnLockFile(tmp);
  76.                         callmailer(LISTSERVMANAGER, from, command);
  77.                         fprintf(mailer, FATALERROR_MSG, 6);
  78.                         pclose(mailer);
  79.                         return(-1);
  80.                         }
  81.                 while(fgets(buf, sizeof(buf), list))
  82.                         {
  83.                         buf[strlen(buf)-1] = '\0';
  84.                         if (!stricmp(buf, adr))
  85.                                 {
  86.                                 fputs(entry->d_name, subslist);
  87.                                 fputs("\n", subslist);
  88.                                 found++;
  89.                                 }
  90.                         }
  91.                 UnLockFile(tmp);
  92.                 fclose(list);
  93.                 }
  94.         closedir(listdir);
  95.         if (!stricmp(request, "delete-all")
  96.         || !stricmp(request, "unsubscribe-all")
  97.         || !stricmp(request, "unsub-all")
  98.         || !stricmp(request, "del-all"))
  99.                 {
  100.                 rewind(subslist);
  101.                 while(fgets(buf, sizeof(tmp), subslist))
  102.                         {
  103.                         buf[strlen(buf) -1] = '\0';
  104.                         sprintf(tmp, "%s%s/List", LISTDIR, buf);
  105.                         LockFile(tmp);
  106.                         list = fopen(tmp, "r");
  107.                         if (list == NULL)
  108.                                 {
  109.                                 UnLockFile(tmp);
  110.                                 callmailer(LISTSERVMANAGER, from, command);
  111.                                 fprintf(mailer, FATALERROR_MSG, 7);
  112.                                 pclose(mailer);
  113.                                 return(-1);
  114.                                 }
  115.                         sprintf(tmp2, "%s.tmp", tmp);
  116.                         LockFile(tmp2);
  117.                         listtmp = fopen(tmp2, "w");
  118.                         if (listtmp == NULL)
  119.                                 {
  120.                                 UnLockFile(tmp2);
  121.                                 callmailer(LISTSERVMANAGER, from, command);
  122.                                 fprintf(mailer, FATALERROR_MSG, 8);
  123.                                 pclose(mailer);
  124.                                 return(-1);
  125.                                 }
  126.                         /* copy the list, omitting the one address */
  127.                         while (fgets(buf, sizeof(buf), list))
  128.                                 {
  129.                                 buf[strlen(buf)-1] = '\0';
  130.                                 if (stricmp(buf, adr))
  131.                                         {
  132.                                         fputs(buf, listtmp);
  133.                                         fputs("\n", listtmp);
  134.                                         }
  135.                                 }
  136.                         fclose(listtmp);
  137.  
  138.                         /* replace the old list with the shortened one */
  139.                         fclose(list);
  140.                         remove(tmp);
  141.                         rename(tmp2, tmp);       /* put updated one in place */
  142.                         UnLockFile(tmp);
  143.                         UnLockFile(tmp2);
  144.                         }
  145.                 }
  146.  
  147.         if (stricmp(from, adr))
  148.                 {
  149.                 callmailer(LISTSERVMANAGER, adr, command);
  150.                 fprintf(mailer,"Per request by %s\n", from);
  151.                 }
  152.         else
  153.                 {
  154.                 callmailer(LISTSERVMANAGER, from, command);
  155.                 fprintf(mailer,"Per your request\n");
  156.                 }
  157.  
  158.         fprintf(mailer,"\t\"%s\"\n", command);
  159.         fclose(subslist);       /* We have to close the file, so mailcat()
  160.                                  * can access it.
  161.                                  */
  162.         if (!found)
  163.                 fprintf(mailer, "'%s' is not subscribed to any mailing lists.\n", adr);
  164.         else {
  165.                 if (stricmp(request, "delete-all")
  166.                 && stricmp(request, "unsubscribe-all")
  167.                 && stricmp(request, "unsub-all")
  168.                 && stricmp(request, "del-all")) {
  169.                         fprintf(mailer,"'%s' is subscribed to the following mailing lists:\n", adr);
  170.                         mailcat(template, "\t");
  171.                 }
  172.                 else {
  173.                         fprintf(mailer,"'%s' was DELETED from the following mailing lists:\n", adr);
  174.                         mailcat(template, "\t");
  175.                         fprintf(mailer, "\n%s", DELETED_MSG);
  176.                 }
  177.         }
  178.         pclose(mailer);
  179.         remove(template);
  180.         fclose(subslist);
  181. }
  182.  
  183.