aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2016-08-02 14:09:28 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2016-08-02 14:09:28 +0200
commitc4822a983be3726e6a07cabb245680c8b728e940 (patch)
tree59b62479ce34b3e500dc1fa1fc94fe885a09505e
parent2073102d4d2231dd2f341c9dc8bde9ea38274369 (diff)
downloadmblaze-c4822a983be3726e6a07cabb245680c8b728e940.tar.gz
mmime: add -c to check if MIME-encoding is needed
-rw-r--r--man/mmime.16
-rw-r--r--mmime.c43
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,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();
}