use of com.zimbra.cs.mime.ParsedMessage in project zm-mailbox by Zimbra.
the class DeleteHeaderTest method testDeleteHeaderUsingMatches.
/*
* Delete header using matches match-type
*/
@SuppressWarnings("unchecked")
@Test
public void testDeleteHeaderUsingMatches() {
try {
String filterScript = "require [\"editheader\"];\n" + " deleteheader :matches \"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);
boolean matchFound = false;
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
Header header = enumeration.nextElement();
if ("X-Test-Header".equals(header.getName())) {
matchFound = true;
break;
}
}
Assert.assertFalse(matchFound);
} catch (Exception e) {
fail("No exception should be thrown: " + e.getMessage());
}
}
use of com.zimbra.cs.mime.ParsedMessage in project zm-mailbox by Zimbra.
the class DeleteHeaderTest method testDeleteHeaderNinthFromBottom.
/*
* Delete header value of 2nd from bottom
*/
@SuppressWarnings("unchecked")
@Test
public void testDeleteHeaderNinthFromBottom() {
try {
String filterScript = "require [\"editheader\"];\n" + " deleteheader :index 9 :last \"X-Dummy-Header\" \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-Dummy-Header".equals(header.getName())) {
Assert.assertEquals("123", header.getValue());
break;
}
}
} catch (Exception e) {
fail("No exception should be thrown: " + e.getMessage());
}
}
use of com.zimbra.cs.mime.ParsedMessage in project zm-mailbox by Zimbra.
the class DeleteHeaderTest method testDeleteHeaderAtIndex.
/*
* Delete header at index
*/
@SuppressWarnings("unchecked")
@Test
public void testDeleteHeaderAtIndex() {
try {
String filterScript = "require [\"editheader\"];\n" + " deleteheader :index 2 \"X-Test-Header\" \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);
boolean matchFound = false;
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
Header header = enumeration.nextElement();
if ("X-Test-Header".equals(header.getName()) && "test2".equals(header.getValue())) {
matchFound = true;
}
}
Assert.assertFalse(matchFound);
} catch (Exception e) {
fail("No exception should be thrown: " + e.getMessage());
}
}
use of com.zimbra.cs.mime.ParsedMessage in project zm-mailbox by Zimbra.
the class EnvelopeTest method testTo_BccTo.
@Test
public void testTo_BccTo() {
/*
* RFC 5228 5.4.
* ----
* If the SMTP transaction involved several RCPT commands, only the data
* from the RCPT command that caused delivery to this user is available
* in the "to" part of the envelope.
* ----
* The bcc recipient (who is specified by RCPT command but not on the
* message header) should not be matched by the 'envelope' test.
*/
String filterScript = "require \"envelope\";\n" + "if envelope :all :is \"to\" \"bccTo@zimbra.com\" {\n" + " tag \"Bcc To\";\n" + "}";
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<tim@example.com>", new String[] { "BODY", "SIZE" }, null);
env.setSender(sender);
// To address
LmtpAddress recipient = new LmtpAddress("<test@zimbra.com>", null, null);
env.addLocalRecipient(recipient);
// Bcc address
recipient = new LmtpAddress("<bccTo@zimbra.com>", null, null);
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(null, ArrayUtil.getFirstElement(msg.getTags()));
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.mime.ParsedMessage in project zm-mailbox by Zimbra.
the class EnvelopeTest method testInvalidHeaderName2.
@Test
public void testInvalidHeaderName2() {
String filterScript = "require \"envelope\";\n" + "if anyof envelope :matches \"from123\" \"t1@zimbra.com\" {\n" + " fileinto \"Junk\";\n" + "}\n";
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<t1@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<xyz@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
try {
Provisioning prov = Provisioning.getInstance();
Account account = prov.createAccount("xyz@zimbra.com", "secret", new HashMap<String, Object>());
account.setMail("xyz@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(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(Mailbox.ID_FOLDER_INBOX, msg.getFolderId());
} catch (Exception e) {
fail("No exception should be thrown");
}
}
Aggregations