Search in sources :

Example 11 with ZimbraMailAdapter

use of com.zimbra.cs.filter.ZimbraMailAdapter in project zm-mailbox by Zimbra.

the class TwitterTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments args, SieveContext ctx) throws SieveException {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    ZimbraMailAdapter adapter = (ZimbraMailAdapter) mail;
    ParsedAddress sender = adapter.getParsedMessage().getParsedSender();
    if (!Strings.isNullOrEmpty(sender.emailPart)) {
        String email = sender.emailPart.toLowerCase();
        if (email.equals("notify@twitter.com")) {
            return true;
        }
    }
    return false;
}
Also used : DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) ParsedAddress(com.zimbra.cs.mime.ParsedAddress)

Example 12 with ZimbraMailAdapter

use of com.zimbra.cs.filter.ZimbraMailAdapter in project zm-mailbox by Zimbra.

the class ZimbraVariablesCtrl method executeBasic.

@Override
protected Object executeBasic(MailAdapter mail, Arguments arguments, Block block, SieveContext context) throws SieveException {
    if (!(mail instanceof ZimbraMailAdapter)) {
        return null;
    }
    ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
    for (Argument arg : arguments.getArgumentList()) {
        if (arg instanceof TagArgument) {
            TagArgument tag = (TagArgument) arg;
            String tagValue = tag.getTag();
            if (RESET.equalsIgnoreCase(tagValue)) {
                mailAdapter.clearValues();
            } else {
                throw new SyntaxException("Invalid tag: [" + tagValue + "]");
            }
        }
    }
    return null;
}
Also used : TagArgument(org.apache.jsieve.TagArgument) StringListArgument(org.apache.jsieve.StringListArgument) Argument(org.apache.jsieve.Argument) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 13 with ZimbraMailAdapter

use of com.zimbra.cs.filter.ZimbraMailAdapter in project zm-mailbox by Zimbra.

the class EnvelopeTest method match.

/**
     * Compares the address with operator
     *
     * @param operator "gt" / "ge" / "lt" / "le" / "eq" / "ne"
     */
private boolean match(MailAdapter mail, String addressPart, String comparator, String matchType, String operator, List<String> headerNames, List<String> keys, SieveContext context) throws SieveException {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    // Iterate over the address fields looking for a match
    boolean isMatched = false;
    List<String> headerValues = Lists.newArrayListWithExpectedSize(2);
    for (final String headerName : headerNames) {
        if ("to".equalsIgnoreCase(headerName)) {
            // RFC 5231 4.2. ... The envelope "to" will always have only one
            // entry, which is the address of the user for whom the Sieve script
            // is running.
            String recipient = null;
            try {
                recipient = ((ZimbraMailAdapter) mail).getMailbox().getAccount().getMail();
            } catch (ServiceException e) {
                recipient = "";
            }
            headerValues.add(getMatchAddressPart(addressPart, recipient));
        } else if ("from".equalsIgnoreCase(headerName)) {
            List<String> values = getMatchingValues(mail, headerName);
            if (values != null) {
                if (matchType.equalsIgnoreCase(COUNT_TAG)) {
                    // RFC 5231 Section 4.2 Match Type COUNT says:
                    // | The envelope "from" will be 0 if the MAIL FROM is empty, or 1 if MAIL
                    // | FROM is not empty.
                    // This method could be called for other match type, such as :value or :is,
                    // remove the empty element only if the match type is :count.
                    values.removeIf(s -> Strings.isNullOrEmpty(s));
                }
                for (String value : values) {
                    headerValues.add(getMatchAddressPart(addressPart, value));
                }
            }
        } else {
            throw new SyntaxException("Unexpected header name as a value for <envelope-part>: '" + headerName + "'");
        }
    }
    if (COUNT_TAG.equals(matchType)) {
        for (final String key : keys) {
            isMatched = ZimbraComparatorUtils.counts(comparator, operator, headerValues, ZimbraComparatorUtils.getMatchKey(addressPart, key), context);
            if (isMatched) {
                break;
            }
        }
    } else {
        Iterator headerValuesIter = headerValues.iterator();
        while (!isMatched && headerValuesIter.hasNext()) {
            List<String> normalizedKeys = Lists.newArrayListWithExpectedSize(keys.size());
            if (DOMAIN_TAG.equalsIgnoreCase(addressPart)) {
                for (String key : keys) {
                    normalizedKeys.add(key.toLowerCase());
                }
            } else {
                normalizedKeys = keys;
            }
            isMatched = match(comparator, matchType, operator, (String) headerValuesIter.next(), normalizedKeys, context);
        }
    }
    return isMatched;
}
Also used : ASCII_NUMERIC_COMPARATOR(com.zimbra.cs.filter.jsieve.ComparatorName.ASCII_NUMERIC_COMPARATOR) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) VALUE_TAG(com.zimbra.cs.filter.jsieve.MatchTypeTags.VALUE_TAG) Strings(com.google.common.base.Strings) ZimbraComparatorUtils(com.zimbra.cs.filter.ZimbraComparatorUtils) Lists(com.google.common.collect.Lists) FilterUtil(com.zimbra.cs.filter.FilterUtil) SieveException(org.apache.jsieve.exception.SieveException) MatchTypeTags(org.apache.jsieve.comparators.MatchTypeTags) DOMAIN_TAG(org.apache.jsieve.tests.AddressPartTags.DOMAIN_TAG) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) Iterator(java.util.Iterator) ASCII_CASEMAP_COMPARATOR(org.apache.jsieve.comparators.ComparatorNames.ASCII_CASEMAP_COMPARATOR) Arguments(org.apache.jsieve.Arguments) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) COUNT_TAG(com.zimbra.cs.filter.jsieve.MatchTypeTags.COUNT_TAG) MailAdapter(org.apache.jsieve.mail.MailAdapter) ALL_TAG(org.apache.jsieve.tests.AddressPartTags.ALL_TAG) Envelope(org.apache.jsieve.tests.optional.Envelope) List(java.util.List) SyntaxException(org.apache.jsieve.exception.SyntaxException) LOCALPART_TAG(org.apache.jsieve.tests.AddressPartTags.LOCALPART_TAG) SieveContext(org.apache.jsieve.SieveContext) StringUtil(com.zimbra.common.util.StringUtil) IS_TAG(org.apache.jsieve.comparators.MatchTypeTags.IS_TAG) ServiceException(com.zimbra.common.service.ServiceException) SyntaxException(org.apache.jsieve.exception.SyntaxException) Iterator(java.util.Iterator) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) List(java.util.List) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 14 with ZimbraMailAdapter

