use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class ReplyTest method testReplyMimeVariables.
@Test
public void testReplyMimeVariables() {
try {
String sampleMsg = "from: test2@zimbra.com\n" + "Return-Path: test2@zimbra.com\n" + "Subject: Hello\n" + "to: test@zimbra.com\n";
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@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);
String filterScript = "set \"var\" \"World\";\n" + "if anyof (true) { reply \"${Subject} ${var}\"" + " stop;" + "}";
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("Hello World", notifyMsg.getFragment());
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class RuleManagerAdminFilterTest method stopAtAdminBefore.
/* @Test
public void variableAdminOnUserOn() throws Exception {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Map<String, Object> attrs = Maps.newHashMap();
attrs = Maps.newHashMap();
Provisioning.getInstance().getServer(account).modify(attrs);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
account.unsetMailAdminSieveScriptBefore();
account.unsetMailSieveScript();
account.unsetMailAdminSieveScriptAfter();
account.setMailAdminSieveScriptBefore(variableScripts[0]);
account.setMailSieveScript(variableScripts[1]);
account.setMailAdminSieveScriptAfter(variableScripts[2]);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox),
mbox, new ParsedMessage(message.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[] tags = msg.getTags();
Assert.assertEquals(3, tags.length);
Assert.assertEquals("before-foo", tags[0]); // ${var} is a valid variable
Assert.assertEquals("enduser-", tags[1]); // Variable feature is on but no definition of ${var}
Assert.assertEquals("after-", tags[2]); // Variable feature is on but no definition of ${var}
}
@Test
public void variableAdminOnUserOn2() throws Exception {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Map<String, Object> attrs = Maps.newHashMap();
attrs = Maps.newHashMap();
Provisioning.getInstance().getServer(account).modify(attrs);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
account.unsetMailAdminSieveScriptBefore();
account.unsetMailSieveScript();
account.unsetMailAdminSieveScriptAfter();
String enduserScript = "require [\"tag\", \"log\", \"variables\"];"
+ "set \"var\" \"bar\";"
+ "tag \"enduser-${var}\";";
account.setMailAdminSieveScriptBefore(variableScripts[0]);
account.setMailSieveScript(enduserScript);
account.setMailAdminSieveScriptAfter(variableScripts[2]);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox),
mbox, new ParsedMessage(message.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[] tags = msg.getTags();
Assert.assertEquals(3, tags.length);
Assert.assertEquals("before-foo", tags[0]); // "foo" is assigned in admin-defined filter
Assert.assertEquals("enduser-bar", tags[1]);// "bar" is assigned in user-defined filter
Assert.assertEquals("after-", tags[2]); // Variable feature is on but no definition of ${var}
}*/
@Test
public void stopAtAdminBefore() throws Exception {
String adminBefore = "tag \"before-admin\";" + "stop;";
String enduser = "tag \"enduser\";";
String adminAfter = "tag \"after\";";
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
RuleManager.clearCachedRules(account);
account.unsetAdminSieveScriptBefore();
account.unsetMailSieveScript();
account.unsetAdminSieveScriptAfter();
account.setAdminSieveScriptBefore(adminBefore);
account.setMailSieveScript(enduser);
account.setAdminSieveScriptAfter(adminAfter);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(message.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[] tags = msg.getTags();
Assert.assertEquals(1, tags.length);
Assert.assertEquals("before-admin", tags[0]);
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class RuleManagerAdminFilterTest method stopInTheAdminRule.
@Test
public void stopInTheAdminRule() throws Exception {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
RuleManager.clearCachedRules(account);
account.unsetAdminSieveScriptBefore();
account.unsetMailSieveScript();
account.unsetAdminSieveScriptAfter();
account.setAdminSieveScriptBefore(scriptAdminBeforeStop);
account.setMailSieveScript(scriptUser);
account.setAdminSieveScriptAfter(scriptAdminAfter);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(message.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("admin-defined-before", ArrayUtil.getFirstElement(msg.getTags()));
}
use of com.zimbra.cs.mailbox.OperationContext in project zm-mailbox by Zimbra.
the class NotifyMailtoTest method test.
/**
* Tests 'notify' filter rule:
* - Set :message (Subject field), :from (From field) and mechanism (mailto:...)
* - Body of the notification message contains non-ascii characters
* - Additional header fields are specified via mechanism (maito:) parameter
*/
@Test
public void test() {
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";
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("<>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<xyz@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
acct1.setMailSieveScript(filterScript1);
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("新しいメールが届きました。 You've got a mail. Chao!", 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 testNotifyMethodCapability_OnlineMaybe.
/**
* Tests the notify_method_capability ("online"/"maybe")
*/
@Test
public void testNotifyMethodCapability_OnlineMaybe() {
String filterScript = "require [\"enotify\", \"tag\"];\n" + "if notify_method_capability\n" + " \"mailto:test2@zimbra.com\"\n" + " \"Online\"\n" + " \"maybe\" { \n" + " tag \"notify_method_capability\";\n" + "}";
try {
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
RuleManager.clearCachedRules(acct1);
acct1.setMailSieveScript(filterScript);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage("From: test1@zimbra.com".getBytes(), false), 0, acct1.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
// ZCS implements the RFC 5436 so that it returns true when 'notify_method_capability'
// checkes whether the "Online" status is "maybe".
Assert.assertEquals(1, ids.size());
Message msg = mbox1.getMessageById(null, ids.get(0).getId());
Assert.assertEquals("notify_method_capability", ArrayUtil.getFirstElement(msg.getTags()));
} catch (Exception e) {
fail("No exception should be thrown");
}
}
Aggregations