home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / ListSERV1_4.lha / ListSERV / source / listsearch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-29  |  6.3 KB  |  163 lines

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