1 36 package POPStats; 37 38 import java.io.IOException ; 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 helpMessage = 51 "Usage : POPStats pop3-server username password\n\n" 52 + "Example: POPStats pop3.mail.com myname mypassword\n\n"; 53 54 57 public static void main(String [] args) { 58 if( args.length > 3) { 59 System.out.println(helpMessage); 60 return; 61 } 62 63 String server = args[0]; 64 String username = args[1]; 65 String password = args[2]; 66 67 boolean sslSupported = false; 68 69 POP3Protocol protocol = new POP3Protocol(server, POP3Protocol.DEFAULT_PORT); 70 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 saslCapabilities = null ; 80 81 try { 82 String [] 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 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 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 e1) { 156 } catch (POP3Exception e1) { 157 } 158 } 159 160 System.exit(0); 161 } 162 163 } 164 | Popular Tags |