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.
 
 
 

200 lines
4.1 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 <ctype.h>
#include <time.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\
");
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 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;
}
}
}
/* Main function. */
int
main(int argc, char *argv[])
{
char c;
string_buffer_t *headers;
struct option options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ NULL, 0, NULL, 0 }
};
setprogname(argv[0]);
headers = sb_new(0);
while ( (c = getopt_long(argc, argv, "hv",
options, NULL)) != -1 ) {
switch ( c ) {
case 'h':
usage(EXIT_SUCCESS);
break;
case '?':
usage(EXIT_FAILURE);
break;
case 'v':
info();
break;
}
}
sb_addf(headers, "Date: %s\r\n", rfc2822_date());
read_headers(stdin, headers);
fprintf(stdout, "%s\r\n", sb_get(headers));
qp_encode_stream(stdin, stdout);
return EXIT_SUCCESS;
}