📄 nnrpd.java
字号:
cmdMode( args );
else if ( command.equalsIgnoreCase( NnrpdUtils.CMD_NEWGROUPS ) )
cmdNewgroups( args );
else if ( command.equalsIgnoreCase( NnrpdUtils.CMD_NEWNEWS ) )
cmdNewnews( args );
else if ( command.equalsIgnoreCase( NnrpdUtils.CMD_NEXT ) )
cmdNext( args );
else if ( command.equalsIgnoreCase( NnrpdUtils.CMD_POST ) )
cmdPost( args );
else if ( command.equalsIgnoreCase( NnrpdUtils.CMD_QUIT ) )
{
cmdQuit( args );
break;
}
else if ( command.equalsIgnoreCase( NnrpdUtils.CMD_SLAVE ) )
cmdSlave( args );
else if ( command.equalsIgnoreCase( NnrpdUtils.CMD_STAT ) )
cmdStat( args );
else if ( command.equalsIgnoreCase( NnrpdUtils.CMD_XGTITLE ) )
cmdXgtitle( args );
else if ( command.equalsIgnoreCase( NnrpdUtils.CMD_XHDR ) )
cmdXhdr( args );
else if ( command.equalsIgnoreCase( NnrpdUtils.CMD_XOVER ) )
cmdXover( args );
else
{
notice( clientHost + " unrecognized " + commandLine );
response( NnrpdUtils.RES_BADCOMMAND );
}
}
catch ( NewsDbException e )
{
warning( clientHost + " " + e );
response( NnrpdUtils.RES_PROGFAULT + ": " + e );
}
catch ( NnrpdException e )
{
warning( clientHost + " " + e );
response( NnrpdUtils.RES_PROGFAULT + ": " + e );
}
}
tk.done();
}
// State variables.
private NewsDbGroup currentGroup = null;
private int currentArtNum = -1;
// Stats variables.
private long startTime = System.currentTimeMillis();
private int groupCount = 0;
private int artCount = 0;
private int postsOk = 0;
private int postsBad = 0;
private int groupArts;
// Command routines.
private void cmdArticle( String[] args ) throws NewsDbException, NnrpdException
{
cmdAhbs( args, 'a' );
}
private String user = null;
private String pass = null;
private void cmdAuthinfo( String[] args ) throws NewsDbException, NnrpdException
{
if ( args.length != 2 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
if ( args[0].equalsIgnoreCase( "user" ) )
{
user = args[1];
response( "381 AUTHINFO PASS required" );
}
else if ( args[0].equalsIgnoreCase( "pass" ) )
{
if ( user == null )
response( "482 AUTHINFO USER required" );
else
{
if ( newsDb.authorize( user, pass ) )
response( "281 authentication ok" );
else
response( "502 authentication failed" );
}
}
else
response( NnrpdUtils.RES_SYNTAXERROR );
}
private void cmdBody( String[] args ) throws NewsDbException, NnrpdException
{
cmdAhbs( args, 'b' );
}
private void cmdDate( String[] args ) throws NewsDbException, NnrpdException
{
if ( args.length != 0 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
Date date = new Date();
String gmtStr = date.toGMTString();
String[] strArr = Utils.splitStr( gmtStr );
int day = Integer.parseInt( strArr[0] );
String months = "JanFebMarAprMayJunJulAugSepOctNovDec";
int month = months.indexOf( strArr[1] ) / 3 + 1;
int year = Integer.parseInt( strArr[2] );
int hour = Integer.parseInt( strArr[3].substring( 0, 2 ) );
int minute = Integer.parseInt( strArr[3].substring( 3, 5 ) );
int second = Integer.parseInt( strArr[3].substring( 6, 8 ) );
response( "111 " +
Fmt.fmt( year, 4, Fmt.ZF ) +
Fmt.fmt( month, 2, Fmt.ZF ) +
Fmt.fmt( day, 2, Fmt.ZF ) +
Fmt.fmt( hour, 2, Fmt.ZF ) +
Fmt.fmt( minute, 2, Fmt.ZF ) +
Fmt.fmt( second, 2, Fmt.ZF ) );
}
private void cmdGroup( String[] args ) throws NewsDbException, NnrpdException
{
if ( args.length != 1 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
NewsDbGroup group = newsDb.getGroup( args[0] );
if ( group == null )
response( NnrpdUtils.RES_NOSUCHGROUP );
else
{
groupLog();
currentGroup = group;
currentArtNum = currentGroup.getFirstArtNum();
response( "211 " +
currentGroup.getNumArts() + " " +
currentArtNum + " " +
currentGroup.getLastArtNum() + " " +
currentGroup.getName() );
}
}
private void cmdHead( String[] args ) throws NewsDbException, NnrpdException
{
cmdAhbs( args, 'h' );
}
private void cmdHelp( String[] args ) throws NewsDbException, NnrpdException
{
response( "100 help text follows" );
pout.println( "ARTICLE [<messageid>|number]" );
pout.println( "AUTHINFO user name|pass password" );
pout.println( "BODY [<messageid>|number]" );
pout.println( "DATE" );
pout.println( "GROUP newsgroup" );
pout.println( "HEAD [<messageid>|number]" );
pout.println( "HELP" );
pout.println( "LAST" );
pout.println( "LIST [active|newsgroups|distributions|schema] [group_pattern]" );
pout.println( "LISTGROUP newsgroup" );
pout.println( "MODE reader" );
pout.println( "NEWGROUPS yymmdd hhmmss [GMT] [distributions]" );
pout.println( "NEWNEWS newsgroups yymmdd hhmmss [GMT] [distributions]" );
pout.println( "NEXT" );
pout.println( "POST" );
pout.println( "QUIT" );
pout.println( "SLAVE" );
pout.println( "STAT [<messageid>|number]" );
pout.println( "XGTITLE [group_pattern]" );
pout.println( "XHDR header [range|<messageid>]" );
pout.println( "XOVER [range]" );
pout.println( "." );
}
private void cmdLast( String[] args ) throws NewsDbException, NnrpdException
{
if ( args.length != 0 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
if ( currentGroup == null )
response( NnrpdUtils.RES_NOCURRGROUP );
else if ( currentArtNum == -1 )
response( NnrpdUtils.RES_NOCURRARTICLE );
else
{
int artNum = currentArtNum;
while ( --artNum >= currentGroup.getFirstArtNum() )
{
NewsDbArticle article = getArticle( currentGroup, artNum );
if ( article == null )
continue;
currentArtNum = artNum;
sendAhbs( article, currentArtNum, 's' );
return;
}
response( "422 no previous article in this group" );
}
}
private void cmdList( String[] args ) throws NewsDbException, NnrpdException
{
if ( args.length > 2 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
if ( args.length == 0 || args[0].equalsIgnoreCase( "active" ) )
{
response( "215 list of newsgroups follows" );
Enumeration en = newsDb.getGroups();
while ( en.hasMoreElements() )
{
NewsDbGroup group = (NewsDbGroup) en.nextElement();
if ( args.length == 2 &&
! Utils.match( args[1], group.getName() ) )
continue;
sendGroup( group );
}
pout.println( "." );
}
else if ( args[0].equalsIgnoreCase( "active.times" ) )
{
response( NnrpdUtils.RES_UNIMPLEMENTED );
}
else if ( args[0].equalsIgnoreCase( "newsgroups" ) )
{
response( "215 list of descriptions follows" );
Enumeration en = newsDb.getGroups();
while ( en.hasMoreElements() )
{
NewsDbGroup group = (NewsDbGroup) en.nextElement();
String groupName = group.getName();
if ( args.length == 2 &&
! Utils.match( args[1], groupName ) )
continue;
String description = group.getDescription();
if ( description != null )
pout.println( groupName + "\t" + group.getDescription() );
}
pout.println( "." );
}
else if ( args[0].equalsIgnoreCase( "distributions" ) )
{
response( NnrpdUtils.RES_UNIMPLEMENTED );
}
else if ( args[0].equalsIgnoreCase( "distrib.pats" ) )
{
response( NnrpdUtils.RES_UNIMPLEMENTED );
}
else if ( args[0].equalsIgnoreCase( "schema" ) ||
args[0].equalsIgnoreCase( "overview.fmt" ) )
{
response( "215 Order of fields in overview database" );
for ( int i = 0; i < overviewFmt.length; ++i )
pout.println( overviewFmt[i] + ":" );
pout.println( "." );
}
else
response( NnrpdUtils.RES_SYNTAXERROR );
}
private void cmdListgroup( String[] args ) throws NewsDbException, NnrpdException
{
if ( args.length != 1 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
NewsDbGroup group = newsDb.getGroup( args[0] );
if ( group == null )
response( NnrpdUtils.RES_NOSUCHGROUP );
else
{
groupLog();
currentGroup = group;
currentArtNum = currentGroup.getFirstArtNum();
response( "211 article list follows" );
// We're going to do the cheap thing here and just count from
// the first article to the last article.
for ( int artNum = currentGroup.getFirstArtNum();
artNum <= currentGroup.getLastArtNum(); ++artNum )
pout.println( artNum );
pout.println( "." );
}
}
private void cmdMode( String[] args ) throws NewsDbException, NnrpdException
{
if ( args.length != 1 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
if ( args[0].equalsIgnoreCase( "reader" ) )
response( "200 reading mode acknowledged" );
else
response( NnrpdUtils.RES_SYNTAXERROR );
}
private void cmdNewgroups( String[] args ) throws NewsDbException, NnrpdException
{
if ( args.length < 2 || args.length > 4 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
String date = args[0];
String time = args[1];
boolean gmt = false;
String distsPat = null;
if ( args.length >= 3 )
if ( args[2].equalsIgnoreCase( "GMT" ) )
{
gmt = true;
if ( args.length == 4 )
distsPat = args[3];
}
else
{
if ( args.length == 4 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
distsPat = args[4];
}
long since = NnrpdUtils.rfc977DateTime( date, time, gmt, -1 );
if ( since == -1 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
response( "231 list of new newsgroups follows" );
Enumeration en;
if ( distsPat == null )
en = newsDb.getGroups( since );
else
en = newsDb.getGroups( since, distsPat );
while ( en.hasMoreElements() )
{
NewsDbGroup group = (NewsDbGroup) en.nextElement();
sendGroup( group );
}
pout.println( "." );
}
private void cmdNewnews( String[] args ) throws NewsDbException, NnrpdException
{
if ( args.length < 3 || args.length > 5 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
String groupsPat = args[0];
String date = args[1];
String time = args[2];
boolean gmt = false;
String distsPat = null;
if ( args.length >= 4 )
if ( args[3].equalsIgnoreCase( "GMT" ) )
{
gmt = true;
if ( args.length == 5 )
distsPat = args[4];
}
else
{
if ( args.length == 5 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
distsPat = args[5];
}
long since = NnrpdUtils.rfc977DateTime( date, time, gmt, -1 );
if ( since == -1 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
notice(
clientHost + " newnews " + groupsPat + " " +
date + " " + time + " " + ( gmt ? "GMT" : "local" ) +
( distsPat != null ? distsPat : "none" ) );
response( "230 list of new articles by message-id follows" );
Enumeration en;
if ( distsPat == null )
en = newsDb.getMessageIds( groupsPat, since );
else
en = newsDb.getMessageIds( groupsPat, since, distsPat );
while ( en.hasMoreElements() )
{
String messageId = (String) en.nextElement();
pout.println( messageId );
}
pout.println( "." );
}
private void cmdNext( String[] args ) throws NewsDbException, NnrpdException
{
if ( args.length != 0 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
if ( currentGroup == null )
response( NnrpdUtils.RES_NOCURRGROUP );
else if ( currentArtNum == -1 )
response( NnrpdUtils.RES_NOCURRARTICLE );
else
{
int artNum = currentArtNum;
while ( ++artNum <= currentGroup.getLastArtNum() )
{
NewsDbArticle article = getArticle( currentGroup, artNum );
if ( article == null )
continue;
currentArtNum = artNum;
sendAhbs( article, currentArtNum, 's' );
return;
}
response( "421 no next article in this group" );
}
}
private void cmdPost( String[] args ) throws NewsDbException, NnrpdException
{
if ( args.length != 0 )
{
response( NnrpdUtils.RES_SYNTAXERROR );
return;
}
if ( ! newsDb.getPostingOk() )
{
notice( clientHost + " noperm post without permission" );
response( "440 posting not allowed" );
return;
}
response( "340 send article to be posted. End with <CR-LF>.<CR-LF>" );
pout.flush();
String text;
try
{
text = NnrpdUtils.readText( din );
}
catch ( IOException e )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -