From c4822a983be3726e6a07cabb245680c8b728e940 Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Tue, 2 Aug 2016 14:09:28 +0200 Subject: mmime: add -c to check if MIME-encoding is needed --- man/mmime.1 | 6 +++++- mmime.c | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/man/mmime.1 b/man/mmime.1 index 1118850..ad7f3ba 100644 --- a/man/mmime.1 +++ b/man/mmime.1 @@ -6,7 +6,7 @@ .Nd encode MIME mails .Sh SYNOPSIS .Nm -.Op Fl r +.Op Fl c | Fl r < .Ar message .Sh DESCRIPTION @@ -29,6 +29,10 @@ consisting of the contents of .Pp The options are as follows: .Bl -tag -width Ds +.It Fl c +Check mode: don't output anything, +exit with status 1 if MIME-encoding the message is required, +or with status 0 else. .It Fl r Raw mode: don't expand MIME parts in the body, generate a .Sq Li text/plain diff --git a/mmime.c b/mmime.c index 93eb01b..8dc5a59 100644 --- a/mmime.c +++ b/mmime.c @@ -17,6 +17,7 @@ #include "blaze822.h" +static int cflag; static int rflag; int gen_b64(uint8_t *s, off_t size) @@ -322,30 +323,66 @@ gen_build() gen_qp((uint8_t *)line, strlen(line), 78, 0); } - if (!rflag) + if (!rflag && !inheader) printf("--%s--\n", sep); free(line); return 0; } +int +check() +{ + off_t bithigh = 0; + off_t bitlow = 0; + off_t linelen = 0; + off_t maxlinelen = 0; + + int c; + int l = -1; + + while ((c = getchar()) != EOF) { + if (c == '\n') { + if (maxlinelen < linelen) + maxlinelen = linelen; + linelen = 0; + } else { + linelen++; + } + if (c != '\t' && c != '\n' && c < 32) + bitlow++; + if (c > 127) + bithigh++; + l = c; + } + + if (bitlow == 0 && bithigh == 0 && maxlinelen <= 72 && l == '\n') + return 0; + else + return 1; +} + int main(int argc, char *argv[]) { srand48(time(0) ^ getpid()); int c; - while ((c = getopt(argc, argv, "r")) != -1) + while ((c = getopt(argc, argv, "cr")) != -1) switch(c) { case 'r': rflag = 1; break; + case 'c': cflag = 1; break; default: usage: - fprintf(stderr, "Usage: mmime [-r] < message\n"); + fprintf(stderr, "Usage: mmime [-c|-r] < message\n"); exit(1); } if (argc != optind) goto usage; + if (cflag) + return check(); + return gen_build(); } -- cgit 1.4.1