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

pull/2/head
Christian Neukirchen 8 years ago
parent 2073102d4d
commit c4822a983b

@ -6,7 +6,7 @@
.Nd encode MIME mails .Nd encode MIME mails
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl r .Op Fl c | Fl r
< <
.Ar message .Ar message
.Sh DESCRIPTION .Sh DESCRIPTION
@ -29,6 +29,10 @@ consisting of the contents of
.Pp .Pp
The options are as follows: The options are as follows:
.Bl -tag -width Ds .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 .It Fl r
Raw mode: don't expand MIME parts in the body, generate a Raw mode: don't expand MIME parts in the body, generate a
.Sq Li text/plain .Sq Li text/plain

@ -17,6 +17,7 @@
#include "blaze822.h" #include "blaze822.h"
static int cflag;
static int rflag; static int rflag;
int gen_b64(uint8_t *s, off_t size) int gen_b64(uint8_t *s, off_t size)
@ -322,30 +323,66 @@ gen_build()
gen_qp((uint8_t *)line, strlen(line), 78, 0); gen_qp((uint8_t *)line, strlen(line), 78, 0);
} }
if (!rflag) if (!rflag && !inheader)
printf("--%s--\n", sep); printf("--%s--\n", sep);
free(line); free(line);
return 0; 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 int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
srand48(time(0) ^ getpid()); srand48(time(0) ^ getpid());
int c; int c;
while ((c = getopt(argc, argv, "r")) != -1) while ((c = getopt(argc, argv, "cr")) != -1)
switch(c) { switch(c) {
case 'r': rflag = 1; break; case 'r': rflag = 1; break;
case 'c': cflag = 1; break;
default: default:
usage: usage:
fprintf(stderr, "Usage: mmime [-r] < message\n"); fprintf(stderr, "Usage: mmime [-c|-r] < message\n");
exit(1); exit(1);
} }
if (argc != optind) if (argc != optind)
goto usage; goto usage;
if (cflag)
return check();
return gen_build(); return gen_build();
} }

Loading…
Cancel
Save