use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class UrlNamespace method getResourceFromMailItem.
/* Returns DavResource for the MailItem. */
public static DavResource getResourceFromMailItem(DavContext ctxt, MailItem item) throws DavException {
DavResource resource = null;
if (item == null) {
return resource;
}
MailItem.Type itemType = item.getType();
try {
MailItem.Type viewType;
switch(itemType) {
case MOUNTPOINT:
Mountpoint mp = (Mountpoint) item;
viewType = mp.getDefaultView();
// don't expose mounted calendars when using iCal style delegation model.
if (!ctxt.useIcalDelegation() && (viewType == MailItem.Type.APPOINTMENT || viewType == MailItem.Type.TASK)) {
resource = new RemoteCalendarCollection(ctxt, mp);
} else if (viewType == MailItem.Type.CONTACT) {
resource = new RemoteAddressbookCollection(ctxt, mp);
} else {
resource = new RemoteCollection(ctxt, mp);
}
break;
case FOLDER:
Folder f = (Folder) item;
viewType = f.getDefaultView();
if (f.getId() == Mailbox.ID_FOLDER_INBOX && DavResource.isSchedulingEnabled()) {
resource = new ScheduleInbox(ctxt, f);
} else if (f.getId() == Mailbox.ID_FOLDER_SENT && DavResource.isSchedulingEnabled()) {
resource = new ScheduleOutbox(ctxt, f);
} else if (viewType == MailItem.Type.APPOINTMENT || viewType == MailItem.Type.TASK) {
resource = getCalendarCollection(ctxt, f);
} else if (viewType == MailItem.Type.CONTACT) {
resource = new AddressbookCollection(ctxt, f);
} else {
resource = new Collection(ctxt, f);
}
break;
case DOCUMENT:
resource = new Notebook(ctxt, (Document) item);
break;
case APPOINTMENT:
case TASK:
resource = new CalendarObject.LocalCalendarObject(ctxt, (CalendarItem) item);
break;
case MESSAGE:
resource = getCalendarItemForMessage(ctxt, (Message) item);
break;
case CONTACT:
resource = new AddressObject(ctxt, (Contact) item);
break;
default:
break;
}
} catch (ServiceException e) {
ZimbraLog.dav.info("cannot create DavResource", e);
}
return resource;
}
use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class FilterUtil method addMessage.
/**
* Adds a message to the given folder. Handles both local folders and mountpoints.
* @return the id of the new message, or <tt>null</tt> if it was a duplicate
*/
public static ItemId addMessage(DeliveryContext context, Mailbox mbox, ParsedMessage pm, String recipient, String folderPath, boolean noICal, int flags, String[] tags, int convId, OperationContext octxt) throws ServiceException {
// Do initial lookup.
Pair<Folder, String> folderAndPath = mbox.getFolderByPathLongestMatch(octxt, Mailbox.ID_FOLDER_USER_ROOT, folderPath);
Folder folder = folderAndPath.getFirst();
String remainingPath = folderAndPath.getSecond();
ZimbraLog.filter.debug("Attempting to file to %s, remainingPath=%s.", folder, remainingPath);
if (folder instanceof Mountpoint) {
Mountpoint mountpoint = (Mountpoint) folder;
ZMailbox remoteMbox = getRemoteZMailbox(mbox, mountpoint);
// Look up remote folder.
String remoteAccountId = mountpoint.getOwnerId();
ItemId id = mountpoint.getTarget();
ZFolder remoteFolder = remoteMbox.getFolderById(id.toString());
if (remoteFolder != null) {
if (remainingPath != null) {
remoteFolder = remoteFolder.getSubFolderByPath(remainingPath);
if (remoteFolder == null) {
String msg = String.format("Subfolder %s of mountpoint %s does not exist.", remainingPath, mountpoint.getName());
throw ServiceException.FAILURE(msg, null);
}
}
}
// File to remote folder.
if (remoteFolder != null) {
byte[] content;
try {
content = pm.getRawData();
} catch (Exception e) {
throw ServiceException.FAILURE("Unable to get message content", e);
}
String msgId = remoteMbox.addMessage(remoteFolder.getId(), com.zimbra.cs.mailbox.Flag.toString(flags), null, 0, content, false);
return new ItemId(msgId, remoteAccountId);
} else {
String msg = String.format("Unable to find remote folder %s for mountpoint %s.", remainingPath, mountpoint.getName());
throw ServiceException.FAILURE(msg, null);
}
} else {
if (!StringUtil.isNullOrEmpty(remainingPath)) {
// Only part of the folder path matched. Auto-create the remaining path.
ZimbraLog.filter.info("Could not find folder %s. Automatically creating it.", folderPath);
folder = mbox.createFolder(octxt, folderPath, new Folder.FolderOptions().setDefaultView(MailItem.Type.MESSAGE));
}
try {
DeliveryOptions dopt = new DeliveryOptions().setFolderId(folder).setNoICal(noICal);
dopt.setFlags(flags).setTags(tags).setConversationId(convId).setRecipientEmail(recipient);
Message msg = mbox.addMessage(octxt, pm, dopt, context);
if (msg == null) {
return null;
} else {
return new ItemId(msg);
}
} catch (IOException e) {
throw ServiceException.FAILURE("Unable to add message", e);
}
}
}
use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class RuleManager method applyRulesToOutgoingMessage.
public static List<ItemId> applyRulesToOutgoingMessage(OperationContext octxt, Mailbox mailbox, ParsedMessage pm, int sentFolderId, boolean noICal, int flags, String[] tags, int convId) throws ServiceException {
List<ItemId> addedMessageIds = null;
OutgoingMessageHandler handler = new OutgoingMessageHandler(mailbox, pm, sentFolderId, noICal, flags, tags, convId, octxt);
ZimbraMailAdapter mailAdapter = new ZimbraMailAdapter(mailbox, handler);
String[] filters = { ADMIN_OUTGOING_FILTER_RULES_BEFORE_CACHE_KEY, OUTGOING_FILTER_RULES_CACHE_KEY, ADMIN_OUTGOING_FILTER_RULES_AFTER_CACHE_KEY };
try {
Account account = mailbox.getAccount();
for (String filter : filters) {
Node node = getRulesNode(account, filter);
if (null != node) {
if (filter.equals(OUTGOING_FILTER_RULES_CACHE_KEY)) {
mailAdapter.setUserScriptExecuting(true);
}
boolean proceed = evaluateScript(mailAdapter, node);
if (!proceed) {
continue;
}
if (mailAdapter.isStop()) {
break;
}
mailAdapter.resetValues();
mailAdapter.resetCapabilities();
}
}
mailAdapter.executeAllActions();
// multiple fileinto may result in multiple copies of the messages in different folders
addedMessageIds = mailAdapter.getAddedMessageIds();
} catch (Exception e) {
ZimbraLog.filter.warn("An error occurred while processing filter rules. Filing message to %s.", handler.getDefaultFolderPath(), e);
} catch (TokenMgrError e) {
ZimbraLog.filter.warn("An error occurred while processing filter rules. Filing message to %s.", handler.getDefaultFolderPath(), e);
}
if (addedMessageIds == null) {
// Filter rules were not processed. File to the default folder.
Message msg = mailAdapter.keep(KeepType.IMPLICIT_KEEP);
addedMessageIds = new ArrayList<ItemId>(1);
addedMessageIds.add(new ItemId(msg));
}
return addedMessageIds;
}
use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class RuleManager method applyRulesToExistingMessage.
public static boolean applyRulesToExistingMessage(OperationContext octxt, Mailbox mbox, int messageId, Node node) throws ServiceException {
Message msg = mbox.getMessageById(octxt, messageId);
ExistingMessageHandler handler = new ExistingMessageHandler(octxt, mbox, messageId, (int) msg.getSize());
ZimbraMailAdapter mailAdapter = new ZimbraMailAdapter(mbox, handler);
try {
SIEVE_FACTORY.evaluate(mailAdapter, node);
mailAdapter.executeAllActions();
} catch (SieveException e) {
throw ServiceException.FAILURE("Unable to evaluate script", e);
}
return handler.filtered();
}
use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class RuleManager method applyRulesToIncomingMessage.
/**
* Adds a message to a mailbox. If filter rules exist, processes
* the filter rules. Otherwise, files to <tt>Inbox</tt> or <tt>Spam</tt>.
*
* @param allowFilterToMountpoint if <tt>false</tt>, rules
* @return the list of message id's that were added, or an empty list.
*/
public static List<ItemId> applyRulesToIncomingMessage(OperationContext octxt, Mailbox mailbox, ParsedMessage pm, int size, String recipient, LmtpEnvelope env, DeliveryContext sharedDeliveryCtxt, int incomingFolderId, boolean noICal, boolean allowFilterToMountpoint) throws ServiceException {
List<ItemId> addedMessageIds = null;
IncomingMessageHandler handler = new IncomingMessageHandler(octxt, sharedDeliveryCtxt, mailbox, recipient, pm, size, incomingFolderId, noICal);
ZimbraMailAdapter mailAdapter = new ZimbraMailAdapter(mailbox, handler);
mailAdapter.setEnvelope(env);
mailAdapter.setAllowFilterToMountpoint(allowFilterToMountpoint);
String[] filters = { ADMIN_FILTER_RULES_BEFORE_CACHE_KEY, FILTER_RULES_CACHE_KEY, ADMIN_FILTER_RULES_AFTER_CACHE_KEY };
try {
boolean applyRules = true;
Account account = mailbox.getAccount();
for (String filter : filters) {
// Determine whether to apply rules
Node node = getRulesNode(account, filter);
if (null == node) {
applyRules = false;
}
if (SpamHandler.isSpam(handler.getMimeMessage()) && !account.getBooleanAttr(Provisioning.A_zimbraSpamApplyUserFilters, false)) {
// Don't apply user filters to spam by default
applyRules = false;
break;
}
if (applyRules) {
if (filter.equals(FILTER_RULES_CACHE_KEY)) {
mailAdapter.setUserScriptExecuting(true);
}
boolean proceed = evaluateScript(mailAdapter, node);
if (!proceed) {
continue;
}
if (mailAdapter.isStop()) {
break;
}
mailAdapter.resetValues();
mailAdapter.resetCapabilities();
}
}
if (applyRules) {
mailAdapter.executeAllActions();
addedMessageIds = mailAdapter.getAddedMessageIds();
}
} catch (ErejectException ex) {
throw DeliveryServiceException.DELIVERY_REJECTED(ex.getMessage(), ex);
} catch (Exception e) {
ZimbraLog.filter.warn("An error occurred while processing filter rules. Filing message to %s.", handler.getDefaultFolderPath(), e);
} catch (TokenMgrError e) {
// Workaround for bug 19576. Certain parse errors can cause JavaCC to throw an Error
// instead of an Exception. Woo.
ZimbraLog.filter.warn("An error occurred while processing filter rules. Filing message to %s.", handler.getDefaultFolderPath(), e);
}
if (addedMessageIds == null) {
// Filter rules were not processed. File to the default folder.
Message msg = mailAdapter.keep(KeepType.IMPLICIT_KEEP);
addedMessageIds = new ArrayList<ItemId>(1);
addedMessageIds.add(new ItemId(msg));
}
return addedMessageIds;
}
Aggregations