summaryrefslogtreecommitdiff
path: root/mu/mu-options.hh
blob: f9575ce4da865cd8aaa6a53e55008a0b8faa28e8 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
** Copyright (C) 2022-2026 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.
**
*/

#ifndef MU_OPTIONS_HH__
#define MU_OPTIONS_HH__

#include <sstream>
#include <string>
#include <vector>
#include <utils/mu-option.hh>
#include <utils/mu-result.hh>
#include <utils/mu-utils.hh>
#include <utils/mu-utils-file.hh>

#include <message/mu-fields.hh>
#include <mu-script.hh>
#include <ctime>
#include <sys/stat.h>

/* command-line options for Mu */
namespace Mu {
struct Options {
	using	OptSize	     = Option<std::size_t>;
	using	SizeVec	     = std::vector<std::size_t>;
	using	OptTStamp    = Option<std::time_t>;
	using	OptFieldId   = Option<Field::Id>;
	using   OptString    = Option<std::string>;
	using	StringVec    = std::vector<std::string>;

	/*
	 * general options
	 */
	bool		quiet;			/**<  don't give any output */
	bool		debug;			/**<  log debug-level info */
	bool		version;		/**<  request mu version */
	bool		log_stderr;		/**<  log to stderr */
	bool		nocolor;		/**<  don't use use ansi-colors */
	bool		verbose;		/**<  verbose output */
	std::string	muhome;			/**<  alternative mu dir */

	/**
	 * Whether by default, we should show color
	 *
	 * @return true or false
	 */
	static bool default_no_color();

	enum struct SubCommand {
		Add, Cfind, Extract, Fields, Find, Help, Index,Info, Init, Labels, Mkdir,
		Move, Remove, Scm, Script, Server, Verify, View,
		// <private>
		__count__
	};
	static constexpr auto SubCommandNum = static_cast<size_t>(SubCommand::__count__);
	static constexpr std::array<SubCommand, SubCommandNum> SubCommands = {{
			SubCommand::Add,
			SubCommand::Cfind,
			SubCommand::Extract,
			SubCommand::Fields,
			SubCommand::Find,
			SubCommand::Help,
			SubCommand::Index,
			SubCommand::Info,
			SubCommand::Init,
			SubCommand::Labels,
			SubCommand::Mkdir,
			SubCommand::Move,
			SubCommand::Remove,
			SubCommand::Scm,
			SubCommand::Script,
			SubCommand::Server,
			SubCommand::Verify,
			SubCommand::View
		}};

	Option<SubCommand> sub_command;			/**< The chosen sub-command, if any. */

	/*
	 * Add
	 */
	struct Add {
		StringVec files;			/**< field to add */
	} add;

	/*
	 * Cfind
	 */
	struct Cfind {
		enum struct Format { Plain, MuttAlias, MuttAddressBook,
			Wanderlust, OrgContact, Bbdb, Csv, Json };
		Format		format;			/**< Output format */
		bool		personal;		/**< only show personal contacts */
		OptTStamp	after;			/**< only last seen after tstamp */
		OptSize		maxnum;			/**< maximum number of results */
		std::string	rx_pattern;		/**< contact regexp to match */
	} cfind;


	struct Crypto {
		bool	auto_retrieve;			/**< auto-retrieve keys */
		bool	decrypt;			/**< decrypt */
	};

	/*
	 * Extract
	 */
	struct Extract: public Crypto {
		std::string	message;		/**<  path to message file */
		bool		save_all;		/**<  extract all parts */
		bool		save_attachments;	/**<  extract all attachment parts */
		SizeVec		parts;			/**<  parts to save /  open */
		std::string	targetdir{};		/**<  where to save attachments */
		bool		overwrite;		/**<  overwrite same-named files */
		bool		play;			/**<  try to 'play' attachment */
		std::string	filename_rx;		/**<  Filename rx to save */
		bool		uncooked{};		/**<  Whether to avoid massaging
							    * the output filename */
	} extract;

	/*
	 * Fields
	 */

	/*
	 * Find
	 */
	struct Find {
		std::string	fields;			/**<  fields to show in output */
		Field::Id	sortfield;		/**<  field to sort by	*/
		OptSize		maxnum;			/**<  max # of entries to print */
		bool		reverse;		/**<  sort in revers order (z->a) */
		bool		threads;		/**<  show message threads */
		bool		clearlinks;		/**<  clear linksdir first */
		std::string	linksdir;		/**<  directory for links */
		OptSize		summary_len;		/**<  max # of lines for summary */
		std::string	bookmark;		/**<  use bookmark */
		bool		analyze;		/**<  analyze query */

