Search in sources :

Example 16 with EditheaderTest

use of com.zimbra.soap.mail.type.EditheaderTest in project zm-mailbox by Zimbra.

the class GetFilterRulesAdminTest method testSieveToSoapReplaceheaderActionBasicWithComparator.

@Test
public void testSieveToSoapReplaceheaderActionBasicWithComparator() throws ServiceException {
    RuleManager.clearCachedRules(account);
    String script = "require [\"editheader\", \"variables\"];\n" + "# rule1\n" + "replaceheader :newname \"X-My-Header2\" :newvalue \"[new] ${1}\" :comparator \"i;octet\" \"X-My-Header\" \"xyz\";";
    account.setAdminSieveScriptBefore(script);
    List<FilterRule> filterRules = RuleManager.getAdminRulesAsXML(account, FilterType.INCOMING, AdminFilterType.BEFORE);
    Assert.assertEquals(filterRules.size(), 1);
    FilterRule filterRule = filterRules.get(0);
    Assert.assertEquals("rule1", filterRule.getName());
    Assert.assertTrue(filterRule.isActive());
    Assert.assertEquals(1, filterRule.getFilterActions().size());
    FilterAction filterAction = filterRule.getFilterActions().get(0);
    Assert.assertTrue(filterAction instanceof FilterAction.ReplaceheaderAction);
    FilterAction.ReplaceheaderAction action = (FilterAction.ReplaceheaderAction) filterAction;
    Assert.assertNull(action.getLast());
    Assert.assertNull(action.getOffset());
    Assert.assertEquals("X-My-Header2", action.getNewName());
    Assert.assertEquals("[new] ${1}", action.getNewValue());
    EditheaderTest test = action.getTest();
    Assert.assertEquals("i;octet", test.getComparator());
    Assert.assertEquals("is", test.getMatchType());
    Assert.assertNull(test.getRelationalComparator());
    Assert.assertNull(test.getCount());
    Assert.assertNull(test.getValue());
    Assert.assertEquals("X-My-Header", test.getHeaderName());
    List<String> values = test.getHeaderValue();
    Assert.assertEquals(1, values.size());
    Assert.assertEquals("xyz", values.get(0));
}
Also used : FilterRule(com.zimbra.soap.mail.type.FilterRule) EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) FilterAction(com.zimbra.soap.mail.type.FilterAction) EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) Test(org.junit.Test)

Example 17 with EditheaderTest

use of com.zimbra.soap.mail.type.EditheaderTest in project zm-mailbox by Zimbra.

the class SieveToSoap method visitReplaceheaderAction.

protected void visitReplaceheaderAction(Node node, VisitPhase phase, RuleProperties props, Boolean last, Integer offset, String newName, String newValue, String matchType, Boolean countComparision, Boolean valueComparision, String relationalComparator, String comparator, String headerName, List<String> headerValue) throws ServiceException {
    if (phase == VisitPhase.begin) {
        EditheaderTest test = new EditheaderTest(matchType, countComparision, valueComparision, relationalComparator, comparator, headerName, headerValue);
        FilterAction.ReplaceheaderAction action = new FilterAction.ReplaceheaderAction(last, offset, test, newName, newValue);
        action.validateReplaceheaderAction();
        addAction(action);
    }
}
Also used : EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) FilterAction(com.zimbra.soap.mail.type.FilterAction)

Example 18 with EditheaderTest

use of com.zimbra.soap.mail.type.EditheaderTest in project zm-mailbox by Zimbra.

the class SieveToSoap method visitDeleteheaderAction.

@Override
protected void visitDeleteheaderAction(Node node, VisitPhase phase, RuleProperties props, Boolean last, Integer offset, String matchType, Boolean countComparision, Boolean valueComparision, String relationalComparator, String comparator, String headerName, List<String> headerValue) throws ServiceException {
    if (phase == VisitPhase.begin) {
        EditheaderTest test = new EditheaderTest(matchType, countComparision, valueComparision, relationalComparator, comparator, headerName, headerValue);
        FilterAction.DeleteheaderAction action = new FilterAction.DeleteheaderAction(last, offset, test);
        action.validateDeleteheaderAction();
        addAction(action);
    }
}
Also used : EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) FilterAction(com.zimbra.soap.mail.type.FilterAction)

