use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class SetVariableTest method testSetMatchVarAndUseInHeader.
@Test
public void testSetMatchVarAndUseInHeader() {
try {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
filterScript = "require [\"variables\"];\n" + "if header :matches [\"To\", \"Cc\"] [\"coyote@**.com\",\"wile@**.com\"]{\n" + " log \"Match 1 ${1}\";\n" + " tag \"${2}\";\n" + "}\n";
account.setMailSieveScript(filterScript);
String raw = "From: sender@zimbra.com\n" + "To: coyote@ACME.Example.COM\n" + "Subject: hello version 1.0 is out\n" + "\n" + "Hello World.";
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
Message msg = mbox.getMessageById(null, ids.get(0).getId());
Assert.assertEquals("ACME.Example", ArrayUtil.getFirstElement(msg.getTags()));
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class SetVariableTest method testVariables_Envelope_KeyList.
@Test
public void testVariables_Envelope_KeyList() {
String sampleMsg = "from: tim@example.com\n" + "to: test@zimbra.com\n" + "Subject: Example\n";
String filterScript = "require [\"envelope\", \"variables\", \"tag\"];\n" + "set \"from_address\" \"tim@example.com\";" + "set \"to_address\" \"test1@zimbra.com\";" + "set \"from_header_name\" \"From\";" + "if envelope :all :is \"from\" [\"to_address\", \"${from_address}\"] {\n" + " tag \"KeyListTag\";" + "}";
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<tim@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().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());
String[] tags = msg.getTags();
Assert.assertEquals(1, tags.length);
Assert.assertEquals("KeyListTag", tags[0]);
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class SetVariableTest method testNonExistingVarIndexWithLeadingZeroes.
@Ignore
public void testNonExistingVarIndexWithLeadingZeroes() {
try {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
filterScript = "if header :matches :comparator \"i;ascii-casemap\" \"Subject\" \"t*t*\" { " + "addheader :last \"X-New-Header\" \"${009}\";}";
account.setMailSieveScript(filterScript);
String raw = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: test 123";
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
Message msg = mbox.getMessageById(null, ids.get(0).getId());
String[] value = msg.getMimeMessage().getHeader("X-New-Header");
Assert.assertEquals("", value[0]);
} catch (Exception e) {
e.printStackTrace();
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class SetVariableTest method testNoRequireDeclaration.
@Test
public void testNoRequireDeclaration() {
try {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
// No "variable" require
filterScript = "set \"var\" \"hello\";\n" + "if header :matches \"Subject\" \"*\" {\n" + " tag \"${var}\";\n" + "}\n";
account.setMailSieveScript(filterScript);
String raw = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: Test\n" + "\n" + "Hello World.";
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Message msg = mbox.getMessageById(null, ids.get(0).getId());
Assert.assertEquals(null, ArrayUtil.getFirstElement(msg.getTags()));
RuleManager.clearCachedRules(account);
// No "enotify" require (for :encodeurl modifier)
filterScript = "require [\"variables\"];\n" + "set :encodeurl :lower \"body_param\" \"Safe body&evil=evilbody\";\n" + "if header :matches \"Subject\" \"*\" {\n" + " tag \"${body_param}\";\n" + "}\n";
account.setMailSieveScript(filterScript);
ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
msg = mbox.getMessageById(null, ids.get(0).getId());
Assert.assertEquals(null, ArrayUtil.getFirstElement(msg.getTags()));
RuleManager.clearCachedRules(account);
// No "variables" require, no ${..} replacement
// ==> ${1} should be appeared as is.
filterScript = "require [\"fileinto\"];\n" + "if header :matches \"Subject\" \"*\" {\n" + " fileinto \"${1}\";\n" + "}\n";
account.setMailSieveScript(filterScript);
ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
msg = mbox.getMessageById(null, ids.get(0).getId());
Folder folder = mbox.getFolderById(null, msg.getFolderId());
Assert.assertEquals("${1}", folder.getName());
} catch (Exception e) {
fail("No exception should be thrown: " + e);
}
}
use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class SetVariableTest method testSetMatchVarAndFileInto.
@Test
public void testSetMatchVarAndFileInto() {
try {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
filterScript = "require [\"fileinto\", \"log\", \"variables\"];\n" + "set \"sub\" \"test\";\n" + "if header :contains \"subject\" \"${sub}\" {\n" + " log \"Subject has test\";\n" + " fileinto \"${sub}\";\n" + "}";
System.out.println(filterScript);
account.setMailSieveScript(filterScript);
String raw = "From: sender@in.telligent.com\n" + "To: coyote@ACME.Example.COM\n" + "Subject: test\n" + "\n" + "Hello World.";
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
Message msg = mbox.getMessageById(null, ids.get(0).getId());
Folder folder = mbox.getFolderById(null, msg.getFolderId());
Assert.assertEquals("test", folder.getName());
RuleManager.clearCachedRules(account);
account.unsetAdminSieveScriptBefore();
account.unsetMailSieveScript();
account.unsetAdminSieveScriptAfter();
filterScript = "require [\"fileinto\", \"log\", \"variables\"];\n" + "set \"sub\" \"test\";\n" + "if header :contains \"subject\" \"Hello ${sub}\" {\n" + " log \"Subject has test\";\n" + " fileinto \"${sub}\";\n" + "}";
System.out.println(filterScript);
account.setMailSieveScript(filterScript);
raw = "From: sender@in.telligent.com\n" + "To: coyote@ACME.Example.COM\n" + "Subject: Hello test\n" + "\n" + "Hello World.";
ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
msg = mbox.getMessageById(null, ids.get(0).getId());
folder = mbox.getFolderById(null, msg.getFolderId());
Assert.assertEquals("test", folder.getName());
RuleManager.clearCachedRules(account);
account.unsetAdminSieveScriptBefore();
account.unsetMailSieveScript();
account.unsetAdminSieveScriptAfter();
filterScript = "require [\"fileinto\", \"variables\"];\n" + "set \"var5\" \"var test 5\";\n" + "if allof (header :matches [\"subject\"] \"${var5}*\") {\n" + " fileinto \"${var5}\";\n" + " set \"var6\" \"var test 6\";\n" + " if allof (header :matches [\"subject\"] \"*${var6}\") {\n" + " fileinto \"${1}\";\n" + " }\n" + "}";
System.out.println(filterScript);
account.setMailSieveScript(filterScript);
raw = "From: sender@in.telligent.com\n" + "To: coyote@ACME.Example.COM\n" + "Subject: var test 5 var test 6\n" + "\n" + "Hello World.";
ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
msg = mbox.getMessageById(null, ids.get(0).getId());
folder = mbox.getFolderById(null, msg.getFolderId());
Assert.assertEquals("var test 5", folder.getName());
} catch (Exception e) {
fail("No exception should be thrown");
}
}
Aggregations