		enum struct Format { Plain, Links, Xml, Json, Json2, Sexp, Exec };
		Format		format;			/**< Output format */
		std::string	exec;			/**<  cmd to execute on matches */
		bool		skip_dups;		/**< show only first with msg id */
		bool		include_related;	/**<  included related messages */
							/**<  for find and cind */
		OptTStamp	after;			/**<  only last seen after T */
		bool		auto_retrieve;		/**<  assume we're online */
		bool		decrypt;		/**<  try to decrypt the body */

		StringVec	query;			/**< search query */
	} find;

	struct Help {
		std::string command;			/**< Help parameter */
	} help;

	/*
	 * Index
	 */
	struct Index {
		bool	nocleanup;			/**< don't cleanup del'd mails */
		bool	lazycheck;			/**< don't check uptodate dirs */
		bool	reindex;			/**< do a full re-index */
	} index;


	/*
	 * Info
	 */
	struct Info {
		std::string topic;			/**< what to get info about? */
	} info;

	/*
	 * Init
	 */
	struct Init {
		std::string	maildir;		/**< where the mails are */
		StringVec	personal_addresses;	/**< personal e-mail addresses */
		StringVec	ignored_addresses;	/**< addresses to be ignored for
							 * the contacts-cache */
		OptSize		max_msg_size;		/**< max size for message files */
		OptSize		batch_size;		/**< db transaction batch size */
		bool		reinit;			/**< re-initialize  */
		bool		support_ngrams;		/**< support CJK etc. ngrams */

	} init;

	/*
	 * Labels
	 */
	struct Labels {
		OptString query;                         /**< Query for the messages to label */
		bool dry_run{};				 /**< Merely print the messages that would be
							  * labeled without doing so */
		StringVec delta_labels;                  /**< labels to add (+) or remove (-) */
		bool read_only{};                        /**< do not require writable store */
		OptString file;                           /** file for import/export */

		enum struct Sub { // sub-subcommands
			Update, //  add/remove labels
			Clear,  // clear all labels
			List,   // list all labels in the store (from cache)
			RestoreList, // restore the labels cache
			Export, // export labels
			Import, // import labels
		};
		Option<Sub> sub; // the chosen sub-command
	} labels;

	/*
	 * Mkdir
	 */
	struct Mkdir {
		StringVec	dirs;			/**< Dir(s) to create */
		mode_t		mode;			/**< Mode for the maildir */
	} mkdir;

	/*
	 * Move
	 */
	struct Move {
		std::string	src;		       /**< Source file */
		std::string	dest;		       /**< Destination dir */
		std::string	flags;		       /**< Flags for destination */
		bool            change_name;           /**< Change basename for destination */
		bool            update_dups;           /**< Update duplicate messages too */
		bool            dry_run;               /**< Just print the result path,
							but do not change anything */
	} move;

	/*
	 * Remove
	 */
	struct Remove {
		StringVec	files;			/**< Files to remove */
	} remove;


	/*
	 * Scm
	 */
	struct Scm {
		OptString script_path; /**< Path to script (optional) */
		StringVec params;      /**< Parameters for script (after "--") */
		bool listen; /**< Whether to start listening on a socket */
		Option<std::string> socket_path; /**< path for the '--listen'
						  * Unix domain socket */
		Option<std::string> eval; /**< expression to evaluate */
	} scm;


	/*
	 * Scripts (i.e., finding scriot)
	 */
	struct Script {
		std::string	name;			/**< name of script */
		StringVec	params;			/**< script params */
	} script;

	/*
	 * Server
	 */
	struct Server {
		bool		commands;		/**< dump docs for commands */
		std::string	eval;			/**< command to evaluate */
		bool		allow_temp_file;	/**< temp-file optimization allowed? */
	} server;

	/*
	 * Verify
	 */
	struct Verify: public Crypto {
		StringVec	files;			/**< message files to verify */
	} verify;
	/*
	 * View
	 */
	struct View: public Crypto {
		bool	terminate;			/**< add \f between msgs in view */
		OptSize summary_len;			/**< max # of lines for summary */

		enum struct Format { Plain, Sexp, Html };
		Format format;				/**< output format*/

		StringVec files;			/**< Message file(s) */
	} view;


	/**
	 * Create an Options structure fo the given command-line arguments.
	 *
	 * @param argc argc
	 * @param argv argc
	 *
	 * @return Options, or an Error
	 */
	static Result<Options> make(int argc, char *argv[]);


	/**
	 * Different commands need different things
	 *
	 */
	enum struct Category {
		None,
		NeedsReadOnlyStore,
		NeedsWritableStore,
	};

	/**
	 * Get the category for some subcommand
	 *
	 * @param sub subcommand
	 *
	 * @return the category
	 */
	static Category category(SubCommand sub);

	/**
	 * Get some well-known Path
	 *
	 * @param path the Path to find
	 *
	 * @return the path name
	 */
	std::string runtime_path(RuntimePath path) const {
		return Mu::runtime_path(path, muhome);
	}
};

} // namepace Mu

#endif /* MU_OPTIONS_HH__ */