use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class NotifyMailtoTest method testEmptyMessageBody.
@Test
public void testEmptyMessageBody() {
String sampleMsg = "Auto-Submitted: \"no\"\n" + "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_EmptyBodyParameter);
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("", notifyMsg.getFragment());
String[] headers = notifyMsg.getMimeMessage().getHeader("Auto-Submitted");
Assert.assertTrue(headers.length == 1);
Assert.assertEquals("auto-notified; owner-email=\"test1@zimbra.com\"", headers[0]);
headers = notifyMsg.getMimeMessage().getHeader("to");
Assert.assertTrue(headers.length == 1);
Assert.assertEquals("test2@zimbra.com, test3@zimbra.com", headers[0]);
headers = notifyMsg.getMimeMessage().getHeader("from");
Assert.assertFalse(notifyMsg.getSender() == null);
Assert.assertEquals("test1@zimbra.com", notifyMsg.getSender());
notifyMsg = mbox3.getMessageById(null, item);
Assert.assertEquals("おしらせ", notifyMsg.getSubject());
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class NotifyMailtoTest method testBackslashEscapeSequence.
@Test
public void testBackslashEscapeSequence() {
String sampleMsg = "Auto-Submitted: \"no\"\n" + "from: xyz@example.com\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 filterScript = "require [\"enotify\"];\n" + "notify :from \"\\\"tes\\\\\\\\t1\\\"@zimbra.com\"" + ":message \"sample me\\\\ssa\\\"ge4\" " + "\"mailto:\\\"tes\\\\\\\\t2\\\"@zimbra.com?body=sample_message\";";
try {
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "\"tes\\\\t2\"@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
Mailbox mbox2 = MailboxManager.getInstance().getMailboxByAccount(acct2);
acct1.setMail("test1@zimbra.com");
RuleManager.clearCachedRules(acct1);
acct1.setMailSieveScript(filterScript);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleMsg.getBytes(), false), 0, acct1.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
Integer item = mbox2.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message notifyMsg = mbox2.getMessageById(null, item);
Assert.assertEquals("sample me\\ssa\"ge4", notifyMsg.getSubject());
Assert.assertEquals("<\"tes\\\\t1\"@zimbra.com>", notifyMsg.getSender());
RuleManager.clearCachedRules(acct1);
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class ReplaceHeaderTest method testReplaceNoValuePattern.
/*
* Replace header when no value-patterns are specified
*/
@SuppressWarnings("unchecked")
@Test
public void testReplaceNoValuePattern() {
try {
String filterScript = "require [\"editheader\"];\n" + " replaceheader :newname \"X-New-Header\" :newvalue \"new value\" :comparator \"i;ascii-casemap\" :matches \"Subject\" \r\n" + " ;\n";
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
RuleManager.clearCachedRules(acct1);
acct1.setMailSieveScript(filterScript);
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), null, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Integer itemId = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message message = mbox1.getMessageById(null, itemId);
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
Header header = enumeration.nextElement();
if ("X-New-Header".equals(header.getName())) {
Assert.assertEquals("new value", header.getValue());
} else if ("Subject".equals(header.getName())) {
fail("Subject header should have been replaced");
}
}
} catch (Exception e) {
fail("No exception should be thrown: " + e.getMessage());
}
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class ReplaceHeaderTest method testReplaceHeaderNonAscii.
/*
* Replace the subject with non-ASCII strings
*/
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderNonAscii() {
try {
String filterScript = "require [\"editheader\"];\n" + "replaceheader :newvalue \"[追加]${1}\" :matches \"Subject\" \"*\";";
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
RuleManager.clearCachedRules(acct1);
acct1.setMailSieveScript(filterScript);
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), null, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Integer itemId = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message message = mbox1.getMessageById(null, itemId);
String newSubject = "";
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
Header temp = enumeration.nextElement();
if ("Subject".equals(temp.getName())) {
newSubject = temp.getValue();
break;
}
}
Assert.assertEquals("=?UTF-8?Q?[=E8=BF=BD=E5=8A=A0]example?=", newSubject);
} catch (Exception e) {
fail("No exception should be thrown: " + e.getMessage());
}
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class ReplaceHeaderTest method testReplaceHeaderAtIndex.
/*
* Replace header value at index
*/
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderAtIndex() {
try {
String filterScript = "require [\"editheader\"];\n" + " replaceheader :index 2 :newvalue \"new test\" :contains \"X-Test-Header\" \"test\" \r\n" + " ;\n";
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
RuleManager.clearCachedRules(acct1);
acct1.setMailSieveScript(filterScript);
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), null, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Integer itemId = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message message = mbox1.getMessageById(null, itemId);
int indexMatch = 0;
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
Header header = enumeration.nextElement();
if ("X-Test-Header".equals(header.getName())) {
indexMatch++;
if (indexMatch == 2) {
Assert.assertEquals("new test", header.getValue());
break;
}
}
}
} catch (Exception e) {
fail("No exception should be thrown: " + e.getMessage());
}
}
Aggregations