mblaze

Unix utilities to deal with Maildir - my mirror
git clone https://pi.duncano.de/git/mblaze.git
Log | Files | Refs | README | COPYING

commit c4822a983be3726e6a07cabb245680c8b728e940
parent 2073102d4d2231dd2f341c9dc8bde9ea38274369
Author: Christian Neukirchen <chneukirchen@gmail.com>
Date:   Tue,  2 Aug 2016 14:09:28 +0200

mmime: add -c to check if MIME-encoding is needed

Diffstat:
man/mmime.1 | 6+++++-
mmime.c | 43++++++++++++++++++++++++++++++++++++++++---
2 files changed, 45 insertions(+), 4 deletions(-)

diff --git 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 @@ -17,6 +17,7 @@ #include "blaze822.h" +static int cflag; static int rflag; int gen_b64(uint8_t *s, off_t size) @@ -322,7 +323,7 @@ gen_build() gen_qp((uint8_t *)line, strlen(line), 78, 0); } - if (!rflag) + if (!rflag && !inheader) printf("--%s--\n", sep); free(line); @@ -330,22 +331,58 @@ gen_build() } 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(); }