Search in sources :

Example 1 with DummyMailAdapter

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

the class AddressBookTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    assert (headers != null);
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    Mailbox mbox = ((ZimbraMailAdapter) mail).getMailbox();
    List<InternetAddress> addrs = new ArrayList<InternetAddress>();
    for (String header : headers) {
        for (String value : mail.getHeader(header)) {
            addrs.add(new InternetAddress(value));
        }
    }
    try {
        return mbox.index.existsInContacts(addrs);
    } catch (IOException e) {
        ZimbraLog.filter.error("Failed to lookup contacts", e);
    }
    return false;
}
Also used : InternetAddress(com.zimbra.common.mime.InternetAddress) Mailbox(com.zimbra.cs.mailbox.Mailbox) ArrayList(java.util.ArrayList) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) IOException(java.io.IOException) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 2 with DummyMailAdapter

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

the class ContactRankingTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments args, SieveContext ctx) throws SieveException {
    assert (headers != null);
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    Mailbox mbox = ((ZimbraMailAdapter) mail).getMailbox();
    List<InternetAddress> addrs = new ArrayList<InternetAddress>();
    for (String header : headers) {
        for (String value : mail.getHeader(header)) {
            addrs.add(new InternetAddress(value));
        }
    }
    try {
        ContactRankings ranking = new ContactRankings(mbox.getAccountId());
        for (InternetAddress addr : addrs) {
            if (ranking.query(addr.getAddress()) > 0) {
                return true;
            }
        }
    } catch (ServiceException e) {
        ZimbraLog.filter.error("Failed to lookup ranking", e);
    }
    return false;
}
Also used : InternetAddress(com.zimbra.common.mime.InternetAddress) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) ContactRankings(com.zimbra.cs.mailbox.ContactRankings) ArrayList(java.util.ArrayList) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 3 with DummyMailAdapter

use of com.zimbra.cs.filter.DummyMailAdapter 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 4 with DummyMailAdapter

use of com.zimbra.cs.filter.DummyMailAdapter 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 5 with DummyMailAdapter

use of com.zimbra.cs.filter.DummyMailAdapter 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

DummyMailAdapter (com.zimbra.cs.filter.DummyMailAdapter)19 ZimbraMailAdapter (com.zimbra.cs.filter.ZimbraMailAdapter)19 ServiceException (com.zimbra.common.service.ServiceException)6 MessagingException (javax.mail.MessagingException)5 Mailbox (com.zimbra.cs.mailbox.Mailbox)4 ParsedAddress (com.zimbra.cs.mime.ParsedAddress)4 ArrayList (java.util.ArrayList)4 Argument (org.apache.jsieve.Argument)4 StringListArgument (org.apache.jsieve.StringListArgument)4 TagArgument (org.apache.jsieve.TagArgument)4 SyntaxException (org.apache.jsieve.exception.SyntaxException)4 InternetAddress (com.zimbra.common.mime.InternetAddress)3 ZimbraComparatorUtils (com.zimbra.cs.filter.ZimbraComparatorUtils)3 ZimbraSieveException (com.zimbra.cs.filter.ZimbraSieveException)3 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)3 SieveException (org.apache.jsieve.exception.SieveException)3 Message (com.zimbra.cs.mailbox.Message)2 ParseException (java.text.ParseException)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2