A mail formatter.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

334 lines
7.2 KiB

/*
* fmail - Mail formatter
* Copyright (C) 2011 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <locale.h>
#include <ctype.h>
#include <time.h>
#include <gpgme.h>
#include <sbuffer.h>
/* Help and informations about the program. */
static void
usage(int status)
{
puts("\
Usage: fmail [options]\n\
Read a mail from standard input and send it.\n");
puts("Options:\n\
-h, --help Display this help message.\n\
-v, --version Display the version message.\n\
");
puts("\
Cryptography options:\n\
-s, --sign Sign the message.\n\
");
printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
exit(status);
}
static void
info(void)
{
printf("\
fmail %s\n\
Copyright (C) 2011 Damien Goutte-Gattat\n\
\n\
This program is released under the GNU General Public License.\n\
See the COPYING file or <http://www.gnu.org/licenses/gpl.html>.\n\
", VERSION);
exit(EXIT_SUCCESS);
}
/* Helper functions. */
static char *
generate_boundary(char *buffer, size_t len)
{
static const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
int i;
for ( i = 0; i < len - 1; i++ )
buffer[i] = chars[rand() % (sizeof(chars) - 1)];
buffer[i] = '\0';
return buffer;
}
static void
qp_encode_stream(FILE *in, FILE *out)
{
int c;
unsigned n;
n = 0;
while ( ! feof(in) ) {
c = fgetc(in);
if ( n >= 72 ) {
fprintf(out, "=\r\n");
n = 0;
}
if ( c == '\t' || c == ' ' ) {
int next = fgetc(in);
if ( next == '\n' ) {
fprintf(out, "=%02X", c);
n += 3;
}
else {
fputc(c, out);
n += 1;
}
ungetc(next, in);
}
else if ( c == '=' ) {
fprintf(out, "=%02X", c);
n += 3;
}
else if ( isprint(c) ) {
fputc(c, out);
n += 1;
}
else if ( c == '\n' ) {
fprintf(out, "\r\n");
n = 0;
}
else if ( c != EOF ) {
fprintf(out, "=%02X", c);
n += 3;
}
}
}
static char *
rfc2822_date(void)
{
time_t timestamp;
struct tm *timestruct;
const char *rfc2822_format = "%a, %d %b %Y %H:%M:%S %z";
static char buffer[32];
timestamp = time(NULL);
timestruct = localtime(&timestamp);
strftime(buffer, sizeof(buffer), rfc2822_format, timestruct);
return buffer;
}
static void
read_headers(FILE *in, string_buffer_t *headers)
{
int c, empty_line;
size_t n;
n = empty_line = 0;
while ( ! empty_line ) {
c = fgetc(in);
if ( c == '\n' ) {
if ( n == 0 )
empty_line = 1;
else {
sb_add(headers, "\r\n");
n = 0;
}
}
else {
sb_addc(headers, c);
n += 1;
}
}
}
static void
process_text_body(FILE *in, FILE *out)
{
fprintf(out, "Content-Type: text/plain; charset=\"utf-8\"\r\n"
"Content-Transfer-Encoding: quoted-printable\r\n"
"Content-Disposition: inline\r\n\r\n");
qp_encode_stream(in, out);
fprintf(out, "\r\n");
}
/* Signing stuff. */
gpgme_ctx_t
initialize_gpgme(void)
{
gpgme_ctx_t ctx;
gpg_error_t gerr;
gpgme_check_version(NULL);
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
if ( (gerr = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR )
errx(EXIT_FAILURE, "cannot initialize GPGME: %s",
gpgme_strerror(gerr));
gpgme_set_armor(ctx, 1);
return ctx;
}
static void
sign_stream(gpgme_ctx_t ctx, FILE *in, FILE *out)
{
gpgme_data_t gin, gout;
char boundary[32], buffer[512];
int n;
gpgme_data_new_from_stream(&gin, in);
gpgme_data_new(&gout);
gpgme_op_sign(ctx, gin, gout, GPGME_SIG_MODE_DETACH);
generate_boundary(boundary, sizeof(boundary));
fprintf(out, "Content-Type: multipart/signed;\r\n"
" boundary=\"%s\";\r\n"
" protocol=\"application/pgp-signature\";\r\n"
" micalg=pgp-sha1\r\n"
"\r\n"
"--%s\r\n",
boundary, boundary);
fseek(in, 0, SEEK_SET);
while ( (n = fread(buffer, 1, sizeof(buffer), in)) > 0 )
fwrite(buffer, 1, n, out);
fprintf(out, "\r\n"
"--%s\r\n"
"Content-Type: application/pgp-signature; name=signature.asc\r\n"
"\r\n",
boundary);
gpgme_data_seek(gout, 0, SEEK_SET);
while ( (n = gpgme_data_read(gout, buffer, sizeof(buffer))) > 0 ) {
int i = 0;
while ( i < n ) {
if ( buffer[i] == '\n' )
fputc('\r', out);
fputc(buffer[i], out);
i += 1;
}
}
fprintf(out, "\r\n--%s--\r\n", boundary);
gpgme_data_release(gin);
gpgme_data_release(gout);
}
/* Main function. */
int
main(int argc, char *argv[])
{
char c;
string_buffer_t *headers;
gpgme_ctx_t sign_ctx;
struct option options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ "sign", 0, NULL, 's' },
{ NULL, 0, NULL, 0 }
};
setprogname(argv[0]);
setlocale(LC_ALL, "");
srand(time(NULL));
headers = sb_new(0);
sign_ctx = NULL;
while ( (c = getopt_long(argc, argv, "hvs",
options, NULL)) != -1 ) {
switch ( c ) {
case 'h':
usage(EXIT_SUCCESS);
break;
case '?':
usage(EXIT_FAILURE);
break;
case 'v':
info();
break;
case 's':
sign_ctx = initialize_gpgme();
break;
}
}
/* Generate automatic headers */
sb_addf(headers, "Date: %s\r\n", rfc2822_date());
sb_addf(headers, "User-Agent: fmail %s\r\n", VERSION);
/* Read user-provided headers. */
read_headers(stdin, headers);
/* Write all headers. */
fprintf(stdout, "%s", sb_get(headers));
if ( sign_ctx ) {
FILE *tmp = tmpfile();
process_text_body(stdin, tmp);
fseek(tmp, 0, SEEK_SET);
sign_stream(sign_ctx, tmp, stdout);
fclose(tmp);
gpgme_release(sign_ctx);
}
else
process_text_body(stdin, stdout);
return EXIT_SUCCESS;
}