summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorAlexandros Frantzis <alf82@freemail.gr>2019-10-01 23:06:38 +0300
committerAlexandros Frantzis <alf82@freemail.gr>2019-10-01 23:36:26 +0300
commita054789ddb60ed1fab26e6d4e6bd36ed926273f1 (patch)
treebd3caad78a0e377816b78276889301d9276344b9 /examples
Initial public release
Diffstat (limited to 'examples')
-rw-r--r--examples/normalize.rs18
-rw-r--r--examples/personal-mda.rs55
2 files changed, 73 insertions, 0 deletions
diff --git a/examples/normalize.rs b/examples/normalize.rs
new file mode 100644
index 0000000..359250a
--- /dev/null
+++ b/examples/normalize.rs
@@ -0,0 +1,18 @@
+// Copyright 2019 Alexandros Frantzis
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at https://mozilla.org/MPL/2.0/.
+//
+// SPDX-License-Identifier: MPL-2.0
+
+//! Writes out the normalized form of an email.
+
+use std::io::{self, Write};
+use mda::{Email, Result};
+
+fn main() -> Result<()> {
+ let email = Email::from_stdin()?;
+ io::stdout().lock().write_all(email.data())?;
+ Ok(())
+}
diff --git a/examples/personal-mda.rs b/examples/personal-mda.rs
new file mode 100644
index 0000000..aa26431
--- /dev/null
+++ b/examples/personal-mda.rs
@@ -0,0 +1,55 @@
+// Copyright 2019 Alexandros Frantzis
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at https://mozilla.org/MPL/2.0/.
+//
+// SPDX-License-Identifier: MPL-2.0
+
+//! An example of a custom MDA.
+
+use std::path::PathBuf;
+
+use mda::{Email, EmailRegex, Result, DeliveryDurability};
+
+fn main() -> Result<()> {
+ // Just some random path to make it highly unlikely that this example will
+ // indvertently mess up something.
+ let root = PathBuf::from("/tmp/my-personal-mail-96f29eb6375cfa37");
+
+ // If we are sure bogofilter is available, the below can be better written as:
+ // let mut email = Email::from_stdin_filtered(&["/usr/bin/bogofilter", "-ep"])?;
+ let mut email = Email::from_stdin()?;
+ if let Ok(new_email) = email.filter(&["/usr/bin/bogofilter", "-ep"]) {
+ email = new_email;
+ }
+
+ // Quicker (but possibly less durable) delivery.
+ email.set_delivery_durability(DeliveryDurability::FileSyncOnly);
+
+ let from = email.header_field("From").unwrap_or("");
+ let bogosity = email.header_field("X-Bogosity").unwrap_or("");
+
+ if bogosity.contains("Spam, tests=bogofilter") ||
+ from.contains("@banneddomain.com") {
+ email.deliver_to_maildir(root.join("spam"))?;
+ return Ok(());
+ }
+
+ let cc = email.header_field("Cc").unwrap_or("");
+ let to = email.header_field("To").unwrap_or("");
+
+ if to.contains("myworkemail@example.com") ||
+ cc.contains("myworkemail@example.com") {
+ if email.body().search("URGENCY RATING: (CRITICAL|URGENT)")? {
+ email.deliver_to_maildir(root.join("inbox/myemail/urgent"))?;
+ } else {
+ email.deliver_to_maildir(root.join("inbox/myemail/normal"))?;
+ }
+ return Ok(());
+ }
+
+ email.deliver_to_maildir(root.join("inbox/unsorted"))?;
+
+ Ok(())
+}