Search in sources :

Example 1 with ZimbraSieveException

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

the class ConversationTest 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;
    Mailbox mbox = adapter.getMailbox();
    List<Conversation> convs;
    try {
        convs = mbox.lookupConversation(adapter.getParsedMessage());
    } catch (ServiceException e) {
        throw new ZimbraSieveException(e);
    }
    if (convs.isEmpty()) {
        return false;
    }
    switch(where) {
        case STARTED:
            for (Conversation conv : convs) {
                if ((conv.getFlagBitmask() & Flag.BITMASK_FROM_ME) > 0) {
                    try {
                        List<Message> msgs = mbox.getMessagesByConversation(null, conv.getId(), SortBy.DATE_ASC, 1);
                        if (!msgs.isEmpty()) {
                            Message msg = msgs.get(0);
                            // oldest.
                            if ((msg.getFlagBitmask() & Flag.BITMASK_FROM_ME) > 0 && !ParsedMessage.isReply(msgs.get(0).getSubject())) {
                                return true;
                            }
                        }
                    } catch (ServiceException e) {
                        throw new ZimbraSieveException(e);
                    }
                }
            }
            break;
        case PARTICIPATED:
            for (Conversation conv : convs) {
                if ((conv.getFlagBitmask() & Flag.BITMASK_FROM_ME) > 0) {
                    return true;
                }
            }
            break;
        default:
            assert false : where;
    }
    return false;
}
Also used : ZimbraSieveException(com.zimbra.cs.filter.ZimbraSieveException) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) Conversation(com.zimbra.cs.mailbox.Conversation) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 2 with ZimbraSieveException

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

the class CurrentDayOfWeekTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
    // First argument MUST be a ":is" tag
    String comparator = null;
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof TagArgument)
            comparator = ((TagArgument) argument).getTag();
    }
    if (!":is".equals(comparator))
        throw new SyntaxException("Expecting \":is\"");
    // Second argument MUST be a list of day of week indices; 0=Sunday, 6=Saturday
    Set<Integer> daysToCheckAgainst = new HashSet<Integer>();
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            List<String> valList = ((StringListArgument) argument).getList();
            for (String val : valList) {
                int day;
                try {
                    day = Integer.valueOf(val);
                } catch (NumberFormatException e) {
                    throw new SyntaxException(e);
                }
                if (day < 0 || day > 6)
                    throw new SyntaxException("Expected values between 0 - 6");
                // In Java 1=Sunday, 7=Saturday
                daysToCheckAgainst.add(day + 1);
            }
        }
    }
    if (daysToCheckAgainst.isEmpty())
        throw new SyntaxException("Expecting at least one value");
    // There MUST NOT be any further arguments
    if (argumentsIter.hasNext())
        throw new SyntaxException("Found unexpected argument(s)");
    TimeZone accountTimeZone;
    try {
        accountTimeZone = Util.getAccountTimeZone(((ZimbraMailAdapter) mail).getMailbox().getAccount());
    } catch (ServiceException e) {
        throw new ZimbraSieveException(e);
    }
    Calendar rightNow = Calendar.getInstance(accountTimeZone);
    return daysToCheckAgainst.contains(rightNow.get(Calendar.DAY_OF_WEEK));
}
Also used : ZimbraSieveException(com.zimbra.cs.filter.ZimbraSieveException) TagArgument(org.apache.jsieve.TagArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) Calendar(java.util.Calendar) StringListArgument(org.apache.jsieve.StringListArgument) TimeZone(java.util.TimeZone) ServiceException(com.zimbra.common.service.ServiceException) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) HashSet(java.util.HashSet)

Example 3 with ZimbraSieveException

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

the class CurrentTimeTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
    // First argument MUST be a tag of ":before" or ":after"
    String comparator = null;
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof TagArgument) {
            String tag = ((TagArgument) argument).getTag();
            if (tag.equals(BEFORE) || tag.equals(AFTER))
                comparator = tag;
            else
                throw new SyntaxException("Found unexpected: \"" + tag + "\"");
        }
    }
    if (comparator == null)
        throw new SyntaxException("Expecting \"" + BEFORE + "\" or \"" + AFTER + "\"");
    // Second argument MUST be a time in "HHmm" format
    DateFormat timeFormat = new SimpleDateFormat("HHmm");
    TimeZone accountTimeZone;
    try {
        accountTimeZone = Util.getAccountTimeZone(((ZimbraMailAdapter) mail).getMailbox().getAccount());
    } catch (ServiceException e) {
        throw new ZimbraSieveException(e);
    }
    timeFormat.setTimeZone(accountTimeZone);
    Date timeArg = null;
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            List<String> valList = ((StringListArgument) argument).getList();
            if (valList.size() != 1)
                throw new SyntaxException("Expecting exactly one time value");
            String timeStr = valList.get(0);
            try {
                timeArg = timeFormat.parse(timeStr);
            } catch (ParseException e) {
                throw new SyntaxException(e);
            }
        }
    }
    if (timeArg == null)
        throw new SyntaxException("Expecting a time value");
    // There MUST NOT be any further arguments
    if (argumentsIter.hasNext())
        throw new SyntaxException("Found unexpected argument(s)");
    Calendar rightNow = Calendar.getInstance(accountTimeZone);
    Calendar timeToCompareWith = Calendar.getInstance(accountTimeZone);
    // set the time part
    timeToCompareWith.setTime(timeArg);
    // now set the right date
    timeToCompareWith.set(rightNow.get(Calendar.YEAR), rightNow.get(Calendar.MONTH), rightNow.get(Calendar.DAY_OF_MONTH));
    return BEFORE.equals(comparator) ? rightNow.getTime().before(timeToCompareWith.getTime()) : rightNow.getTime().after(timeToCompareWith.getTime());
}
Also used : ZimbraSieveException(com.zimbra.cs.filter.ZimbraSieveException) TagArgument(org.apache.jsieve.TagArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) Calendar(java.util.Calendar) StringListArgument(org.apache.jsieve.StringListArgument) Date(java.util.Date) TimeZone(java.util.TimeZone) ServiceException(com.zimbra.common.service.ServiceException) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Aggregations

ServiceException (com.zimbra.common.service.ServiceException)3 DummyMailAdapter (com.zimbra.cs.filter.DummyMailAdapter)3 ZimbraMailAdapter (com.zimbra.cs.filter.ZimbraMailAdapter)3 ZimbraSieveException (com.zimbra.cs.filter.ZimbraSieveException)3 Calendar (java.util.Calendar)2 TimeZone (java.util.TimeZone)2 Argument (org.apache.jsieve.Argument)2 StringListArgument (org.apache.jsieve.StringListArgument)2 TagArgument (org.apache.jsieve.TagArgument)2 SyntaxException (org.apache.jsieve.exception.SyntaxException)2 Conversation (com.zimbra.cs.mailbox.Conversation)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 Message (com.zimbra.cs.mailbox.Message)1 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)1 DateFormat (java.text.DateFormat)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1