<commit_before>#include "config.h"

#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/stat.h>

#ifdef HAVE_LIBSSL
#include <openssl/ssl.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#endif /* HAVE_LIBSSL */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <syslog.h>
#include <fcntl.h>
#include <sysexits.h>
#include <utime.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <syslog.h>

#include <snet.h>

#include "denser.h"
#include "ll.h"
#include "queue.h"
#include "envelope.h"
#include "ml.h"
#include "line_file.h"
#include "smtp.h"
#include "expand.h"
#include "simta.h"


    int
bounce_text( struct envelope *bounce_env, char *t1, char *t2, char *t3 )
{
    char			*text;
    size_t			len;

    if ( bounce_env->e_err_text == NULL ) {
	if (( bounce_env->e_err_text = line_file_create()) == NULL ) {
	    syslog( LOG_ERR, "bounce_text line_file_create: %m" );
	    return( -1 );
	}
    }

    if ( t3 != NULL ) {
	len = strlen( t1 ) + strlen( t2 ) + strlen( t3 ) + 1;

	if (( text = (char*)malloc( len )) == NULL ) {
	    syslog( LOG_ERR, "bounce_text malloc: %m" );
	    return( -1 );
	}
	sprintf( text, "%s%s%s", t1, t2, t3 );

	if ( line_append( bounce_env->e_err_text, text, NO_COPY ) == NULL ) {
	    syslog( LOG_ERR, "bounce_text line_append: %m" );
	    free( text );
	    return( -1 );
	}

    } else if ( t2 != NULL ) {
	len = strlen( t1 ) + strlen( t2 ) + 1;

	if (( text = (char*)malloc( len )) == NULL ) {
	    syslog( LOG_ERR, "bounce_text malloc: %m" );
	    return( -1 );
	}
	sprintf( text, "%s%s", t1, t2 );

	if ( line_append( bounce_env->e_err_text, text, NO_COPY ) == NULL ) {
	    syslog( LOG_ERR, "bounce_text line_append: %m" );
	    free( text );
	    return( -1 );
	}

    } else {
	if ( line_append( bounce_env->e_err_text, t1, COPY ) == NULL ) {
	    syslog( LOG_ERR, "bounce_text line_append: %m" );
	    return( -1 );
	}
    }

    return( 0 );
}


    void
bounce_stdout( struct envelope *bounce_env )
{
    struct line                 *l;
    struct recipient		*r;

    if (( bounce_env->e_err_text == NULL ) ||
	    (( l = bounce_env->e_err_text->l_first ) == NULL )) {
	return;
    }

    printf( "***   Bounce Message %s  ***\n", bounce_env->e_id );

    /* dfile message headers */
    printf(  "From: mailer-daemon@%s\n", simta_hostname );
    for ( r = bounce_env->e_rcpt; r != NULL; r = r->r_next ) {
	printf(  "To: <%s>\n", r->r_rcpt );
    }
    printf(  "\n" );

    while ( l != NULL ) {
	printf(  "%s\n", l->line_data );
	l = l->line_next;
    }
}


    ino_t
