KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > model > accounts > MailAccount


1 package SnowMailClient.model.accounts;
2
3 import SnowMailClient.utils.*;
4 import SnowMailClient.view.*;
5 import SnowMailClient.view.dialogs.*;
6 import SnowMailClient.MailEngine.*;
7 import SnowMailClient.*;
8 import SnowMailClient.crypto.*;
9
10 import snow.utils.storage.*;
11 import snow.crypto.*;
12 import snow.concurrent.*;
13
14 import java.util.*;
15 import java.util.concurrent.*;
16 import java.net.*;
17
18
19 /** contains information about a mail account and his connection
20 */

21 public final class MailAccount implements Vectorizable {
22
23   private String JavaDoc address = "?";
24   private String JavaDoc name = "?";
25   private String JavaDoc smtp = "?";
26   private String JavaDoc pop = "?";
27   private String JavaDoc accountUserName = "?";
28   private String JavaDoc accountPassword = "?";
29   private boolean useToRead = true;
30   private boolean useToSend = true;
31   private int popPort = 110;
32   private int sslPopPort = 995;
33   private int sslSMTPPort = 21; // often or also 25
34
private int smtpPort = 25;
35   private boolean signOutgoingMails = false;
36   private boolean allowUnsecurePasswordProtocols = false; // As AUTH LOGIN of Yahoo [2005]
37

38
39   // Used for downloading messages. Used to parallelize downloads (one per account at a time)
40
public final TaskManager messagesDownloadExecutor = new TaskManager();
41
42
43   // used when sending accounts on the net to reduce data size
44
public boolean storeAccountLogInVectorRepresentation = false;
45
46
47   // log ALL traffic
48
private final AccountLog accountLog = new AccountLog();
49
50   private SecurePopConnection securePopConnection;
51   private SecureSmtpConnection secureSmtpConnection;
52
53
54   public MailAccount() { }
55
56
57   public final AccountLog getAccountLog() { return accountLog; }
58
59   public final String JavaDoc getAddress() { return address; }
60   public void setAddress(String JavaDoc a) { address = a; }
61
62   public final String JavaDoc getName() { return name; }
63   public final void setName(String JavaDoc a) { name = a; }
64
65   public final String JavaDoc getAccountUserName() { return accountUserName; }
66   public final void setAccountUserName(String JavaDoc a) { accountUserName = a; }
67
68   public final String JavaDoc getAccountPassword() { return accountPassword; }
69   public final void setAccountPassword(String JavaDoc a) { accountPassword = a; }
70
71   public final String JavaDoc getPop() { return pop; }
72   public final void setPop(String JavaDoc a) { pop = a; }
73
74   public final int getSSLPopPort() { return sslPopPort; }
75   public final int getSSLSMTPPort() { return sslSMTPPort;}
76
77   public final void setSSLPOPPort(int p) { sslPopPort = p; }
78   public final void setSSLSMTPPort(int p) { sslSMTPPort = p;}
79
80   public final int getPopPort() { return popPort; }
81   public final void setPopPort(int a) { popPort = a; }
82
83   public final String JavaDoc getSMTP() { return smtp; }
84   public final void setSMTP(String JavaDoc a) { smtp = a; }
85
86   public final int getSMTPPort() { return smtpPort; }
87   public final void setSMTPPort(int a) { smtpPort = a; }
88
89   public final boolean getUseToReadMails() { return useToRead; }
90   public final void setUseToReadMails(boolean doIt) { useToRead = doIt; }
91
92   public final boolean getUseToSendMails() { return useToSend; }
93   public final void setUseToSendMails(boolean doIt) { useToSend = doIt; }
94
95   public final boolean getAllowUnsecurePasswordProtocols() { return this.allowUnsecurePasswordProtocols; }
96   public final void setAllowUnsecurePasswordProtocols(boolean doIt) { this.allowUnsecurePasswordProtocols = doIt; }
97
98   /** @return true if this address is a snowraver account
99   */

100   public final boolean isSnowraverAccount()
101   {
102     return address.toLowerCase().endsWith("snowraver.org");
103    // || address.toLowerCase().endsWith("localhost"); // for tests
104
}
105
106
107    //
108
// RSA
109
//
110
private String JavaDoc rsan = "";
111    private String JavaDoc rsad = "";
112    private String JavaDoc rsae = "";
113    private int rsalength = 0;
114
115
116    public final String JavaDoc getRSA_n() { return rsan; }
117    public final void setRSA_n(String JavaDoc n) { rsan=n; }
118    public final String JavaDoc getRSA_e() { return rsae; }
119    public final void setRSA_e(String JavaDoc e) { rsae=e; }
120    public final String JavaDoc getRSA_d() { return rsad; }
121    public final void setRSA_d(String JavaDoc d) { rsad=d; }
122    public final int getRSA_Length() { return rsalength; }
123    public final void setRSA_Length(int l) { rsalength=l; }
124
125    /** secure close.
126       Important Should be called at the end in order to delete the messages marked as delete.
127    */

128    public final synchronized void closePOPConnection()
129    {
130      try
131      {
132        if( securePopConnection!=null && securePopConnection.isAlive())
133        {
134          securePopConnection.terminateSession();
135        }
136      }
137      catch(Exception JavaDoc e)
138      {
139         e.printStackTrace();
140      }
141    }
142
143    public final String JavaDoc toString() { return "Mail account "+this.getAddress(); }
144
145    /** manages a POP connection.
146       Caution: this looks if the connection is alive, sending a NOOP
147         => retrieve this once
148    */

149    public final synchronized SecurePopConnection getCheckedPopConnection() throws Exception JavaDoc
150    {
151        if( securePopConnection!=null && securePopConnection.isAlive())
152        {
153            return securePopConnection;
154        }
155        else
156        {
157            try
158            {
159               securePopConnection = new SecurePopConnection(this);
160               return securePopConnection;
161            }
162            catch(BadPasswordException e)
163            {
164               throw new Exception JavaDoc("Bad Password for "+this.address+"\n Error="+e.getMessage());
165            }
166        }
167    }
168
169
170    /** manages a secure SMTP connection
171     */

172    public final synchronized SecureSmtpConnection getSMTPConnection() throws Exception JavaDoc
173    {
174        if( secureSmtpConnection!=null && secureSmtpConnection.isAlive())
175        {
176            return secureSmtpConnection;
177        }
178        else
179        {
180            // host name required
181
String JavaDoc hname = "?";
182            try
183            {
184               InetAddress local = InetAddress.getLocalHost();
185               hname = local.getHostName();
186            }
187            catch(Exception JavaDoc ignored){}
188
189            try
190            {
191               secureSmtpConnection = new SecureSmtpConnection(this,
192                 hname,
193                 false
194               );
195               return secureSmtpConnection;
196            }
197            catch(BadPasswordException ee)
198            {
199               throw new Exception JavaDoc("Bad Password for "+this.address
200                  +"\n Error="+ee.getMessage());
201            }
202        }
203    }
204
205
206   /** load from a vector
207    */

208 @SuppressWarnings JavaDoc("unchecked")
209   public final void createFromVectorRepresentation(Vector<Object JavaDoc> v) throws VectorizeException
210   {
211      int ver = (Integer JavaDoc) v.get(0);
212
213      if(ver==6)
214      {
215          address = (String JavaDoc) v.get(1);
216          name = (String JavaDoc) v.get(2);
217          pop = (String JavaDoc) v.get(3);
218          smtp = (String JavaDoc) v.get(4);
219          accountUserName = (String JavaDoc) v.get(5);
220          accountPassword = (String JavaDoc) v.get(6);
221          useToRead = (Boolean JavaDoc) v.get(7);
222          useToSend = (Boolean JavaDoc) v.get(8);
223          popPort = (Integer JavaDoc) v.get(9);
224          smtpPort = (Integer JavaDoc) v.get(10);
225          rsan = (String JavaDoc) v.get(11);
226          rsad = (String JavaDoc) v.get(12);
227          rsae = (String JavaDoc) v.get(13);
228          rsalength = (Integer JavaDoc) v.get(14);
229
230          accountLog.createFromVectorRepresentation((Vector<Object JavaDoc>) v.get(15));
231          this.sslPopPort = (Integer JavaDoc) v.get(16);
232          this.sslSMTPPort = (Integer JavaDoc) v.get(17);
233          useSSL_POP = (Boolean JavaDoc) v.get(18);
234          useSSL_SMTP = (Boolean JavaDoc) v.get(19);
235          if(v.size()>20)
236          {
237            signOutgoingMails = (Boolean JavaDoc) v.get(20);
238          }
239          if(v.size()>21)
240          {
241            this.allowUnsecurePasswordProtocols = (Boolean JavaDoc) v.get(21);
242          }
243
244      }
245   }
246
247
248   private boolean useSSL_POP = false;
249   private boolean useSSL_SMTP = false;
250
251   public final boolean useSSLPOP() { return useSSL_POP;}
252   public final boolean useSSLSMTP() { return useSSL_SMTP;}
253
254   public final void setUseSSLPOP(boolean b) { useSSL_POP = b; }
255   public final void setUseSSLSMTP(boolean b) { useSSL_SMTP = b; }
256
257
258   // save in a vector
259
//
260
public final Vector<Object JavaDoc> getVectorRepresentation() throws VectorizeException
261   {
262     Vector<Object JavaDoc> v = new Vector<Object JavaDoc>();
263     v.addElement(6); // version
264
v.addElement(address);
265     v.addElement(name);
266     v.addElement(pop);
267     v.addElement(smtp);
268     v.addElement(accountUserName);
269     v.addElement(accountPassword);
270     v.addElement(useToRead);
271     v.addElement(useToSend);
272     v.addElement(popPort);
273     v.addElement(smtpPort);
274     v.addElement(rsan);
275     v.addElement(rsad);
276     v.addElement(rsae);
277     v.addElement(rsalength);
278     if(storeAccountLogInVectorRepresentation==false)
279     {
280        accountLog.maxLength = 5000;
281        accountLog.checkLength();
282     }
283     v.addElement(accountLog.getVectorRepresentation()); // 15
284

285     v.addElement(this.sslPopPort);
286     v.addElement(this.sslSMTPPort);
287     v.addElement(this.useSSL_POP);
288     v.addElement(this.useSSL_SMTP);
289     v.addElement(signOutgoingMails); // 20
290
v.addElement(allowUnsecurePasswordProtocols);
291     return v;
292   }
293 }
Popular Tags