nntpServer.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.
/*
* NNTP 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: NNTP Server
* Description: Server NNTP
* Copyright: Copyright (c) 2004
* Company:
* @author Matteo Baccan
* @version 1.0
*/
import java.net.*;
import java.io.*;
import java.util.*;
import org.html2pop3.utils.*;
import org.html2pop3.plugin.nntp.*;
public class nntpServer extends baseServer {
public nntpServer( html2pop3 p ){
super(p);
}
//private ServerSocket ss = null;
public void run(){
nntpThread thread;
try {
if( parent.getPortNNTP()>0 ){
ss = new ServerSocket( parent.getPortNNTP(), parent.getClient(), InetAddress.getByName( parent.getHost() ) );
while( true ){
// Faccio partire il Thread
Socket socket = null;
try {
// Attendo il client
socket = ss.accept();
} catch (Throwable e) {
if( isFinish ) return;
else throw e;
}
// Metto anche il timeout ai socket 60 minuti
socket.setSoTimeout( 60*60*1000 );
// Aggiungo un keepalive .. per I client pigri
setKeepAlive( socket );
thread = new nntpThread( socket );
thread.start();
}
}
} catch (java.net.BindException be ) {
System.out.println( "Errore! Porta " +parent.getPortNNTP() +" gia in uso, cambiare porta nel config.cfg e fare un restart del server NNTP" );
if( parent.getGuiError() ){
MsgBox message = new MsgBox("HTML2POP3 server NNTP", "Errore! Porta " +parent.getPortNNTP() +" gia in uso, cambiare porta nel config.cfg e fare un restart del server NNTP", false);
}
} catch (Throwable e) {
e.printStackTrace();
System.out.println( e.getMessage() );
}
}
class nntpThread extends Thread {
private Socket socket;
public nntpThread( Socket socket ){
this.socket = socket;
}
// Inizio thread di gestione del socket
public void run() {
try {
manage( socket );
} catch (java.net.SocketException se) {
System.err.println( "NNTP server: chiusura connessione: " +se.getMessage() );
} catch (Throwable e) {
e.printStackTrace();
System.out.println( e.getMessage() );
}
try {
socket.close();
} catch (Throwable e) { }
}
private void manage( Socket socket ) throws Throwable {
// Source
InputStream SI = socket.getInputStream();
OutputStream SO = socket.getOutputStream();
htmlTool html = new htmlTool();
String cIP = socket.getInetAddress().getHostAddress();
// IP Filter
if( !parent.getNNTPIpFilter().isAllow( new String[]{cIP} ) ){
System.err.println( "500 IP (" +cIP +") deny" );
html.putData( SO, "500 IP (" +cIP +") deny\r\n" );
return;
}
///*
// initial banner
html.putData( SO, "200 HTML2POP3 news server (" +version.getVersion() +") ready\r\n" );
String cServer = "nntp";
nntpbase sp = null;
if( cServer.equalsIgnoreCase("nntp") ){
sp = new pluginnntp();
//} else {
//System.err.println( "NNTP server: errore, server mancante." );
//sp = new pluginnntp();
}
boolean bExit = false;
String cGroup = "";
String cArt = "";
String cUser = "";
String cPwd = "";
// main loop
while( !bExit ){
String cLine = html.getLineNOCRLF( SI );
String cLineUpper = cLine.toUpperCase();
System.err.println( "NNTP server: " +cLine );
//12345678901234
if( cLineUpper.startsWith("AUTHINFO USER") ){
if( cLine.length()<=13 ) {
html.putData( SO, "501 Syntax error in parameters or arguments to AUTHINFO USER command\r\n" );
} else {
cUser = cLine.substring(13).trim();
html.putData( SO, "381 More Authentication Required\r\n" );
}
//12345678901234
} else if( cLineUpper.startsWith("AUTHINFO PASS") ){
if( cLine.length()<=13 ) {
html.putData( SO, "501 Syntax error in parameters or arguments to AUTHINFO PASS command\r\n" );
} else {
cPwd = cLine.substring(13).trim();
html.putData( SO, "281 Authentication Accepted\r\n" );
}
} else if( cLineUpper.startsWith("MODE READER") ){
html.putData( SO, "200 OK\r\n" );
// Da .CFG con dati fake di 1 msg
} else if( cLineUpper.startsWith("LIST") ){
html.putData( SO, "215 list of newsgroups follows\r\n" );
sp.streamList( SO );
//Vector aGroup = sp.list();
//for( int nCur=0; nCur<aGroup.size(); nCur++ ){
//html.putData( SO, ((String)aGroup.elementAt( nCur )) +" 00001 00001 n\r\n" );
//}
html.putData( SO, ".\r\n" );
} else if( cLineUpper.startsWith("GROUP") ){
if( cLine.length()<=5 ) {
html.putData( SO, "411 No Such Group\r\n" );
} else {
cGroup = cLine.substring(5).trim();
long []nMsg = sp.group( cGroup );
html.putData( SO, "211 " +(nMsg[1]-nMsg[0]) +" " +nMsg[0] +" " +nMsg[1] +" " +cGroup +" group selected\r\n" );
}
} else if( cLineUpper.startsWith("XOVER") ){
if( cGroup.length()==0 ) {
html.putData( SO, "412 No Group Selected\r\n" );
} else if( cLine.length()<=5 ) {
html.putData( SO, "500 Syntax error in parameters or arguments to XOVER command\r\n" );
} else {
String cFrom = cLine.substring(5).trim();
String cTo = "";
int nSub = cFrom.indexOf("-");
if( nSub!=-1 ){
if( nSub==cFrom.length()-1 ){
cFrom = cFrom.substring(0,nSub);
cTo = "-1";
} else {
cTo = cFrom.substring(nSub+1);
cFrom = cFrom.substring(0,nSub);
}
} else {
cTo = cFrom;
}
Vector aRet = sp.xover( Double.valueOf( cFrom ).longValue(), Double.valueOf( cTo ).longValue() );
html.putData( SO, "224 Overview Information Follows\r\n" );
for( int nCur=0; nCur<aRet.size(); nCur++ ){
html.putData( SO, ((String)aRet.elementAt( nCur )) +"\r\n" );
}
html.putData( SO, ".\r\n" );
}
//12345678
} else if( cLineUpper.startsWith("ARTICLE") ){
if( cLine.length()<=7 ) {
html.putData( SO, "423 No Such Article In Group\r\n" );
} else {
String cCurArt = cLine.substring(7).trim();
String cBody = sp.article( Double.valueOf(cCurArt).longValue() );
if( cBody==null ){
html.putData( SO, "501 Error Retrieving Article In Group\r\n" );
} else {
html.putData( SO, "220 " +cCurArt +"\r\n" );
html.putData( SO, cBody +"\r\n" );
html.putData( SO, ".\r\n" );
}
}
//12345678
//} else if( cLineUpper.startsWith("STAT") ){
//if( cLine.length()<=4 ) {
//html.putData( SO, "501 Syntax error in parameters or arguments to STAT command\r\n" );
//} else {
//cArt = cLine.substring(4).trim();
//html.putData( SO, "223 " +cArt +" article retrieved - statistics\r\n" );
//}
//12345678
//} else if( cLineUpper.startsWith("HEAD") ){
//html.putData( SO, "221 " +cArt +" article retrieved - head\r\n" );
//html.putData( SO, "xxxxxxxxxxxxxxx\r\n" );
//html.putData( SO, ".\r\n" );
//12345678
//} else if( cLineUpper.startsWith("BODY") ){
//html.putData( SO, "221 " +cArt +" article retrieved - body\r\n" );
//html.putData( SO, "xxxxxxxxxxxxxxx\r\n" );
//html.putData( SO, ".\r\n" );
//12345678
//} else if( cLineUpper.startsWith("NEXT") ){
//html.putData( SO, "222 " +cArt +" article retrieved - statistics\r\n" );
//html.putData( SO, "xxxxxxxxxxxxxxx\r\n" );
//html.putData( SO, ".\r\n" );
} else if( cLineUpper.startsWith("QUIT") ){
html.putData( SO, "205 HTML2POP3 QUIT\r\n" );
bExit = true;
} else {
html.putData( SO, "500 Syntax Error or Unknown Command\r\n" );
}
}
//*/
}
}
}
The table below shows all metrics for nntpServer.java.



