#include "xbcomm.h"

static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
		          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

char *anonDataFilePath( VOIDPARM )
{
    char *envPath;
    char fpath[ MAXPATHLEN ];

	sprintf( fpath, "%s.anondat", anonFile );
	if ( NOT fileExists( fpath ) )
	{
	    envPath = (char *)getenv( "ANONDIR" );	
	    if ( envPath ISNT NULL AND strlen( envPath ) )
	        sprintf( fpath, "%s/%s.anondat", envPath, anonFile );
	}
	return( copyString( fpath ) );
}

char *anonFileParse( VOIDPARM )
{
    char errString[ 128 ], tempStr[ 256 ];
    char *fpath;
    FILE *theFile;

	if ( anonOther )
	    free( anonOther );
	anonOther = NULL;
	if ( NOT fileExists( fpath = anonDataFilePath() ) )
	{
	    sprintf( errString, "Couldn't find %s.anondat for anonymous data",
		     anonFile );
	    return( copyString( errString ) );
	}
	if ( NOT ( theFile = fopen( fpath, "r" ) ) )
	{
	    sprintf( errString, "Couldn't open %s for anonymous data",
		     fpath );
	    return( copyString( errString ) );
	}
	while ( lineRead( theFile, tempStr ) ISNT EOF )
	{
	    if ( NOT strncmp( tempStr, 
		    USERKEY, strlen( USERKEY ) ) )
		/* Specification of the users anonymous name */
		anonUser = copyString( tempStr + strlen( USERKEY ) );
	    else if ( NOT strncmp( tempStr, 
		    USERALIASKEY, strlen( USERALIASKEY ) ) )
		/* Specification of the users anonymous alias string */
		anonAlias = copyString( tempStr + strlen( USERALIASKEY ) );
	    else if ( NOT strncmp( tempStr, 
		    USERHOSTKEY, strlen( USERHOSTKEY ) ) )
		/* Specification of the user's posting host */
		anonHost = copyString( tempStr + strlen( USERHOSTKEY ) );
	    else if ( NOT strncmp( tempStr, 
		    SIGPATHKEY, strlen( SIGPATHKEY ) ) )
		/* Specification of where to find the anonymous .sig file */
		anonSig = copyString( tempStr + strlen( SIGPATHKEY ) );
	    else if ( NOT strncmp( tempStr, 
		    SITEHOSTKEY, strlen( SITEHOSTKEY ) ) )
		/* Specification of the anonymous user's site host */
		anonSite = copyString( tempStr + strlen( SITEHOSTKEY ) );
	    else if ( NOT strncmp( tempStr, 
		    SITEPATHKEY, strlen( SITEPATHKEY ) ) )
		/* Specification of the path to the anonymous user's site */
		anonPath = copyString( tempStr + strlen( SITEPATHKEY ) );
	    else
		/* No idea what this is, but just add it on to the current */
		/* string of "other stuff" */
		anonOther = addString( anonOther, tempStr, "\n" );
	}
	fclose( theFile );
	if ( NOT anonUser )
	{
	    if ( anonSite )
		sprintf( errString, "Anonymous user not specified" );
	    else
		sprintf( errString, "Anonymous user, site not specified" );
	    return( copyString( errString ) );
	}
	else if ( NOT anonSite )
	{
	    sprintf( errString, "Anonymous site not specified" );
	    return( copyString( errString ) );
	}
	return( NULL );
}
			
VOIDPARM addAnonHeader( whatFile, partNum )
FILE *whatFile;
int partNum;
{
    char tempStr[ 256 ];
    long numSecs;
    struct tm *timeStruct;

	fprintf( whatFile, "From: %s\n", anonUser );
	fprintf( whatFile, "Originator: %s@%s\n", anonUser, anonSite );
	if ( anonAlias )
	    fprintf( whatFile, "Sender: %s@%s (%s)\n", 
		     anonUser, anonSite, anonAlias );
	else
	    fprintf( whatFile, "Sender: %s@%s\n", anonUser, anonSite );
	if ( anonPath )
	    fprintf( whatFile, "Path: %s!%s!%s\n", 
		     anonPath, anonSite, anonUser );
	time( &numSecs );
	timeStruct = localtime( &numSecs );
	sprintf( tempStr, "%d%s%d.%02d%02d%02d.%d", 
		     ( timeStruct->tm_year >= 92 )
		     ? timeStruct->tm_year + 1900
		     : timeStruct->tm_year + 2000, 
		     months[ timeStruct->tm_mon ], timeStruct->tm_mday, 
		     timeStruct->tm_hour, timeStruct->tm_min, 
		     timeStruct->tm_sec, partNum );
	fprintf( whatFile, "Message-ID: <%s@%s>\n", tempStr, anonSite );
	if ( anonHost )
	    fprintf( whatFile, "Nntp-Posting-Host: %s.%s\n", 
		     anonHost, anonSite );
	if ( anonOther )
	    fprintf( whatFile, "%s\n", anonOther );
}


syntax highlighted by Code2HTML, v. 0.9.1