bounce_dfile_out( struct envelope *bounce_env, SNET *message )
{
    int				ret = 0;
    char                        dfile_fname[ MAXPATHLEN ];
    int                         dfile_fd;
    FILE                        *dfile;
    struct line                 *l;
    char                        *line;
    time_t                      clock;
    struct tm                   *tm;
    char                        daytime[ 35 ];
    struct stat			sbuf;
    struct recipient		*r;

    sprintf( dfile_fname, "%s/D%s", bounce_env->e_dir, bounce_env->e_id );

    if (( dfile_fd = open( dfile_fname, O_WRONLY | O_CREAT | O_EXCL, 0600 ))
            < 0 ) {
        syslog( LOG_ERR, "bounce_dfile_out open %s: %m", dfile_fname );
	return( 0 );
    }

    if (( dfile = fdopen( dfile_fd, "w" )) == NULL ) {
	syslog( LOG_ERR, "bounce_dfile_out fdopen %s: %m", dfile_fname );
        if ( close( dfile_fd ) != 0 ) {
	    syslog( LOG_ERR, "bounce_dfile_out fclose %s: %m", dfile_fname );
	}
	return( 0 );
    }

    if ( time( &clock ) < 0 ) {
        syslog( LOG_ERR, "bounce_dfile_out time: %m" );
        goto cleanup;
    }

    if (( tm = localtime( &clock )) == NULL ) {
        syslog( LOG_ERR, "bounce_dfile_out localtime: %m" );
        goto cleanup;
    }

    if ( strftime( daytime, sizeof( daytime ), "%a, %e %b %Y %T", tm )
            == 0 ) {
        syslog( LOG_ERR, "bounce_dfile_out strftime: %m" );
        goto cleanup;
    }

    /* dfile message headers */
    if ( *(bounce_env->e_mail) == '\0' ) {
	fprintf( dfile, "From: mailer-daemon@%s\n", simta_hostname );
    } else {
	fprintf( dfile, "From: %s\n", bounce_env->e_mail );
    }
    for ( r = bounce_env->e_rcpt; r != NULL; r = r->r_next ) {
	printf(  "To: <%s>\n", r->r_rcpt );
    }
    fprintf( dfile, "Date: %s\n", daytime );
    fprintf( dfile, "Message-ID: <%s@%s>\n", bounce_env->e_id, simta_hostname );
    fprintf( dfile, "Subject: undeliverable mail\n" );
    fprintf( dfile, "\n" );

    for ( l = bounce_env->e_err_text->l_first; l != NULL; l = l->line_next ) {
	fprintf( dfile, "%s\n", l->line_data );
    }
    fprintf( dfile, "\n" );

    if ( message != NULL ) {
	fprintf( dfile, "\n" );
	fprintf( dfile, "Bounced message:\n" );
	fprintf( dfile, "\n" );

	while (( line = snet_getline( message, NULL )) != NULL ) {
	    fprintf( dfile, "%s\n", line );
	}
    }

    if ( fstat( dfile_fd, &sbuf ) != 0 ) {
	syslog( LOG_ERR, "bounce_dfile_out fstat %s: %m", dfile_fname );
        goto cleanup;
    }
<commit_msg>bounce address is always mailer-daemon<commit_after>#include "config.h"

#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/stat.h>

#ifdef HAVE_LIBSSL
#include <openssl/ssl.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#endif /* HAVE_LIBSSL */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <syslog.h>
#include <fcntl.h>
#include <sysexits.h>
#include <utime.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <syslog.h>

#include <snet.h>

#include "denser.h"
#include "ll.h"
#include "queue.h"
#include "envelope.h"
#include "ml.h"
#include "line_file.h"
#include "smtp.h"
#include "expand.h"
#include "simta.h"


    int
bounce_text( struct envelope *bounce_env, char *t1, char *t2, char *t3 )
{
    char			*text;
    size_t			len;

    if ( bounce_env->e_err_text == NULL ) {
	if (( bounce_env->e_err_text = line_file_create()) == NULL ) {
	    syslog( LOG_ERR, "bounce_text line_file_create: %m" );
	    return( -1 );
	}
    }

    if ( t3 != NULL ) {
	len = strlen( t1 ) + strlen( t2 ) + strlen( t3 ) + 1;

	if (( text = (char*)malloc( len )) == NULL ) {
	    syslog( LOG_ERR, "bounce_text malloc: %m" );
	    return( -1 );
	}
	sprintf( text, "%s%s%s", t1, t2, t3 );

	if ( line_append( bounce_env->e_err_text, text, NO_COPY ) == NULL ) {
	    syslog( LOG_ERR, "bounce_text line_append: %m" );
	    free( text );
	    return( -1 );
	}

    } else if ( t2 != NULL ) {
	len = strlen( t1 ) + strlen( t2 ) + 1;

	if (( text = (char*)malloc( len )) == NULL ) {
	    syslog( LOG_ERR, "bounce_text malloc: %m" );
	    return( -1 );
	}
	sprintf( text, "%s%s", t1, t2 );

	if ( line_append( bounce_env->e_err_text, text, NO_COPY ) == NULL ) {
	    syslog( LOG_ERR, "bounce_text line_append: %m" );
	    free( text );
	    return( -1 );
	}

    } else {
	if ( line_append( bounce_env->e_err_text, t1, COPY ) == NULL ) {
	    syslog( LOG_ERR, "bounce_text line_append: %m" );
	    return( -1 );
	}
    }

    return( 0 );
}


    void
bounce_stdout( struct envelope *bounce_env )
{
    struct line                 *l;
    struct recipient		*r;

    if (( bounce_env->e_err_text == NULL ) ||
	    (( l = bounce_env->e_err_text->l_first ) == NULL )) {
	return;
    }

    printf( "***   Bounce Message %s  ***\n", bounce_env->e_id );

    /* dfile message headers */
    printf(  "From: mailer-daemon@%s\n", simta_hostname );
    for ( r = bounce_env->e_rcpt; r != NULL; r = r->r_next ) {
	printf(  "To: <%s>\n", r->r_rcpt );
    }
    printf(  "\n" );

    while ( l != NULL ) {
	printf(  "%s\n", l->line_data );
	l = l->line_next;
    }
}


    ino_t
bounce_dfile_out( struct envelope *bounce_env, SNET *message )
{
    int				ret = 0;
    char                        dfile_fname[ MAXPATHLEN ];
    int                         dfile_fd;
    FILE                        *dfile;
    struct line                 *l;
    char                        *line;
    time_t                      clock;
    struct tm                   *tm;
    char                        daytime[ 35 ];
    struct stat			sbuf;
    struct recipient		*r;

    sprintf( dfile_fname, "%s/D%s", bounce_env->e_dir, bounce_env->e_id );

    if (( dfile_fd = open( dfile_fname, O_WRONLY | O_CREAT | O_EXCL, 0600 ))
            < 0 ) {
        syslog( LOG_ERR, "bounce_dfile_out open %s: %m", dfile_fname );
	return( 0 );
    }

    if (( dfile = fdopen( dfile_fd, "w" )) == NULL ) {
	syslog( LOG_ERR, "bounce_dfile_out fdopen %s: %m", dfile_fname );
        if ( close( dfile_fd ) != 0 ) {
	    syslog( LOG_ERR, "bounce_dfile_out fclose %s: %m", dfile_fname );
	}
	return( 0 );
    }

    if ( time( &clock ) < 0 ) {
        syslog( LOG_ERR, "bounce_dfile_out time: %m" );
        goto cleanup;
    }

    if (( tm = localtime( &clock )) == NULL ) {
        syslog( LOG_ERR, "bounce_dfile_out localtime: %m" );
        goto cleanup;
    }

    if ( strftime( daytime, sizeof( daytime ), "%a, %e %b %Y %T", tm )
            == 0 ) {
        syslog( LOG_ERR, "bounce_dfile_out strftime: %m" );
        goto cleanup;
    }

    /* dfile message headers */
    fprintf( dfile, "From: mailer-daemon@%s\n", simta_hostname );
    for ( r = bounce_env->e_rcpt; r != NULL; r = r->r_next ) {
	printf(  "To: <%s>\n", r->r_rcpt );
    }
    fprintf( dfile, "Date: %s\n", daytime );
    fprintf( dfile, "Message-ID: <%s@%s>\n", bounce_env->e_id, simta_hostname );
    fprintf( dfile, "Subject: undeliverable mail\n" );
    fprintf( dfile, "\n" );

    for ( l = bounce_env->e_err_text->l_first; l != NULL; l = l->line_next ) {
	fprintf( dfile, "%s\n", l->line_data );
    }
    fprintf( dfile, "\n" );

    if ( message != NULL ) {
	fprintf( dfile, "\n" );
	fprintf( dfile, "Bounced message:\n" );
	fprintf( dfile, "\n" );

	while (( line = snet_getline( message, NULL )) != NULL ) {
	    fprintf( dfile, "%s\n", line );
	}
    }

    if ( fstat( dfile_fd, &sbuf ) != 0 ) {
	syslog( LOG_ERR, "bounce_dfile_out fstat %s: %m", dfile_fname );
        goto cleanup;
    }

{'fraction_non_alphanumeric': 0.17390054613394654, 'fraction_numerical': 0.0075692248730478105, 'mean_word_length': 2.418932197838192, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1098590', 'n_tokens_mistral': 4340, 'n_tokens_neox': 4169, 'n_words': 971}