himalaya 0.5.7

Command-line interface for email management
Documentation
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Module related to message CLI.
//!
//! This module provides subcommands, arguments and a command matcher related to message.

use anyhow::Result;
use clap::{self, App, Arg, ArgMatches, SubCommand};
use log::{debug, info, trace};

use crate::{
    mbox::mbox_arg,
    msg::{flag_arg, msg_arg, tpl_arg},
    ui::table_arg,
};

type Seq<'a> = &'a str;
type PageSize = usize;
type Page = usize;
type Mbox<'a> = &'a str;
type TextMime<'a> = &'a str;
type Raw = bool;
type All = bool;
type RawMsg<'a> = &'a str;
type Query = String;
type AttachmentPaths<'a> = Vec<&'a str>;
type MaxTableWidth = Option<usize>;
type Encrypt = bool;
type Criteria = String;

/// Message commands.
#[derive(Debug, PartialEq, Eq)]
pub enum Cmd<'a> {
    Attachments(Seq<'a>),
    Copy(Seq<'a>, Mbox<'a>),
    Delete(Seq<'a>),
    Forward(Seq<'a>, AttachmentPaths<'a>, Encrypt),
    List(MaxTableWidth, Option<PageSize>, Page),
    Move(Seq<'a>, Mbox<'a>),
    Read(Seq<'a>, TextMime<'a>, Raw),
    Reply(Seq<'a>, All, AttachmentPaths<'a>, Encrypt),
    Save(RawMsg<'a>),
    Search(Query, MaxTableWidth, Option<PageSize>, Page),
    Sort(Criteria, Query, MaxTableWidth, Option<PageSize>, Page),
    Send(RawMsg<'a>),
    Write(AttachmentPaths<'a>, Encrypt),

    Flag(Option<flag_arg::Cmd<'a>>),
    Tpl(Option<tpl_arg::Cmd<'a>>),
}

/// Message command matcher.
pub fn matches<'a>(m: &'a ArgMatches) -> Result<Option<Cmd<'a>>> {
    info!("entering message command matcher");

    if let Some(m) = m.subcommand_matches("attachments") {
        info!("attachments command matched");
        let seq = m.value_of("seq").unwrap();
        debug!("seq: {}", seq);
        return Ok(Some(Cmd::Attachments(seq)));
    }

    if let Some(m) = m.subcommand_matches("copy") {
        info!("copy command matched");
        let seq = m.value_of("seq").unwrap();
        debug!("seq: {}", seq);
        let mbox = m.value_of("mbox-target").unwrap();
        debug!(r#"target mailbox: "{:?}""#, mbox);
        return Ok(Some(Cmd::Copy(seq, mbox)));
    }

    if let Some(m) = m.subcommand_matches("delete") {
        info!("copy command matched");
        let seq = m.value_of("seq").unwrap();
        debug!("seq: {}", seq);
        return Ok(Some(Cmd::Delete(seq)));
    }

    if let Some(m) = m.subcommand_matches("forward") {
        info!("forward command matched");
        let seq = m.value_of("seq").unwrap();
        debug!("seq: {}", seq);
        let paths: Vec<&str> = m.values_of("attachments").unwrap_or_default().collect();
        debug!("attachments paths: {:?}", paths);
        let encrypt = m.is_present("encrypt");
        debug!("encrypt: {}", encrypt);
        return Ok(Some(Cmd::Forward(seq, paths, encrypt)));
    }

    if let Some(m) = m.subcommand_matches("list") {
        info!("list command matched");
        let max_table_width = m
            .value_of("max-table-width")
            .and_then(|width| width.parse::<usize>().ok());
        debug!("max table width: {:?}", max_table_width);
        let page_size = m.value_of("page-size").and_then(|s| s.parse().ok());
        debug!("page size: {:?}", page_size);
        let page = m
            .value_of("page")
            .unwrap_or("1")
            .parse()
            .ok()
            .map(|page| 1.max(page) - 1)
            .unwrap_or_default();
        debug!("page: {}", page);
        return Ok(Some(Cmd::List(max_table_width, page_size, page)));
    }

    if let Some(m) = m.subcommand_matches("move") {
        info!("move command matched");
        let seq = m.value_of("seq").unwrap();
        debug!("seq: {}", seq);
        let mbox = m.value_of("mbox-target").unwrap();
        debug!("target mailbox: {:?}", mbox);
        return Ok(Some(Cmd::Move(seq, mbox)));
    }

    if let Some(m) = m.subcommand_matches("read") {
        info!("read command matched");
        let seq = m.value_of("seq").unwrap();
        debug!("seq: {}", seq);
        let mime = m.value_of("mime-type").unwrap();
        debug!("text mime: {}", mime);
        let raw = m.is_present("raw");
        debug!("raw: {}", raw);
        return Ok(Some(Cmd::Read(seq, mime, raw)));
    }

    if let Some(m) = m.subcommand_matches("reply") {
        info!("reply command matched");
        let seq = m.value_of("seq").unwrap();
        debug!("seq: {}", seq);
        let all = m.is_present("reply-all");
        debug!("reply all: {}", all);
        let paths: Vec<&str> = m.values_of("attachments").unwrap_or_default().collect();
        debug!("attachments paths: {:?}", paths);
        let encrypt = m.is_present("encrypt");
        debug!("encrypt: {}", encrypt);

        return Ok(Some(Cmd::Reply(seq, all, paths, encrypt)));
    }

    if let Some(m) = m.subcommand_matches("save") {
        info!("save command matched");
        let msg = m.value_of("message").unwrap_or_default();
        trace!("message: {}", msg);
        return Ok(Some(Cmd::Save(msg)));
    }

    if let Some(m) = m.subcommand_matches("search") {
        info!("search command matched");
        let max_table_width = m
            .value_of("max-table-width")
            .and_then(|width| width.parse::<usize>().ok());
        debug!("max table width: {:?}", max_table_width);
        let page_size = m.value_of("page-size").and_then(|s| s.parse().ok());
        debug!("page size: {:?}", page_size);
        let page = m
            .value_of("page")
            .unwrap()
            .parse()
            .ok()
            .map(|page| 1.max(page) - 1)
            .unwrap_or_default();
        debug!("page: {}", page);
        let query = m
            .values_of("query")
            .unwrap_or_default()
            .fold((false, vec![]), |(escape, mut cmds), cmd| {
                match (cmd, escape) {
                    // Next command is an arg and needs to be escaped
                    ("subject", _) | ("body", _) | ("text", _) => {
                        cmds.push(cmd.to_string());
                        (true, cmds)
                    }
                    // Escaped arg commands
                    (_, true) => {
                        cmds.push(format!("\"{}\"", cmd));
                        (false, cmds)
                    }
                    // Regular commands
                    (_, false) => {
                        cmds.push(cmd.to_string());
                        (false, cmds)
                    }
                }
            })
            .1
            .join(" ");
        debug!("query: {}", query);
        return Ok(Some(Cmd::Search(query, max_table_width, page_size, page)));
    }

    if let Some(m) = m.subcommand_matches("sort") {
        info!("sort command matched");
        let max_table_width = m
            .value_of("max-table-width")
            .and_then(|width| width.parse::<usize>().ok());
        debug!("max table width: {:?}", max_table_width);
        let page_size = m.value_of("page-size").and_then(|s| s.parse().ok());
        debug!("page size: {:?}", page_size);
        let page = m
            .value_of("page")
            .unwrap()
            .parse()
            .ok()
            .map(|page| 1.max(page) - 1)
            .unwrap_or_default();
        debug!("page: {:?}", page);
        let criteria = m
            .values_of("criterion")
            .unwrap_or_default()
            .collect::<Vec<_>>()
            .join(" ");
        debug!("criteria: {:?}", criteria);
        let query = m
            .values_of("query")
            .unwrap_or_default()
            .fold((false, vec![]), |(escape, mut cmds), cmd| {
                match (cmd, escape) {
                    // Next command is an arg and needs to be escaped
                    ("subject", _) | ("body", _) | ("text", _) => {
                        cmds.push(cmd.to_string());
                        (true, cmds)
                    }
                    // Escaped arg commands
                    (_, true) => {
                        cmds.push(format!("\"{}\"", cmd));
                        (false, cmds)
                    }
                    // Regular commands
                    (_, false) => {
                        cmds.push(cmd.to_string());
                        (false, cmds)
                    }
                }
            })
            .1
            .join(" ");
        debug!("query: {:?}", query);
        return Ok(Some(Cmd::Sort(
            criteria,
            query,
            max_table_width,
            page_size,
            page,
        )));
    }

    if let Some(m) = m.subcommand_matches("send") {
        info!("send command matched");
        let msg = m.value_of("message").unwrap_or_default();
        trace!("message: {}", msg);
        return Ok(Some(Cmd::Send(msg)));
    }

    if let Some(m) = m.subcommand_matches("write") {
        info!("write command matched");
        let attachment_paths: Vec<&str> = m.values_of("attachments").unwrap_or_default().collect();
        debug!("attachments paths: {:?}", attachment_paths);
        let encrypt = m.is_present("encrypt");
        debug!("encrypt: {}", encrypt);
        return Ok(Some(Cmd::Write(attachment_paths, encrypt)));
    }

    if let Some(m) = m.subcommand_matches("template") {
        return Ok(Some(Cmd::Tpl(tpl_arg::matches(m)?)));
    }

    if let Some(m) = m.subcommand_matches("flag") {
        return Ok(Some(Cmd::Flag(flag_arg::matches(m)?)));
    }

    info!("default list command matched");
    Ok(Some(Cmd::List(None, None, 0)))
}

/// Message sequence number argument.
pub fn seq_arg<'a>() -> Arg<'a, 'a> {
    Arg::with_name("seq")
        .help("Specifies the targetted message")
        .value_name("SEQ")
        .required(true)
}

/// Message sequence range argument.
pub fn seq_range_arg<'a>() -> Arg<'a, 'a> {
    Arg::with_name("seq-range")
        .help("Specifies targetted message(s)")
        .long_help("Specifies a range of targetted messages. The range follows the [RFC3501](https://datatracker.ietf.org/doc/html/rfc3501#section-9) format: `1:5` matches messages with sequence number between 1 and 5, `1,5` matches messages with sequence number 1 or 5, * matches all messages.")
        .value_name("SEQ")
        .required(true)
}

/// Message reply all argument.
pub fn reply_all_arg<'a>() -> Arg<'a, 'a> {
    Arg::with_name("reply-all")
        .help("Includes all recipients")
        .short("A")
        .long("all")
}

/// Message page size argument.
fn page_size_arg<'a>() -> Arg<'a, 'a> {
    Arg::with_name("page-size")
        .help("Page size")
        .short("s")
        .long("size")
        .value_name("INT")
}

/// Message page argument.
fn page_arg<'a>() -> Arg<'a, 'a> {
    Arg::with_name("page")
        .help("Page number")
        .short("p")
        .long("page")
        .value_name("INT")
        .default_value("0")
}

