⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nnrpd.java

📁 java高级使用教程 全书一共分六章
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	    {
	    warning( clientHost + "problem reading post: " + e );
	    ++postsBad;
	    response( "441 posting failed: " + e );
	    return;
	    }
	try
	    {
	    newsDb.post( text );
	    notice( clientHost + " post ok" );
	    ++postsOk;
	    response( "240 article posted ok" );
	    }
	catch ( NewsDbException e )
	    {
	    notice( clientHost + " post failed: " + e );
	    ++postsBad;
	    response( "441 posting failed: " + e );
	    }
	}

    private void cmdSlave( String[] args ) throws NewsDbException, NnrpdException
	{
	if ( args.length != 0 )
	    {
	    response( NnrpdUtils.RES_SYNTAXERROR );
	    return;
	    }
	response( "202 slave status noted" );
	}

    private void cmdQuit( String[] args ) throws NewsDbException, NnrpdException
	{
	sessionLog();
	response( "205 closing connection - goodbye!" );
	}

    private void cmdStat( String[] args ) throws NewsDbException, NnrpdException
	{
	cmdAhbs( args, 's' );
	}

    private void cmdXgtitle( String[] args ) throws NewsDbException, NnrpdException
	{
	if ( args.length > 1 )
	    {
	    response( NnrpdUtils.RES_SYNTAXERROR );
	    return;
	    }
	String groupsPat;
	if ( args.length == 1 )
	    groupsPat = args[0];
	else
	    {
	    if ( currentGroup == null )
		{
		response( NnrpdUtils.RES_NOCURRGROUP );
		return;
		}
	    groupsPat = currentGroup.getName();
	    }
	response( "282 list follows" );
	Enumeration en = newsDb.getGroups();
	while ( en.hasMoreElements() )
	    {
	    NewsDbGroup group = (NewsDbGroup) en.nextElement();
	    String groupName = group.getName();
	    if ( Utils.match( groupsPat, groupName ) )
		{
		String description = group.getDescription();
		if ( description != null )
		    pout.println( groupName + "\t" + group.getDescription() );
		}
	    }
	pout.println( "." );
	}

    private void cmdXhdr( String[] args ) throws NewsDbException, NnrpdException
	{
	if ( args.length < 1 || args.length > 2 )
	    {
	    response( NnrpdUtils.RES_SYNTAXERROR );
	    return;
	    }
	String header = args[0];
	if ( args.length == 1 )
	    {
	    // Use current article.
	    if ( currentGroup == null )
		response( NnrpdUtils.RES_NOCURRGROUP );
	    else if ( currentArtNum == -1 )
		response( NnrpdUtils.RES_NOCURRARTICLE );
	    else
		sendXhdrRange( currentArtNum, currentArtNum, header );
	    }
	else if ( args[1].charAt( 0 ) == '<' )
	    {
	    // By message-id.
	    String messageId = args[1];
	    NewsDbArticle article = getArticle( messageId );
	    if ( article == null )
		response( NnrpdUtils.RES_NOSUCHARTICLE );
	    else
		{
		response(
		    "221 " + header + " header of article " + messageId );
		sendXhdrLine( article, header, messageId );
		pout.println( "." );
		}
	    }
	else
	    {
	    int[] range = parseRange( args[1] );
	    if ( range == null )
		response( NnrpdUtils.RES_SYNTAXERROR );
	    else
		sendXhdrRange( range[0], range[1], header );
	    }
	}
    
    private void sendXhdrRange( int firstArtNum, int lastArtNum, String header ) throws NewsDbException, NnrpdException
	{
	response( "221 " + header + " fields follow" );
	for ( int artNum = firstArtNum; artNum <= lastArtNum; ++artNum )
	    {
	    NewsDbArticle article = getArticle( currentGroup, artNum );
	    if ( article != null )
		sendXhdrLine( article, header, Integer.toString( artNum ) );
	    }
	pout.println( "." );
	}

    private void sendXhdrLine( NewsDbArticle article, String header, String ident )
	{
	String val = article.getHeader( header );
	if ( val == null )
	    pout.println( ident + " (none)" );
	else
	    pout.println( ident + " " + val );
	}

    private void cmdXover( String[] args ) throws NewsDbException, NnrpdException
	{
	if ( args.length > 1 )
	    {
	    response( NnrpdUtils.RES_SYNTAXERROR );
	    return;
	    }
	if ( args.length == 0 )
	    {
	    // Use current article.
	    if ( currentGroup == null )
		response( NnrpdUtils.RES_NOCURRGROUP );
	    else if ( currentArtNum == -1 )
		response( NnrpdUtils.RES_NOCURRARTICLE );
	    else
		sendXoverRange( currentGroup, currentArtNum, currentArtNum );
	    }
	else
	    {
	    int[] range = parseRange( args[0] );
	    if ( range == null )
		response( NnrpdUtils.RES_SYNTAXERROR );
	    else
		sendXoverRange( currentGroup, range[0], range[1] );
	    }
	}
    
    private void sendXoverRange( NewsDbGroup group, int firstArtNum, int lastArtNum ) throws NewsDbException, NnrpdException
	{
	int numArts = lastArtNum - firstArtNum + 1;
	String[][] results =
	    newsDb.getHeaders( overviewFmt, group, firstArtNum, lastArtNum );
	if ( results == null )
	    {
	    // Fall back on conventional weapons.
	    results = new String[numArts][overviewFmt.length];
	    for ( int i = 0; i < numArts; ++i )
		{
		int artNum = firstArtNum + i;
		NewsDbArticle article = getArticle( group, artNum );
		if ( article != null )
		    for ( int j = 0; j < overviewFmt.length; ++j )
			results[i][j] = article.getHeader( overviewFmt[j] );
		}
	    }
	response( "224 data follows" );
	for ( int i = 0; i < numArts; ++i )
	    {
	    if ( results[i] == null )
		continue;	// non-existent article
	    int artNum = firstArtNum + i;
	    StringBuffer buf = new StringBuffer();
	    buf.append( Integer.toString( artNum ) );
	    for ( int j = 0; j < overviewFmt.length; ++j )
		{
		buf.append( '\t' );
		buf.append( results[i][j] );
		}
	    pout.println( buf.toString() );
	    }
	pout.println( "." );
	}

    private void cmdAhbs( String[] args, char which ) throws NewsDbException, NnrpdException
	{
	if ( args.length == 0 )
	    if ( currentArtNum == -1 )
		response( NnrpdUtils.RES_NOCURRARTICLE );
	    else
		cmdAhbsNum( currentArtNum, which );
	else if ( args.length > 1 )
	    response( NnrpdUtils.RES_SYNTAXERROR );
	else if ( args[0].charAt( 0 ) == '<' )
	    cmdAhbsId( args[0], which );
	else
	    {
	    int artNum = Utils.parseInt( args[0], -1 );
	    if ( artNum == -1 )
		{
		response( NnrpdUtils.RES_SYNTAXERROR );
		return;
		}
	    cmdAhbsNum( artNum, which );
	    }
	}

    private void cmdAhbsNum( int artNum, char which ) throws NewsDbException, NnrpdException
	{
	if ( currentGroup == null )
	    response( NnrpdUtils.RES_NOCURRGROUP );
	else
	    {
	    NewsDbArticle article = getArticle( currentGroup, artNum );
	    if ( article == null )
		{
		response( NnrpdUtils.RES_NOSUCHARTICLENUM );
		return;
		}
	    currentArtNum = artNum;
	    sendAhbs( article, artNum, which );
	    }
	}

    private void cmdAhbsId( String messageId, char which ) throws NewsDbException, NnrpdException
	{
	NewsDbArticle article = getArticle( messageId );
	if ( article == null )
	    {
	    response( NnrpdUtils.RES_NOSUCHARTICLE );
	    return;
	    }
	sendAhbs( article, -1, which );
	}

    private void sendAhbs( NewsDbArticle article, int artNum, char which ) throws NewsDbException, NnrpdException
	{
	artLog();
	response(
	    whichStatus( which ) + " " + artNum + " " +
	    article.getHeader( "Message-ID" ) + whichStr( which ) );
	switch ( which )
	    {
	    case 'a':
	    pout.print( article.getText() );
	    break;
	    case 'h':
	    pout.print(
		article.getText().substring( 0, article.getHeadLen() ) );
	    break;
	    case 'b':
	    pout.print( article.getText().substring( article.getBodyStart() ) );
	    break;
	    case 's':
	    return;
	    }
	pout.println( "." );
	}

    private static int whichStatus( char which )
	{
	switch ( which )
	    {
	    case 'a': return 220;
	    case 'h': return 221;
	    case 'b': return 222;
	    case 's': return 223;
	    }
	return 500;
	}

    private static String whichStr( char which )
	{
	switch ( which )
	    {
	    case 'a': return " head and body follow";
	    case 'h': return " head follows";
	    case 'b': return " body follows";
	    case 's': return " request text separately";
	    }
	return "???";
	}


    private void sendGroup( NewsDbGroup group )
	{
	pout.println(
	    group.getName() + " " + group.getLastArtNum() + " " +
	    group.getFirstArtNum() + " " + group.getFlag() );
	}
    

    /// Send a response line.
    private void response( String resp ) throws NnrpdException
	{
	debug( clientHost + " > " + resp );
	pout.println( resp );
	}


    private NewsDbArticle getArticle( NewsDbGroup group, int artNum ) throws NewsDbException
	{
	NewsDbArticle article = articleCache.getArticle( group, artNum );
	if ( article == null )
	    {
	    article = newsDb.getArticle( group, artNum );
	    if ( article != null )
		articleCache.addArticle( article, group, artNum );
	    }
	return article;
	}

    private NewsDbArticle getArticle( String messageId ) throws NewsDbException
	{
	NewsDbArticle article = articleCache.getArticle( messageId );
	if ( article == null )
	    {
	    article = newsDb.getArticle( messageId );
	    if ( article != null )
		articleCache.addArticle( article, messageId );
	    }
	return article;
	}
    

    /// Parse a range spec for the XHDR and XOVER commands.
    private int[] parseRange( String rangeStr )
	{
	try
	    {
	    int[] range = new int[2];
	    int dash = rangeStr.indexOf( '-' );
	    if ( dash == -1 )
		{
		range[0] = range[1] = Integer.parseInt( rangeStr );
		}
	    else
		{
		String firstStr = rangeStr.substring( 0, dash );
		String lastStr = rangeStr.substring( dash + 1 );
		if ( firstStr.length() == 0 )
		    range[0] = currentGroup.getFirstArtNum();
		else
		    range[0] = Integer.parseInt( firstStr );
		if ( lastStr.length() == 0 )
		    range[1] = currentGroup.getLastArtNum();
		else
		    range[1] = Integer.parseInt( lastStr );
		}
	    return range;
	    }
	catch ( NumberFormatException e )
	    {
	    return null;
	    }
	}
    

    /// Log an article transfer.
    private void artLog() throws NnrpdException
	{
	++artCount;
	++groupArts;
	}

    /// Log group stats.  Call just before you change groups, or exit.
    private void groupLog() throws NnrpdException
	{
	if ( currentGroup != null )
	    {
	    notice(
		clientHost + " group " + currentGroup.getName() +
		" " + groupArts );
	    ++groupCount;
	    }
	groupArts = 0;
	}
    
    /// Log session stats.  Call upon exit.
    private void sessionLog() throws NnrpdException
	{
	long now = System.currentTimeMillis();
	groupLog();
	notice(
	    clientHost + " exit articles " + artCount +
	    " groups " + groupCount );
	if ( postsOk != 0 || postsBad != 0 )
	    notice(
		clientHost + " posts received " + postsOk +
		" rejected " + postsBad );
	notice( clientHost + " elapsed " + ( now - startTime ) / 1000 );
	}


    private void debug( String message ) throws NnrpdException
	{
	nnrpd.debug( message );
	}

    private void info( String message ) throws NnrpdException
	{
	nnrpd.info( message );
	}

    private void notice( String message ) throws NnrpdException
	{
	nnrpd.notice( message );
	}

    private void warning( String message ) throws NnrpdException
	{
	nnrpd.warning( message );
	}

    private void error( String message ) throws NnrpdException
	{
	nnrpd.error( message );
	}

    private void crit( String message ) throws NnrpdException
	{
	nnrpd.crit( message );
	}

    private void alert( String message ) throws NnrpdException
	{
	nnrpd.alert( message );
	}

    private void emerg( String message ) throws NnrpdException
	{
	nnrpd.emerg( message );
	}

    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -