<commit_before>
/*
 * sendsbr.c -- routines to help WhatNow/Send along
 *
 * This code is Copyright (c) 2002, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include <h/mh.h>
#include <h/fmt_scan.h>
#include <h/fmt_compile.h>
#include <h/signals.h>
#include <setjmp.h>
#include <fcntl.h>
#include <h/mime.h>
#include <h/tws.h>
#include <h/utils.h>
#include <h/mts.h>

#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#include <time.h>

#ifdef OAUTH_SUPPORT
#include <h/oauth.h>

static int setup_oauth_params(char *[], int *, const char *, const char **);
#endif /* OAUTH_SUPPORT */

int debugsw = 0;		/* global */
int forwsw  = 1;
int inplace = 1;
int pushsw  = 0;
int splitsw = -1;
int unique  = 0;
int verbsw  = 0;

char *altmsg   = NULL;		/*  .. */
char *annotext = NULL;
char *distfile = NULL;

static jmp_buf env;

/*
 * static prototypes
 */
static void alert (char *, int);
static int tmp_fd (void);
static void anno (int, struct stat *);
static void annoaux (int);
static int splitmsg (char **, int, char *, char *, struct stat *, int);
static int sendaux (char **, int, char *, char *, struct stat *);
static void handle_sendfrom(char **, int *, char *, const char *);
static int get_from_header_info(const char *, const char **, const char **, const char **);
static const char *get_message_header_info(FILE *, char *);
static void merge_profile_entry(const char *, const char *, char *[], int *);
static void armed_done (int) NORETURN;

/*
 * Entry point into (back-end) routines to send message.
 */

