🐝 I really like where this mua is(was?) headed, but it seems as though there has not been much activity recently.
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.

447 lines
18 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 the Execute mode.
  22. */
  23. pub use melib::thread::{SortField, SortOrder};
  24. use nom::{digit, not_line_ending, IResult};
  25. use std;
  26. pub mod actions;
  27. use actions::MailboxOperation;
  28. pub mod history;
  29. pub use crate::actions::AccountAction::{self, *};
  30. pub use crate::actions::Action::{self, *};
  31. pub use crate::actions::ComposeAction::{self, *};
  32. pub use crate::actions::ListingAction::{self, *};
  33. pub use crate::actions::MailingListAction::{self, *};
  34. pub use crate::actions::TabAction::{self, *};
  35. pub use crate::actions::TagAction::{self, *};
  36. pub use crate::actions::ViewAction::{self, *};
  37. use std::str::FromStr;
  38. /* Create a const table with every command part that can be auto-completed and its description */
  39. macro_rules! define_commands {
  40. ( [$({ tags: [$( $tags:literal),*], desc: $desc:literal, parser: ($parser:item)}),*]) => {
  41. pub const COMMAND_COMPLETION: &[(&str, &str)] = &[$($( ($tags, $desc ) ),*),* ];
  42. $( $parser )*
  43. };
  44. }
  45. pub fn quoted_argument(input: &[u8]) -> IResult<&[u8], &str> {
  46. if input.is_empty() {
  47. return IResult::Error(nom::ErrorKind::Custom(0));
  48. }
  49. if input[0] == b'"' {
  50. let mut i = 1;
  51. while i < input.len() {
  52. if input[i] == b'\"' && input[i - 1] != b'\\' {
  53. return IResult::Done(&input[i + 1..], unsafe {
  54. std::str::from_utf8_unchecked(&input[1..i])
  55. });
  56. }
  57. i += 1;
  58. }
  59. return IResult::Error(nom::ErrorKind::Custom(0));
  60. } else {
  61. return map_res!(input, is_not!(" "), std::str::from_utf8);
  62. }
  63. }
  64. define_commands!([
  65. { tags: ["set"],
  66. desc: "set [seen/unseen], toggles message's Seen flag.",
  67. parser:
  68. ( named!(
  69. envelope_action<Action>,
  70. alt_complete!(
  71. preceded!(
  72. ws!(tag!("set")),
  73. alt_complete!(
  74. map!(ws!(tag!("seen")), |_| Listing(SetSeen))
  75. | map!(ws!(tag!("unseen")), |_| Listing(SetUnseen))
  76. )
  77. ) | map!(preceded!(tag!("delete"), eof!()), |_| Listing(Delete))
  78. )
  79. ); )
  80. },
  81. { tags: ["close"],
  82. desc: "close non-sticky tabs",
  83. parser: (
  84. named!(close<Action>, map!(ws!(tag!("close")), |_| Tab(Close)));
  85. )
  86. },
  87. { tags: ["goto"],
  88. desc: "goto [n], switch to nth mailbox in this account",
  89. parser: (
  90. named!(
  91. goto<Action>,
  92. preceded!(tag!("go "), map!(call!(usize_c), Action::ViewMailbox))
  93. );
  94. )
  95. },
  96. { tags: ["subsort"],
  97. desc: "subsort [date/subject] [asc/desc], sorts first level replies in threads.",
  98. parser: (
  99. named!(
  100. subsort<Action>,
  101. do_parse!(tag!("subsort ") >> p: pair!(sortfield, sortorder) >> (SubSort(p.0, p.1)))
  102. );
  103. )
  104. },
  105. { tags: ["sort"],
  106. desc: "sort [date/subject] [asc/desc], sorts threads.",
  107. parser: (
  108. named!(
  109. sort<Action>,
  110. do_parse!(
  111. tag!("sort ") >> p: separated_pair!(sortfield, tag!(" "), sortorder) >> (Sort(p.0, p.1))
  112. )
  113. );
  114. )
  115. },
  116. { tags: ["set", "set plain", "set threaded", "set compact"],
  117. desc: "set [plain/threaded/compact/conversations], changes the mail listing view",
  118. parser: (
  119. named!(
  120. toggle<Action>,
  121. preceded!(tag!("set "), alt_complete!(threaded | plain | compact | conversations))
  122. );
  123. )
  124. },
  125. { tags: ["toggle_thread_snooze"],
  126. desc: "turn off new notifications for this thread",
  127. parser: (
  128. named!(toggle_thread_snooze<Action>,
  129. map!(ws!(tag!("toggle_thread_snooze")), |_| ToggleThreadSnooze)
  130. );
  131. )
  132. },
  133. { tags: ["search"],
  134. desc: "search <TERM>, searches list with given term",
  135. parser:(
  136. named!(search<Action>,
  137. do_parse!(
  138. ws!(tag!("search"))
  139. >> string: map_res!(call!(not_line_ending), std::str::from_utf8)
  140. >> (Listing(Search(String::from(string))))
  141. )
  142. );
  143. )
  144. },
  145. { tags: ["list-archive", "list-post", "list-unsubscribe", "list-"],
  146. desc: "list-[unsubscribe/post/archive]",
  147. parser: (
  148. named!(
  149. mailinglist<Action>,
  150. alt_complete!(
  151. map!(ws!(tag!("list-post")), |_| MailingListAction(ListPost))
  152. | map!(ws!(tag!("list-unsubscribe")), |_| MailingListAction(
  153. ListUnsubscribe
  154. ))
  155. | map!(ws!(tag!("list-archive")), |_| MailingListAction(
  156. ListArchive
  157. ))
  158. )
  159. );
  160. )
  161. },
  162. { tags: ["setenv "],
  163. desc:"setenv VAR=VALUE",
  164. parser: (
  165. named!( setenv<Action>,
  166. do_parse!(
  167. ws!(tag!("setenv"))
  168. >> key: map_res!(take_until1!("="), std::str::from_utf8)
  169. >> ws!(tag!("="))
  170. >> val: map_res!(call!(not_line_ending), std::str::from_utf8)
  171. >> (SetEnv(key.to_string(), val.to_string()))
  172. )
  173. );
  174. )
  175. },
  176. { tags: ["printenv "],
  177. desc: "printenv VAR",
  178. parser:(
  179. named!( printenv<Action>,
  180. do_parse!(
  181. ws!(tag!("env"))
  182. >> key: map_res!(call!(not_line_ending), std::str::from_utf8)
  183. >> (PrintEnv(key.to_string()))
  184. )
  185. );
  186. )
  187. },
  188. /* Pipe pager contents to binary */
  189. { tags: ["pipe "],
  190. desc: "pipe EXECUTABLE ARGS",
  191. parser:(
  192. named!( pipe<Action>,
  193. alt_complete!(
  194. do_parse!(
  195. ws!(tag!("pipe"))
  196. >> bin: quoted_argument
  197. >> is_a!(" ")
  198. >> args: separated_list!(is_a!(" "), quoted_argument)
  199. >> ({
  200. View(Pipe(bin.to_string(), args.into_iter().map(String::from).collect::<Vec<String>>()))
  201. })) | do_parse!(
  202. ws!(tag!("pipe"))
  203. >> bin: ws!(quoted_argument)
  204. >> ({
  205. View(Pipe(bin.to_string(), Vec::new()))
  206. })
  207. ))
  208. );
  209. )
  210. },
  211. { tags: ["add-attachment "],
  212. desc: "add-attachment PATH",
  213. parser:(
  214. named!( add_attachment<Action>,
  215. alt_complete!(
  216. do_parse!(
  217. ws!(tag!("add-attachment"))
  218. >> ws!(tag!("<"))
  219. >> cmd: quoted_argument
  220. >> (Compose(AddAttachmentPipe(cmd.to_string()))))
  221. | do_parse!(
  222. ws!(tag!("add-attachment"))
  223. >> path: quoted_argument
  224. >> (Compose(AddAttachment(path.to_string()))))
  225. )
  226. );
  227. )
  228. },
  229. { tags: ["remove-attachment "],
  230. desc: "remove-attachment INDEX",
  231. parser:(
  232. named!( remove_attachment<Action>,
  233. do_parse!(
  234. ws!(tag!("remove-attachment"))
  235. >> idx: map_res!(quoted_argument, usize::from_str)
  236. >> (Compose(RemoveAttachment(idx)))
  237. )
  238. );
  239. )
  240. },
  241. { tags: ["toggle sign "],
  242. desc: "switch between sign/unsign for this draft",
  243. parser:(
  244. named!( toggle_sign<Action>,
  245. do_parse!(
  246. ws!(tag!("toggle sign"))
  247. >> (Compose(ToggleSign))
  248. )
  249. );
  250. )
  251. },
  252. { tags: ["create-mailbox "],
  253. desc: "create-mailbox ACCOUNT MAILBOX_PATH",
  254. parser:(
  255. named!( create_mailbox<Action>,
  256. do_parse!(
  257. ws!(tag!("create-mailbox"))
  258. >> account: quoted_argument
  259. >> is_a!(" ")
  260. >> path: quoted_argument
  261. >> (Mailbox(account.to_string(), MailboxOperation::Create(path.to_string())))
  262. )
  263. );
  264. )
  265. },
  266. { tags: ["subscribe-mailbox "],
  267. desc: "subscribe-mailbox ACCOUNT MAILBOX_PATH",
  268. parser:(
  269. named!( sub_mailbox<Action>,
  270. do_parse!(
  271. ws!(tag!("subscribe-mailbox"))
  272. >> account: quoted_argument
  273. >> is_a!(" ")
  274. >> path: quoted_argument
  275. >> (Mailbox(account.to_string(), MailboxOperation::Subscribe(path.to_string())))
  276. )
  277. );
  278. )
  279. },
  280. { tags: ["unsubscribe-mailbox "],
  281. desc: "unsubscribe-mailbox ACCOUNT MAILBOX_PATH",
  282. parser:(
  283. named!( unsub_mailbox<Action>,
  284. do_parse!(
  285. ws!(tag!("unsubscribe-mailbox"))
  286. >> account: quoted_argument
  287. >> is_a!(" ")
  288. >> path: quoted_argument
  289. >> (Mailbox(account.to_string(), MailboxOperation::Unsubscribe(path.to_string())))
  290. )
  291. );
  292. )
  293. },
  294. { tags: ["rename-mailbox "],
  295. desc: "rename-mailbox ACCOUNT MAILBOX_PATH_SRC MAILBOX_PATH_DEST",
  296. parser:(
  297. named!( rename_mailbox<Action>,
  298. do_parse!(
  299. ws!(tag!("rename-mailbox"))
  300. >> account: quoted_argument
  301. >> is_a!(" ")
  302. >> src: quoted_argument
  303. >> is_a!(" ")
  304. >> dest: quoted_argument
  305. >> (Mailbox(account.to_string(), MailboxOperation::Rename(src.to_string(), dest.to_string())))
  306. )
  307. );
  308. )
  309. },
  310. { tags: ["delete-mailbox "],
  311. desc: "delete-mailbox ACCOUNT MAILBOX_PATH",
  312. parser:(
  313. named!( delete_mailbox<Action>,
  314. do_parse!(
  315. ws!(tag!("delete-mailbox"))
  316. >> account: quoted_argument
  317. >> is_a!(" ")
  318. >> path: quoted_argument
  319. >> (Mailbox(account.to_string(), MailboxOperation::Delete(path.to_string())))
  320. )
  321. );
  322. )
  323. },
  324. { tags: ["reindex "],
  325. desc: "reindex ACCOUNT, rebuild account cache in the background",
  326. parser:(
  327. named!( reindex<Action>,
  328. do_parse!(
  329. ws!(tag!("reindex"))
  330. >> account: quoted_argument
  331. >> (AccountAction(account.to_string(), ReIndex))
  332. )
  333. );
  334. )
  335. },
  336. { tags: ["open-in-tab"],
  337. desc: "opens envelope view in new tab",
  338. parser:(
  339. named!( open_in_new_tab<Action>,
  340. do_parse!(
  341. ws!(tag!("open-in-tab"))
  342. >> (Listing(OpenInNewTab))
  343. )
  344. );
  345. )
  346. },
  347. { tags: ["save-attachment "],
  348. desc: "save-attachment INDEX PATH",
  349. parser:(
  350. named!( save_attachment<Action>,
  351. do_parse!(
  352. ws!(tag!("save-attachment"))
  353. >> idx: map_res!(quoted_argument, usize::from_str)
  354. >> path: ws!(quoted_argument)
  355. >> (View(SaveAttachment(idx, path.to_string())))
  356. )
  357. );
  358. )
  359. },
  360. { tags: ["tag", "tag add", "tag remove"],
  361. desc: "tag [add/remove], edits message's tags.",
  362. parser:
  363. ( named!(
  364. tag<Action>,
  365. preceded!(
  366. ws!(tag!("tag")),
  367. alt_complete!(
  368. do_parse!(
  369. ws!(tag!("add"))
  370. >> tag: ws!(quoted_argument)
  371. >> (Listing(Tag(Add(tag.to_string())))))
  372. | do_parse!(
  373. ws!(tag!("remove"))
  374. >> tag: ws!(quoted_argument)
  375. >> (Listing(Tag(Remove(tag.to_string())))))
  376. )
  377. )
  378. ); )
  379. }
  380. ]);
  381. named!(
  382. usize_c<usize>,
  383. map_res!(
  384. map_res!(ws!(digit), std::str::from_utf8),
  385. std::str::FromStr::from_str
  386. )
  387. );
  388. named!(
  389. sortfield<SortField>,
  390. map_res!(
  391. map_res!(take_until_s!(" "), std::str::from_utf8),
  392. std::str::FromStr::from_str
  393. )
  394. );
  395. named!(
  396. sortorder<SortOrder>,
  397. map_res!(
  398. map_res!(call!(not_line_ending), std::str::from_utf8),
  399. std::str::FromStr::from_str
  400. )
  401. );
  402. named!(
  403. threaded<Action>,
  404. map!(ws!(tag!("threaded")), |_| Listing(SetThreaded))
  405. );
  406. named!(
  407. plain<Action>,
  408. map!(ws!(tag!("plain")), |_| Listing(SetPlain))
  409. );
  410. named!(
  411. compact<Action>,
  412. map!(ws!(tag!("compact")), |_| Listing(SetCompact))
  413. );
  414. named!(
  415. conversations<Action>,
  416. map!(ws!(tag!("conversations")), |_| Listing(SetConversations))
  417. );
  418. named!(
  419. listing_action<Action>,
  420. alt_complete!(toggle | envelope_action | search | toggle_thread_snooze | open_in_new_tab | tag)
  421. );
  422. named!(
  423. compose_action<Action>,
  424. alt_complete!(add_attachment | remove_attachment | toggle_sign)
  425. );
  426. named!(account_action<Action>, alt_complete!(reindex));
  427. named!(view<Action>, alt_complete!(pipe | save_attachment));
  428. named!(pub parse_command<Action>,
  429. alt_complete!( goto | listing_action | sort | subsort | close | mailinglist | setenv | printenv | view | compose_action | create_mailbox | sub_mailbox | unsub_mailbox | delete_mailbox | rename_mailbox | account_action )
  430. );