use of com.zimbra.cs.service.util.ItemId in project zm-mailbox by Zimbra.
the class SetVariableTest method testVariablesCombo.
@Test
public void testVariablesCombo() {
try {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
// set "company" "ACME";
// set "a.b" "おしらせ"; (or any non-ascii characters [おしらせ])
// set "c_d" "C";
// set "1" "One"; ==> Should be ignored or error [Note 1]
// set "23" "twenty three"; ==> Should be ignored or error [Note 1]
// set "combination" "Hello ${company}!!";
filterScript = "require [\"variables\"];\n" + "set \"company\" \"おしらせ\" ;\n" + "set \"c_d\" \"C\";\n" + "set \"combination\" \"Hello ${company}!!\"; " + "if header :matches \"Subject\" \"*\" {\n" + " tag \"${combination}\";\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("Hello おしらせ!!", ArrayUtil.getFirstElement(msg.getTags()));
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.service.util.ItemId in project zm-mailbox by Zimbra.
the class SetVariableTest method testSetVarWithModifiersValid.
@Test
public void testSetVarWithModifiersValid() {
try {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
filterScript = "require [\"variables\"];\n" + "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("hello", ArrayUtil.getFirstElement(msg.getTags()));
RuleManager.clearCachedRules(account);
filterScript = "require [\"variables\"];\n" + "set :length \"var\" \"hello\";\n" + "if header :matches \"Subject\" \"*\" {\n" + " tag \"${var}\";\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("5", ArrayUtil.getFirstElement(msg.getTags()));
RuleManager.clearCachedRules(account);
filterScript = "require [\"variables\"];\n" + "set :lower \"var\" \"heLLo\";\n" + "if header :matches \"Subject\" \"*\" {\n" + " tag \"${var}\";\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("hello", ArrayUtil.getFirstElement(msg.getTags()));
RuleManager.clearCachedRules(account);
filterScript = "require [\"variables\"];\n" + "set :upper \"var\" \"test\";\n" + "if header :matches \"Subject\" \"*\" {\n" + " tag \"${var}\";\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("TEST", ArrayUtil.getFirstElement(msg.getTags()));
RuleManager.clearCachedRules(account);
filterScript = "require [\"variables\"];\n" + "set :lowerfirst \"var\" \"WORLD\";\n" + "if header :matches \"Subject\" \"*\" {\n" + " tag \"${var}\";\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("wORLD", ArrayUtil.getFirstElement(msg.getTags()));
RuleManager.clearCachedRules(account);
filterScript = "require [\"variables\"];\n" + "set :UPPERFIRST \"var\" \"example\";\n" + "if header :matches \"Subject\" \"*\" {\n" + " tag \"${var}\";\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("Example", ArrayUtil.getFirstElement(msg.getTags()));
RuleManager.clearCachedRules(account);
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("safe+body%26evil%3Devilbody", ArrayUtil.getFirstElement(msg.getTags()));
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.service.util.ItemId in project zm-mailbox by Zimbra.
the class SetVariableTest method testSetMatchVarAndUseInHeaderForAddress.
@Test
public void testSetMatchVarAndUseInHeaderForAddress() {
try {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
filterScript = "require [\"variables\"];\n" + "if address :comparator \"i;ascii-casemap\" :matches \"To\" \"coyote@**.com\"{\n" + " tag \"${2}\";\n" + "}";
account.setMailSieveScript(filterScript);
String raw = "From: sender@in.telligent.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.service.util.ItemId in project zm-mailbox by Zimbra.
the class SocialcastTestTest method test.
@Test
public void test() throws Exception {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
account.setMailSieveScript("if socialcast { tag \"socialcast\"; }");
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
// bulk from socialcast
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: do-not-reply@socialcast.com\nSubject: test".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.assertTrue(msg.getTags().length == 0);
// notification from socialcast
ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: do-not-reply@socialcast.com\nReply-To: share@socialcast.com\nSubject: test".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());
Assert.assertEquals("socialcast", ArrayUtil.getFirstElement(msg.getTags()));
}
use of com.zimbra.cs.service.util.ItemId 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");
}
}
Aggregations