mblaze

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

commit 27b21d7e204cfa17723702d3c04c0ee6df603fbf
parent 56e89c9a557cd1182001a8b1130d7f10f40cc96a
Author: Christian Neukirchen <chneukirchen@gmail.com>
Date:   Tue,  2 Aug 2016 16:13:25 +0200

rfc2045: import walk_mime

Diffstat:
blaze822.h | 4++++
mshow.c | 69+++++++++++++--------------------------------------------------------
rfc2045.c | 34++++++++++++++++++++++++++++++++++
3 files changed, 51 insertions(+), 56 deletions(-)

diff --git a/blaze822.h b/blaze822.h @@ -37,6 +37,10 @@ int blaze822_mime_body(struct message *msg, char **cto, char **bodyo, size_t *bo int blaze822_multipart(struct message *msg, struct message **imsg); int blaze822_mime_parameter(char *s, char *name, char **starto, char **stopo); +typedef enum { MIME_CONTINUE, MIME_STOP, MIME_PRUNE } blaze822_mime_action; +typedef blaze822_mime_action (*blaze822_mime_callback)(int, struct message *, char *, size_t); +blaze822_mime_action blaze822_walk_mime(struct message *, int, blaze822_mime_callback); + // seq.c char *blaze822_seq_open(char *file); diff --git a/mshow.c b/mshow.c @@ -144,16 +144,6 @@ tlmimetype(char *ct) return strndup(ct, s-ct); } -typedef enum { - MIME_CONTINUE, - MIME_STOP, - MIME_PRUNE, -} mime_action; - -typedef mime_action (*mime_callback)(int, struct message *, char *, size_t); - -mime_action walk_mime(struct message *msg, int depth, mime_callback visit); - char * mime_filename(struct message *msg) { @@ -172,7 +162,7 @@ mime_filename(struct message *msg) static void choose_alternative(struct message *msg, int depth); -mime_action +blaze822_mime_action render_mime(int depth, struct message *msg, char *body, size_t bodylen) { char *ct = blaze822_hdr(msg, "content-type"); @@ -194,7 +184,7 @@ render_mime(int depth, struct message *msg, char *body, size_t bodylen) } char *cmd; - mime_action r = MIME_CONTINUE; + blaze822_mime_action r = MIME_CONTINUE; if (filters && ((cmd = blaze822_chdr(filters, mt)) || @@ -222,7 +212,7 @@ render_mime(int depth, struct message *msg, char *body, size_t bodylen) printf(" filter=\"%s\" ---\n", cmd); struct message *imsg = blaze822_mem(output, outlen); if (imsg) - walk_mime(imsg, depth+1, render_mime); + blaze822_walk_mime(imsg, depth+1, render_mime); blaze822_free(imsg); } else if (e >= 65 && e <= 80) { // choose N-64th part struct message *imsg = 0; @@ -230,7 +220,7 @@ render_mime(int depth, struct message *msg, char *body, size_t bodylen) printf(" selector=\"%s\" part=%d ---\n", cmd, n); while (blaze822_multipart(msg, &imsg)) { if (--n == 0) - walk_mime(imsg, depth+1, render_mime); + blaze822_walk_mime(imsg, depth+1, render_mime); } blaze822_free(imsg); } else { @@ -272,7 +262,7 @@ nofilter: r = MIME_PRUNE; } else if (strncmp(ct, "multipart/", 10) == 0) { - ; // default mime_walk action + ; // default blaze822_mime_walk action } else { printf("no filter or default handler\n"); } @@ -313,11 +303,11 @@ choose_alternative(struct message *msg, int depth) imsg = 0; while (blaze822_multipart(msg, &imsg)) if (--n == 0) - walk_mime(imsg, depth+1, render_mime); + blaze822_walk_mime(imsg, depth+1, render_mime); blaze822_free(imsg); } -mime_action +blaze822_mime_action reply_mime(int depth, struct message *msg, char *body, size_t bodylen) { (void) depth; @@ -346,7 +336,7 @@ reply_mime(int depth, struct message *msg, char *body, size_t bodylen) return MIME_CONTINUE; } -mime_action +blaze822_mime_action list_mime(int depth, struct message *msg, char *body, size_t bodylen) { (void) body; @@ -367,39 +357,6 @@ list_mime(int depth, struct message *msg, char *body, size_t bodylen) return MIME_CONTINUE; } -mime_action -walk_mime(struct message *msg, int depth, mime_callback visit) -{ - char *ct, *body, *bodychunk; - size_t bodylen; - - mime_action r = MIME_CONTINUE; - - if (blaze822_mime_body(msg, &ct, &body, &bodylen, &bodychunk)) { - - mime_action r = visit(depth, msg, body, bodylen); - - if (r == MIME_CONTINUE) { - if (strncmp(ct, "multipart/", 10) == 0) { - struct message *imsg = 0; - while (blaze822_multipart(msg, &imsg)) { - r = walk_mime(imsg, depth+1, visit); - if (r == MIME_STOP) - break; - } - } else if (strncmp(ct, "message/rfc822", 14) == 0) { - struct message *imsg = blaze822_mem(body, bodylen); - if (imsg) - walk_mime(imsg, depth+1, visit); - } - } - - free(bodychunk); - } - - return r; -} - void list(char *file) { @@ -408,7 +365,7 @@ list(char *file) return; mimecount = 0; printf("%s\n", file); - walk_mime(msg, 0, list_mime); + blaze822_walk_mime(msg, 0, list_mime); } void @@ -417,7 +374,7 @@ reply(char *file) struct message *msg = blaze822_file(file); if (!msg) return; - walk_mime(msg, 0, reply_mime); + blaze822_walk_mime(msg, 0, reply_mime); } static int extract_argc; @@ -458,7 +415,7 @@ writefile(char *name, char *buf, ssize_t len) return 0; } -mime_action +blaze822_mime_action extract_mime(int depth, struct message *msg, char *body, size_t bodylen) { (void) body; @@ -525,7 +482,7 @@ extract_cb(char *file) if (!msg) return; mimecount = 0; - walk_mime(msg, 0, extract_mime); + blaze822_walk_mime(msg, 0, extract_mime); } void @@ -708,7 +665,7 @@ show(char *file) } mimecount = 0; - walk_mime(msg, 0, render_mime); + blaze822_walk_mime(msg, 0, render_mime); done: blaze822_free(msg); diff --git a/rfc2045.c b/rfc2045.c @@ -2,6 +2,7 @@ #include <strings.h> #include <string.h> +#include <stdlib.h> #include "blaze822.h" #include "blaze822_priv.h" @@ -165,3 +166,36 @@ blaze822_multipart(struct message *msg, struct message **imsg) return 1; } + +blaze822_mime_action +blaze822_walk_mime(struct message *msg, int depth, blaze822_mime_callback visit) +{ + char *ct, *body, *bodychunk; + size_t bodylen; + + blaze822_mime_action r = MIME_CONTINUE; + + if (blaze822_mime_body(msg, &ct, &body, &bodylen, &bodychunk)) { + + r = visit(depth, msg, body, bodylen); + + if (r == MIME_CONTINUE) { + if (strncmp(ct, "multipart/", 10) == 0) { + struct message *imsg = 0; + while (blaze822_multipart(msg, &imsg)) { + r = blaze822_walk_mime(imsg, depth+1, visit); + if (r == MIME_STOP) + break; + } + } else if (strncmp(ct, "message/rfc822", 14) == 0) { + struct message *imsg = blaze822_mem(body, bodylen); + if (imsg) + blaze822_walk_mime(imsg, depth+1, visit); + } + } + + free(bodychunk); + } + + return r; +}