use of com.zimbra.cs.lmtpserver.LmtpEnvelope in project zm-mailbox by Zimbra.
the class NotifyMailtoTest method testNotify_variable.
/**
* Tests a sieve rule with variable parameters.
*/
@Test
public void testNotify_variable() {
String filterScript = "require [\"enotify\", \"tag\"];\n" + "if envelope :matches [\"To\"] \"*\" {set \"rcptto\" \"${1}\";}\n" + "if envelope :matches [\"From\"] \"*\" {set \"mailfrom\" \"${1}\";}\n" + "if header :matches \"Date\" \"*\" {set \"dateheader\" \"${1}\";}\n" + "if header :matches \"From\" \"*\" {set \"fromheader\" \"${1}\";}\n" + "if header :matches \"Subject\" \"*\" {set \"subjectheader\" \"${1}\";}\n" + "if anyof(not envelope :is [\"From\"] \"\") {\n" + " set \"subjectparam\" \"Notification\";\n" + " set \"bodyparam\" text:\r\n" + "Hello ${rcptto},\n" + "A new massage has arrived.\n" + "Sent: ${dateheader}\n" + "From: ${fromheader}\n" + "Subject: ${subjectheader}\r\n" + ".\r\n" + ";\n" + " notify :message \"${subjectparam}\"\n" + " :from \"${rcptto}\"\n" + " \"mailto:test2@zimbra.com?body=${bodyparam}\";\n" + "}";
String sampleMsg = "Auto-Submitted: \"no\"\n" + "from: xyz@example.com\n" + "Date: Tue, 11 Oct 2016 12:01:37 +0900\n" + "Subject: [acme-users] [fwd] version 1.0 is out\n" + "to: foo@example.com, baz@example.com\n" + "cc: qux@example.com\n";
String expectedNotifyMsg = "Hello test1@zimbra.com,\n" + "A new massage has arrived.\n" + "Sent: Tue, 11 Oct 2016 12:01:37 0900\n" + "From: xyz@example.com\n" + "Subject: [acme-users] [fwd] version 1.0 is out";
try {
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
Mailbox mbox2 = MailboxManager.getInstance().getMailboxByAccount(acct2);
RuleManager.clearCachedRules(acct1);
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<test2@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<test1@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
acct1.setMailSieveScript(filterScript);
acct1.setMail("test1@zimbra.com");
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleMsg.getBytes(), false), 0, acct1.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
// The triggered message should be delivered to the target mailbox
Assert.assertEquals(1, ids.size());
// Notification message should be delivered to mailto addresses
Integer item = mbox2.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message notifyMsg = mbox2.getMessageById(null, item);
// Verify the subject line of the notification message
Assert.assertEquals("Notification", notifyMsg.getSubject());
// Verify the from header of the notification message
String[] headers = notifyMsg.getMimeMessage().getHeader("from");
Assert.assertTrue(headers.length == 1);
Assert.assertEquals("test1@zimbra.com", headers[0]);
// Verify the message body of the notification message
MimeMessage mm = notifyMsg.getMimeMessage();
List<MPartInfo> parts = Mime.getParts(mm);
Set<MPartInfo> bodies = Mime.getBody(parts, false);
Assert.assertEquals(1, bodies.size());
for (MPartInfo body : bodies) {
Object mimeContent = body.getMimePart().getContent();
Assert.assertTrue(mimeContent instanceof String);
String deliveredNotifyMsg = (String) mimeContent;
Assert.assertEquals(expectedNotifyMsg, deliveredNotifyMsg);
}
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.lmtpserver.LmtpEnvelope in project zm-mailbox by Zimbra.
the class NotifyMailtoTest method testNotOverridableParams.
/**
* Verify the "header fields of the notification message
* that are normally related to an individual new message
* (such as "Message-ID" and "Date") are generated for the
* notification message in the normal manner, and MUST NOT
* be copied from the triggering message" (RFC 5436-2.7)
*/
@Test
public void testNotOverridableParams() {
String sampleMsg = "from: xyz@example.com\n" + "Subject: [acme-users] [fwd] version 1.0 is out\n" + "to: test1@zimbra.com\n";
try {
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
Account acct3 = Provisioning.getInstance().get(Key.AccountBy.name, "test3@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
Mailbox mbox2 = MailboxManager.getInstance().getMailboxByAccount(acct2);
Mailbox mbox3 = MailboxManager.getInstance().getMailboxByAccount(acct3);
RuleManager.clearCachedRules(acct1);
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<xyz@example.com>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<test1@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
acct1.setMailSieveScript(filterScript_MsgIDDate);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleMsg.getBytes(), false), 0, acct1.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
// The triggered message should be delivered to the target mailbox
Assert.assertEquals(1, ids.size());
// Notification message should be delivered to mailto and to= addresses
Integer item = mbox2.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message notifyMsg = mbox2.getMessageById(null, item);
Assert.assertEquals("notifybody", notifyMsg.getFragment());
String[] headers = notifyMsg.getMimeMessage().getHeader("Message-ID");
Assert.assertTrue(headers.length == 1);
Assert.assertNotSame("dummymessageid", headers[0]);
headers = notifyMsg.getMimeMessage().getHeader("Date");
Assert.assertTrue(headers.length == 1);
Assert.assertNotSame("dummydate", headers[0]);
} catch (Exception e) {
fail("No exception should be thrown:" + e);
}
}
use of com.zimbra.cs.lmtpserver.LmtpEnvelope in project zm-mailbox by Zimbra.
the class HeaderTest method setEnvelopeInfo.
private LmtpEnvelope setEnvelopeInfo() {
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<abc@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient1 = new LmtpAddress("<xyz@zimbra.com>", null, null);
LmtpAddress recipient2 = new LmtpAddress("<uvw@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient1);
env.addLocalRecipient(recipient2);
return env;
}
use of com.zimbra.cs.lmtpserver.LmtpEnvelope in project zm-mailbox by Zimbra.
the class EnvelopeTest method testMailFromDoubleQuote.
@Test
public void testMailFromDoubleQuote() {
String filterScript = "require \"envelope\";\n" + "if envelope :all :is \"from\" \"ti\\\"m@example.com\" {\n" + " tag \"From\";\n" + "}";
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<\"ti\\\"m\"@example.com>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<xyz@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
try {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
account.setMailSieveScript(filterScript);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(sampleMsg.getBytes(), false), 0, account.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
Message msg = mbox.getMessageById(null, ids.get(0).getId());
Assert.assertEquals("From", ArrayUtil.getFirstElement(msg.getTags()));
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.lmtpserver.LmtpEnvelope in project zm-mailbox by Zimbra.
the class EscapeSequencesTest method doTestEnvelopeEscapePattern.
public void doTestEnvelopeEscapePattern(String filterScript) {
String sampleMsg = "from: tim@example.com\n" + "to: test@zimbra.com\n" + "Subject: Example\n";
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<user123@example.com>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<test1@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
try {
Account account = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
account.setMailSieveScript(filterScript);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(triggeringMsg.getBytes(), false), 0, account.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(0, ids.size());
} catch (Exception e) {
fail("No exception should be thrown " + e);
}
}
Aggregations