001: package org.claros.intouch.webmail.controllers;
002:
003: import javax.mail.Message;
004:
005: import org.claros.commons.auth.models.AuthProfile;
006: import org.claros.commons.configuration.PropertyFile;
007: import org.claros.commons.mail.models.ConnectionMetaHandler;
008: import org.claros.commons.mail.models.ConnectionProfile;
009: import org.claros.intouch.webmail.factory.FolderControllerFactory;
010: import org.claros.intouch.webmail.models.FolderDbObject;
011:
012: /**
013: * @author Umut Gokbayrak
014: */
015: public class InboxControllerBase {
016: protected ConnectionProfile profile;
017: protected AuthProfile auth;
018: protected ConnectionMetaHandler handler = null;
019:
020: /**
021: *
022: * @param profile
023: * @param auth
024: * @param handler
025: */
026: public InboxControllerBase(AuthProfile auth,
027: ConnectionProfile profile, ConnectionMetaHandler handler) {
028: this .profile = profile;
029: this .auth = auth;
030: this .handler = handler;
031: }
032:
033: /**
034: * @param auth
035: * @param msg
036: * @return
037: */
038: public String findDestinationFolderId(Message msg) throws Exception {
039: String strSpamCheck = PropertyFile.getConfiguration(
040: "/config/config.xml").getString(
041: "common-params.spam-check-enabled");
042: boolean spamEnabled = false;
043: try {
044: spamEnabled = Boolean.valueOf(strSpamCheck).booleanValue();
045: } catch (Exception e) {
046: }
047:
048: boolean isSpam = false;
049: if (spamEnabled) {
050: isSpam = SpamController.isSpam(auth, msg);
051: }
052: FolderControllerFactory factory = new FolderControllerFactory(
053: auth, profile, handler);
054: FolderController fc = factory.getFolderController();
055: String folderId = null;
056: FolderDbObject folder = null;
057:
058: if (isSpam) {
059: folder = fc.getJunkFolder();
060: if (profile.getProtocol().equals(
061: org.claros.commons.mail.utility.Constants.POP3)) {
062: folderId = folder.getId().toString();
063: } else {
064: folderId = folder.getFolderName();
065: }
066: } else {
067: folder = fc.getInboxFolder();
068: if (profile.getProtocol().equals(
069: org.claros.commons.mail.utility.Constants.POP3)) {
070: folderId = folder.getId().toString();
071: } else {
072: folderId = folder.getFolderName();
073: }
074: }
075: return folderId;
076:
077: /*
078: // search if any user specified filter matches the content
079: List filters = FilterController.getFilters(auth);
080: if (filters != null) {
081: Filter filter = null;
082: for (int i=0; i<filters.size(); i++) {
083: filter = (Filter)filters.get(i);
084: String fPort = filter.getPortion();
085: String mPort = getPortion(msg, fPort).toUpperCase();
086: String fCond = filter.getCondition();
087: String fKeyWord = filter.getKeyword().toUpperCase();
088: if (fCond.equals(Constants.CONDITION_CONTAINS)) {
089: if (mPort.indexOf(fKeyWord) >= 0) {
090: if (filter.getAction().equals(Constants.ACTION_DELETE)) {
091: return null;
092: } else {
093: return filter.getDestination();
094: }
095: }
096: } else if (fCond.equals(Constants.CONDITION_EQUALS)) {
097: if (mPort.equals(fKeyWord)) {
098: if (filter.getAction().equals(Constants.ACTION_DELETE)) {
099: return null;
100: } else {
101: return filter.getDestination();
102: }
103: }
104: } if (fCond.equals(Constants.CONDITION_NOT_CONTAINS)) {
105: if (mPort.indexOf(fKeyWord) < 0) {
106: if (filter.getAction().equals(Constants.ACTION_DELETE)) {
107: return null;
108: } else {
109: return filter.getDestination();
110: }
111: }
112: }
113: }
114: }
115:
116: // if at the preferences page, user clicked not to perform a spam
117: // check on the contacts in address book.
118: String safe = UserPrefsController.getUserSetting(auth, UserPrefConstants.safeContacts);
119: if (safe == null) {
120: safe = "no";
121: }
122: boolean isSafe = (safe.equals("yes") ? true : false);
123: boolean contactIsSafe = false;
124: if (isSafe) {
125: // check to see if the user exists in the address book
126: Address[] adrs = msg.getFrom();
127: if (adrs != null && adrs.length == 1) {
128: Address adr = adrs[0];
129: if (adr instanceof InternetAddress) {
130: InternetAddress iAdr = (InternetAddress)adr;
131: Contact tmp = ContactsController.searchContactByEmail(auth.getUsername(), iAdr.getAddress());
132: if (tmp != null) {
133: contactIsSafe = true;
134: }
135: }
136: }
137: }
138:
139: FolderControllerFactory factory = new FolderControllerFactory(auth, profile, handler);
140: FolderController fc = factory.getFolderController();
141:
142: String folderId = null;
143: FolderDbItem folder = null;
144: if (!contactIsSafe) {
145: // none of the user specified filters matched. Do spam filtering.
146: // if enabled do spam analysis and probability is higher than
147: // user accepts, move it to junk mail folder.
148: boolean isSpam = false;
149: try {
150: isSpam = SpamController.isSpam(auth, msg);
151: } catch (Exception e) {
152: // if spam controller can no parse the message it is
153: // probable that it is an illegal format. Treat as spam.
154: isSpam = true;
155: }
156: if (isSpam) {
157: folder = fc.getJunkFolder();
158: if (profile.getProtocol().equals(org.claros.commons.mail.utility.Constants.POP3)) {
159: folderId = folder.getId().toString();
160: } else {
161: folderId = folder.getFolderName();
162: }
163: } else {
164: folder = fc.getInboxFolder();
165: if (profile.getProtocol().equals(org.claros.commons.mail.utility.Constants.POP3)) {
166: folderId = folder.getId().toString();
167: } else {
168: folderId = folder.getFolderName();
169: }
170: }
171: } else {
172: // if the contact is safe deliver mail to INBOX
173: folder = fc.getInboxFolder();
174: if (profile.getProtocol().equals(org.claros.commons.mail.utility.Constants.POP3)) {
175: folderId = folder.getId().toString();
176: } else {
177: folderId = org.claros.commons.mail.utility.Constants.FOLDER_INBOX;
178: }
179: }
180: return folderId;
181: */
182: }
183:
184: /**
185: * @param msg
186: * @param fPort
187: * @return
188: */
189: /*
190: private String getPortion(Message msg, String fPort) throws MessagingException {
191: if (fPort.equals(Constants.PORTION_SUBJECT)) {
192: return msg.getSubject();
193: } else if (fPort.equals(Constants.PORTION_FROM)) {
194: return Utility.addressArrToString(msg.getFrom());
195: } else if (fPort.equals(Constants.PORTION_TO)) {
196: return Utility.addressArrToString(msg.getRecipients(Message.RecipientType.TO));
197: } else if (fPort.equals(Constants.PORTION_CC)) {
198: return Utility.addressArrToString(msg.getRecipients(Message.RecipientType.CC));
199: } else if (fPort.equals(Constants.PORTION_MESSAGE_BODY)) {
200: Email email = MessageParser.parseMessage(msg);
201: return email.getBodyText();
202: }
203: return "";
204: }
205: */
206: }
|