Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
This commit is contained in:
parent
93e3a48004
commit
b38dea0597
25901 changed files with 9982925 additions and 369 deletions
79
gaia/apps/email/js/ext/imap/protocol/textparser.js
Executable file
79
gaia/apps/email/js/ext/imap/protocol/textparser.js
Executable file
|
|
@ -0,0 +1,79 @@
|
|||
define(
|
||||
[
|
||||
'mimeparser',
|
||||
'mimefuncs',
|
||||
'exports'
|
||||
],
|
||||
function(
|
||||
MimeParser,
|
||||
mimefuncs,
|
||||
exports
|
||||
) {
|
||||
|
||||
|
||||
/**
|
||||
* Simple wrapper around mimeparser hacks allows us to reuse data from the
|
||||
* BODYSTRUCT request that contained the mime type, etc....
|
||||
*
|
||||
* var parser = $textparser.TextParser(
|
||||
* bodyInfo.bodyReps[n]
|
||||
* );
|
||||
*
|
||||
* // msg is some stream thing from fetcher
|
||||
*
|
||||
* msg.on('data', parser.parse.bind(parser));
|
||||
* msg.on('end', function() {
|
||||
* var content = parser.complete();
|
||||
* });
|
||||
*
|
||||
*/
|
||||
function TextParser(partDef) {
|
||||
this._partDef = partDef;
|
||||
var parser = this._parser = new MimeParser();
|
||||
this._totalBytes = 0;
|
||||
|
||||
// TODO: escape?
|
||||
var charsetPart = '', formatPart = '';
|
||||
if (partDef.params && partDef.params.charset) {
|
||||
charsetPart = '; charset="' + partDef.params.charset.toLowerCase() + '"';
|
||||
}
|
||||
|
||||
if (partDef.params && partDef.params.format) {
|
||||
formatPart = '; format="' + partDef.params.format.toLowerCase() + '"';
|
||||
}
|
||||
parser.write('Content-Type: ' + partDef.type.toLowerCase() +
|
||||
'/' + partDef.subtype.toLowerCase() + charsetPart +
|
||||
formatPart + '\r\n');
|
||||
|
||||
if (partDef.encoding) {
|
||||
parser.write('Content-Transfer-Encoding: ' + partDef.encoding + '\r\n');
|
||||
}
|
||||
|
||||
parser.write('\r\n'); // Finish headers.
|
||||
|
||||
if (partDef.pendingBuffer) {
|
||||
this.parse(partDef.pendingBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
TextParser.prototype = {
|
||||
parse: function(buffer) {
|
||||
this._totalBytes += buffer.length;
|
||||
this._parser.write(buffer);
|
||||
},
|
||||
|
||||
complete: function() {
|
||||
this._parser.end();
|
||||
|
||||
var str = mimefuncs.charset.decode(this._parser.node.content, 'utf-8');
|
||||
|
||||
return {
|
||||
bytesFetched: this._totalBytes,
|
||||
text: str
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
exports.TextParser = TextParser;
|
||||
|
||||
});
|
||||
Loading…
Reference in a new issue