home *** CD-ROM | disk | FTP | other *** search
- /*
- * $Filename: listsearch.c $
- * $Revision: 2.2 $
- * $Date: 1994/01/17 15:11:03 $
- *
- * Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Id: listsearch.c,v 2.2 1994/01/17 15:11:03 simons Exp simons $
- *
- */
-
-
- /************************************* includes ***********/
- #include <functions.h>
-
- #include "listserv.h"
- #include "protos.h"
-
- /************************************* global variables ***/
- static char rcsid[] = "$Id: listsearch.c,v 2.2 1994/01/17 15:11:03 simons Exp simons $";
-
- /************************************* program ************/
- 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, command);
- fprintf(mailer, FATALERROR_MSG, 5);
- 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, command);
- fprintf(mailer, FATALERROR_MSG, 6);
- 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++;
- }
- }
- 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, command);
- fprintf(mailer, FATALERROR_MSG, 7);
- pclose(mailer);
- return(-1);
- }
- sprintf(tmp2, "%s.tmp", tmp);
- LockFile(tmp2);
- listtmp = fopen(tmp2, "w");
- if (listtmp == NULL)
- {
- UnLockFile(tmp2);
- callmailer(LISTSERVMANAGER, from, command);
- fprintf(mailer, FATALERROR_MSG, 8);
- 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);
- mailcat(template, "\t");
- }
- else {
- fprintf(mailer,"'%s' was DELETED from the following mailing lists:\n", adr);
- mailcat(template, "\t");
- fprintf(mailer, "\n%s", DELETED_MSG);
- }
- }
- pclose(mailer);
- remove(template);
- fclose(subslist);
- }
-
-