Example 19 with EditheaderTest

use of com.zimbra.soap.mail.type.EditheaderTest in project zm-mailbox by Zimbra.

the class SoapToSieve method handleAction.

private String handleAction(FilterAction action, boolean isAdminScript) throws ServiceException {
    if (action instanceof FilterAction.KeepAction) {
        return "keep";
    } else if (action instanceof FilterAction.DiscardAction) {
        return "discard";
    } else if (action instanceof FilterAction.FileIntoAction) {
        FilterAction.FileIntoAction fileinto = (FilterAction.FileIntoAction) action;
        String folderPath = fileinto.getFolder();
        boolean copy = fileinto.isCopy();
        if (StringUtil.isNullOrEmpty(folderPath)) {
            throw ServiceException.INVALID_REQUEST("Missing folderPath", null);
        }
        if (copy) {
            return String.format("fileinto :copy \"%s\"", FilterUtil.escape(folderPath));
        } else {
            return String.format("fileinto \"%s\"", FilterUtil.escape(folderPath));
        }
    } else if (action instanceof FilterAction.TagAction) {
        FilterAction.TagAction tag = (FilterAction.TagAction) action;
        String tagName = tag.getTag();
        if (StringUtil.isNullOrEmpty(tagName)) {
            throw ServiceException.INVALID_REQUEST("Missing tag", null);
        }
        return String.format("tag \"%s\"", FilterUtil.escape(tagName));
    } else if (action instanceof FilterAction.FlagAction) {
        FilterAction.FlagAction flag = (FilterAction.FlagAction) action;
        String flagName = flag.getFlag();
        if (StringUtil.isNullOrEmpty(flagName)) {
            throw ServiceException.INVALID_REQUEST("Missing flag", null);
        }
        return String.format("flag \"%s\"", Sieve.Flag.valueOf(flagName));
    } else if (action instanceof FilterAction.RedirectAction) {
        FilterAction.RedirectAction redirect = (FilterAction.RedirectAction) action;
        String address = redirect.getAddress();
        boolean copy = redirect.isCopy();
        if (StringUtil.isNullOrEmpty(address)) {
            throw ServiceException.INVALID_REQUEST("Missing address", null);
        }
        if (copy) {
            return String.format("redirect :copy \"%s\"", FilterUtil.escape(address));
        } else {
            return String.format("redirect \"%s\"", FilterUtil.escape(address));
        }
    } else if (action instanceof FilterAction.ReplyAction) {
        FilterAction.ReplyAction reply = (FilterAction.ReplyAction) action;
        String content = reply.getContent();
        if (StringUtil.isNullOrEmpty(content)) {
            throw ServiceException.INVALID_REQUEST("Missing reply content", null);
        }
        return new StringBuilder("reply text:\r\n").append(getDotStuffed(content)).append("\r\n.\r\n").toString();
    } else if (action instanceof FilterAction.NotifyAction) {
        FilterAction.NotifyAction notify = (FilterAction.NotifyAction) action;
        String emailAddr = notify.getAddress();
        if (StringUtil.isNullOrEmpty(emailAddr)) {
            throw ServiceException.INVALID_REQUEST("Missing address", null);
        }
        String subjectTemplate = Strings.nullToEmpty(notify.getSubject());
        String bodyTemplate = Strings.nullToEmpty(notify.getContent());
        int maxBodyBytes = MoreObjects.firstNonNull(notify.getMaxBodySize(), -1);
        String origHeaders = Strings.nullToEmpty(notify.getOrigHeaders());
        if (!subjectTemplate.isEmpty() && containsSubjectHeader(origHeaders)) {
            throw ServiceException.INVALID_REQUEST("subject conflict", null);
        }
        return new StringBuilder("notify ").append(StringUtil.enclose(FilterUtil.escape(emailAddr), '"')).append(' ').append(StringUtil.enclose(subjectTemplate, '"')).append(' ').append("text:\r\n").append(getDotStuffed(bodyTemplate)).append("\r\n.\r\n").append(maxBodyBytes < 0 ? "" : " " + maxBodyBytes).append(origHeaders.isEmpty() ? "" : " " + getSieveMultiValue(origHeaders)).toString();
    } else if (action instanceof FilterAction.RFCCompliantNotifyAction) {
        FilterAction.RFCCompliantNotifyAction notify = (FilterAction.RFCCompliantNotifyAction) action;
        String from = notify.getFrom();
        String importance = Strings.nullToEmpty(notify.getImportance());
        String options = Strings.nullToEmpty(notify.getOptions());
        String message = Strings.nullToEmpty(notify.getMessage());
        String method = Strings.nullToEmpty(notify.getMethod());
        if (StringUtil.isNullOrEmpty(method)) {
            throw ServiceException.INVALID_REQUEST("Missing notification mechanism", null);
        }
        StringBuilder filter = new StringBuilder("notify ");
        if (!from.isEmpty()) {
            filter.append(":from ").append(StringUtil.enclose(FilterUtil.escape(from), '"')).append(' ');
        }
        if (!importance.isEmpty()) {
            filter.append(":importance ").append(StringUtil.enclose(importance, '"')).append(' ');
        }
        if (!options.isEmpty()) {
            filter.append(":options ").append(StringUtil.enclose(options, '"')).append(' ');
        }
        if (!message.isEmpty()) {
            filter.append(":message ").append(StringUtil.enclose(message, '"')).append(' ');
        }
        if (method.indexOf("\n") < 0) {
            filter.append(StringUtil.enclose(method, '"'));
        } else {
            filter.append("text:\r\n").append(method).append("\r\n.\r\n");
        }
        return filter.toString();
    } else if (action instanceof FilterAction.StopAction) {
        return "stop";
    } else if (action instanceof FilterAction.RejectAction) {
        FilterAction.RejectAction rejectAction = (FilterAction.RejectAction) action;
        return handleRejectAction(rejectAction);
    } else if (action instanceof FilterAction.ErejectAction) {
        FilterAction.ErejectAction erejectAction = (FilterAction.ErejectAction) action;
        return handleRejectAction(erejectAction);
    } else if (action instanceof FilterAction.LogAction) {
        FilterAction.LogAction logAction = (FilterAction.LogAction) action;
        StringBuilder sb = new StringBuilder();
        sb.append("log");
        FilterAction.LogAction.LogLevel level = logAction.getLevel();
        if (level != null) {
            if (!(FilterAction.LogAction.LogLevel.fatal == level || FilterAction.LogAction.LogLevel.error == level || FilterAction.LogAction.LogLevel.warn == level || FilterAction.LogAction.LogLevel.info == level || FilterAction.LogAction.LogLevel.debug == level || FilterAction.LogAction.LogLevel.trace == level)) {
                String message = "Invalid log action: Invalid log level found: " + level.toString();
                throw ServiceException.PARSE_ERROR(message, null);
            }
            sb.append(" :").append(level.toString());
        }
        sb.append(" \"").append(logAction.getContent()).append("\"");
        return sb.toString();
    } else if (action instanceof FilterAction.AddheaderAction) {
        if (!isAdminScript) {
            throw ServiceException.PARSE_ERROR("Invalid addheader action: addheader action is not allowed in user scripts", null);
        }
        FilterAction.AddheaderAction addheaderAction = (FilterAction.AddheaderAction) action;
        if (StringUtil.isNullOrEmpty(addheaderAction.getHeaderName()) || StringUtil.isNullOrEmpty(addheaderAction.getHeaderValue())) {
            throw ServiceException.PARSE_ERROR("Invalid addheader action: Missing headerName or headerValue", null);
        }
        StringBuilder sb = new StringBuilder();
        sb.append("addheader");
        if (addheaderAction.getLast() != null && addheaderAction.getLast()) {
            sb.append(" :last");
        }
        sb.append(" \"").append(addheaderAction.getHeaderName()).append("\"");
        sb.append(" \"").append(addheaderAction.getHeaderValue()).append("\"");
        return sb.toString();
    } else if (action instanceof FilterAction.ReplaceheaderAction) {
        if (!isAdminScript) {
            throw ServiceException.PARSE_ERROR("Invalid replaceheader action: replaceheader action is not allowed in user scripts", null);
        }
        FilterAction.ReplaceheaderAction replaceheaderAction = (FilterAction.ReplaceheaderAction) action;
        replaceheaderAction.validateReplaceheaderAction();
        StringBuilder sb = new StringBuilder();
        sb.append("replaceheader");
        if (replaceheaderAction.getLast() != null && replaceheaderAction.getLast()) {
            sb.append(" :last");
        }
        if (replaceheaderAction.getOffset() != null) {
            sb.append(" :index ").append(replaceheaderAction.getOffset());
        }
        if (!StringUtil.isNullOrEmpty(replaceheaderAction.getNewName())) {
            sb.append(" :newname ").append("\"").append(replaceheaderAction.getNewName()).append("\"");
        }
        if (!StringUtil.isNullOrEmpty(replaceheaderAction.getNewValue())) {
            sb.append(" :newvalue ").append("\"").append(replaceheaderAction.getNewValue()).append("\"");
        }
        EditheaderTest test = replaceheaderAction.getTest();
        if (test.getCount() != null && test.getCount()) {
            sb.append(" :count");
        } else if (test.getValue() != null && test.getValue()) {
            sb.append(" :value");
        }
        if (!StringUtil.isNullOrEmpty(test.getRelationalComparator())) {
            sb.append(" \"").append(test.getRelationalComparator()).append("\"");
        }
        if (!StringUtil.isNullOrEmpty(test.getComparator())) {
            sb.append(" :comparator ").append("\"").append(test.getComparator()).append("\"");
        }
        if (!StringUtil.isNullOrEmpty(test.getMatchType())) {
            sb.append(" :").append(test.getMatchType());
        }
        if (!StringUtil.isNullOrEmpty(test.getHeaderName())) {
            sb.append(" \"").append(test.getHeaderName()).append("\"");
        }
        List<String> headerValues = test.getHeaderValue();
        if (headerValues != null && !headerValues.isEmpty()) {
            if (headerValues.size() > 1) {
                sb.append(" [");
            }
            boolean first = true;
            for (String value : headerValues) {
                if (first) {
                    first = false;
                } else {
                    sb.append(",");
                }
                sb.append(" \"").append(value).append("\"");
            }
            if (headerValues.size() > 1) {
                sb.append(" ]");
            }
        }
        return sb.toString();
    } else if (action instanceof FilterAction.DeleteheaderAction) {
        if (!isAdminScript) {
            throw ServiceException.PARSE_ERROR("Invalid deleteheader action: deleteheader action is not allowed in user scripts", null);
        }
        FilterAction.DeleteheaderAction deleteheaderAction = (FilterAction.DeleteheaderAction) action;
        deleteheaderAction.validateDeleteheaderAction();
        StringBuilder sb = new StringBuilder();
        sb.append("deleteheader");
        if (deleteheaderAction.getLast() != null && deleteheaderAction.getLast()) {
            sb.append(" :last");
        }
        if (deleteheaderAction.getOffset() != null) {
            sb.append(" :index ").append(deleteheaderAction.getOffset());
        }
        EditheaderTest test = deleteheaderAction.getTest();
        if (test.getCount() != null && test.getCount()) {
            sb.append(" :count");
        } else if (test.getValue() != null && test.getValue()) {
            sb.append(" :value");
        }
        if (!StringUtil.isNullOrEmpty(test.getRelationalComparator())) {
            sb.append(" \"").append(test.getRelationalComparator()).append("\"");
        }
        if (!StringUtil.isNullOrEmpty(test.getComparator())) {
            sb.append(" :comparator ").append("\"").append(test.getComparator()).append("\"");
        }
        if (!StringUtil.isNullOrEmpty(test.getMatchType())) {
            sb.append(" :").append(test.getMatchType());
        }
        if (!StringUtil.isNullOrEmpty(test.getHeaderName())) {
            sb.append(" \"").append(test.getHeaderName()).append("\"");
        }
        List<String> headerValues = test.getHeaderValue();
        if (headerValues != null && !headerValues.isEmpty()) {
            if (headerValues.size() > 1) {
                sb.append(" [");
            }
            boolean first = true;
            for (String value : headerValues) {
                if (first) {
                    first = false;
                } else {
                    sb.append(",");
                }
                sb.append(" \"").append(value).append("\"");
            }
            if (headerValues.size() > 1) {
                sb.append(" ]");
            }
        }
        return sb.toString();
    } else {
        ZimbraLog.soap.debug("Ignoring unexpected action: %s", action);
    }
    return null;
}
Also used : List(java.util.List) EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) FilterAction(com.zimbra.soap.mail.type.FilterAction)