/// Message attachment argument.
pub fn attachment_arg<'a>() -> Arg<'a, 'a> {
    Arg::with_name("attachments")
        .help("Adds attachment to the message")
        .short("a")
        .long("attachment")
        .value_name("PATH")
        .multiple(true)
}

/// Message encrypt argument.
pub fn encrypt_arg<'a>() -> Arg<'a, 'a> {
    Arg::with_name("encrypt")
        .help("Encrypts the message")
        .short("e")
        .long("encrypt")
}

/// Message subcommands.
pub fn subcmds<'a>() -> Vec<App<'a, 'a>> {
    vec![
        flag_arg::subcmds(),
        tpl_arg::subcmds(),
        vec![
            SubCommand::with_name("attachments")
                .aliases(&["attachment", "att", "a"])
                .about("Downloads all message attachments")
                .arg(msg_arg::seq_arg()),
            SubCommand::with_name("list")
                .aliases(&["lst", "l"])
                .about("Lists all messages")
                .arg(page_size_arg())
                .arg(page_arg())
                .arg(table_arg::max_width()),
            SubCommand::with_name("search")
                .aliases(&["s", "query", "q"])
                .about("Lists messages matching the given IMAP query")
                .arg(page_size_arg())
                .arg(page_arg())
                .arg(table_arg::max_width())
                .arg(
                    Arg::with_name("query")
                        .help("IMAP query")
                        .long_help("The IMAP query format follows the [RFC3501](https://tools.ietf.org/html/rfc3501#section-6.4.4). The query is case-insensitive.")
                        .value_name("QUERY")
                        .multiple(true)
                        .required(true),
                ),
            SubCommand::with_name("sort")
                .about("Sorts messages by the given criteria and matching the given IMAP query")
                .arg(page_size_arg())
                .arg(page_arg())
                .arg(table_arg::max_width())
		.arg(
		    Arg::with_name("criterion")
			.long("criterion")
			.short("c")
			.help("Defines the message sorting preferences")
			.value_name("CRITERION:ORDER")
			.takes_value(true)
			.multiple(true)
			.required(true)
			.possible_values(&[
			    "arrival", "arrival:asc", "arrival:desc",
			    "cc", "cc:asc", "cc:desc",
			    "date", "date:asc", "date:desc",
			    "from", "from:asc", "from:desc",
			    "size", "size:asc", "size:desc",
			    "subject", "subject:asc", "subject:desc",
			    "to", "to:asc", "to:desc",
			]),
		)
                .arg(
                    Arg::with_name("query")
                        .help("IMAP query")
                        .long_help("The IMAP query format follows the [RFC3501](https://tools.ietf.org/html/rfc3501#section-6.4.4). The query is case-insensitive.")
                        .value_name("QUERY")
			.default_value("ALL")
                        .raw(true),
                ),
            SubCommand::with_name("write")
                .about("Writes a new message")
                .arg(attachment_arg())
                .arg(encrypt_arg()),
            SubCommand::with_name("send")
                .about("Sends a raw message")
                .arg(Arg::with_name("message").raw(true)),
            SubCommand::with_name("save")
                .about("Saves a raw message")
                .arg(Arg::with_name("message").raw(true)),
            SubCommand::with_name("read")
                .about("Reads text bodies of a message")
                .arg(seq_arg())
                .arg(
                    Arg::with_name("mime-type")
                        .help("MIME type to use")
                        .short("t")
                        .long("mime-type")
                        .value_name("MIME")
                        .possible_values(&["plain", "html"])
                        .default_value("plain"),
                )
                .arg(
                    Arg::with_name("raw")
                        .help("Reads raw message")
                        .long("raw")
                        .short("r"),
                ),
            SubCommand::with_name("reply")
                .aliases(&["rep", "r"])
                .about("Answers to a message")
                .arg(seq_arg())
                .arg(reply_all_arg())
                .arg(attachment_arg())
		.arg(encrypt_arg()),
            SubCommand::with_name("forward")
                .aliases(&["fwd", "f"])
                .about("Forwards a message")
                .arg(seq_arg())
                .arg(attachment_arg())
		.arg(encrypt_arg()),
            SubCommand::with_name("copy")
                .aliases(&["cp", "c"])
                .about("Copies a message to the targetted mailbox")
                .arg(seq_arg())
                .arg(mbox_arg::target_arg()),
            SubCommand::with_name("move")
                .aliases(&["mv"])
                .about("Moves a message to the targetted mailbox")
                .arg(seq_arg())
                .arg(mbox_arg::target_arg()),
            SubCommand::with_name("delete")
                .aliases(&["del", "d", "remove", "rm"])
                .about("Deletes a message")
                .arg(seq_arg()),
        ],
    ]
    .concat()
}