rohrpost

A commandline mail client to change the world as we see it.
git clone git://r-36.net/rohrpost
Log | Files | Refs | LICENSE

commit fd58a3320d9f5f3e9175153bc77389a9acbd176b
parent eeb642fbf88b64e1120f8aeba85dd3b699e3c2be
Author: Christoph Lohmann <20h@r-36.net>
Date:   Fri,  7 Mar 2014 20:41:15 +0100

Enable raw output of parts.

This is needed for encrypted emails.

Diffstat:
ind.c | 4++--
ind.h | 2+-
mime.c | 7++++++-
mime.h | 3+++
pager.c | 2+-
part.c | 51++++++++++++++++++++++++++++++++++-----------------
path.c | 1+
sieve.c | 2+-
8 files changed, 49 insertions(+), 23 deletions(-)

diff --git a/ind.c b/ind.c @@ -304,11 +304,11 @@ writeallfd(int fd, void *data, int len) } int -writefile(char *file, void *data, int len) +writefile(char *file, void *data, int len, char *mode) { FILE *fd; - fd = fopen(file, "w+"); + fd = fopen(file, mode); if (fd == NULL) return 1; if (writeall(fd, data, len)) diff --git a/ind.h b/ind.h @@ -32,7 +32,7 @@ char *expandhome(char *path); int writeall(FILE *fd, void *data, int len); int writeallfd(int fd, void *data, int len); -int writefile(char *file, void *data, int len); +int writefile(char *file, void *data, int len, char *mode); int getfilesize(char *file); char *readtoeof(FILE *fd, int *len); char *readtoeoffd(int fd, int *len); diff --git a/mime.c b/mime.c @@ -71,6 +71,8 @@ mime_free(mime_t *mime) free(mime->charset); if (mime->boundary != NULL) free(mime->boundary); + if (mime->rawhdrs != NULL) + free(mime->rawhdrs); free(mime); } @@ -900,8 +902,11 @@ mime_parsebufintern(mime_t *mime, char *str, int len) /* * End of headers. */ - if (strlen(buf) == 0) + if (strlen(buf) == 0) { + mime->rawhdrs = memdupz(str, (p - str)); + mime->rawhdrslen = p - str; goto mimeparsebufbodyparse; + } /* * "key: value" diff --git a/mime.h b/mime.h @@ -18,6 +18,9 @@ struct mime_t { char *body; int bodylen; + char *rawhdrs; + int rawhdrslen; + char *partid; char *ct; char *cte; diff --git a/pager.c b/pager.c @@ -66,7 +66,7 @@ runstring(void (*runner)(char *), char *str, int docmp) int tfd, len; tname = mktmpfile("rohrpost", &tfd); - if (writefile(tname, str, strlen(str))) + if (writefile(tname, str, strlen(str), "w+")) edie("runstring writefile"); runner(tname); diff --git a/part.c b/part.c @@ -49,7 +49,7 @@ part_print(char *id, mime_t *mime) int part_write(char *id, mime_t *mime, llist_t *partl, char *type, - char *filename, int bequiet, int tostdout) + char *filename, int bequiet, int tostdout, int raw) { llistelem_t *part; char *content, *lfilename, *lcontent; @@ -76,21 +76,32 @@ part_write(char *id, mime_t *mime, llist_t *partl, char *type, } clen = 0; - content = mime_decodepartencoding(mime, &clen); - if (content == NULL) { - lcontent = mime->body; - clen = mime->bodylen; - } else { - lcontent = content; + lcontent = mime->body; + clen = mime->bodylen; + if (!raw) { + content = mime_decodepartencoding(mime, &clen); + if (content != NULL) { + lcontent = content; + } else { + clen = mime->bodylen; + } } //printf("mime->body = \"%s\"\ncontent = \"%s\"\n", // mime->body, lcontent); if (tostdout) { + if (raw) + writeallfd(1, mime->rawhdrs, mime->rawhdrslen); writeallfd(1, lcontent, clen); } else { - if (writefile(lfilename, lcontent, clen)) + if (raw) { + writefile(lfilename, mime->rawhdrs, + mime->rawhdrslen, "w+"); + } + if (writefile(lfilename, lcontent, clen, + (raw)? "a+" : "w+")) { edie("Could not write file %s.", lfilename); + } } if (!bequiet && !tostdout) @@ -105,8 +116,10 @@ part_write(char *id, mime_t *mime, llist_t *partl, char *type, if (mime->parts->len > 0) { forllist(mime->parts, part) { if (!part_write(id, (mime_t *)part->data, partl, - type, filename, bequiet, tostdout)) + type, filename, bequiet, tostdout, + raw)) { dowrite = 1; + } } } @@ -116,7 +129,7 @@ part_write(char *id, mime_t *mime, llist_t *partl, char *type, void partusage(void) { - die("usage: %s [-hq] [-c cfg] [-m folder] [-s|-g|-f filename] " + die("usage: %s [-hqr] [-c cfg] [-m folder] [-s|-g|-f filename] " "[-t type|-p part|-a|-l]" " [msgs]\n", argv0); } @@ -139,8 +152,7 @@ partmain(int argc, char *argv[]) LISTPARTS = 0x04, GIVENNAME = 0x08, TOSTDOUT = 0x10, - - NOARGS = 0x20, + RAW = 0x20, }; retc = 1; @@ -176,6 +188,9 @@ partmain(int argc, char *argv[]) case 'q': status |= BEQUIET; break; + case 'r': + status |= RAW; + break; case 's': status |= TOSTDOUT; break; @@ -227,9 +242,10 @@ partmain(int argc, char *argv[]) } else { retc = part_write(NULL, mime, partl, type, filename, status & BEQUIET, - status & TOSTDOUT); + status & TOSTDOUT, status & RAW); } + llist_free(partl); mime_free(mime); return retc; @@ -259,11 +275,14 @@ partmain(int argc, char *argv[]) imap_die(imap, "imap_init"); if (imap_select(imap, selected)) imap_die(imap, "imap_select"); + free(selected); msgs = imap_fetchraw(imap, ids); if (msgs == NULL) imap_die(imap, "imap_fetchraw"); llist_efree(ids); + imap_close(imap); + imap_free(imap); forllist(msgs, msg) { elem = llist_get((llist_t *)msg->data, "literal"); @@ -283,16 +302,14 @@ partmain(int argc, char *argv[]) } else { retc = part_write((char *)ide->data, mime, partl, type, filename, status & BEQUIET, - status & TOSTDOUT); + status & TOSTDOUT, status & RAW); } mime_free(mime); } + llist_free(partl); llist_efree(msgs); - free(selected); - imap_close(imap); - imap_free(imap); return retc; } diff --git a/path.c b/path.c @@ -125,6 +125,7 @@ path_mkbasepath(char *cfgn) if (chdir(cwd) < 0) edie("chdir"); + free(cwd); return rpath; } diff --git a/sieve.c b/sieve.c @@ -581,7 +581,7 @@ sievemain(int argc, char *argv[]) sieve_die("sieve_getscript"); if (argc > 1) { - if (writefile(argv[1], data, strlen(data))) + if (writefile(argv[1], data, strlen(data), "w+")) edie("writefile"); free(data); return 0;