1 /**
2  * Parses and handles Internet mail/news messages.
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <vladimir@thecybershadow.net>
12  */
13 
14 module ae.net.ietf.message;
15 
16 import std.algorithm;
17 import std.array;
18 import std.base64;
19 import std.conv;
20 import std.datetime;
21 import std.exception;
22 import std.regex;
23 import std.string;
24 import std.uri;
25 import std.utf;
26 
27 // TODO: Replace with logging?
28 debug(RFC850) import std.stdio : stderr;
29 
30 import ae.net.ietf.headers;
31 import ae.utils.array;
32 import ae.utils.iconv;
33 import ae.utils.mime;
34 import ae.utils.regex;
35 import ae.utils.text;
36 import ae.utils.time;
37 
38 import ae.net.ietf.wrap;
39 
40 alias ae.utils.text.ascii.ascii ascii; // https://d.puremagic.com/issues/show_bug.cgi?id=12156
41 alias std..string.indexOf indexOf;
42 
43 struct Xref
44 {
45 	string group;
46 	int num;
47 }
48 
49 class Rfc850Message
50 {
51 	/// The raw message (as passed in a constructor).
52 	ascii message;
53 
54 	/// The message ID, as specified at creation or in the Message-ID field.
55 	/// Includes the usual angular brackets.
56 	string id;
57 
58 	/// Cross-references - for newsgroups posts, list of groups where it was
59 	/// posted, and article number in said group.
60 	Xref[] xref;
61 
62 	/// The thread subject, with the leading "Re: " and list ID stripped.
63 	string subject;
64 
65 	/// The original message subject, as it appears in the message.
66 	string rawSubject;
67 
68 	/// The author's name, in UTF-8, stripped of quotes (no email address).
69 	string author;
70 
71 	/// The author's email address, stripped of angular brackets.
72 	string authorEmail;
73 
74 	/// Message date/time.
75 	SysTime time;
76 
77 	/// A list of Message-IDs that this post is in reply to.
78 	/// The most recent message (and direct parent) comes last.
79 	string[] references;
80 
81 	/// Whether this post is a reply.
82 	bool reply;
83 
84 	/// This message's headers.
85 	Headers headers;
86 
87 	/// The raw body of this message (or message part),
88 	/// i.e. the part of `this.message` after the headers.
89 	ascii rawContent;
90 
91 	/// The text contents of this message (UTF-8).
92 	/// "null" in case of an error.
93 	string content;
94 
95 	/// The contents of this message (depends on mimeType).
96 	ubyte[] data;
97 
98 	/// Explanation for null content.
99 	string error;
100 
101 	/// Reflow options (RFC 2646).
102 	bool flowed, delsp;
103 
104 	/// For a multipart message, contains the child parts.
105 	/// May nest more than one level.
106 	Rfc850Message[] parts;
107 
108 	/// Properties of a multipart message's part.
109 	string name, fileName, description, mimeType;
110 
111 	/// Parses a message string and creates a Rfc850Message.
112 	this(ascii message)
113 	{
114 		this.message = message;
115 		debug(RFC850) scope(failure) stderr.writeln("Failure while parsing message: ", id);
116 
117 		// Split headers from message, parse headers
118 
119 		// TODO: this breaks binary encodings, FIXME
120 		auto text = message.fastReplace("\r\n", "\n");
121 		auto headerEnd = text.indexOf("\n\n");
122 		if (headerEnd < 0) headerEnd = text.length;
123 		auto header = text[0..headerEnd];
124 		header = header.fastReplace("\n\t", " ").fastReplace("\n ", " ");
125 
126 		// TODO: Use a proper spec-conforming header parser
127 		foreach (s; header.fastSplit('\n'))
128 		{
129 			if (s == "") break;
130 
131 			auto p = s.indexOf(": ");
132 			if (p<0) continue;
133 			//assert(p>0, "Bad header line: " ~ s);
134 			headers[s[0..p]] = s[p+2..$];
135 		}
136 
137 		// Decode international characters in headers
138 
139 		string defaultEncoding = guessDefaultEncoding(headers.get("User-Agent", null));
140 
141 		foreach (string key, ref string value; headers)
142 			if (hasHighAsciiChars(value))
143 				value = decodeEncodedText(value, defaultEncoding);
144 
145 		// Decode transfer encoding
146 
147 		rawContent = text[min(headerEnd+2, $)..$];
148 
149 		if ("Content-Transfer-Encoding" in headers)
150 			try
151 				rawContent = decodeTransferEncoding(rawContent, headers["Content-Transfer-Encoding"]);
152 			catch (Exception e)
153 			{
154 				rawContent = null;
155 				error = "Error decoding " ~ headers["Content-Transfer-Encoding"] ~ " message: " ~ e.msg;
156 			}
157 
158 		// Decode message
159 
160 		data = cast(ubyte[])rawContent;
161 
162 		TokenHeader contentType, contentDisposition;
163 		if ("Content-Type" in headers)
164 			contentType = decodeTokenHeader(headers["Content-Type"]);
165 		if ("Content-Disposition" in headers)
166 			contentDisposition = decodeTokenHeader(headers["Content-Disposition"]);
167 		mimeType = toLower(contentType.value);
168 		flowed = contentType.properties.get("format", "fixed").icmp("flowed")==0;
169 		delsp = contentType.properties.get("delsp", "no").icmp("yes") == 0;
170 
171 		if (rawContent)
172 		{
173 			if (!mimeType || mimeType == "text/plain")
174 			{
175 				if ("charset" in contentType.properties)
176 					content = decodeEncodedText(rawContent, contentType.properties["charset"]);
177 				else
178 					content = decodeEncodedText(rawContent, defaultEncoding);
179 			}
180 			else
181 			if (mimeType.startsWith("multipart/") && "boundary" in contentType.properties)
182 			{
183 				string boundary = contentType.properties["boundary"];
184 				auto end = rawContent.indexOf("--" ~ boundary ~ "--");
185 				if (end < 0)
186 					end = rawContent.length;
187 				rawContent = rawContent[0..end];
188 
189 				auto rawParts = rawContent.split("--" ~ boundary ~ "\n");
190 				foreach (rawPart; rawParts[1..$])
191 				{
192 					auto part = new Rfc850Message(rawPart);
193 					if (part.content && !content)
194 						content = part.content;
195 					parts ~= part;
196 				}
197 
198 				if (!content)
199 				{
200 					if (rawParts.length && rawParts[0].asciiStrip().length)
201 						content = rawParts[0]; // default content to multipart stub
202 					else
203 						error = "Couldn't find text part in this " ~ mimeType ~ " message";
204 				}
205 			}
206 			else
207 				error = "Don't know how parse " ~ mimeType ~ " message";
208 		}
209 
210 		// Strip PGP signature away to a separate "attachment"
211 
212 		enum PGP_START = "-----BEGIN PGP SIGNED MESSAGE-----\n";
213 		enum PGP_DELIM = "\n-----BEGIN PGP SIGNATURE-----\n";
214 		enum PGP_END   = "\n-----END PGP SIGNATURE-----";
215 		if (content.startsWith(PGP_START) &&
216 		    content.contains(PGP_DELIM) &&
217 		    content.asciiStrip().endsWith(PGP_END))
218 		{
219 			// Don't attempt to create meaningful signature files... just get the clutter out of the way
220 			content = content.asciiStrip();
221 			auto p = content.indexOf(PGP_DELIM);
222 			auto part = new Rfc850Message(content[p+PGP_DELIM.length..$-PGP_END.length]);
223 			content = content[PGP_START.length..p];
224 			p = content.indexOf("\n\n");
225 			if (p >= 0)
226 				content = content[p+2..$];
227 			part.fileName = "pgp.sig";
228 			parts ~= part;
229 		}
230 
231 		// Decode UU-encoded attachments
232 
233 		if (content.contains("\nbegin "))
234 		{
235 			auto r = regex(`^begin [0-7]+ \S+$`);
236 			auto lines = content.split("\n");
237 			size_t start;
238 			bool started;
239 			string fn;
240 
241 			for (size_t i=0; i<lines.length; i++)
242 				if (!started && !match(lines[i], r).empty)
243 				{
244 					start = i;
245 					fn = lines[i].split(" ")[2];
246 					started = true;
247 				}
248 				else
249 				if (started && lines[i] == "end" && lines[i-1]=="`")
250 				{
251 					started = false;
252 					try
253 					{
254 						auto data = uudecode(lines[start+1..i]);
255 
256 						auto part = new Rfc850Message();
257 						part.fileName = fn;
258 						part.mimeType = guessMime(fn);
259 						part.data = data;
260 						parts ~= part;
261 
262 						lines = lines[0..start] ~ lines[i+1..$];
263 						i = start-1;
264 					}
265 					catch (Exception e)
266 						debug(RFC850) stderr.writeln(e);
267 				}
268 
269 			content = lines.join("\n");
270 		}
271 
272 		// Parse message-part properties
273 
274 		name = contentType.properties.get("name", string.init);
275 		fileName = contentDisposition.properties.get("filename", string.init);
276 		description = headers.get("Content-Description", string.init);
277 		if (name == fileName)
278 			name = null;
279 
280 		// Decode references
281 
282 		if ("References" in headers)
283 		{
284 			reply = true;
285 			auto refs = asciiStrip(headers["References"]);
286 			while (refs.startsWith("<"))
287 			{
288 				auto p = refs.indexOf(">");
289 				if (p < 0)
290 					break;
291 				references ~= refs[0..p+1];
292 				refs = asciiStrip(refs[p+1..$]);
293 			}
294 		}
295 		else
296 		if ("In-Reply-To" in headers)
297 			references = [headers["In-Reply-To"]];
298 
299 		// Decode subject
300 
301 		subject = rawSubject = "Subject" in headers ? decodeRfc1522(headers["Subject"]) : null;
302 		if (subject.startsWith("Re: "))
303 		{
304 			subject = subject[4..$];
305 			reply = true;
306 		}
307 
308 		// Decode author
309 
310 		static string[2] decodeAuthor(string header)
311 		{
312 			string author, authorEmail;
313 			author = authorEmail = header;
314 			if ((author.indexOf('@') < 0 && author.indexOf(" at ") >= 0)
315 			 || (author.indexOf("<") < 0 && author.indexOf(">") < 0 && author.indexOf(" (") > 0 && author.endsWith(")")))
316 			{
317 				// Mailing list archive format
318 				assert(author == authorEmail);
319 				if (author.indexOf(" (") > 0 && author.endsWith(")"))
320 				{
321 					authorEmail = author[0 .. author.lastIndexOf(" (")].replace(" at ", "@");
322 					author      = author[author.lastIndexOf(" (")+2 .. $-1].decodeRfc1522();
323 				}
324 				else
325 				{
326 					authorEmail = author.replace(" at ", "@");
327 					author = author[0 .. author.lastIndexOf(" at ")];
328 				}
329 			}
330 			if (author.indexOf('<')>=0 && author.endsWith('>'))
331 			{
332 				auto p = author.indexOf('<');
333 				authorEmail = author[p+1..$-1];
334 				author = decodeRfc1522(asciiStrip(author[0..p]));
335 			}
336 
337 			if (author.length>2 && author[0]=='"' && author[$-1]=='"')
338 				author = decodeRfc1522(asciiStrip(author[1..$-1]));
339 			if ((author == authorEmail || author == "") && authorEmail.indexOf("@") > 0)
340 				author = authorEmail[0..authorEmail.indexOf("@")];
341 			return [author, authorEmail];
342 		}
343 
344 		list(author, authorEmail) = decodeAuthor("From" in headers ? decodeRfc1522(headers["From"]) : null);
345 
346 		if (headers.get("List-Post", null) == "<mailto:" ~ authorEmail ~ ">" && "Reply-To" in headers)
347 			list(null, authorEmail) = decodeAuthor(decodeRfc1522(headers["Reply-To"].findSplit(", ")[0]));
348 
349 		// Decode cross-references
350 
351 		if ("Xref" in headers)
352 		{
353 			auto xrefStrings = split(headers["Xref"], " ")[1..$];
354 			foreach (str; xrefStrings)
355 			{
356 				auto segs = str.split(":");
357 				xref ~= Xref(segs[0], to!int(segs[1]));
358 			}
359 		}
360 		else
361 		if (headers.get("Sender", null).canFind("-bounces@") && !headers["Sender"].representation.any!(c => c.among('<', '>', '"')))
362 			xref = [Xref(headers["Sender"].findSplit("-bounces@")[0])];
363 		else
364 		if ("List-Unsubscribe" in headers)
365 			xref = headers["List-Unsubscribe"].split(", ").filter!(s => s.canFind("/options/")).map!(s => Xref(s.split("/")[$-1].stripRight('>'))).array();
366 		else
367 		if ("List-ID" in headers)
368 		{
369 			auto listID = headers["List-ID"];
370 			auto parts = listID.findSplit(" <");
371 			if (parts[1].length)
372 				listID = parts[2].findSplit(".")[0];
373 			else
374 				listID = parts[0].replace(`"`, ``);
375 			xref = [Xref(listID)];
376 		}
377 
378 		// Decode message ID
379 
380 		if ("Message-ID" in headers && !id)
381 			id = headers["Message-ID"];
382 
383 		// Decode post time
384 
385 		time = Clock.currTime; // default value
386 
387 		if ("NNTP-Posting-Date" in headers)
388 			time = parseTime!`D, j M Y H:i:s O \(\U\T\C\)`(headers["NNTP-Posting-Date"].strip());
389 		else
390 		if ("Date" in headers)
391 		{
392 			auto str = headers["Date"].strip();
393 			str = str.replace(re!`([+\-]\d\d\d\d) \(.*\)$`, "$1");
394 			try
395 				time = parseTime!(TimeFormats.RFC850)(str);
396 			catch (Exception e)
397 			try
398 				time = parseTime!(`D, j M Y H:i:s O`)(str);
399 			catch (Exception e)
400 			try
401 				time = parseTime!(`D, j M Y H:i:s e`)(str);
402 			catch (Exception e)
403 			try
404 				time = parseTime!(`D, j M Y H:i O`)(str);
405 			catch (Exception e)
406 			try
407 				time = parseTime!(`D, j M Y H:i e`)(str);
408 			catch (Exception e)
409 			{
410 				// fall-back to default (class creation time)
411 				// TODO: better behavior?
412 			}
413 		}
414 	}
415 
416 	private this() {} // for attachments and templates
417 
418 	/// Create a template Rfc850Message for a new posting to the specified groups.
419 	static Rfc850Message newPostTemplate(string groups)
420 	{
421 		auto post = new Rfc850Message();
422 		foreach (group; groups.split(","))
423 			post.xref ~= Xref(group);
424 		return post;
425 	}
426 
427 	@property WrapFormat wrapFormat()
428 	{
429 		return flowed ? delsp ? WrapFormat.flowedDelSp : WrapFormat.flowed : WrapFormat.heuristics;
430 	}
431 
432 	/// Create a template Rfc850Message for a reply to this message.
433 	Rfc850Message replyTemplate()
434 	{
435 		auto post = new Rfc850Message();
436 		post.reply = true;
437 		post.xref = this.xref;
438 		post.references = this.references ~ this.id;
439 		post.subject = this.rawSubject;
440 		if (!post.subject.startsWith("Re:"))
441 			post.subject = "Re: " ~ post.subject;
442 
443 		auto paragraphs = unwrapText(this.content, this.wrapFormat);
444 		foreach (i, ref paragraph; paragraphs)
445 			if (paragraph.quotePrefix.length)
446 				paragraph.quotePrefix = ">" ~ paragraph.quotePrefix;
447 			else
448 			{
449 				if (paragraph.text == "-- " || paragraph.text == "_______________________________________________")
450 				{
451 					paragraphs = paragraphs[0..i];
452 					break;
453 				}
454 				paragraph.quotePrefix = paragraph.text.length ? "> " : ">";
455 			}
456 		while (paragraphs.length && paragraphs[$-1].text.length==0)
457 			paragraphs = paragraphs[0..$-1];
458 
459 		auto replyTime = time;
460 		replyTime.timezone = UTC();
461 		post.content =
462 			"On " ~ replyTime.formatTime!`l, j F Y \a\t H:i:s e`() ~ ", " ~ this.author ~ " wrote:\n" ~
463 			wrapText(paragraphs) ~
464 			"\n\n";
465 		post.flowed = true;
466 		post.delsp = false;
467 
468 		return post;
469 	}
470 
471 	/// Set the message text.
472 	/// Rewraps as necessary.
473 	void setText(string text)
474 	{
475 		this.content = wrapText(unwrapText(text, WrapFormat.input));
476 		this.flowed = true;
477 		this.delsp = false;
478 	}
479 
480 	/// Write this Message instance's fields to their appropriate headers.
481 	void compileHeaders()
482 	{
483 		assert(id);
484 
485 		headers["Message-ID"] = id;
486 		headers["From"] = format(`%s <%s>`, author, authorEmail);
487 		headers["Subject"] = subject;
488 		headers["Newsgroups"] = xref.map!(x => x.group)().join(",");
489 		headers["Content-Type"] = format("text/plain; charset=utf-8; format=%s; delsp=%s", flowed ? "flowed" : "fixed", delsp ? "yes" : "no");
490 		headers["Content-Transfer-Encoding"] = "8bit";
491 		if (references.length)
492 		{
493 			headers["References"] = references.join(" ");
494 			headers["In-Reply-To"] = references[$-1];
495 		}
496 		if (time == SysTime.init)
497 			time = Clock.currTime();
498 		headers["Date"] = time.formatTime!(TimeFormats.RFC2822);
499 		headers["User-Agent"] = "ae.net.ietf.message";
500 	}
501 
502 	/// Construct the headers and message fields.
503 	void compile()
504 	{
505 		compileHeaders();
506 
507 		string[] lines;
508 		foreach (name, value; headers)
509 		{
510 			if (value.hasHighAsciiChars())
511 				value = value.encodeRfc1522();
512 			auto line = name ~ ": " ~ value;
513 			auto lineStart = name.length + 2;
514 
515 			foreach (c; line)
516 				enforce(c >= 32, "Control characters in header: %(%s%)".format([line]));
517 
518 			while (line.length >= 80)
519 			{
520 				auto p = line[0..80].lastIndexOf(' ');
521 				if (p < lineStart)
522 				{
523 					p = 80 + line[80..$].indexOf(' ');
524 					if (p < 80)
525 						break;
526 				}
527 				lines ~= line[0..p];
528 				line = line[p..$];
529 				lineStart = 1;
530 			}
531 			lines ~= line;
532 		}
533 
534 		message =
535 			lines.join("\r\n") ~
536 			"\r\n\r\n" ~
537 			splitAsciiLines(content).join("\r\n");
538 	}
539 
540 	/// Get the Message-ID that this message is in reply to.
541 	@property string parentID()
542 	{
543 		return references.length ? references[$-1] : null;
544 	}
545 
546 	/// Return the oldest known ancestor of this post, possibly
547 	/// this post's ID if it is the first one in the thread.
548 	/// May not be the thread ID - some UAs/services
549 	/// cut off or strip the "References" header.
550 	@property string firstAncestorID()
551 	{
552 		return references.length ? references[0] : id;
553 	}
554 }
555 
556 unittest
557 {
558 	auto post = new Rfc850Message("From: msonke at example.org (=?ISO-8859-1?Q?S=F6nke_Martin?=)\n\nText");
559 	assert(post.author == "Sönke Martin");
560 	assert(post.authorEmail == "msonke@example.org");
561 
562 	post = new Rfc850Message("Date: Tue, 06 Sep 2011 14:52 -0700\n\nText");
563 	assert(post.time.year == 2011);
564 
565 	post = new Rfc850Message("List-ID: phobos\nSubject: [phobos] Subject\n\nText");
566 	assert(post.xref == [Xref("phobos")]);
567 
568 	post = new Rfc850Message("Sender: dmd-beta-bounces@puremagic.com\n\nText");
569 	assert(post.xref == [Xref("dmd-beta")]);
570 }
571 
572 private:
573 
574 /// Decode headers with international characters in them.
575 string decodeRfc1522(string str)
576 {
577 	auto words = str.split(" ");
578 	bool[] encoded = new bool[words.length];
579 
580 	foreach (wordIndex, ref word; words)
581 		if (word.length > 6 && word.startsWith("=?") && word.endsWith("?="))
582 		{
583 			auto parts = split(word[2..$-2], "?");
584 			if (parts.length != 3)
585 				continue;
586 			auto charset = parts[0];
587 			auto encoding = parts[1];
588 			auto text = parts[2];
589 
590 			switch (toUpper(encoding))
591 			{
592 			case "Q":
593 				text = decodeQuotedPrintable(text, true);
594 				break;
595 			case "B":
596 				text = cast(ascii)Base64.decode(text);
597 				break;
598 			default:
599 				continue /*foreach*/;
600 			}
601 
602 			word = decodeEncodedText(text, charset);
603 			encoded[wordIndex] = true;
604 		}
605 
606 	string result;
607 	foreach (wordIndex, word; words)
608 	{
609 		if (wordIndex > 0 && !(encoded[wordIndex-1] && encoded[wordIndex]))
610 			result ~= ' ';
611 		result ~= word;
612 	}
613 
614 	try
615 	{
616 		import std.utf;
617 		validate(result);
618 	}
619 	catch (Exception e)
620 		result = toUtf8(cast(ascii)result, "ISO-8859-1", true);
621 
622 	return result;
623 }
624 
625 /// Encodes an UTF-8 string to be used in headers.
626 string encodeRfc1522(string str)
627 {
628 	if (!str.hasHighAsciiChars())
629 		return str;
630 
631 	string[] words;
632 	bool wasIntl = false;
633 	foreach (word; str.split(" "))
634 	{
635 		bool isIntl = word.hasHighAsciiChars();
636 		if (wasIntl && isIntl)
637 			words[$-1] ~= " " ~ word;
638 		else
639 			words ~= word;
640 		wasIntl = isIntl;
641 	}
642 
643 	enum CHUNK_LENGTH_THRESHOLD = 20;
644 
645 	foreach (ref word; words)
646 	{
647 		if (!word.hasHighAsciiChars())
648 			continue;
649 		string[] output;
650 		string s = word;
651 		while (s.length)
652 		{
653 			size_t ptr = 0;
654 			while (ptr < s.length && ptr < CHUNK_LENGTH_THRESHOLD)
655 				ptr += stride(s, ptr);
656 			output ~= encodeRfc1522Chunk(s[0..ptr]);
657 			s = s[ptr..$];
658 		}
659 		word = output.join(" ");
660 	}
661 	return words.join(" ");
662 }
663 
664 string encodeRfc1522Chunk(string str) pure
665 {
666 	auto result = "=?UTF-8?B?" ~ Base64.encode(cast(ubyte[])str) ~ "?=";
667 	return result;
668 }
669 
670 unittest
671 {
672 	auto text = "В лесу родилась ёлочка";
673 	assert(decodeRfc1522(encodeRfc1522(text)) == text);
674 
675 	// Make sure email address isn't mangled
676 	assert(encodeRfc1522("Sönke Martin <msonke@example.org>").endsWith(" <msonke@example.org>"));
677 }
678 
679 string decodeQuotedPrintable(string s, bool inHeaders)
680 {
681 	auto r = appender!string();
682 	for (int i=0; i<s.length; )
683 		if (s[i]=='=')
684 		{
685 			if (i+1 >= s.length || s[i+1] == '\n')
686 				i+=2; // escape newline
687 			else
688 				r.put(cast(char)to!ubyte(s[i+1..i+3], 16)), i+=3;
689 		}
690 		else
691 		if (s[i]=='_' && inHeaders)
692 			r.put(' '), i++;
693 		else
694 			r.put(s[i++]);
695 	return r.data;
696 }
697 
698 string guessDefaultEncoding(string userAgent)
699 {
700 	switch (userAgent)
701 	{
702 		case "DFeed":
703 			// Early DFeed versions did not specify the encoding
704 			return "utf8";
705 		default:
706 			return "windows1252";
707 	}
708 }
709 
710 // http://d.puremagic.com/issues/show_bug.cgi?id=7016
711 static import ae.sys.cmd;
712 
713 string decodeEncodedText(ascii s, string textEncoding)
714 {
715 	try
716 		return toUtf8(s, textEncoding, false);
717 	catch (Exception e)
718 	{
719 		debug(RFC850) stderr.writefln("iconv fallback for %s (%s)", textEncoding, e.msg);
720 		try
721 		{
722 			import ae.sys.cmd;
723 			return iconv(s, textEncoding);
724 		}
725 		catch (Exception e)
726 		{
727 			debug(RFC850) stderr.writefln("ISO-8859-1 fallback (%s)", e.msg);
728 			return toUtf8(s, "ISO-8859-1", false);
729 		}
730 	}
731 }
732 
733 string decodeTransferEncoding(string data, string encoding)
734 {
735 	switch (toLower(encoding))
736 	{
737 	case "7bit":
738 		return data;
739 	case "quoted-printable":
740 		return decodeQuotedPrintable(data, false);
741 	case "base64":
742 		//return cast(string)Base64.decode(data.replace("\n", ""));
743 	{
744 		auto s = data.fastReplace("\n", "");
745 		scope(failure) debug(RFC850) stderr.writeln(s);
746 		return cast(string)Base64.decode(s);
747 	}
748 	default:
749 		return data;
750 	}
751 }
752 
753 ubyte[] uudecode(string[] lines)
754 {
755 	// TODO: optimize
756 	//auto data = appender!(ubyte[]);  // OPTLINK says no
757 	ubyte[] data;
758 	foreach (line; lines)
759 	{
760 		if (!line.length || line.startsWith("`"))
761 			continue;
762 		ubyte len = to!ubyte(line[0] - 32);
763 		line = line[1..$];
764 		while (line.length % 4)
765 			line ~= 32;
766 		ubyte[] lineData;
767 		while (line.length)
768 		{
769 			uint v = 0;
770 			foreach (c; line[0..4])
771 				if (c == '`') // same as space
772 					v <<= 6;
773 				else
774 				{
775 					enforce(c >= 32 && c < 96, [c]);
776 					v = (v<<6) | (c - 32);
777 				}
778 
779 			auto a = cast(ubyte[])((&v)[0..1]);
780 			lineData ~= a[2];
781 			lineData ~= a[1];
782 			lineData ~= a[0];
783 
784 			line = line[4..$];
785 		}
786 		while (len > lineData.length)
787 			lineData ~= 0;
788 		data ~= lineData[0..len];
789 	}
790 	return data;
791 }