Created
June 4, 2014 07:17
-
-
Save andris9/4f868ec8478188023bab to your computer and use it in GitHub Desktop.
Parse uuencoded attachments from a text message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Install uue decoder: | |
// npm install uue | |
var message = | |
'This is a message with uuencoded attachment\n' + | |
'\n' + | |
'begin 644 cat.txt\n' + | |
'#0V%T\n' + | |
'`\n' + | |
'end\n'; | |
var uue = require('uue'); | |
var attachments = []; | |
message = message. | |
// no mathcing past newlines in JS, need to remove these | |
replace(/\r?\n/g, '\u0000'). | |
replace(/(?:^|\u0000)begin \d{3} ([^\u0000]+).*?\u0000`\u0000end(\u0000|$)/g, function(uuencoded, name) { | |
uuencoded = uuencoded.replace(/\u0000/g, '\n'); | |
attachments.push({ | |
name: name, | |
content: uue.decodeFile(uuencoded, name) | |
}); | |
return ''; | |
}). | |
// bring back removed newlines | |
replace(/\u0000/g, '\n'); | |
console.log(message); | |
// This is a message with uuencoded attachment | |
console.log(attachments); | |
// [{filename: 'cat.txt', content: <Buffer 43 61 74>}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment