/* $Source$ $Revision$ Copyright (C) 2000 by CyberSolutions GmbH, Germany. This file is part of OpenPetidomo. OpenPetidomo 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, or (at your option) any later version. OpenPetidomo 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. */ #include #include #include #include #include #include #include "libtext/text.h" #include "petidomo.h" int ArchiveMail(const struct Mail * MailStruct, const char * listname) { const struct List_Config * ListConfig; struct stat sb; FILE * fh; char * filename; char * path; u_int counter; int rc; struct tm * timeptr; char * date; time_t clock; assert(MailStruct != NULL); assert(listname != NULL); /* Initialize internals. */ ListConfig = getListConfig(listname); clock = time(NULL); timeptr = localtime(&clock); date = asctime(timeptr); rc = -1; /* Sanity checks. */ if (ListConfig->archivepath == NULL) return 0; /* Construct the path to the log file or directory. */ if (*(ListConfig->archivepath) == '/') path = xstrdup(ListConfig->archivepath); else { path = text_easy_sprintf("lists/%s/%s", listname, ListConfig->archivepath); path = xstrdup(path); } /* Check whether we have a file or a directory. */ if (stat(path, &sb) == 0 && (sb.st_mode & S_IFDIR) != 0) { /* Read the "active"-file to see at what article number we were. */ filename = text_easy_sprintf("%s/.active", path); fh = fopen(filename, "r"); if (fh != NULL) { fscanf(fh, "%u", &counter); fclose(fh); } else { if (errno != ENOENT) syslog(LOG_ERR, "Failed to read file \"%s\": %m", filename); else counter = 0; } /* Store the article. */ do { filename = text_easy_sprintf("%s/%u", path, counter); if (stat(filename, &sb) == -1) { if (errno == ENOENT) { fh = fopen(filename, "a"); if (fh != NULL) { fprintf(fh, "From %s-owner@%s %s", listname, ListConfig->fqdn, date); fprintf(fh, "%s\n", MailStruct->Header); fprintf(fh, "%s\n", MailStruct->Body); fclose(fh); rc = 0; } else syslog(LOG_ERR, "Failed opening file \"%s\" for writing: %m", filename); break; } else { syslog(LOG_ERR, "An error while trying to access the log " \ "directory \"%s\": %m", path); break; } } } while (++counter); /* until break */ /* Write the current "active" number back to the file. */ counter++; filename = text_easy_sprintf("%s/.active", path); fh = fopen(filename, "w"); if (fh != NULL) { fprintf(fh, "%u", counter); fclose(fh); } else syslog(LOG_ERR, "Failed to write to file \"%s\": %m", filename); } else { /* Simply append the article to this file. */ fh = fopen(path, "a"); if (fh != NULL) { /* Write an envelope first. */ fprintf(fh, "From %s-owner@%s %s", listname, ListConfig->fqdn, date); fprintf(fh, "%s\n", MailStruct->Header); fprintf(fh, "%s\n", MailStruct->Body); fclose(fh); rc = 0; } else syslog(LOG_ERR, "Failed opening file \"%s\" for writing: %m", path); } free(path); return rc; }