You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

821 lines
36 KiB

  1. /*
  2. * meli
  3. *
  4. * Copyright 2017-2018 Manos Pitsidianakis
  5. *
  6. * This file is part of meli.
  7. *
  8. * meli is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * meli is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with meli. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /*! A parser module for user commands passed through Command mode.
  22. */
  23. use crate::melib::parser::BytesExt;
  24. use melib::nom::{
  25. self,
  26. branch::alt,
  27. bytes::complete::{is_a, is_not, tag, take_until},
  28. character::complete::{digit1, not_line_ending},
  29. combinator::{map, map_res},
  30. multi::separated_list,
  31. sequence::{pair, preceded, separated_pair},
  32. IResult,
  33. };
  34. pub use melib::thread::{SortField, SortOrder};
  35. use melib::MeliError;
  36. pub mod actions;
  37. use actions::MailboxOperation;
  38. use std::collections::HashSet;
  39. pub mod history;
  40. pub use crate::actions::AccountAction::{self, *};
  41. pub use crate::actions::Action::{self, *};
  42. pub use crate::actions::ComposeAction::{self, *};
  43. pub use crate::actions::ListingAction::{self, *};
  44. pub use crate::actions::MailingListAction::{self, *};
  45. pub use crate::actions::TabAction::{self, *};
  46. pub use crate::actions::TagAction::{self, *};
  47. pub use crate::actions::ViewAction::{self, *};
  48. use std::str::FromStr;
  49. /// Helper macro to convert an array of tokens into a TokenStream
  50. macro_rules! to_stream {
  51. ($token: expr) => {
  52. TokenStream {
  53. tokens: &[$token],
  54. }
  55. };
  56. ($($tokens:expr),*) => {
  57. TokenStream {
  58. tokens: &[$($token),*],
  59. }
  60. };
  61. }
  62. /// Macro to create a const table with every command part that can be auto-completed and its description
  63. macro_rules! define_commands {
  64. ( [$({ tags: [$( $tags:literal),*], desc: $desc:literal, tokens: $tokens:expr, parser: ($parser:item)}),*]) => {
  65. pub const COMMAND_COMPLETION: &[(&str, &str, TokenStream)] = &[$($( ($tags, $desc, TokenStream { tokens: $tokens } ) ),*),* ];
  66. $( $parser )*
  67. };
  68. }
  69. pub fn quoted_argument(input: &[u8]) -> IResult<&[u8], &str> {
  70. if input.is_empty() {
  71. return Err(nom::Err::Error((input, nom::error::ErrorKind::Tag)));
  72. }
  73. if input[0] == b'"' {
  74. let mut i = 1;
  75. while i < input.len() {
  76. if input[i] == b'\"' && input[i - 1] != b'\\' {
  77. return Ok((&input[i + 1..], unsafe {
  78. std::str::from_utf8_unchecked(&input[1..i])
  79. }));
  80. }
  81. i += 1;
  82. }
  83. Err(nom::Err::Error((input, nom::error::ErrorKind::Tag)))
  84. } else {
  85. map_res(is_not(" "), std::str::from_utf8)(input)
  86. }
  87. }
  88. #[derive(Debug, Copy, Clone)]
  89. pub struct TokenStream {
  90. tokens: &'static [TokenAdicity],
  91. }
  92. use Token::*;
  93. use TokenAdicity::*;
  94. impl TokenStream {
  95. fn matches<'s>(&self, s: &mut &'s str, sugg: &mut HashSet<String>) -> Vec<(&'s str, Token)> {
  96. let mut tokens = vec![];
  97. for t in self.tokens.iter() {
  98. let mut ptr = 0;
  99. while ptr + 1 < s.len() && s.as_bytes()[ptr].is_ascii_whitespace() {
  100. ptr += 1;
  101. }
  102. *s = &s[ptr..];
  103. //println!("{:?} {:?}", t, s);
  104. if s.is_empty() {
  105. match t.inner() {
  106. Literal(lit) => {
  107. sugg.insert(format!(" {}", lit));
  108. }
  109. Alternatives(v) => {
  110. for t in v.iter() {
  111. //println!("adding empty suggestions for {:?}", t);
  112. let mut _s = *s;
  113. t.matches(&mut _s, sugg);
  114. }
  115. }
  116. Seq(_s) => {}
  117. RestOfStringValue => {}
  118. AttachmentIndexValue
  119. | MailboxIndexValue
  120. | IndexValue
  121. | Filepath
  122. | AccountName
  123. | MailboxPath
  124. | QuotedStringValue
  125. | AlphanumericStringValue => {}
  126. }
  127. return tokens;
  128. }
  129. match t.inner() {
  130. Literal(lit) => {
  131. if lit.starts_with(*s) && lit.len() != s.len() {
  132. sugg.insert(lit[s.len()..].to_string());
  133. return tokens;
  134. } else if s.starts_with(lit) {
  135. tokens.push((&s[..lit.len()], *t.inner()));
  136. *s = &s[lit.len()..];
  137. } else {
  138. return vec![];
  139. }
  140. }
  141. Alternatives(v) => {
  142. let mut cont = true;
  143. for t in v.iter() {
  144. let mut _s = *s;
  145. let mut m = t.matches(&mut _s, sugg);
  146. if !m.is_empty() {
  147. tokens.extend(m.drain(..));
  148. //println!("_s is empty {}", _s.is_empty());
  149. cont = !_s.is_empty();
  150. *s = _s;
  151. break;
  152. }
  153. }
  154. if !cont {
  155. *s = "";
  156. }
  157. }
  158. Seq(_s) => {
  159. return vec![];
  160. }
  161. RestOfStringValue => {
  162. tokens.push((*s, *t.inner()));
  163. return tokens;
  164. }
  165. AttachmentIndexValue
  166. | MailboxIndexValue
  167. | IndexValue
  168. | Filepath
  169. | AccountName
  170. | MailboxPath
  171. | QuotedStringValue
  172. | AlphanumericStringValue => {
  173. let mut ptr = 0;
  174. while ptr + 1 < s.len() && !s.as_bytes()[ptr].is_ascii_whitespace() {
  175. ptr += 1;
  176. }
  177. tokens.push((&s[..ptr + 1], *t.inner()));
  178. *s = &s[ptr + 1..];
  179. }
  180. }
  181. }
  182. tokens
  183. }
  184. }
  185. /// `Token` wrapper that defines how many times a token is expected to be repeated
  186. #[derive(Debug, Copy, Clone)]
  187. pub enum TokenAdicity {
  188. ZeroOrOne(Token),
  189. ZeroOrMore(Token),
  190. One(Token),
  191. OneOrMore(Token),
  192. }
  193. impl TokenAdicity {
  194. fn inner(&self) -> &Token {
  195. match self {
  196. ZeroOrOne(ref t) => t,
  197. ZeroOrMore(ref t) => t,
  198. One(ref t) => t,
  199. OneOrMore(ref t) => t,
  200. }
  201. }
  202. }
  203. /// A token encountered in the UI's command execution bar
  204. #[derive(Debug, Copy, Clone)]
  205. pub enum Token {
  206. Literal(&'static str),
  207. Filepath,
  208. Alternatives(&'static [TokenStream]),
  209. Seq(&'static [TokenAdicity]),
  210. AccountName,
  211. MailboxPath,
  212. QuotedStringValue,
  213. RestOfStringValue,
  214. AlphanumericStringValue,
  215. AttachmentIndexValue,
  216. MailboxIndexValue,
  217. IndexValue,
  218. }
  219. fn eof(input: &[u8]) -> IResult<&[u8], ()> {
  220. if input.is_empty() {
  221. Ok((input, ()))
  222. } else {
  223. Err(nom::Err::Error((input, nom::error::ErrorKind::Tag)))
  224. }
  225. }
  226. define_commands!([
  227. { tags: ["set"],
  228. desc: "set [seen/unseen], toggles message's Seen flag.",
  229. tokens: &[One(Literal("set")), One(Alternatives(&[to_stream!(One(Literal("seen"))), to_stream!(One(Literal("unseen")))]))],
  230. parser: (
  231. fn seen_flag<'a>(input: &'a [u8]) -> IResult<&'a [u8], Action> {
  232. let (input, _) = tag("set")(input.trim())?;
  233. let (input, _) = is_a(" ")(input)?;
  234. alt((map(tag("seen"), |_| Listing(SetSeen)), map(tag("unseen"), |_| Listing(SetUnseen))))(input)
  235. }
  236. )
  237. },
  238. { tags: ["delete"],
  239. desc: "delete message",
  240. tokens: &[One(Literal("delete"))],
  241. parser: (
  242. fn delete_message<'a>(input: &'a [u8]) -> IResult<&'a [u8], Action> {
  243. map(preceded(tag("delete"), eof), |_| Listing(Delete))(input)
  244. }
  245. )
  246. },
  247. { tags: ["copyto", "moveto"],
  248. desc: "copy/move message",
  249. tokens: &[One(Alternatives(&[to_stream!(One(Literal("copyto"))), to_stream!(One(Literal("moveto")))])), ZeroOrOne(AccountName), One(MailboxPath)],
  250. parser: (
  251. fn copymove<'a>(input: &'a [u8]) -> IResult<&'a [u8], Action> {
  252. alt((
  253. |input: &'a [u8]| -> IResult<&'a [u8], Action> {
  254. let (input, _) = tag("copyto")(input.trim())?;
  255. let (input, _) = is_a(" ")(input)?;
  256. let (input, path) = quoted_argument(input)?;
  257. let (input, _) = eof(input)?;
  258. Ok( (input, { Listing(CopyTo(path.to_string())) }))
  259. },
  260. |input: &'a [u8]| -> IResult<&'a [u8], Action> {
  261. let (input, _) = tag("copyto")(input.trim())?;
  262. let (input, _) = is_a(" ")(input)?;
  263. let (input, account) = quoted_argument(input)?;
  264. let (input, _) = is_a(" ")(input)?;
  265. let (input, path) = quoted_argument(input)?;
  266. let (input, _) = eof(input)?;
  267. Ok( (input, { Listing(CopyToOtherAccount(account.to_string(), path.to_string())) }))
  268. },
  269. |input: &'a [u8]| -> IResult<&'a [u8], Action> {
  270. let (input, _) = tag("moveto")(input.trim())?;
  271. let (input, _) = is_a(" ")(input)?;
  272. let (input, path) = quoted_argument(input)?;
  273. let (input, _) = eof(input)?;
  274. Ok( (input, { Listing(MoveTo(path.to_string())) }))
  275. },
  276. |input: &'a [u8]| -> IResult<&'a [u8], Action> {
  277. let (input, _) = tag("moveto")(input.trim())?;
  278. let (input, _) = is_a(" ")(input)?;
  279. let (input, account) = quoted_argument(input)?;
  280. let (input, _) = is_a(" ")(input)?;
  281. let (input, path) = quoted_argument(input)?;
  282. let (input, _) = eof(input)?;
  283. Ok( (input, { Listing(MoveToOtherAccount(account.to_string(), path.to_string())) }))
  284. }
  285. ))(input)
  286. }
  287. )
  288. },
  289. { tags: ["close"],
  290. desc: "close non-sticky tabs",
  291. tokens: &[One(Literal("close"))],
  292. parser: (
  293. fn close(input: &[u8]) -> IResult<&[u8], Action> {
  294. map(tag("close"), |_| Tab(Close))(input.trim())
  295. }
  296. )
  297. },
  298. { tags: ["go"],
  299. desc: "go [n], switch to nth mailbox in this account",
  300. tokens: &[One(Literal("goto")), One(MailboxIndexValue)],
  301. parser: (
  302. fn goto(input: &[u8]) -> IResult<&[u8], Action> {
  303. let (input, _) = tag("go")(input)?;
  304. let (input, _) = is_a(" ")(input)?;
  305. map(usize_c, Action::ViewMailbox)(input)
  306. }
  307. )
  308. },
  309. { tags: ["subsort"],
  310. desc: "subsort [date/subject] [asc/desc], sorts first level replies in threads.",
  311. tokens: &[One(Literal("subsort")), One(Alternatives(&[to_stream!(One(Literal("date"))), to_stream!(One(Literal("subject")))])), One(Alternatives(&[to_stream!(One(Literal("asc"))), to_stream!(One(Literal("desc")))])) ],
  312. parser: (
  313. fn subsort(input: &[u8]) -> IResult<&[u8], Action> {
  314. let (input, _) = tag("subsort")(input)?;
  315. let (input, _) = is_a(" ")(input)?;
  316. let (input, p) = pair(sortfield, sortorder)(input)?;
  317. Ok((input, SubSort(p.0, p.1)))
  318. }
  319. )
  320. },
  321. { tags: ["sort"],
  322. desc: "sort [date/subject] [asc/desc], sorts threads.",
  323. tokens: &[One(Literal("sort")), One(Alternatives(&[to_stream!(One(Literal("date"))), to_stream!(One(Literal("subject")))])), One(Alternatives(&[to_stream!(One(Literal("asc"))), to_stream!(One(Literal("desc")))])) ],
  324. parser: (
  325. fn sort(input: &[u8]) -> IResult<&[u8], Action> {
  326. let (input, _) = tag("sort")(input)?;
  327. let (input, _) = is_a(" ")(input)?;
  328. let (input, p) = separated_pair(sortfield, tag(" "), sortorder)(input)?;
  329. Ok((input, (Sort(p.0, p.1))))
  330. }
  331. )},
  332. { tags: ["set", "set plain", "set threaded", "set compact"],
  333. desc: "set [plain/threaded/compact/conversations], changes the mail listing view",
  334. tokens: &[One(Literal("set")), One(Alternatives(&[to_stream!(One(Literal("plain"))), to_stream!(One(Literal("threaded"))), to_stream!(One(Literal("compact"))), to_stream!(One(Literal("conversations")))]))],
  335. parser: (
  336. fn toggle(input: &[u8]) -> IResult<&[u8], Action> {
  337. let (input, _) = tag("set")(input.trim())?;
  338. let (input, _) = is_a(" ")(input)?;
  339. alt((threaded, plain, compact, conversations))(input)
  340. }
  341. )
  342. },
  343. { tags: ["toggle_thread_snooze"],
  344. desc: "turn off new notifications for this thread",
  345. tokens: &[One(Literal("toggle_thread_snooze"))],
  346. parser: (
  347. fn toggle_thread_snooze(input: &[u8]) -> IResult<&[u8], Action> {
  348. map(tag("toggle_thread_snooze"), |_| ToggleThreadSnooze)(input.trim())
  349. }
  350. )
  351. },
  352. { tags: ["search"],
  353. desc: "search <TERM>, searches list with given term",
  354. tokens: &[One(Literal("search")), One(RestOfStringValue)],
  355. parser:(
  356. fn search(input: &[u8]) -> IResult<&[u8], Action> {
  357. let (input, _) = tag("search")(input.trim())?;
  358. let (input, _) = is_a(" ")(input)?;
  359. let (input, string) = map_res(not_line_ending, std::str::from_utf8)(input)?;
  360. Ok((input, Listing(Search(String::from(string)))))
  361. }
  362. )
  363. },
  364. { tags: ["select"],
  365. desc: "select <TERM>, selects envelopes matching with given term",
  366. tokens: &[One(Literal("select")), One(RestOfStringValue)],
  367. parser:(
  368. fn select(input: &[u8]) -> IResult<&[u8], Action> {
  369. let (input, _) = tag("select")(input.trim())?;
  370. let (input, _) = is_a(" ")(input)?;
  371. let (input, string) = map_res(not_line_ending, std::str::from_utf8)(input)?;
  372. Ok((input, Listing(Select(String::from(string)))))
  373. }
  374. )
  375. },
  376. { tags: ["list-archive", "list-post", "list-unsubscribe", "list-"],
  377. desc: "list-[unsubscribe/post/archive]",
  378. tokens: &[One(Alternatives(&[to_stream!(One(Literal("list-archive"))), to_stream!(One(Literal("list-post"))), to_stream!(One(Literal("list-unsubscribe")))]))],
  379. parser: (
  380. fn mailinglist(input: &[u8]) -> IResult<&[u8], Action> {
  381. alt((
  382. map(tag("list-post"), |_| MailingListAction(ListPost))
  383. , map(tag("list-unsubscribe"), |_| MailingListAction(
  384. ListUnsubscribe
  385. ))
  386. , map(tag("list-archive"), |_| MailingListAction(
  387. ListArchive
  388. ))
  389. ))(input.trim())
  390. }
  391. )
  392. },
  393. { tags: ["setenv "],
  394. desc: "setenv VAR=VALUE",
  395. tokens: &[One(Literal("setenv")), OneOrMore(Seq(&[One(AlphanumericStringValue), One(Literal("=")), One(QuotedStringValue)]))],
  396. parser: (
  397. fn setenv(input: &[u8]) -> IResult<&[u8], Action> {
  398. let (input, _) = tag("setenv")(input.trim())?;
  399. let (input, _) = is_a(" ")(input)?;
  400. let (input, key) = map_res(take_until("="), std::str::from_utf8)(input)?;
  401. let (input, _) = tag("=")(input.trim())?;
  402. let (input, val) = map_res(not_line_ending, std::str::from_utf8)(input)?;
  403. Ok((input, SetEnv(key.to_string(), val.to_string())))
  404. }
  405. )
  406. },
  407. { tags: ["printenv "],
  408. desc: "printenv VAR",
  409. tokens: &[],
  410. parser:(
  411. fn printenv(input: &[u8]) -> IResult<&[u8], Action> {
  412. let (input, _) = tag("printenv")(input.ltrim())?;
  413. let (input, _) = is_a(" ")(input)?;
  414. let (input, key) = map_res(not_line_ending, std::str::from_utf8)(input.trim())?;
  415. Ok((input, PrintEnv(key.to_string())))
  416. }
  417. )
  418. },
  419. /* Pipe pager contents to binary */
  420. { tags: ["pipe "],
  421. desc: "pipe EXECUTABLE ARGS",
  422. tokens: &[One(Literal("pipe")), One(Filepath), ZeroOrMore(QuotedStringValue)],
  423. parser:(
  424. fn pipe<'a>(input: &'a [u8]) -> IResult<&'a [u8], Action> {
  425. alt((
  426. |input: &'a [u8]| -> IResult<&'a [u8], Action> {
  427. let (input, _) = tag("pipe")(input.trim())?;
  428. let (input, _) = is_a(" ")(input)?;
  429. let (input, bin) = quoted_argument(input)?;
  430. let (input, _) = is_a(" ")(input)?;
  431. let (input, args) = separated_list(is_a(" "), quoted_argument)(input)?;
  432. Ok((input, {
  433. View(Pipe(bin.to_string(), args.into_iter().map(String::from).collect::<Vec<String>>()))
  434. }))
  435. },
  436. |input: &'a [u8]| -> IResult<&'a [u8], Action> {
  437. let (input, _) = tag("pipe")(input.trim())?;
  438. let (input, _) = is_a(" ")(input)?;
  439. let (input, bin) = quoted_argument(input.trim())?;
  440. Ok((input, {
  441. View(Pipe(bin.to_string(), Vec::new()))
  442. }))
  443. }
  444. ))(input)
  445. }
  446. )
  447. },
  448. { tags: ["add-attachment "],
  449. desc: "add-attachment PATH",
  450. tokens: &[One(Literal("add-attachment")), One(Filepath)],
  451. parser:(
  452. fn add_attachment<'a>(input: &'a [u8]) -> IResult<&'a [u8], Action> {
  453. alt((
  454. |input: &'a [u8]| -> IResult<&'a [u8], Action> {
  455. let (input, _) = tag("add-attachment")(input.trim())?;
  456. let (input, _) = is_a(" ")(input)?;
  457. let (input, _) = tag("<")(input.trim())?;
  458. let (input, _) = is_a(" ")(input)?;
  459. let (input, cmd) = quoted_argument(input)?;
  460. Ok((input, Compose(AddAttachmentPipe(cmd.to_string()))))
  461. }, |input: &'a [u8]| -> IResult<&'a [u8], Action> {
  462. let (input, _) = tag("add-attachment")(input.trim())?;
  463. let (input, _) = is_a(" ")(input)?;
  464. let (input, path) = quoted_argument(input)?;
  465. Ok((input, Compose(AddAttachment(path.to_string()))))
  466. }
  467. ))(input)
  468. }
  469. )
  470. },
  471. { tags: ["remove-attachment "],
  472. desc: "remove-attachment INDEX",
  473. tokens: &[One(Literal("remove-attachment")), One(IndexValue)],
  474. parser:(
  475. fn remove_attachment(input: &[u8]) -> IResult<&[u8], Action> {
  476. let (input, _) = tag("remove-attachment")(input.trim())?;
  477. let (input, _) = is_a(" ")(input)?;
  478. let (input, idx) = map_res(quoted_argument, usize::from_str)(input)?;
  479. Ok((input, Compose(RemoveAttachment(idx))))
  480. }
  481. )
  482. },
  483. { tags: ["save-draft"],
  484. desc: "save draft",
  485. tokens: &[One(Literal("save-draft"))],
  486. parser:(
  487. fn save_draft(input: &[u8]) -> IResult<&[u8], Action> {
  488. let (input, _) = tag("save-draft")(input.trim())?;
  489. let (input, _) = eof(input)?;
  490. Ok((input, Compose(SaveDraft)))
  491. }
  492. )
  493. },
  494. { tags: ["toggle sign "],
  495. desc: "switch between sign/unsign for this draft",
  496. tokens: &[One(Literal("toggle")), One(Literal("sign"))],
  497. parser:(
  498. fn toggle_sign(input: &[u8]) -> IResult<&[u8], Action> {
  499. let (input, _) = tag("toggle")(input)?;
  500. let (input, _) = is_a(" ")(input)?;
  501. let (input, _) = tag("sign")(input)?;
  502. Ok((input, Compose(ToggleSign)))
  503. }
  504. )
  505. },
  506. { tags: ["create-mailbox "],
  507. desc: "create-mailbox ACCOUNT MAILBOX_PATH",
  508. tokens: &[One(Literal("create-mailbox")), One(AccountName), One(MailboxPath)],
  509. parser:(
  510. fn create_mailbox(input: &[u8]) -> IResult<&[u8], Action> {
  511. let (input, _) = tag("create-mailbox")(input.trim())?;
  512. let (input, _) = is_a(" ")(input)?;
  513. let (input, account) = quoted_argument(input)?;
  514. let (input, _) = is_a(" ")(input)?;
  515. let (input, path) = quoted_argument(input)?;
  516. Ok((input,Mailbox(account.to_string(), MailboxOperation::Create(path.to_string()))))
  517. }
  518. )
  519. },
  520. { tags: ["subscribe-mailbox "],
  521. desc: "subscribe-mailbox ACCOUNT MAILBOX_PATH",
  522. tokens: &[One(Literal("subscribe-mailbox")), One(AccountName), One(MailboxPath)],
  523. parser:(
  524. fn sub_mailbox(input: &[u8]) -> IResult<&[u8], Action> {
  525. let (input, _) = tag("subscribe-mailbox")(input.trim())?;
  526. let (input, _) = is_a(" ")(input)?;
  527. let (input, account) = quoted_argument(input)?;
  528. let (input, _) = is_a(" ")(input)?;
  529. let (input, path) = quoted_argument(input)?;
  530. Ok((input,Mailbox(account.to_string(), MailboxOperation::Subscribe(path.to_string()))))
  531. }
  532. )
  533. },
  534. { tags: ["unsubscribe-mailbox "],
  535. desc: "unsubscribe-mailbox ACCOUNT MAILBOX_PATH",
  536. tokens: &[One(Literal("unsubscribe-mailbox")), One(AccountName), One(MailboxPath)],
  537. parser:(
  538. fn unsub_mailbox(input: &[u8]) -> IResult<&[u8], Action> {
  539. let (input, _) = tag("unsubscribe-mailbox")(input.trim())?;
  540. let (input, _) = is_a(" ")(input)?;
  541. let (input, account) = quoted_argument(input)?;
  542. let (input, _) = is_a(" ")(input)?;
  543. let (input, path) = quoted_argument(input)?;
  544. Ok((input, Mailbox(account.to_string(), MailboxOperation::Unsubscribe(path.to_string()))))
  545. }
  546. )
  547. },
  548. { tags: ["rename-mailbox "],
  549. desc: "rename-mailbox ACCOUNT MAILBOX_PATH_SRC MAILBOX_PATH_DEST",
  550. tokens: &[One(Literal("rename-mailbox")), One(AccountName), One(MailboxPath), One(MailboxPath)],
  551. parser:(
  552. fn rename_mailbox(input: &[u8]) -> IResult<&[u8], Action> {
  553. let (input, _) = tag("rename-mailbox")(input.trim())?;
  554. let (input, _) = is_a(" ")(input)?;
  555. let (input, account) = quoted_argument(input)?;
  556. let (input, _) = is_a(" ")(input)?;
  557. let (input, src) = quoted_argument(input)?;
  558. let (input, _) = is_a(" ")(input)?;
  559. let (input, dest) = quoted_argument(input)?;
  560. Ok((input, Mailbox(account.to_string(), MailboxOperation::Rename(src.to_string(), dest.to_string()))))
  561. }
  562. )
  563. },
  564. { tags: ["delete-mailbox "],
  565. desc: "delete-mailbox ACCOUNT MAILBOX_PATH",
  566. tokens: &[One(Literal("delete-mailbox")), One(AccountName), One(MailboxPath)],
  567. parser:(
  568. fn delete_mailbox(input: &[u8]) -> IResult<&[u8], Action> {
  569. let (input, _) = tag("delete-mailbox")(input.trim())?;
  570. let (input, _) = is_a(" ")(input)?;
  571. let (input, account) = quoted_argument(input)?;
  572. let (input, _) = is_a(" ")(input)?;
  573. let (input, path) = quoted_argument(input)?;
  574. Ok ((input, Mailbox(account.to_string(), MailboxOperation::Delete(path.to_string()))))
  575. }
  576. )
  577. },
  578. { tags: ["reindex "],
  579. desc: "reindex ACCOUNT, rebuild account cache in the background",
  580. tokens: &[One(Literal("reindex")), One(AccountName)],
  581. parser:(
  582. fn reindex(input: &[u8]) -> IResult<&[u8], Action> {
  583. let (input, _) = tag("reindex")(input.trim())?;
  584. let (input, _) = is_a(" ")(input)?;
  585. let (input, account) = quoted_argument(input)?;
  586. Ok( (input, AccountAction(account.to_string(), ReIndex)))
  587. }
  588. )
  589. },
  590. { tags: ["open-in-tab"],
  591. desc: "opens envelope view in new tab",
  592. tokens: &[One(Literal("open-in-tab"))],
  593. parser:(
  594. fn open_in_new_tab(input: &[u8]) -> IResult<&[u8], Action> {
  595. let (input, _) = tag("open-in-tab")(input.trim())?;
  596. Ok((input, Listing(OpenInNewTab)))
  597. }
  598. )
  599. },
  600. { tags: ["save-attachment "],
  601. desc: "save-attachment INDEX PATH",
  602. tokens: &[One(Literal("save-attachment")), One(AttachmentIndexValue), One(Filepath)],
  603. parser:(
  604. fn save_attachment(input: &[u8]) -> IResult<&[u8], Action> {
  605. let (input, _) = tag("save-attachment")(input.trim())?;
  606. let (input, _) = is_a(" ")(input)?;
  607. let (input, idx) = map_res(quoted_argument, usize::from_str)(input)?;
  608. let (input, _) = is_a(" ")(input)?;
  609. let (input, path) = quoted_argument(input.trim())?;
  610. Ok((input, View(SaveAttachment(idx, path.to_string()))))
  611. }
  612. )
  613. },
  614. { tags: ["tag", "tag add", "tag remove"],
  615. desc: "tag [add/remove], edits message's tags.",
  616. tokens: &[One(Literal("tag")), One(Alternatives(&[to_stream!(One(Literal("add"))), to_stream!(One(Literal("remove")))]))],
  617. parser: (
  618. fn _tag<'a>(input: &'a [u8]) -> IResult<&'a [u8], Action> {
  619. preceded(
  620. tag("tag"),
  621. alt((|input: &'a [u8]| -> IResult<&'a [u8], Action> {
  622. let (input, _) = tag("add")(input.trim())?;
  623. let (input, _) = is_a(" ")(input)?;
  624. let (input, tag) = quoted_argument(input.trim())?;
  625. Ok((input, Listing(Tag(Add(tag.to_string())))))
  626. }, |input: &'a [u8]| -> IResult<&'a [u8], Action> {
  627. let (input, _) = tag("remove")(input.trim())?;
  628. let (input, _) = is_a(" ")(input)?;
  629. let (input, tag) = quoted_argument(input.trim())?;
  630. Ok((input, Listing(Tag(Remove(tag.to_string())))))
  631. }
  632. ))
  633. )(input.trim())
  634. }
  635. )
  636. },
  637. { tags: ["print "],
  638. desc: "print ACCOUNT SETTING",
  639. tokens: &[One(Literal("print")), One(AccountName), One(QuotedStringValue)],
  640. parser:(
  641. fn print_setting(input: &[u8]) -> IResult<&[u8], Action> {
  642. let (input, _) = tag("print")(input.trim())?;
  643. let (input, _) = is_a(" ")(input)?;
  644. let (input, account) = quoted_argument(input)?;
  645. let (input, _) = is_a(" ")(input)?;
  646. let (input, setting) = quoted_argument(input)?;
  647. Ok((input, AccountAction(account.to_string(), PrintSetting(setting.to_string()))))
  648. }
  649. )
  650. }
  651. ]);
  652. fn usize_c(input: &[u8]) -> IResult<&[u8], usize> {
  653. map_res(
  654. map_res(digit1, std::str::from_utf8),
  655. std::str::FromStr::from_str,
  656. )(input.trim())
  657. }
  658. fn sortfield(input: &[u8]) -> IResult<&[u8], SortField> {
  659. map_res(
  660. map_res(take_until(" "), std::str::from_utf8),
  661. std::str::FromStr::from_str,
  662. )(input.trim())
  663. }
  664. fn sortorder(input: &[u8]) -> IResult<&[u8], SortOrder> {
  665. map_res(
  666. map_res(not_line_ending, std::str::from_utf8),
  667. std::str::FromStr::from_str,
  668. )(input)
  669. }
  670. fn threaded(input: &[u8]) -> IResult<&[u8], Action> {
  671. map(tag("threaded"), |_| Listing(SetThreaded))(input.trim())
  672. }
  673. fn plain(input: &[u8]) -> IResult<&[u8], Action> {
  674. map(tag("plain"), |_| Listing(SetPlain))(input.trim())
  675. }
  676. fn compact(input: &[u8]) -> IResult<&[u8], Action> {
  677. map(tag("compact"), |_| Listing(SetCompact))(input.trim())
  678. }
  679. fn conversations(input: &[u8]) -> IResult<&[u8], Action> {
  680. map(tag("conversations"), |_| Listing(SetConversations))(input.trim())
  681. }
  682. fn listing_action(input: &[u8]) -> IResult<&[u8], Action> {
  683. alt((
  684. toggle,
  685. seen_flag,
  686. delete_message,
  687. copymove,
  688. search,
  689. select,
  690. toggle_thread_snooze,
  691. open_in_new_tab,
  692. _tag,
  693. ))(input)
  694. }
  695. fn compose_action(input: &[u8]) -> IResult<&[u8], Action> {
  696. alt((add_attachment, remove_attachment, toggle_sign, save_draft))(input)
  697. }
  698. fn account_action(input: &[u8]) -> IResult<&[u8], Action> {
  699. alt((reindex, print_setting))(input)
  700. }
  701. fn view(input: &[u8]) -> IResult<&[u8], Action> {
  702. alt((pipe, save_attachment))(input)
  703. }
  704. pub fn parse_command(input: &[u8]) -> Result<Action, MeliError> {
  705. alt((
  706. goto,
  707. listing_action,
  708. sort,
  709. subsort,
  710. close,
  711. mailinglist,
  712. setenv,
  713. printenv,
  714. view,
  715. compose_action,
  716. create_mailbox,
  717. sub_mailbox,
  718. unsub_mailbox,
  719. delete_mailbox,
  720. rename_mailbox,
  721. account_action,
  722. ))(input)
  723. .map(|(_, v)| v)
  724. .map_err(|err| err.into())
  725. }
  726. #[test]
  727. #[ignore]
  728. fn test_parser() {
  729. use std::io;
  730. let mut input = String::new();
  731. loop {
  732. input.clear();
  733. match io::stdin().read_line(&mut input) {
  734. Ok(_n) => {
  735. let mut sugg = Default::default();
  736. //print!("{}", input);
  737. for (_tags, desc, tokens) in COMMAND_COMPLETION.iter() {
  738. let m = tokens.matches(&mut input.as_str().trim(), &mut sugg);
  739. if !m.is_empty() {
  740. print!("{:?} ", desc);
  741. println!(" result = {:#?}\n\n", m);
  742. }
  743. }
  744. println!(
  745. "suggestions = {:#?}",
  746. sugg.into_iter()
  747. .map(|s| format!(
  748. "{}{}",
  749. input.as_str().trim(),
  750. if input.trim().is_empty() {
  751. s.trim()
  752. } else {
  753. s.as_str()
  754. }
  755. ))
  756. .collect::<Vec<String>>()
  757. );
  758. if input.trim() == "quit" {
  759. break;
  760. }
  761. }
  762. Err(error) => println!("error: {}", error),
  763. }
  764. }
  765. println!("alright");
  766. }
  767. /// Get command suggestions for input
  768. pub fn command_completion_suggestions(input: &str) -> Vec<String> {
  769. use crate::melib::ShellExpandTrait;
  770. let mut sugg = Default::default();
  771. for (_tags, _desc, tokens) in COMMAND_COMPLETION.iter() {
  772. let _m = tokens.matches(&mut &(*input), &mut sugg);
  773. if _m.is_empty() {
  774. continue;
  775. }
  776. if let Some((s, Filepath)) = _m.last() {
  777. let p = std::path::Path::new(s);
  778. sugg.extend(p.complete(true).into_iter().map(|m| m.into()));
  779. }
  780. }
  781. sugg.into_iter()
  782. .map(|s| {
  783. format!(
  784. "{}{}",
  785. input.trim(),
  786. if input.trim().is_empty() {
  787. s.trim()
  788. } else {
  789. s.as_str()
  790. }
  791. )
  792. })
  793. .collect::<Vec<String>>()
  794. }