summaryrefslogtreecommitdiff
path: root/mu/mu-cmd-init.cc
blob: 8463ce7b275777f7aa8e320d16ce043f9459b3a6 (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
/*
** Copyright (C) 2023-2025 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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.
**
*/

#include "config.h"

#include "mu-cmd.hh"

using namespace Mu;

#ifndef BUILD_TESTS

Result<void>
Mu::mu_cmd_init(const Options& opts)
{
	auto store = std::invoke([&]()->Result<Store> {

		/*
		 * reinit
		 */
		if (opts.init.reinit)
			return Store::make(opts.runtime_path(RuntimePath::XapianDb),
					   Store::Options::ReInit|Store::Options::Writable);
		/*
		 * full init
		 */

		/* not provided, nor could we find a good default */
		if (opts.init.maildir.empty())
			return Err(Error::Code::InvalidArgument,
				   "missing --maildir parameter and could "
				   "not determine default");
		else if (!g_path_is_absolute(opts.init.maildir.c_str()))
			return Err(Error{Error::Code::File,
					"--maildir is not absolute"});

		MemDb mdb;
		Config conf{mdb};

		if (opts.init.max_msg_size)
			conf.set<Config::Id::MaxMessageSize>(*opts.init.max_msg_size);
		if (opts.init.batch_size && *opts.init.batch_size != 0)
			conf.set<Config::Id::BatchSize>(*opts.init.batch_size);
		if (!opts.init.personal_addresses.empty())
			conf.set<Config::Id::PersonalAddresses>(opts.init.personal_addresses);
		if (!opts.init.ignored_addresses.empty())
			conf.set<Config::Id::IgnoredAddresses>(opts.init.ignored_addresses);
		if (opts.init.support_ngrams)
			conf.set<Config::Id::SupportNgrams>(true);

		return Store::make_new(opts.runtime_path(RuntimePath::XapianDb),
				       opts.init.maildir, conf);
	});

	if (!store)
		return Err(store.error());

	if (!opts.quiet) {

		mu_println("mu has been {} with the following properties:",
			   opts.init.reinit ? "reinitialized" : "created");
		// mildly hacky
		Options opts_copy{opts};
		opts_copy.info.topic = "store";
		mu_cmd_info(*store, opts_copy);

		mu_println("Database is empty. You can use 'mu index' to fill it.");
	}

	return Ok();
}



#else /* BUILD_TESTS */

/*
 * Tests.
 *
 */
#include <config.h>
#include <mu-store.hh>
#include "utils/mu-test-utils.hh"


static void
test_mu_init_basic()
{
	TempDir temp_dir{};

	const auto mu_home{temp_dir.path()};

	auto res1 = run_command({MU_PROGRAM, "--quiet", "init",
			"--muhome", mu_home, "--maildir" , MU_TESTMAILDIR2});
	assert_valid_command(res1);

	auto&& store = unwrap(Store::make(join_paths(temp_dir.path(), "xapian")));
	g_assert_true(store.empty());
}

static void
test_mu_init_maildir()
{
	TempDir temp_dir{};

	const auto mu_home{temp_dir.path()};

	g_setenv("MAILDIR", MU_TESTMAILDIR2, 1);
	auto res1 = run_command({MU_PROGRAM, "--quiet", "init",
			"--muhome", mu_home});
	assert_valid_command(res1);

	auto&& store = unwrap(Store::make(join_paths(temp_dir.path(), "xapian")));
	g_assert_true(store.empty());
	assert_equal(store.root_maildir(), MU_TESTMAILDIR2);
}


static void
test_mu_init_personal()
{
	TempDir temp_dir{};

	const auto mu_home{temp_dir.path()};

	g_setenv("MAILDIR", MU_TESTMAILDIR2, 1);
	auto res1 = run_command({MU_PROGRAM, "--quiet", "init",
			"--muhome", mu_home, "--personal-address", "foo@example.com",
			"--my-address", "bar@example.com", // backward compat
			"--personal-address", "/h.llo@example\\.com/"});
	assert_valid_command(res1);

	auto&& store = unwrap(Store::make(join_paths(temp_dir.path(), "xapian")));
	g_assert_true(store.empty());
	assert_equal(store.root_maildir(), MU_TESTMAILDIR2);

	const auto& ccache{store.contacts_cache()};

	// just some basic tests to see the parameters made it through.

	g_assert_true(ccache.is_personal("foo@example.com"));
	g_assert_true(ccache.is_personal("bar@example.com"));
	g_assert_true(ccache.is_personal("hello@example.com"));
	g_assert_true(ccache.is_personal("hallo@example.com"));

	g_assert_false(ccache.is_personal("faa@example.com"));
	g_assert_false(ccache.is_personal("baa@example.com"));
}

int
main(int argc, char* argv[])
{
	mu_test_init(&argc, &argv);

	g_test_add_func("/cmd/init/basic", test_mu_init_basic);
	g_test_add_func("/cmd/init/maildir", test_mu_init_maildir);
	g_test_add_func("/cmd/init/personal", test_mu_init_personal);

	return g_test_run();
}

#endif /*BUILD_TESTS*/