1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.voicemail.impl.mail.store;
18 
19 import android.content.Context;
20 import android.net.Network;
21 import com.android.voicemail.impl.imap.ImapHelper;
22 import com.android.voicemail.impl.mail.MailTransport;
23 import com.android.voicemail.impl.mail.Message;
24 import com.android.voicemail.impl.mail.MessagingException;
25 import com.android.voicemail.impl.mail.internet.MimeMessage;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import org.apache.james.mime4j.MimeException;
29 
30 public class ImapStore {
31   /**
32    * A global suggestion to Store implementors on how much of the body should be returned on
33    * FetchProfile.Item.BODY_TRUNCATED requests. We'll use 125k now.
34    */
35   public static final int FETCH_BODY_TRUNCATED_SUGGESTED_SIZE = (125 * 1024);
36 
37   private final Context context;
38   private final ImapHelper helper;
39   private final String username;
40   private final String password;
41   private final MailTransport transport;
42   private ImapConnection connection;
43 
44   public static final int FLAG_NONE = 0x00; // No flags
45   public static final int FLAG_SSL = 0x01; // Use SSL
46   public static final int FLAG_TLS = 0x02; // Use TLS
47   public static final int FLAG_AUTHENTICATE = 0x04; // Use name/password for authentication
48   public static final int FLAG_TRUST_ALL = 0x08; // Trust all certificates
49   public static final int FLAG_OAUTH = 0x10; // Use OAuth for authentication
50 
51   /** Contains all the information necessary to log into an imap server */
ImapStore( Context context, ImapHelper helper, String username, String password, int port, String serverName, int flags, Network network)52   public ImapStore(
53       Context context,
54       ImapHelper helper,
55       String username,
56       String password,
57       int port,
58       String serverName,
59       int flags,
60       Network network) {
61     this.context = context;
62     this.helper = helper;
63     this.username = username;
64     this.password = password;
65     transport = new MailTransport(context, this.getImapHelper(), network, serverName, port, flags);
66   }
67 
getContext()68   public Context getContext() {
69     return context;
70   }
71 
getImapHelper()72   public ImapHelper getImapHelper() {
73     return helper;
74   }
75 
getUsername()76   public String getUsername() {
77     return username;
78   }
79 
getPassword()80   public String getPassword() {
81     return password;
82   }
83 
84   /** Returns a clone of the transport associated with this store. */
cloneTransport()85   MailTransport cloneTransport() {
86     return transport.clone();
87   }
88 
89   /** Returns UIDs of Messages joined with "," as the separator. */
joinMessageUids(Message[] messages)90   static String joinMessageUids(Message[] messages) {
91     StringBuilder sb = new StringBuilder();
92     boolean notFirst = false;
93     for (Message m : messages) {
94       if (notFirst) {
95         sb.append(',');
96       }
97       sb.append(m.getUid());
98       notFirst = true;
99     }
100     return sb.toString();
101   }
102 
103   static class ImapMessage extends MimeMessage {
104     private ImapFolder folder;
105 
ImapMessage(String uid, ImapFolder folder)106     ImapMessage(String uid, ImapFolder folder) {
107       this.uid = uid;
108       this.folder = folder;
109     }
110 
setSize(int size)111     public void setSize(int size) {
112       this.size = size;
113     }
114 
115     @Override
parse(InputStream in)116     public void parse(InputStream in) throws IOException, MessagingException, MimeException {
117       super.parse(in);
118     }
119 
setFlagInternal(String flag, boolean set)120     public void setFlagInternal(String flag, boolean set) throws MessagingException {
121       super.setFlag(flag, set);
122     }
123 
124     @Override
setFlag(String flag, boolean set)125     public void setFlag(String flag, boolean set) throws MessagingException {
126       super.setFlag(flag, set);
127       folder.setFlags(new Message[] {this}, new String[] {flag}, set);
128     }
129   }
130 
131   static class ImapException extends MessagingException {
132     private static final long serialVersionUID = 1L;
133 
134     private final String status;
135     private final String statusMessage;
136     private final String alertText;
137     private final String responseCode;
138 
ImapException( String message, String status, String statusMessage, String alertText, String responseCode)139     public ImapException(
140         String message,
141         String status,
142         String statusMessage,
143         String alertText,
144         String responseCode) {
145       super(message);
146       this.status = status;
147       this.statusMessage = statusMessage;
148       this.alertText = alertText;
149       this.responseCode = responseCode;
150     }
151 
getStatus()152     public String getStatus() {
153       return status;
154     }
155 
getStatusMessage()156     public String getStatusMessage() {
157       return statusMessage;
158     }
159 
getAlertText()160     public String getAlertText() {
161       return alertText;
162     }
163 
getResponseCode()164     public String getResponseCode() {
165       return responseCode;
166     }
167   }
168 
closeConnection()169   public void closeConnection() {
170     if (connection != null) {
171       connection.close();
172       connection = null;
173     }
174   }
175 
getConnection()176   public ImapConnection getConnection() {
177     if (connection == null) {
178       connection = new ImapConnection(this);
179     }
180     return connection;
181   }
182 }
183