int
sendsbr (char **vec, int vecp, char *program, char *draft, struct stat *st,
         int rename_drft, const char *auth_svc)
{
    int status, i;
    pid_t child;
    char buffer[BUFSIZ], file[BUFSIZ];
    struct stat sts;
    char **buildvec, *buildprogram;
    char *volatile drft = draft;
    /* nvecs is volatile to prevent warning from gcc about possible clobbering
       by longjmp. */
    volatile int nvecs = vecp;
    int *nvecsp = (int *) &nvecs;

    /*
     * Run the mimebuildproc (which is by default mhbuild) on the message
     * with the addition of the "-auto" flag
     */

    switch (child = fork()) {
    case NOTOK:
	adios("fork", "unable to");
	break;

    case OK:
	buildvec = argsplit(buildmimeproc, &buildprogram, &i);
	buildvec[i++] = "-auto";
	if (distfile)
	    buildvec[i++] = "-dist";
	buildvec[i++] = (char *) drft;
	buildvec[i] = NULL;
	execvp(buildprogram, buildvec);
	fprintf(stderr, "unable to exec ");
	perror(buildmimeproc);
	_exit(-1);
	break;

    default:
    	if (pidXwait(child, buildmimeproc))
	    return NOTOK;
	break;
    }

    done=armed_done;
    switch (setjmp (env)) {
    case OK:
	/*
	 * If given -push and -unique (which is undocumented), then
	 * rename the draft file.  I'm not quite sure why.
	 */
	if (pushsw && unique) {
	    char *cp = m_mktemp2(drft, invo_name, NULL, NULL);
	    if (cp == NULL) {
		adios(NULL, "unable to create temporary file in %s",
		      get_temp_dir());
	    }
	    if (rename (drft, strncpy(file, cp, sizeof(file))) == NOTOK)
		adios (file, "unable to rename %s to", drft);
	    drft = file;
	}

	/*
	 * Add in any necessary profile entries for xoauth
	 */

	if (auth_svc) {
		const char *errmsg;
		if (!setup_oauth_params(vec, nvecsp, auth_svc, &errmsg))
			adios(NULL, "%s", errmsg);
	}

        /*
         * Rework the vec based on From: header in draft, as specified
         * by sendfrom-address entries in profile.
         */
        if (context_find_prefix("sendfrom-")) {
            handle_sendfrom(vec, nvecsp, draft, auth_svc);<commit_msg>Check result of setup_oauth_params() for != OK instead of non-zero.<commit_after>
/*
 * sendsbr.c -- routines to help WhatNow/Send along
 *
 * This code is Copyright (c) 2002, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include <h/mh.h>
#include <h/fmt_scan.h>
#include <h/fmt_compile.h>
#include <h/signals.h>
#include <setjmp.h>
#include <fcntl.h>
#include <h/mime.h>
#include <h/tws.h>
#include <h/utils.h>
#include <h/mts.h>

#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#include <time.h>

#ifdef OAUTH_SUPPORT
#include <h/oauth.h>

static int setup_oauth_params(char *[], int *, const char *, const char **);
#endif /* OAUTH_SUPPORT */

int debugsw = 0;		/* global */
int forwsw  = 1;
int inplace = 1;
int pushsw  = 0;
int splitsw = -1;
int unique  = 0;
int verbsw  = 0;

char *altmsg   = NULL;		/*  .. */
char *annotext = NULL;
char *distfile = NULL;

static jmp_buf env;

/*
 * static prototypes
 */
static void alert (char *, int);
static int tmp_fd (void);
static void anno (int, struct stat *);
static void annoaux (int);
static int splitmsg (char **, int, char *, char *, struct stat *, int);
static int sendaux (char **, int, char *, char *, struct stat *);
static void handle_sendfrom(char **, int *, char *, const char *);
static int get_from_header_info(const char *, const char **, const char **, const char **);
static const char *get_message_header_info(FILE *, char *);
static void merge_profile_entry(const char *, const char *, char *[], int *);
static void armed_done (int) NORETURN;

/*
 * Entry point into (back-end) routines to send message.
 */

int
sendsbr (char **vec, int vecp, char *program, char *draft, struct stat *st,
         int rename_drft, const char *auth_svc)
{
    int status, i;
    pid_t child;
    char buffer[BUFSIZ], file[BUFSIZ];
    struct stat sts;
    char **buildvec, *buildprogram;
    char *volatile drft = draft;
    /* nvecs is volatile to prevent warning from gcc about possible clobbering
       by longjmp. */
    volatile int nvecs = vecp;
    int *nvecsp = (int *) &nvecs;

    /*
     * Run the mimebuildproc (which is by default mhbuild) on the message
     * with the addition of the "-auto" flag
     */

    switch (child = fork()) {
    case NOTOK:
	adios("fork", "unable to");
	break;

    case OK:
	buildvec = argsplit(buildmimeproc, &buildprogram, &i);
	buildvec[i++] = "-auto";
	if (distfile)
	    buildvec[i++] = "-dist";
	buildvec[i++] = (char *) drft;
	buildvec[i] = NULL;
	execvp(buildprogram, buildvec);
	fprintf(stderr, "unable to exec ");
	perror(buildmimeproc);
	_exit(-1);
	break;

    default:
    	if (pidXwait(child, buildmimeproc))
	    return NOTOK;
	break;
    }

    done=armed_done;
    switch (setjmp (env)) {
    case OK:
	/*
	 * If given -push and -unique (which is undocumented), then
	 * rename the draft file.  I'm not quite sure why.
	 */
	if (pushsw && unique) {
	    char *cp = m_mktemp2(drft, invo_name, NULL, NULL);
	    if (cp == NULL) {
		adios(NULL, "unable to create temporary file in %s",
		      get_temp_dir());
	    }
	    if (rename (drft, strncpy(file, cp, sizeof(file))) == NOTOK)
		adios (file, "unable to rename %s to", drft);
	    drft = file;
	}

	/*
	 * Add in any necessary profile entries for xoauth
	 */

	if (auth_svc) {
		const char *errmsg;
		if (setup_oauth_params(vec, nvecsp, auth_svc, &errmsg) != OK) {
			adios(NULL, errmsg);
		}
	}

        /*
         * Rework the vec based on From: header in draft, as specified
         * by sendfrom-address entries in profile.
         */
        if (context_find_prefix("sendfrom-")) {
            handle_sendfrom(vec, nvecsp, draft, auth_svc);
{'fraction_non_alphanumeric': 0.13905847239180572, 'fraction_numerical': 0.003527336860670194, 'mean_word_length': 3.191017623649801, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '4959364', 'n_tokens_mistral': 2699, 'n_tokens_neox': 2573, 'n_words': 835}