KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > POPStats


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package POPStats;
37
38 import java.io.IOException JavaDoc;
39
40 import org.columba.ristretto.auth.AuthenticationException;
41 import org.columba.ristretto.auth.AuthenticationFactory;
42 import org.columba.ristretto.auth.NoSuchAuthenticationException;
43 import org.columba.ristretto.log.RistrettoLogger;
44 import org.columba.ristretto.pop3.POP3Exception;
45 import org.columba.ristretto.pop3.POP3Protocol;
46 import org.columba.ristretto.pop3.ScanListEntry;
47
48 public class POPStats {
49
50     private static final String JavaDoc helpMessage =
51         "Usage : POPStats pop3-server username password\n\n"
52             + "Example: POPStats pop3.mail.com myname mypassword\n\n";
53
54     /**
55      * @param args
56      */

57     public static void main(String JavaDoc[] args) {
58         if( args.length > 3) {
59             System.out.println(helpMessage);
60             return;
61         }
62
63         String JavaDoc server = args[0];
64         String JavaDoc username = args[1];
65         String JavaDoc password = args[2];
66         
67         boolean sslSupported = false;
68         
69         POP3Protocol protocol = new POP3Protocol(server, POP3Protocol.DEFAULT_PORT);
70         //RistrettoLogger.setLogStream(System.out);
71

72         try {
73             System.out.println("Connecting to " + server + " ...");
74             protocol.openPort();
75             
76             if( protocol.isApopSupported() ) {
77                 System.out.println("APOP authentication supported");
78             }
79             String JavaDoc saslCapabilities = null ;
80             
81             try {
82             String JavaDoc[] capabilities = protocol.capa();
83             for( int i=0; i<capabilities.length && saslCapabilities == null; i++) {
84                 if( capabilities[i].toLowerCase().startsWith("sasl") ) {
85                     saslCapabilities = capabilities[i];
86                     System.out.println("SASL authentication method(s) supported: " + saslCapabilities);
87                 }
88                 
89                 if( capabilities[i].toLowerCase().startsWith("stls") ) {
90                     System.out.println("SSL supported");
91                     sslSupported = true;
92                 }
93                 
94             } } catch (POP3Exception e1) {
95                 System.out.println("CAPA command not supported");
96             }
97
98             if( sslSupported ) {
99                 System.out.println("Establishing secure SSL connection...");
100                 protocol.startTLS();
101             }
102             
103             boolean authenticated = false;
104             
105             try {
106                 if( protocol.isApopSupported() ) {
107                     System.out.println("Authenticating with APOP...");
108                     protocol.apop(username, password.toCharArray());
109                 } else if ( saslCapabilities != null ) {
110                     try {
111                         String JavaDoc authMethod = AuthenticationFactory.getInstance().getSecurestMethod(saslCapabilities);
112                         System.out.println("Authenticating with SASL "+ authMethod +"...");
113                         protocol.auth(authMethod, username, password.toCharArray());
114                         
115                     } catch (NoSuchAuthenticationException e) {
116                         System.err.println( "No Authentication Method available: "+ e.getLocalizedMessage());
117                         protocol.quit();
118                         return;
119                     }
120                 }
121             } catch (AuthenticationException e) {
122                 System.err.println( "Authentication error: "+ e.getLocalizedMessage());
123                 System.err.println( "Fallback to USER/PASS method");
124             } catch (POP3Exception e1) {
125                 System.err.println( "Authentication error: "+ e1.getMessage());
126                 System.err.println( "Fallback to USER/PASS method");
127             }
128             
129             if( protocol.getState() != POP3Protocol.TRANSACTION) {
130                 protocol.userPass(username, password.toCharArray());
131             }
132             
133             System.out.println("Getting mailbox statisics...");
134             ScanListEntry[] list = protocol.list();
135             System.out.println(list.length + " messages on server");
136             int maxSize = -1;
137             int sumSize = 0;
138             for( int i=0; i<list.length; i++) {
139                 sumSize += list[i].getSize();
140                 maxSize = Math.max(maxSize, list[i].getSize());
141             }
142             System.out.println("Total mailbox size: " + (sumSize / 1024) + " kB");
143             System.out.println("Mean message size: " + (sumSize / (1024 * list.length)) + " kB");
144             System.out.println("Largest message size: " + (maxSize / 1024) + " kB");
145             
146             protocol.quit();
147             
148         } catch (IOException JavaDoc e) {
149             System.err.println("IO Error: " + e.getLocalizedMessage());
150             
151         } catch (POP3Exception e) {
152             System.err.println("POP3 Error: " + e.getMessage());
153             try {
154                 protocol.quit();
155             } catch (IOException JavaDoc e1) {
156             } catch (POP3Exception e1) {
157             }
158         }
159         
160         System.exit(0);
161     }
162
163 }
164
Popular Tags