Example 20 with EditheaderTest

use of com.zimbra.soap.mail.type.EditheaderTest in project zm-mailbox by Zimbra.

the class GetFilterRulesAdminTest method testSieveToSoapReplaceheaderActionBasic.

/**
 ************replaceheader**************
 */
// TODO: start writing unit tests
@Test
public void testSieveToSoapReplaceheaderActionBasic() throws ServiceException {
    RuleManager.clearCachedRules(account);
    String script = "require [\"editheader\", \"variables\"];\n" + "# rule1\n" + "replaceheader :newvalue \"[new] ${1}\" \"X-My-Header\" \"xyz\";";
    account.setAdminSieveScriptBefore(script);
    List<FilterRule> filterRules = RuleManager.getAdminRulesAsXML(account, FilterType.INCOMING, AdminFilterType.BEFORE);
    Assert.assertEquals(filterRules.size(), 1);
    FilterRule filterRule = filterRules.get(0);
    Assert.assertEquals("rule1", filterRule.getName());
    Assert.assertTrue(filterRule.isActive());
    Assert.assertEquals(1, filterRule.getFilterActions().size());
    FilterAction filterAction = filterRule.getFilterActions().get(0);
    Assert.assertTrue(filterAction instanceof FilterAction.ReplaceheaderAction);
    FilterAction.ReplaceheaderAction action = (FilterAction.ReplaceheaderAction) filterAction;
    Assert.assertNull(action.getLast());
    Assert.assertNull(action.getOffset());
    Assert.assertNull(action.getNewName());
    Assert.assertEquals("[new] ${1}", action.getNewValue());
    EditheaderTest test = action.getTest();
    Assert.assertEquals("i;ascii-casemap", test.getComparator());
    Assert.assertEquals("is", test.getMatchType());
    Assert.assertNull(test.getRelationalComparator());
    Assert.assertNull(test.getCount());
    Assert.assertNull(test.getValue());
    Assert.assertEquals("X-My-Header", test.getHeaderName());
    List<String> values = test.getHeaderValue();
    Assert.assertEquals(1, values.size());
    Assert.assertEquals("xyz", values.get(0));
}
Also used : FilterRule(com.zimbra.soap.mail.type.FilterRule) EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) FilterAction(com.zimbra.soap.mail.type.FilterAction) EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) Test(org.junit.Test)

Aggregations

EditheaderTest (com.zimbra.soap.mail.type.EditheaderTest)33 FilterRule (com.zimbra.soap.mail.type.FilterRule)30 Test (org.junit.Test)30 FilterAction (com.zimbra.soap.mail.type.FilterAction)19 ArrayList (java.util.ArrayList)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)14 DeleteheaderAction (com.zimbra.soap.mail.type.FilterAction.DeleteheaderAction)7 ReplaceheaderAction (com.zimbra.soap.mail.type.FilterAction.ReplaceheaderAction)7 List (java.util.List)1