Esempio n. 1
0
int main(int argc, char *argv[])
{
	MM_CTX *ctx;
	struct mm_mimeheader *header, *lastheader;
	struct mm_warning *warning, *lastwarning;
	struct mm_mimepart *part;
	struct mm_content *ct;
	FILE *out;
	int r, i, parts,st;

	mm_library_init();
	mm_codec_registerdefaultcodecs();
	if (argc <= 1 || isatty(0)) {
		fprintf(stderr, "Usage: mimepipe exe < /mime/message.eml\n");
		exit(1);
	}

	ctx = mm_context_new();
	if (mm_parse_seekable(ctx, stdin, MM_PARSE_LOOSE, 0) == -1
			|| mm_errno != MM_ERROR_NONE) {
		/* nada */
		fprintf(stderr, "%s\n", mm_error.error_msg);
		exit(1);
	}

	parts = mm_context_countparts(ctx);
	for (i = 1; i < mm_context_countparts(ctx); i++) {
		part = mm_context_getpart(ctx, i);
		out = setup(argv+1);
		r = mm_mimepart_decode_to(part, out);
		fflush(out);
		fclose(out);
		while (wait(&st) == -1);
		if (!r) continue;

		if (!WIFEXITED(st)) continue;
		switch (WEXITSTATUS(st)) {
		case 97: /* stop now */
			exit(111);
		case 100: /* stop now */
			exit(100);
		case 98: /* stop now */
			exit(0);
		case 99: /* stop now */
			exit(99);
		};
	}
	exit(0);
}
Esempio n. 2
0
int
main(int argc, char **argv)
{
	MM_CTX *ctx;
	struct mm_mimeheader *header, *lastheader;
	struct mm_warning *warning, *lastwarning;
	struct mm_mimepart *part;
	struct mm_content *ct;
	int parts, i;
	struct stat st;
	int fd;
	char *buf;
	int scan_mode = 0;

	lastheader = NULL;

	while ((i = getopt(argc, argv, "m")) != -1) {
		switch(i) {
		case 'm':
			scan_mode = 1;
			break;
		default:
			usage();
		}
	}

	argc -= optind;
	argv += optind;

	if (argc < 1) {
		usage();
	}
	
#ifdef __HAVE_LEAK_DETECTION
	/* Initialize memory leak detection if compiled in */
	MM_leakd_init();
#endif

	/* Initialize MiniMIME library */
	mm_library_init();

	/* Register all default codecs (base64/qp) */
	mm_codec_registerdefaultcodecs();

	do {
		/* Create a new context */
		ctx = mm_context_new();

		/* Parse a file into our context */
		if (scan_mode == 0) {
			i = mm_parse_file(ctx, argv[0], MM_PARSE_LOOSE, 0);
		} else {
			if (stat(argv[0], &st) == -1) {
				err(1, "stat");
			}
	
			if ((fd = open(argv[0], O_RDONLY)) == -1) {
				err(1, "open");
			}

			buf = (char *)malloc(st.st_size);
			if (buf == NULL) {
				err(1, "malloc");
			}	

			if (read(fd, buf, st.st_size) != st.st_size) {
				err(1, "read");
			}

			close(fd);
			buf[st.st_size] = '\0';
			
			i = mm_parse_mem(ctx, buf, MM_PARSE_LOOSE, 0);
		}

		if (i == -1 || mm_errno != MM_ERROR_NONE) {	
			printf("ERROR: %s at line %d\n", mm_error_string(), mm_error_lineno());
			exit(1);
		}

		/* Get the number of MIME parts */
		parts = mm_context_countparts(ctx);
		if (parts == 0) {
			printf("ERROR: got zero MIME parts, huh\n");
			exit(1);
		} else {
			if (mm_context_iscomposite(ctx)) {
				printf("Got %d MIME parts\n", parts - 1);
			} else {
				printf("Flat message (not multipart)\n");
			}
		}

		/* Get the main MIME part */
		part = mm_context_getpart(ctx, 0);
		if (part == NULL) {
			fprintf(stderr, "Could not get envelope part\n");
			exit(1);
		}

		printf("Printing envelope headers:\n");
		/* Print all headers */
		if (mm_mimepart_headers_start(part, &lastheader) == -1) {
			fprintf(stderr, "No headers in envelope\n");
			exit(1);
		}
		while ((header = mm_mimepart_headers_next(part, &lastheader)) != NULL) {
			printf("%s: %s\n", header->name, header->value);
		}

		printf("%s\n", mm_content_tostring(part->type));
		printf("\n");
		
		ct = part->type;
		assert(ct != NULL);

		if (mm_context_iscomposite(ctx) == 0) {
			printf("Printing body part for FLAT message:\n");
			part = mm_context_getpart(ctx, 0);
			printf("%s", part->body);
		}	

		/* Loop through all MIME parts beginning with 1 */
		for (i = 1; i < mm_context_countparts(ctx); i++) {
			char *decoded;

			printf("Printing headers for MIME part %d\n", i);

			/* Get the current MIME entity */
			part = mm_context_getpart(ctx, i);
			if (part == NULL) {
				fprintf(stderr, "Should have %d parts but "
				    "couldn't retrieve part %d",
				    mm_context_countparts(ctx), i);
				exit(1);
			}

			/* Print all headers */
			if (mm_mimepart_headers_start(part, &lastheader) == -1) {
				printf("Ups no headers\n");
			}
			while ((header = mm_mimepart_headers_next(part, &lastheader)) != NULL) {
				printf("%s: %s\n", header->name, header->value);
			}

			printf("%s\n", mm_content_tostring(part->type));

			/* Print MIME part body */
			printf("\nPrinting message BODY:\n%s\n", (char *)part->opaque_body);
			decoded = mm_mimepart_decode(part);
			if (decoded != NULL) {
				printf("DECODED:\n%s\n", decoded);
				free(decoded);
			}
		}
		
		/* Print out all warnings that we might have received */
		if (mm_context_haswarnings(ctx) > 0) {
			lastwarning = NULL;
			fprintf(stderr, "WARNINGS:\n");
			while ((warning = mm_warning_next(ctx, &lastwarning)) 
			    != NULL) {
				fprintf(stderr, " -> %s\n", warning->message);
			}
		}

		printf("ENVELOPE:\n");

		do {
			char *env;
			size_t env_len;

			mm_context_flatten(ctx, &env, &env_len, 0);
			printf("%s", env);

		} while (0);	

		mm_context_free(ctx);
		ctx = NULL;

#ifdef __HAVE_LEAK_DETECTION
		MM_leakd_printallocated();
#endif

	} while (0);

	return 0;
}