use of com.zimbra.cs.filter.ZimbraMailAdapter in project zm-mailbox by Zimbra.

the class Reject method executeBasic.

@Override
protected Object executeBasic(MailAdapter mail, Arguments arguments, Block block, SieveContext context) throws SieveException {
    if (!(mail instanceof ZimbraMailAdapter)) {
        return null;
    }
    ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
    Account account = null;
    try {
        account = mailAdapter.getMailbox().getAccount();
        if (account.isSieveRejectMailEnabled()) {
            mailAdapter.setDiscardActionPresent();
            return super.executeBasic(mail, arguments, block, context);
        } else {
            mail.addAction(new ActionKeep());
        }
    } catch (ServiceException e) {
        ZimbraLog.filter.warn("Exception in executing reject action", e);
    }
    return null;
}
Also used : Account(com.zimbra.cs.account.Account) ServiceException(com.zimbra.common.service.ServiceException) ActionKeep(org.apache.jsieve.mail.ActionKeep) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 15 with ZimbraMailAdapter

use of com.zimbra.cs.filter.ZimbraMailAdapter in project zm-mailbox by Zimbra.

the class SetVariable method executeBasic.

@Override
protected Object executeBasic(MailAdapter mail, Arguments arguments, Block block, SieveContext context) throws SieveException {
    if (!(mail instanceof ZimbraMailAdapter)) {
        return null;
    }
    ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
    this.validateArguments(arguments, context);
    Map<String, String> existingVars = mailAdapter.getVariables();
    List<String> matchedValues = mailAdapter.getMatchedValues();
    String[] operations = new String[OPERATIONS_IDX];
    String key = null;
    String value = null;
    int index = 0;
    for (Argument a : arguments.getArgumentList()) {
        if (a instanceof TagArgument) {
            TagArgument tag = (TagArgument) a;
            String tagValue = tag.getTag();
            if (isValidModifier(tagValue)) {
                operations[getIndex(tagValue)] = tagValue.toLowerCase();
            }
        } else {
            String argument = ((StringListArgument) a).getList().get(0);
            if (index == 0) {
                key = FilterUtil.handleQuotedAndEncodedVar(argument);
            } else {
                if (argument.contains("${")) {
                    value = FilterUtil.replaceVariables(mailAdapter, argument);
                } else {
                    value = argument;
                }
            }
            ++index;
        }
    }
    value = applyModifiers(value, operations);
    mailAdapter.addVariable(key, value);
    return null;
}
Also used : TagArgument(org.apache.jsieve.TagArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) TagArgument(org.apache.jsieve.TagArgument) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Aggregations

ZimbraMailAdapter (com.zimbra.cs.filter.ZimbraMailAdapter)29 DummyMailAdapter (com.zimbra.cs.filter.DummyMailAdapter)19 ServiceException (com.zimbra.common.service.ServiceException)9 ArrayList (java.util.ArrayList)8 MessagingException (javax.mail.MessagingException)8 Argument (org.apache.jsieve.Argument)8 StringListArgument (org.apache.jsieve.StringListArgument)8 TagArgument (org.apache.jsieve.TagArgument)8 SyntaxException (org.apache.jsieve.exception.SyntaxException)8 SieveException (org.apache.jsieve.exception.SieveException)5 Mailbox (com.zimbra.cs.mailbox.Mailbox)4 ParsedAddress (com.zimbra.cs.mime.ParsedAddress)4 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)4 InternetAddress (com.zimbra.common.mime.InternetAddress)3 Account (com.zimbra.cs.account.Account)3 ZimbraComparatorUtils (com.zimbra.cs.filter.ZimbraComparatorUtils)3 ZimbraSieveException (com.zimbra.cs.filter.ZimbraSieveException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Message (com.zimbra.cs.mailbox.Message)2 IOException (java.io.IOException)2