html2pop3.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
|
![]() |
![]() |
HTML2POP3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* HTML2POP3 server
*
* Copyright 2004 Matteo Baccan
* www - http://www.baccan.it
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA (or visit
* their web site at http://www.gnu.org/).
*
*/
/**
* Title: html2pop3 Server
* Description: Server POP3
* Copyright: Copyright (c) 2004
* Company:
* @author Matteo Baccan
* @version 1.0
*/
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.*;
import org.html2pop3.utils.*;
import org.html2pop3.utils.message.*;
import org.html2pop3.plugin.pop3.*;
import org.html2pop3.plugin.nntp.*;
import org.html2pop3.plugin.smtp.*;
public class html2pop3 extends Thread {
static public void main( String args[] ){
logStreamMemo psm = new logStreamMemo( System.out, "html2pop3.log" );
System.setOut( psm );
System.setErr( psm );
// Imposto l'eventuale config
html2pop3.parseCommandLine( args );
// Partenza
html2pop3 html2pop3 = new html2pop3();
html2pop3.start();
}
private static String cConfig = "config.cfg";
static public String getConfig(){
return cConfig;
}
static public void setConfig(String c){
cConfig = c;
}
static public void parseCommandLine( String args[] ){
for( int nPar=0; nPar<args.length; nPar++ ){
if( args[nPar].equalsIgnoreCase("-config") && nPar+1<args.length )
html2pop3.setConfig( args[nPar+1] );
//if( args[nPar].equalsIgnoreCase("-?") )
// TODO
}
}
class SortedProperties extends Properties {
public synchronized Enumeration keys() {
Enumeration keysEnum = super.keys();
Vector keyList = new Vector();
while(keysEnum.hasMoreElements()){
keyList.addElement(keysEnum.nextElement());
}
boolean bSort;
do {
bSort = true;
for( int nPos=0; nPos<keyList.size()-1; nPos++ ){
Object a = keyList.elementAt( nPos );
Object b = keyList.elementAt( nPos+1 );
Collator c = Collator.getInstance();
if( c.compare( (String)a, (String)b) > 0 ){
keyList.setElementAt( a, nPos+1 );
keyList.setElementAt( b, nPos );
bSort = false;
}
}
} while( !bSort );
// Not 1.1.4 compatibile
//Collections.sort(keyList);
return keyList.elements();
}
}
//private Properties p = new Properties();
private SortedProperties p = new SortedProperties();
private String cLocalHost = "localhost";
private int cLocalPort = 110;
private int cLocalPortSMTP = 25;
private int cLocalPortNNTP = 119;
private int nClient = 10;
private boolean bDelete = true;
private boolean bDeleteOptimized = true;
private boolean bGuiError = true;
private boolean bLifo = true;
private boolean bOutlook2002Timeout = true;
private int nMaxEmail = -1;
private boolean bIsWin32 = true;
public html2pop3(){
// Imposta il flag win32, con JDK MS non viene chiamato il metodo
setNonWin32();
// Load configuration file
load();
}
/** @conditional (JVM14) */
private void setNonWin32() {
bIsWin32 = false;
}
public boolean getIsWin32(){
return bIsWin32;
}
public boolean getOutlook2002Timeout(){
return bOutlook2002Timeout;
}
public String getHost(){
return cLocalHost;
}
public void setHost( String c){
cLocalHost = c;
}
public int getPort(){
return cLocalPort;
}
public void setPort( int p ){
cLocalPort = p;
}
public int getPortSMTP(){
return cLocalPortSMTP;
}
public void setPortSMTP( int p ){
cLocalPortSMTP = p;
}
public int getPortNNTP(){
return cLocalPortNNTP;
}
public void setPortNNTP( int p ){
cLocalPortNNTP = p;
}
public int getMaxEmail(){
return nMaxEmail;
}
public void setMaxEmail( int p ){
nMaxEmail = p;
}
public int getClient(){
return nClient;
}
public void setClient( int p ){
nClient = p;
}
public void setDelete( boolean b ){
bDelete = b;
}
public boolean getDelete(){
return bDelete;
}
public void setGuiError( boolean b ){
bGuiError = b;
}
public boolean getGuiError(){
return bGuiError;
}
public void setDeleteOptimized( boolean b ){
bDeleteOptimized = b;
}
public boolean getDeleteOptimized(){
return bDeleteOptimized;
}
public boolean getLifo(){
return bLifo;
}
public void setLifo( boolean b ){
bLifo = b;
}
public boolean load(){
boolean bRet = false;
try {
FileInputStream fis = new FileInputStream( cConfig );
p.clear();
p.load(fis);
fis.close();
// Host + port
this.cLocalHost = p.getProperty("host","localhost");
this.cLocalPort = Double.valueOf( p.getProperty("port","110") ).intValue();
this.cLocalPortSMTP = Double.valueOf( p.getProperty("portsmtp","25") ).intValue();
this.cLocalPortNNTP = Double.valueOf( p.getProperty("portnntp","119") ).intValue();
this.nClient = Double.valueOf( p.getProperty("concurrentclient","10") ).intValue();
this.bDelete = p.getProperty("delete","true").equalsIgnoreCase("true");
this.bDeleteOptimized = p.getProperty("deleteoptimized","true").equalsIgnoreCase("true");
this.bGuiError = p.getProperty("guierror","true").equalsIgnoreCase("true");
this.bLifo = p.getProperty("coda","lifo").equalsIgnoreCase("lifo");
this.bOutlook2002Timeout = p.getProperty("outlook2002.timeout","true").equalsIgnoreCase("true");
// Email per session
this.nMaxEmail = Double.valueOf( p.getProperty("maxdownloadpersession","-1") ).intValue();
// POP3 default
pop3message.setSponsor( p.getProperty("sponsor","true").equalsIgnoreCase("true") );
pop3message.setAddHTML( p.getProperty("htmlattach","true").equalsIgnoreCase("true") );
// RSS
pluginrss.setConfig( getConfigPath(), "rss.cfg" );
// NNTP
pluginnntp.setConfig( getConfigPath(), "nntp.cfg" );
// Size del log
logStreamMemo.setLogSize( Double.valueOf( p.getProperty("logsize","1000000") ).intValue() );
// Plugin specific
plugintiscali.setDelete( p.getProperty("tiscali.delete","true").equalsIgnoreCase("true") );
plugintin.setDelete( p.getProperty("tin.delete","true").equalsIgnoreCase("true") );
// Tunneling server
pluginpop3.setDefaultServer( p.getProperty("tunnelingserver","http://www.baccan.it/pop3/") );
pluginsmtp.setDefaultServer( p.getProperty("tunnelingserver","http://www.baccan.it/pop3/") );
// I vecchi JDK non hanno il metodo di modifica delle proprieta' direttamente
// sul system
Properties sp = System.getProperties();
sp.put("proxyUser" , p.getProperty("proxyuser" ,"") );
sp.put("proxyPassword", p.getProperty("proxypassword","") );
// proxy + port
sp.put("http.proxyHost", p.getProperty("proxyhost","") );
sp.put("http.proxyPort", p.getProperty("proxyport","") );
//qui imposto il proxy di uscita per https e user/pass tramite authenticator
//per il momento imposto gli stessi valori del proxy http.
//Prevedere la possibilit‡ di impostare un proxy diverso in fase di conf.
System.getProperties().put("https.proxyHost",System.getProperty("http.proxyHost"));
System.getProperties().put("https.proxyPort",System.getProperty("http.proxyPort"));
if( p.getProperty("proxyhost","").length()>0 )
sp.put("http.proxySet" ,"true");
else
sp.put("http.proxySet" ,"false");
System.setProperties( sp );
// Filter
pop3i = getFilter("pop3.ipfilter");
pop3p = getFilter("pop3.pluginfilter");
pop3u = getFilter("pop3.userfilter");
pop3g = getFilter("pop3.globalfilter");
smtpi = getFilter("smtp.ipfilter");
smtpp = getFilter("smtp.pluginfilter");
smtpu = getFilter("smtp.userfilter");
smtpg = getFilter("smtp.globalfilter");
nntpi = getFilter("nntp.ipfilter");
bRet = true;
} catch (java.io.FileNotFoundException fnf) {
System.out.println( "Non riesco a leggere il file " +getConfigFullPath() +", lanciare html2pop3 dalla directory di " +cConfig );
} catch (IOException e) {
e.printStackTrace();
System.out.println( e.getMessage() );
}
return bRet;
}
private filter getFilter( String cRoot ){
filter ret = new filter();
int nFilter = 1;
while( true ){
String cFilter = p.getProperty(cRoot+nFilter,"");
int nSep = cFilter.indexOf(";");
if( nSep<=0 ) break;
//System.out.println("Add: " +cFilter);
String cRule = cFilter.substring( 0, nSep );
cFilter = cFilter.substring( nSep+1 );
Vector v = new Vector();
StringTokenizer st = new StringTokenizer( cFilter, ";" );
while (st.hasMoreTokens()) {
v.addElement( st.nextToken() );
}
String []o = new String[v.size()];
for( int nPos=0; nPos<o.length; nPos++ )
o[nPos] = (String)v.elementAt( nPos );
ret.add( cRule, o );
nFilter++;
}
return ret;
}
private filter pop3i = new filter();
private filter pop3p = new filter();
private filter pop3u = new filter();
private filter pop3g = new filter();
private filter smtpi = new filter();
private filter smtpp = new filter();
private filter smtpu = new filter();
private filter smtpg = new filter();
private filter nntpi = new filter();
public filter getPOP3IpFilter() { return pop3i; }
public filter getPOP3PluginFilter(){ return pop3p; }
public filter getPOP3UserFilter() { return pop3u; }
public filter getPOP3GlobalFilter(){ return pop3g; }
public filter getSMTPIpFilter() { return smtpi; }
public filter getSMTPPluginFilter(){ return smtpp; }
public filter getSMTPUserFilter() { return smtpu; }
public filter getSMTPGlobalFilter(){ return smtpg; }
public filter getNNTPIpFilter() { return nntpi; }
private String getConfigFullPath() {
String cRet = "";
try {
File file = new File( cConfig );
cRet = file.getAbsolutePath();
} catch (Throwable e) {
}
return cRet;
}
public String getConfigPath() {
String cRet = File.separator;
try {
int nPos = getConfigFullPath().lastIndexOf( File.separator );
if( nPos!=-1 ) cRet = getConfigFullPath().substring( 0, nPos+1 );
} catch (Throwable e) {
}
return cRet;
}
public void save(){
try {
// Host + port
p.put("host", cLocalHost );
p.put("port", ""+cLocalPort );
p.put("portsmtp", ""+cLocalPortSMTP );
p.put("portnntp", ""+cLocalPortNNTP );
p.put("concurrentclient", ""+nClient );
p.put("delete", ""+bDelete );
p.put("deleteoptimized", ""+bDeleteOptimized );
p.put("guierror", ""+bGuiError );
if( bLifo )
p.put("coda", "lifo" );
else
p.put("coda", "fifo" );
// POP3 default
p.put("sponsor" ,""+pop3message.getSponsor() );
p.put("htmlattach",""+pop3message.getAddHTML() );
// Email per sessione
p.put("maxdownloadpersession",""+nMaxEmail);
// Proxy
p.put("proxyuser" ,System.getProperty("proxyUser" ,""));
p.put("proxypassword",System.getProperty("proxyPassword",""));
// proxy + port
p.put("proxyhost",System.getProperty("http.proxyHost",""));
p.put("proxyport",System.getProperty("http.proxyPort",""));
// DEPRECATA la uso per retrocompatiblita' con JDK MS e vecchi JDK
p.save(new FileOutputStream( cConfig ), null);
} catch (Throwable e) {
e.printStackTrace();
System.out.println( e.getMessage() );
}
}
configChange cc;
public void run(){
pop3Server pop3;
smtpServer smtp;
nntpServer nntp;
try {
printInfo();
pop3 = new pop3Server( this );
pop3.start();
smtp = new smtpServer( this );
smtp.start();
nntp = new nntpServer( this );
nntp.start();
cc = new configChange( this );
cc.start();
while( true ){
// Faccio partire il Thread
try {
Thread.sleep( 1000 );
} catch (Throwable e) {
}
if( isRestart ){
System.out.println( "Restarting ..." );
pop3.finish();
while( pop3.isAlive() ) Thread.sleep( 100 );
smtp.finish();
while( smtp.isAlive() ) Thread.sleep( 100 );
nntp.finish();
while( nntp.isAlive() ) Thread.sleep( 100 );
pop3 = new pop3Server( this );
pop3.start();
smtp = new smtpServer( this );
smtp.start();
nntp = new nntpServer( this );
nntp.start();
cc = new configChange( this );
cc.start();
printInfo();
isRestart = false;
}
}
} catch (Throwable e) {
e.printStackTrace();
System.out.println( e.getMessage() );
}
}
public void printInfo(){
String cVer = version.getVersion();
if( cVer.length()==4 ) cVer += " ";
System.out.println( "+---------------------------------------------------------------------------+" );
System.out.println( "| POP3/SMTP/NNTP simulation server Version " +cVer +" |" );
System.out.println( "| Matteo Baccan Opensource Software http://www.baccan.it |" );
System.out.println( "+---------------------------------------------------------------------------+" );
if( bIsWin32 ){
System.out.println( "Java Version: " +System.getProperty("java.version") );
} else {
System.out.println( "Java Runtime: " +System.getProperty("java.runtime.name") );
System.out.println( "Java Version: " +System.getProperty("java.vm.version") );
}
System.out.println( "Config path: " +getConfigFullPath() );
if( cLocalPort>0 )
System.out.println( "Server POP3 ready at " +cLocalHost +":" +cLocalPort +" max clients: " +nClient );
else
System.out.println( "Server POP3 disabled" );
if( cLocalPortSMTP>0 )
System.out.println( "Server SMTP ready at " +cLocalHost +":" +cLocalPortSMTP +" max clients: " +nClient );
else
System.out.println( "Server SMTP disabled" );
if( cLocalPortNNTP>0 )
System.out.println( "Server NNTP ready at " +cLocalHost +":" +cLocalPortNNTP +" max clients: " +nClient );
else
System.out.println( "Server NNTP disabled" );
if( bLifo )
System.out.println( "Message download queue: LIFO" );
else
System.out.println( "Message download queue: FIFO" );
if( System.getProperty("http.proxyHost","").length()>0 ){
System.out.println( "Proxy enabled " +System.getProperty("http.proxyHost","") +":" +System.getProperty("http.proxyPort") );
if( System.getProperty("proxyUser","").length()>0 )
System.out.println( "Proxy-authorization Basic " +System.getProperty("proxyUser","") );
}
if( bDelete ) {
System.out.println( "Le cancellazioni SONO abilitate, il client di posta potra' cancellare la posta" );
if( bDeleteOptimized ){
System.out.println( "Le cancellazioni sono fatte DOPO la sconnessione del client di posta" );
} else {
System.out.println( "Le cancellazioni sono fatte PRIMA della sconnessione del client di posta" );
}
} else {
System.out.println( "Le cancellazioni NON sono abilitate, il client di posta non potra' cancellare la posta" );
}
if( bGuiError ){
System.out.println( "Errori gravi visualizzati con messagebox GUI e file di log" );
} else {
System.out.println( "Errori gravi visualizzati solo nel file di log" );
}
System.out.println( "Sponsor: " +pop3message.getSponsor() );
System.out.println( "Attach email originale nella posta emulata: " +pop3message.getAddHTML() );
System.out.println( "Numero di download massimi per sessione: " +nMaxEmail );
System.out.println( "Dimensione del file di log: " +logStreamMemo.getLogSize() );
System.out.println( "Tunneling server " +p.getProperty("tunnelingserver","http://www.baccan.it/pop3/") );
System.out.println( "-----------------------------------------------------------------------------" );
System.out.println( "Plugin specific setting" );
System.out.println( "-----------------------------------------------------------------------------" );
System.out.println( "tiscali: modalita' di cancellazione " +(plugintiscali.getDelete()?"CANCELLA":"MUOVE nel cestino") );
System.out.println( "tin: modalita' di cancellazione " +(plugintin.getDelete()?"CANCELLA":"MUOVE nel cestino") );
System.out.println( "outlook 2002: timeout " +(bOutlook2002Timeout?"ATTIVO":"DISATTIVO") );
System.out.println( "-----------------------------------------------------------------------------" );
}
private boolean isRestart = false;
public void restart(){
try {
isRestart = true;
cc.finish();
} catch (Throwable e) {}
}
class configChange extends Thread {
long timestamp;
html2pop3 parent;
boolean bLoop;
public configChange( html2pop3 p ){
timestamp = 0;
parent = p;
bLoop = true;
}
public void finish(){
bLoop = false;
}
public void run(){
try {
while( bLoop ){
File f = new File( parent.getConfigFullPath() );
if( timestamp==0 ){
timestamp = f.lastModified();
} else if( timestamp!=f.lastModified() ){
parent.load();
parent.restart();
System.out.println("Config change ...");
bLoop = false;
}
if( bLoop ) Thread.sleep(1000);
}
} catch (Throwable e) {}
}
}
}
The table below shows all metrics for html2pop3.java.



