-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.c
150 lines (129 loc) · 4.48 KB
/
archive.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (c) 1995-2019 Peter Simons <simons@cryp.to>
* 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/>.
*/
#include <config.h>
#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"
int
ArchiveMail(const struct Mail * MailStruct, const char * listname)
{
const struct List_Config * ListConfig;
struct stat sb;
FILE * fh;
char * filename;
unsigned int counter;
int rc;
struct tm * timeptr;
char * date;
time_t t;
assert(MailStruct != NULL);
assert(listname != NULL);
/* Initialize internals. */
ListConfig = getListConfig(listname);
t = time(NULL);
timeptr = localtime(&t);
date = asctime(timeptr);
rc = -1;
/* Sanity checks. */
if (ListConfig->archivepath == NULL)
return 0;
/* Check whether we have a file or a directory. */
if (stat(ListConfig->archivepath, &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", 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));
}
return rc;
}