aboutsummaryrefslogtreecommitdiff
path: root/src/cache.c
blob: 10f8a9a59231f39e64dfac67825b7a24a974384d (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* This file is part of mailfromd.
   Copyright (C) 2005, 2006, 2007 Sergey Poznyakoff

   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, 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., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

#define MF_SOURCE_NAME  MF_SOURCE_CACHE

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <mailutils/mailutils.h>
#include "mailfromd.h"

struct cache_result {
	time_t timestamp;
	mf_status status;
};

#define EXPIRE_INTERVAL(s) \
 ((s) == mf_success ? cache_format->expire_interval : negative_expire_interval)

mf_status
cache_get(char *email)
{
	mf_status rc;
	DBM_FILE db;
	DBM_DATUM key;
	DBM_DATUM contents;
	int readonly = 0;
	int res;

	if (!cache_format->enabled)
		return mf_failure;
	
	debug1(50, "getting cache info for %s", email);
	if (mu_dbm_open(cache_format->dbname, &db, MU_STREAM_RDWR, 0600,
            &readonly)) {
		mu_error(_("mu_dbm_open(%s) failed: %s"), cache_format->dbname,
			 mu_dbm_strerror());
		return mf_failure;
	}
	if (readonly) 
		debug1(1, "cannot lock %s: switching to read-only mode",
		       cache_format->dbname);
	
	memset (&key, 0, sizeof key);
	memset (&contents, 0, sizeof contents);
	MU_DATUM_PTR (key) = email;
	MU_DATUM_SIZE (key) = strlen (email) + 1;
	if ((res = mu_dbm_fetch(&db, key, &contents)) == 0) {
		char timebuf[80];
		struct cache_result *res = (struct cache_result *)
			                     MU_DATUM_PTR(contents);
		time_t t = time(NULL);
		debug3(50, "found status: %s (%d), time: %s",
		       mf_status_str(res->status), res->status,
		       mailfromd_timestr(res->timestamp,
					 timebuf, sizeof timebuf));
		
		if (t - res->timestamp > EXPIRE_INTERVAL(res->status)) {
			if (!readonly) {
				debug1(50,
				       "removing expired entry for %s",email);
				if (mu_dbm_delete(&db, key))
					mu_error(_("Cannot remove record `%s' from `%s': %s"),
						 email, db.name, 
		                                 mu_dbm_strerror());
			}
			rc = mf_failure;
		} else {
			rc = res->status;
		}
	} else {
		if (res != MU_ERR_NOENT)
			mu_error(_("Cannot fetch record `%s' from `%s': %s"),
				 email, db.name, mu_dbm_strerror());
		rc = mf_failure;
	}
	mu_dbm_datum_free(&contents);
	mu_dbm_close(&db);
	return rc;
}

void
cache_insert(char *email, mf_status rc)
{
	DBM_FILE db;
	DBM_DATUM key;
	DBM_DATUM contents;
	struct cache_result res;
	char timebuf[80];
	
	if (!cache_format->enabled)
		return;

	time(&res.timestamp);
	res.status = rc;
	debug4(50,"inserting cache info for %s. status=%s (%d), time=%s",
	       email, mf_status_str(rc), rc,
	       mailfromd_timestr(res.timestamp, timebuf, sizeof timebuf));
	if (mu_dbm_open(cache_format->dbname, &db, MU_STREAM_RDWR, 
                        0600, NULL)) {
		mu_error(_("mu_dbm_open(%s) failed: %s"), cache_format->dbname,
			 mu_dbm_strerror());
		return;
	}

	memset(&key, 0, sizeof key);
	memset(&contents, 0, sizeof contents);
	MU_DATUM_PTR(key) = email;
	MU_DATUM_SIZE(key) = strlen (email) + 1;
	MU_DATUM_PTR(contents) = (void*)&res;
	MU_DATUM_SIZE(contents) = sizeof(res);
	if (mu_dbm_insert(&db, key, contents, 1))
		mu_error(_("Cannot insert datum `%s' into `%s': %s"), 
                         email, db.name, mu_dbm_strerror());
	
	mu_dbm_close(&db);
}

static void
cache_print_item(const char *email, size_t size, const void *content)
{
	const struct cache_result *res = content;

	size--; /* Size includes the trailing nul */
	printf("%*.*s", size, size, email);
	printf(" %10.10s ", mf_status_str(res->status));
	format_time_str(stdout, res->timestamp);
	putchar('\n');
}

static int
cache_expire_item(const void *content)
{
	const struct cache_result *res = (struct cache_result *)content;
	return !res || time(NULL) - res->timestamp > EXPIRE_INTERVAL(res->status);
}

mf_status
cache_get2(char *email, char *client_addr)
{
	mf_status rc = mf_failure;
	size_t size;

	if (!cache_format->enabled)
		return mf_failure;
	size = strlen(email) + 1 + strlen(client_addr) + 1;
	char *key = malloc(size);
	if (key) {
		strcat(strcat(strcpy(key, email), ":"), client_addr);
		rc = cache_get(key);
		free(key);
	}
	return rc;
}

void
cache_insert2(char *email, char *client_addr, mf_status rc)
{
	if (!cache_format->enabled)
		return;
	else { 
		size_t size = strlen(email) + 1 + strlen(client_addr) + 1;
		char *key = malloc(size);
		if (key) {
			strcat(strcat(strcpy(key, email), ":"), client_addr);
			cache_insert(key, rc);
			free(key);
		}
	}
}

static struct db_format cache_format_struct = {
	"cache",
	DEFAULT_DATABASE,
	1,
	DEFAULT_EXPIRE_INTERVAL,
	cache_print_item,
	cache_expire_item
};

struct db_format *cache_format = &cache_format_struct;

Return to:

Send suggestions and report system problems to the System administrator.