home *** CD-ROM | disk | FTP | other *** search
- #include <functions.h>
-
- #include "listserv.h"
- #include "protos.h"
-
- static char rcsid[] = "$Id: listsearch.c,v 1.3.1.9 1993/12/29 06:56:57 simons Exp simons $";
-
- int listsearch(char *from,
- char *command)
- {
- FILE *list;
- FILE *listtmp;
- FILE *subslist;
- DIR *listdir;
- struct dirent *entry;
- char adr[256], tmp[512], tmp2[512], request[256], buf[BUFSIZ];
- char *template = tmpnam(NULL);
- int i, found = 0;
-
- printf("listsearch %s %s \n", from, command);
- i = sscanf(command,"%s%s", request, adr);
- if ((i < 1) || (i > 2))
- sendhelp(from, command);
- if (i == 1)
- strcpy(adr, from);
-
- listdir = opendir(LISTDIR);
- if (listdir == NULL)
- {
- perror(LISTDIR);
- exit(1);
- }
- if (!(subslist = fopen(template, "w+")))
- {
- callmailer(LISTSERVMANAGER, from, "");
- fprintf(mailer,"Error[5] processing request. Please try later.\n");
- fprintf(mailer,">%s\n", command);
- fflush(mailer);
- pclose(mailer);
- return(-1);
- }
- while (entry = readdir(listdir))
- {
- sprintf(tmp, "%s%s/List", LISTDIR, entry->d_name);
- LockFile(tmp);
- list = fopen(tmp, "r");
- if (list == NULL)
- {
- UnLockFile(tmp);
- callmailer(LISTSERVMANAGER, from, "");
- fprintf(mailer,"Error[6] processing request. Please try later.\n");
- fprintf(mailer,">%s\n", command);
- fflush(mailer);
- pclose(mailer);
- return(-1);
- }
- while(fgets(buf, sizeof(buf), list))
- {
- buf[strlen(buf)-1] = '\0';
- if (!stricmp(buf, adr))
- {
- fputs(entry->d_name, subslist);
- fputs("\n", subslist);
- found++;
- }
- }
- fflush(list);
- UnLockFile(tmp);
- fclose(list);
- }
- closedir(listdir);
- if (!stricmp(request, "delete-all")
- || !stricmp(request, "unsubscribe-all")
- || !stricmp(request, "unsub-all")
- || !stricmp(request, "del-all"))
- {
- rewind(subslist);
- while(fgets(buf, sizeof(tmp), subslist))
- {
- buf[strlen(buf) -1] = '\0';
- sprintf(tmp, "%s%s/List", LISTDIR, buf);
- LockFile(tmp);
- list = fopen(tmp, "r");
- if (list == NULL)
- {
- UnLockFile(tmp);
- callmailer(LISTSERVMANAGER, from, "");
- fprintf(mailer, "Error[7] processing request. Please try later.\n");
- fprintf(mailer, ">%s\n", command);
- fflush(mailer);
- pclose(mailer);
- return(-1);
- }
- sprintf(tmp2, "%s.tmp", tmp);
- LockFile(tmp2);
- listtmp = fopen(tmp2, "w");
- if (listtmp == NULL)
- {
- UnLockFile(tmp2);
- callmailer(LISTSERVMANAGER, from, "");
- fprintf(mailer,"Error[8] processing request. Please try later.\n");
- fprintf(mailer,">%s\n", command);
- fflush(mailer);
- pclose(mailer);
- return(-1);
- }
- /* copy the list, omitting the one address */
- while (fgets(buf, sizeof(buf), list))
- {
- buf[strlen(buf)-1] = '\0';
- if (stricmp(buf, adr))
- {
- fputs(buf, listtmp);
- fputs("\n", listtmp);
- }
- }
- fclose(listtmp);
-
- /* replace the old list with the shortened one */
- fclose(list);
- remove(tmp);
- rename(tmp2, tmp); /* put updated one in place */
- UnLockFile(tmp);
- UnLockFile(tmp2);
- }
- }
-
- if (stricmp(from, adr))
- {
- callmailer(LISTSERVMANAGER, adr, command);
- fprintf(mailer,"Per request by %s\n", from);
- }
- else
- {
- callmailer(LISTSERVMANAGER, from, command);
- fprintf(mailer,"Per your request\n");
- }
-
- fprintf(mailer,"\t\"%s\"\n", command);
- fclose(subslist); /* We have to close the file, so mailcat()
- * can access it.
- */
- if (!found)
- fprintf(mailer, "'%s' is not subscribed to any mailing lists.\n", adr);
- else
- {
- if (stricmp(request, "delete-all")
- && stricmp(request, "unsubscribe-all")
- && stricmp(request, "unsub-all")
- && stricmp(request, "del-all"))
- fprintf(mailer,"'%s' is subscribed to the following mailing lists:\n", adr);
- else
- fprintf(mailer,"'%s' was DELETED from the following mailing lists:\n", adr);
- mailcat(template, "\t");
- }
- fflush(mailer);
- pclose(mailer);
- remove(template);
- fflush(subslist);
- fclose(subslist);
- }
-
-