view f-demime/initconv.c @ 6:cd2dcf6eed67

add top Makefile
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 06 May 2023 15:24:38 +0000
parents 7e0d08176f32
children
line wrap: on
line source

/*
 * This module contains functions that implement initiation of various
 * conversions performed by f-demime.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "defs.h"

extern enum msg_state msg_state;
extern char cont_te_buf[HDR_BUF_SIZE];
extern int got_cont_type, got_cont_te;
extern char *att_filename_base;
extern int qpdec_err_flag;

void (*dec_outf)();
FILE *tempfile;
int text_is_utf8;

static int
create_tempfile()
{
	char template[16];
	int fd;

	strcpy(template, "/tmp/fdemXXXXXX");
	fd = mkstemp(template);
	if (fd < 0) {
		perror("mkstemp");
		return(-1);
	}
	unlink(template);
	tempfile = fdopen(fd, "r+w");
	if (!tempfile) {
		perror("fdopen");
		close(fd);
		return(-1);
	}
	return(0);
}

static int
init_tempfile()
{
	int rc;

	rc = create_tempfile();
	if (!rc)
		return(0);
	if (got_cont_te)
		fputs(cont_te_buf, stdout);
	puts("X-Fdemime-Error: unable to create temp file for conversion");
	putchar('\n');
	msg_state = MSG_STATE_BODY_PASS;
	return(-1);
}

static void
implicit_text_plain()
{
	if (!got_cont_type)
		puts("Content-Type: text/plain (f-demime implied)");
}

static void
grok_charset_attr(csa)
	char *csa;
{
	text_is_utf8 = !strcasecmp(csa, "UTF-8") || !strcmp(csa, "csUTF8");
}

void
init_base64_text_plain(charset_attr)
	char *charset_attr;
{
	implicit_text_plain();
	if (init_tempfile() < 0)
		return;
	grok_charset_attr(charset_attr);
	base64_dec_init();
	ptext_conv_init();
	msg_state = MSG_STATE_PTEXT_B64;
}

void
init_qp_text_plain(charset_attr)
	char *charset_attr;
{
	implicit_text_plain();
	if (init_tempfile() < 0)
		return;
	grok_charset_attr(charset_attr);
	qpdec_err_flag = 0;
	ptext_conv_init();
	msg_state = MSG_STATE_PTEXT_QP;
}

void
init_base64_text_other()
{
	if (init_tempfile() < 0)
		return;
	base64_dec_init();
	b2q_conv_init();
	msg_state = MSG_STATE_B64_TO_QP;
}

void
init_base64_nontext()
{
	fputs(cont_te_buf, stdout);
	if (init_attach_out() < 0) {
		puts("X-Fdemime-Error: unable to create save file");
		putchar('\n');
		msg_state = MSG_STATE_BODY_PASS;
		return;
	}
	printf("X-Fdemime-Saved: %s\n", att_filename_base);
	base64_dec_init();
	msg_state = MSG_STATE_BLOB_B64;
}