rohrpost

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

mimetest.c (3439B)


      1 /*
      2  * Copy me if you can.
      3  * by 20h
      4  *
      5  * % gcc -g -std=gnu99 -I.. -lssl -o mimetest mimetest.c ../ind.c ../llist.c \
      6  *	../mime.c ../parser.c ../net.c ../strio.c ../base64.c
      7  *
      8  * % cat ../proto/mime.simple.example | ./mimetest
      9  * % cat ../proto/mime.complex.example | ./mimetest
     10  */
     11 
     12 #include <unistd.h>
     13 #include <stdlib.h>
     14 #include <stdio.h>
     15 #include <string.h>
     16 
     17 #include "ind.h"
     18 #include "llist.h"
     19 #include "mime.h"
     20 
     21 int
     22 main(int argc, char *argv[])
     23 {
     24 	char *test[] = {
     25 		"=?utf-8?B?TmV1ZSBOYWNocmljaHQgdm9uIERlaW4gTX"
     26 		"lTcGFjZSBUZWFtIGJlaSBNeVNwYWNl?=",
     27 		"(=?ISO-8859-1?Q?a?=)",
     28 		"(=?ISO-8859-1?Q?a?= b)",
     29 		"(=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=)",
     30 		"(=?ISO-8859-1?Q?a?=  =?ISO-8859-1?Q?b?=)",
     31 		"(=?ISO-8859-1?Q?a?=  \r\n   =?ISO-8859-1?Q?b?=)",
     32 		"(=?ISO-8859-1?Q?a_b?=)",
     33 		"(=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?=)",
     34 		"=?US-ASCII*EN?Q?Keith_Moore?= <moore@cs.utk.edu>"
     35 	};
     36 	char *hdrs[] = {
     37 		"Content-Type: application/msword;\r\n"
     38 		"        name=\"=?iso-8859-1?Q?Antrag__F=F6rderung_lehrbe"
     39 		"zogener_Projekte_=5FV3=2Edoc?=\"\r\n"
     40 		"Content-Transfer-Encoding: base64\r\n"
     41 		"Content-Disposition: attachment;\r\n"
     42 		"        filename*=iso-8859-1''Antrag%20%20F%F6rde"
     43 		"rung%20lehrbezogener%20Projekte%20_V3.doc\r\n"
     44 		"Content-Description: =?iso-8859-1?Q?Antrag__F"
     45 		"=F6rderung_lehrbezogener_Projekte_=5FV3=2Edoc?="
     46 		"(=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?="
     47 		"  =?utf-8?B?TmV1ZSBOYWNocmljaHQgdm9uIERlaW4gTX"
     48 		"lTcGFjZSBUZWFtIGJlaSBNeVNwYWNl?=)\r\n",
     49 
     50 		"Content-Type: application/x-stuff;\r\n"
     51 		" title*0*=us-ascii'en'This%20is%20even%20more%20\r\n"
     52 		" title*1*=%2A%2A%2Afun%2A%2A%2A%20\r\n"
     53 		" title*2=\"isn't it!\"\r\n",
     54 
     55 		"Content-Type: application/x-stuff;\r\n"
     56 		" title*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\r\n",
     57 
     58 		"Content-Type: message/external-body; access-type=URL;\r\n"
     59 		" URL*0=\"ftp://\";\r\n"
     60 		" URL*1=\"cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer."
     61 		"tar\"\r\n",
     62 
     63 		"Content-Type: message/external-body; access-type=URL;\r\n"
     64 		"  URL=\"ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mai"
     65 		"ler.tar\"\r\n"
     66 	};
     67 
     68 	char *str;
     69 	int len, i;
     70 	mime_t *mime;
     71 	llistelem_t *hdr, *param;
     72 	llist_t *paraml;
     73 
     74 	if (argc < 2 || !strcmp(argv[1], "-d")) {
     75 		str = readstdin(&len);
     76 		mime = mime_parsebuf(str, len);
     77 		free(str);
     78 
     79 		printf("\nmessage:\n");
     80 		mime_print(mime);
     81 
     82 		printf("Getting Subject:\n");
     83 		hdr = llist_ciget(mime->hdrs, "subject");
     84 		if (hdr == NULL)
     85 			die("Could not find 'Subject' header in mail!\n");
     86 		printf("Subject: %s\n", (char *)hdr->data);
     87 
     88 		mime_free(mime);
     89 	}
     90 	if (argc > 1)
     91 		return 0;
     92 
     93 	for (i = 0; i < nelem(test)-1; i++) {
     94 		printf("\ntest%d: %s\n", i, test[i]);
     95 		str = mime_decodeheader(test[i]);
     96 		printf("result: %s\n", str);
     97 		free(str);
     98 	}
     99 
    100 	for (i = 0; i < nelem(hdrs); i++) {
    101 		printf("\nhdrs%d: %s\n", i, hdrs[i]);
    102 		mime = mime_parsebuf(hdrs[i], strlen(hdrs[i]));
    103 		if (mime == NULL) {
    104 			printf("Test failed!\n");
    105 			continue;
    106 		}
    107 
    108 		printf("Result:\n");
    109 		mime_print(mime);
    110 
    111 		forllist(mime->hdrs, hdr) {
    112 			printf("\n");
    113 			paraml = mime_parseheader((char *)hdr->data);
    114 
    115 			printf("Param %s:\n", hdr->key);
    116 			forllist(paraml, param) {
    117 				if (param->data != NULL) {
    118 					str = mime_decodeheader(
    119 							(char *)param->data);
    120 					printf("%s = %s\n", param->key, str);
    121 				} else {
    122 					str = mime_decodeheader(param->key);
    123 					printf("%s\n", str);
    124 				}
    125 				free(str);
    126 			}
    127 			llist_free(paraml);
    128 		}
    129 
    130 		mime_free(mime);
    131 	}
    132 
    133 	return 0;
    134 }
    135