aboutsummaryrefslogtreecommitdiff
path: root/lib/dbcfg.c
blob: f47545f7486188b223aef1eae735534d2144ecfd (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
/* This file is part of Mailfromd.
   Copyright (C) 2005-2018 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, see <http://www.gnu.org/licenses/>. */

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

#include <mailutils/mailutils.h>
#include <mailutils/cfg.h>
#include <mailutils/alloc.h>
#include "libmf.h"
#include "mfdb.h"

static int
cb_db_expire_interval(void *data, mu_config_value_t *arg)
{
	time_t *iptr = data;
	struct timeval tv;
	int rc;
	
	rc = config_cb_timeout(&tv, arg);
	*iptr = tv.tv_sec;
	return rc;
}

static int
cb_db_positive_expire_interval(void *data, mu_config_value_t *arg)
{
	struct db_format *fmt = data;
	struct timeval tv;
	int rc;
	
	if (fmt != cache_format) {
		mu_error (_("positive-expire-interval is valid only "
			    "for cache database"));
		return 0;
	}

	rc = config_cb_timeout(&tv, arg);
	if (rc == 0)
		fmt->expire_interval = tv.tv_sec;
	return rc;
}

static int
cb_db_negative_expire_interval(void *data, mu_config_value_t *arg)
{
	struct db_format *fmt = data;
	struct timeval tv;
	int rc = config_cb_timeout(&tv, arg);
	
	if (fmt == cache_format)
		negative_expire_interval = tv.tv_sec;
	else
		mu_error(_("negative-expire-interval is valid only "
			   "for cache database"));
	return rc;
}

int
cb_database_file(void *data, mu_config_value_t *arg)
{
	char **pname = data;
	if (mu_cfg_assert_value_type(arg, MU_CFG_STRING))
		return 1;
	*pname = mu_strdup(arg->v.string);
	return 0;
}
	
struct mu_cfg_param database_section_param[] = {
	{ "file", mu_cfg_callback, NULL,
	  offsetof(struct db_format, dbname), cb_database_file,
	  N_("Name of the database file"),
          N_("name: string") },
	{ "enable", mu_c_bool, NULL, offsetof(struct db_format, enabled), NULL,
	  N_("Enable or disable the database") },
	{ "expire-interval", mu_cfg_callback, NULL,
	  offsetof(struct db_format, expire_interval), cb_db_expire_interval,
	  N_("Set database record expiration interval"),
	  N_("arg: interval") },
	{ "positive-expire-interval", mu_cfg_callback, NULL, 0,
	  cb_db_positive_expire_interval,
	  N_("Set positive expiration interval (for database \"cache\" only)"),
	  N_("arg: interval") },
	{ "negative-expire-interval", mu_cfg_callback, NULL, 0,
	  cb_db_negative_expire_interval,
	  N_("Set negative expiration interval "
	     "(for database id \"cache\")"),
	  N_("arg: interval") },
	{ NULL }
};

static int
database_section_parser (enum mu_cfg_section_stage stage,
			 const mu_cfg_node_t *node,
			 const char *section_label, void **section_data,
			 void *call_data,
			 mu_cfg_tree_t *tree)
{
	switch (stage) {
	case mu_cfg_section_start: {
		struct db_format *fmt;

		if (mu_cfg_assert_value_type(node->label, MU_CFG_STRING))
			return 0;
		fmt = db_format_lookup(node->label->v.string);
		if (fmt == NULL) {
			mu_error(_("unknown database format: %s"),
				 node->label->v.string);
			return 0;
		}
		*section_data = fmt;
	}
		break;

	case mu_cfg_section_end:
		break;
	}
	return 0;
}

int
cb_database_mode(void *data, mu_config_value_t *arg)
{
	unsigned long mode;
	char *end;
	if (mu_cfg_assert_value_type(arg, MU_CFG_STRING))
		return 1;
	mode = strtoul(arg->v.string, &end, 0);
	if (*end || (mode & ~0777)) {
		mu_error("%s", _("invalid file mode"));
		return 1;
	} else
		*(int*)data = mode;
	return 0;
}

void
database_cfg_init()
{
	struct mu_cfg_section *section;
	if (mu_create_canned_section("database", &section) == 0) {
		section->label = N_("id");
		section->parser = database_section_parser;
		section->docstring = N_("Define a database.");
		mu_cfg_section_add_params(section, database_section_param);
	}
#if 0
//  FIXME: If MU had canned params, I could've done something along these
//  lines.  Unfortunately, it doesn't
		
	if (mu_create_canned_param ("database-mode", &param) == 0) {
		param->type = mu_cfg_callback;
		param->callback = cb_database_mode;
		param->docstring =
			N_("Configure file mode for database files");
		param->argname = N_("mode: octal");
	}
#endif
}

Return to:

Send suggestions and report system problems to the System administrator.