/* strippayment.c - Remove a multipart/postage container from a message Copyright (C) 2003 Ian Gulliver This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include "firemake.h" void foldback(struct firestring_estr_t *headers, struct firestring_estr_t *name) { struct firestring_estr_t *fullheader; fullheader = firemime_get_full_header(headers,name,&ESTR_S("\n")); if (fullheader == NULL) return; memmove(fullheader->s,fullheader->s + fullheader->l,headers->l - (fullheader->s - headers->s + fullheader->l)); headers->l -= fullheader->l; } int handler(struct firemime_part *stack, int depth, void *opaque) { if (depth == 0) { if (firestring_estr_estrcasecmp(&stack[depth].type,&ESTR_S("multipart"),0) != 0 || firestring_estr_estrcasecmp(&stack[depth].subtype,&ESTR_S("postage"),0) != 0) { firestring_fprintf(stdout,"%e\n%e",&stack[depth].headers,&stack[depth].body); exit(0); } return 0; } if (firestring_estr_estrcasecmp(&stack[depth].type,&ESTR_S("application"),0) != 0 || firestring_estr_starts(&stack[depth].subtype,"postage-") != 0) { /* this is the critical part */ foldback(&stack[0].headers,&ESTR_S("Content-Type")); foldback(&stack[0].headers,&ESTR_S("Content-Transfer-Encoding")); foldback(&stack[0].headers,&ESTR_S("Content-Dispostion")); foldback(&stack[0].headers,&ESTR_S("Content-ID")); firestring_fprintf(stdout,"%e%e\n%e",&stack[0].headers,&stack[depth].headers,&stack[depth].body); exit(0); } return 1; } int main(int argc, char *argv[]) { struct firestring_estr_t message; int l,r; if (argc != 1) { firestring_fprintf(stderr,"Usage: %s\n",argv[0]); exit(100); } firestring_estr_alloc(&message,4096); while ((l = read(0,&message.s[message.l],message.a - message.l)) > 0) { message.l += l; firestring_estr_expand(&message,message.l + 4096); } /* we now have a message; call the magic parser */ if ((r = firemime_parse(&message,handler,&ESTR_S("\n"),NULL)) != 1) { firestring_fprintf(stderr,"Error in MIME parsing: %s\n",firemime_strerror(r)); exit(100); } firestring_fprintf(stderr,"No non-postage part found in multipart/postage\n"); return 100; }