petidomo/archive.c

151 lines
4.5 KiB
C
Raw Normal View History

2000-12-13 13:19:03 +00:00
/*
2019-07-10 13:00:47 +00:00
* Copyright (c) 1995-2019 Peter Simons <simons@cryp.to>
2010-02-24 16:01:14 +00:00
* Copyright (c) 2000-2001 Cable & Wireless GmbH
* Copyright (c) 1999-2000 CyberSolutions GmbH
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
2000-12-13 13:19:03 +00:00
#include <config.h>
2000-12-13 13:19:03 +00:00
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "libtext/text.h"
#include "petidomo.h"
2000-12-13 13:19:03 +00:00
int
ArchiveMail(const struct Mail * MailStruct, const char * listname)
{
2000-12-13 13:19:03 +00:00
const struct List_Config * ListConfig;
struct stat sb;
FILE * fh;
char * filename;
unsigned int counter;
2000-12-13 13:19:03 +00:00
int rc;
struct tm * timeptr;
char * date;
time_t t;
2000-12-13 13:19:03 +00:00
assert(MailStruct != NULL);
assert(listname != NULL);
/* Initialize internals. */
ListConfig = getListConfig(listname);
t = time(NULL);
timeptr = localtime(&t);
2000-12-13 13:19:03 +00:00
date = asctime(timeptr);
rc = -1;
/* Sanity checks. */
if (ListConfig->archivepath == NULL)
return 0;
2000-12-13 13:19:03 +00:00
/* Check whether we have a file or a directory. */
if (stat(ListConfig->archivepath, &sb) == 0 && (sb.st_mode & S_IFDIR) != 0)
{
2000-12-13 13:19:03 +00:00
/* Read the "active"-file to see at what article number we
2000-12-13 13:19:03 +00:00
were. */
filename = text_easy_sprintf("%s/.active", ListConfig->archivepath);
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\": %s", filename, strerror(errno));
else
counter = 0;
}
/* Store the article. */
do
{
filename = text_easy_sprintf("%s/%u", ListConfig->archivepath, 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: %s", filename, strerror(errno));
break;
}
else
{
syslog(LOG_ERR, "An error while trying to access the log " \
"directory \"%s\": %s", ListConfig->archivepath, strerror(errno));
break;
}
}
}
while (++counter); /* until break */
/* Write the current "active" number back to the file. */
counter++;
filename = text_easy_sprintf("%s/.active", ListConfig->archivepath);
fh = fopen(filename, "w");
if (fh != NULL)
{
fprintf(fh, "%u", counter);
fclose(fh);
}
else
syslog(LOG_ERR, "Failed to write to file \"%s\": %s", filename, strerror(errno));
}
else
{
/* Simply append the article to this file. */
fh = fopen(ListConfig->archivepath, "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: %s", ListConfig->archivepath, strerror(errno));
}
2000-12-13 13:19:03 +00:00
return rc;
}