Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Oct 7, 2010
0 parents commit 7488afb
Show file tree
Hide file tree
Showing 6 changed files with 1,488 additions and 0 deletions.
58 changes: 58 additions & 0 deletions README.md
@@ -0,0 +1,58 @@
mailparser
==========

**mailparser** is an asynchronous and non-blocking parser for mime encoded e-mail messages. Handles even large
attachments with ease - attachments are parsed in chunks that can be saved into disk or sent to database while parsing.

**mailparser** parses raw source of e-mail messages to convert mime-stream into a structured object.

Requirements
------------

You need to have (node-iconv)[http://github.com/bnoordhuis/node-iconv] installed. Update *mime.js* to point to the correct location.

Usage
-----

Create a new *mailparser* object

var mp = new MailParser();

Set up listener for different events

* Get mail headers as a structured object

mp.on("header", function(headers){
console.log(headers);
});

* Get mail body as a structured object

mp.on("body", function(body){
console.log(body);
});

* Get part of a binary attachment in the form of a Buffer

mp.on("astream", function(id, buffer){
console.log("attachment id" + id);
console.log(buffer);
});

* Attachment parsing completed

mp.on("aend", function(id){
console.log("attachment " + id + " finished");
});

Feed the parser with data

mp.feed(part1_of_the_message);
mp.feed(part2_of_the_message);
mp.feed(part3_of_the_message);
...
mp.feed(partN_of_the_message);

Finish the feeding

mp.end();
304 changes: 304 additions & 0 deletions datetime.js
@@ -0,0 +1,304 @@
/*
* More info at: http://phpjs.org
*
* This is version: 3.18
* php.js is copyright 2010 Kevin van Zonneveld.
*
* Portions copyright Brett Zamir (http://brett-zamir.me), Kevin van Zonneveld
* (http://kevin.vanzonneveld.net), Onno Marsman, Theriault, Michael White
* (http://getsprink.com), Waldo Malqui Silva, Paulo Freitas, Jonas Raoni
* Soares Silva (http://www.jsfromhell.com), Jack, Philip Peterson, Ates Goral
* (http://magnetiq.com), Legaev Andrey, Ratheous, Alex, Martijn Wieringa,
* Nate, lmeyrick (https://sourceforge.net/projects/bcmath-js/), Philippe
* Baumann, Enrique Gonzalez, Webtoolkit.info (http://www.webtoolkit.info/),
* Ash Searle (http://hexmen.com/blog/), travc, Jani Hartikainen, Carlos R. L.
* Rodrigues (http://www.jsfromhell.com), Ole Vrijenhoek, WebDevHobo
* (http://webdevhobo.blogspot.com/), T.Wild,
* http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript,
* pilus, GeekFG (http://geekfg.blogspot.com), Rafał Kukawski
* (http://blog.kukawski.pl), Johnny Mast (http://www.phpvrouwen.nl), Michael
* Grier, Erkekjetter, d3x, marrtins, Andrea Giammarchi
* (http://webreflection.blogspot.com), stag019, mdsjack
* (http://www.mdsjack.bo.it), Chris, Steven Levithan
* (http://blog.stevenlevithan.com), Arpad Ray (mailto:arpad@php.net), David,
* Joris, Tim de Koning (http://www.kingsquare.nl), Marc Palau, Michael White,
* Public Domain (http://www.json.org/json2.js), gettimeofday, felix, Aman
* Gupta, Pellentesque Malesuada, Thunder.m, Tyler Akins (http://rumkin.com),
* Karol Kowalski, Felix Geisendoerfer (http://www.debuggable.com/felix),
* Alfonso Jimenez (http://www.alfonsojimenez.com), Diplom@t
* (http://difane.com/), majak, Mirek Slugen, Mailfaker
* (http://www.weedem.fr/), Breaking Par Consulting Inc
* (http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CFB006C45F7),
* Josh Fraser
* (http://onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/),
* Martin (http://www.erlenwiese.de/), Paul Smith, KELAN, Robin, saulius, AJ,
* Oleg Eremeev, Steve Hilder, gorthaur, Kankrelune
* (http://www.webfaktory.info/), Caio Ariede (http://caioariede.com), Lars
* Fischer, Sakimori, Imgen Tata (http://www.myipdf.com/), uestla, Artur
* Tchernychev, Wagner B. Soares, Christoph, nord_ua, class_exists, Der Simon
* (http://innerdom.sourceforge.net/), echo is bad, XoraX
* (http://www.xorax.info), Ozh, Alan C, Taras Bogach, Brad Touesnard, MeEtc
* (http://yass.meetcweb.com), Peter-Paul Koch
* (http://www.quirksmode.org/js/beat.html), T0bsn, Tim Wiel, Bryan Elliott,
* jpfle, JT, Thomas Beaucourt (http://www.webapp.fr), David Randall, Frank
* Forte, Eugene Bulkin (http://doubleaw.com/), noname, kenneth, Hyam Singer
* (http://www.impact-computing.com/), Marco, Raphael (Ao RUDLER), Ole
* Vrijenhoek (http://www.nervous.nl/), David James, Steve Clay, Jason Wong
* (http://carrot.org/), T. Wild, Paul, J A R, LH, strcasecmp, strcmp, JB,
* Daniel Esteban, strftime, madipta, Valentina De Rosa, Marc Jansen,
* Francesco, Stoyan Kyosev (http://www.svest.org/), metjay, Soren Hansen,
* 0m3r, Sanjoy Roy, Shingo, sankai, sowberry, hitwork, Rob, Norman "zEh"
* Fuchs, Subhasis Deb, josh, Yves Sucaet, Ulrich, Scott Baker, ejsanders,
* Nick Callen, Steven Levithan (stevenlevithan.com), Aidan Lister
* (http://aidanlister.com/), Philippe Jausions
* (http://pear.php.net/user/jausions), Zahlii, Denny Wardhana, Oskar Larsson
* Högfeldt (http://oskar-lh.name/), Brian Tafoya
* (http://www.premasolutions.com/), johnrembo, Gilbert, duncan, Thiago Mata
* (http://thiagomata.blog.com), Alexander Ermolaev
* (http://snippets.dzone.com/user/AlexanderErmolaev), Linuxworld, lmeyrick
* (https://sourceforge.net/projects/bcmath-js/this.), Jon Hohle, Pyerre,
* merabi, Saulo Vallory, HKM, ChaosNo1, djmix, Lincoln Ramsay, Adam Wallner
* (http://web2.bitbaro.hu/), paulo kuong, jmweb, Orlando, kilops, dptr1988,
* DxGx, Pedro Tainha (http://www.pedrotainha.com), Bayron Guevara, Le Torbi,
* James, Douglas Crockford (http://javascript.crockford.com), Devan
* Penner-Woelk, Jay Klehr, Kheang Hok Chin (http://www.distantia.ca/), Luke
* Smith (http://lucassmith.name), Rival, Amir Habibi
* (http://www.residence-mixte.com/), Blues (http://tech.bluesmoon.info/), Ben
* Bryan, booeyOH, Dreamer, Cagri Ekin, Diogo Resende, Howard Yeend, Pul,
* 3D-GRAF, jakes, Yannoo, Luke Godfrey, daniel airton wermann
* (http://wermann.com.br), Allan Jensen (http://www.winternet.no), Benjamin
* Lupton, davook, Atli Þór, Maximusya, Leslie Hoare, Bug?, setcookie, YUI
* Library: http://developer.yahoo.com/yui/docs/YAHOO.util.DateLocale.html,
* Blues at http://hacks.bluesmoon.info/strftime/strftime.js, Andreas,
* Michael, Christian Doebler, Gabriel Paderni, Marco van Oort, Philipp
* Lenssen, Arnout Kazemier (http://www.3rd-Eden.com), penutbutterjelly, Anton
* Ongson, DtTvB (http://dt.in.th/2008-09-16.string-length-in-bytes.html),
* meo, Greenseed, Yen-Wei Liu, mk.keck, William, rem, Jamie Beck
* (http://www.terabit.ca/), Russell Walker (http://www.nbill.co.uk/),
* Garagoth, Dino, Andrej Pavlovic, gabriel paderni, FGFEmperor, Scott Cariss,
* Slawomir Kaniecki, ReverseSyntax, Mateusz "loonquawl" Zalega, Francois,
* Kirk Strobeck, Billy, vlado houba, Jalal Berrami, date, Itsacon
* (http://www.itsacon.net/), Martin Pool, Pierre-Luc Paour, ger, john
* (http://www.jd-tech.net), mktime, Simon Willison
* (http://simonwillison.net), Nick Kolosov (http://sammy.ru), marc andreu,
* Arno, Nathan, Kristof Coomans (SCK-CEN Belgian Nucleair Research Centre),
* Fox, nobbler, stensi, Matteo, Riddler (http://www.frontierwebdev.com/),
* Tomasz Wesolowski, T.J. Leahy, rezna, Eric Nagel, Alexander M Beedie, baris
* ozdil, Greg Frazier, Bobby Drake, Ryan W Tenney (http://ryan.10e.us), Tod
* Gentille, Rafał Kukawski, FremyCompany, Manish, Cord, fearphage
* (http://http/my.opera.com/fearphage/), Victor, Brant Messenger
* (http://www.brantmessenger.com/), Matt Bradley, Luis Salazar
* (http://www.freaky-media.com/), Tim de Koning, taith, Rick Waldron, Mick@el
*
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL KEVIN VAN ZONNEVELD BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
this.strtotime = function(str, now) {
// http://kevin.vanzonneveld.net
// + original by: Caio Ariede (http://caioariede.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: David
// + improved by: Caio Ariede (http://caioariede.com)
// + improved by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Wagner B. Soares
// + bugfixed by: Artur Tchernychev
// % note 1: Examples all have a fixed timestamp to prevent tests to fail because of variable time(zones)
// * example 1: strtotime('+1 day', 1129633200);
// * returns 1: 1129719600
// * example 2: strtotime('+1 week 2 days 4 hours 2 seconds', 1129633200);
// * returns 2: 1130425202
// * example 3: strtotime('last month', 1129633200);
// * returns 3: 1127041200
// * example 4: strtotime('2009-05-04 08:30:00');
// * returns 4: 1241418600

var i, match, s, strTmp = '', parse = '';

strTmp = str;
strTmp = strTmp.replace(/\s{2,}|^\s|\s$/g, ' '); // unecessary spaces
strTmp = strTmp.replace(/[\t\r\n]/g, ''); // unecessary chars

if (strTmp == 'now') {
return (new Date()).getTime()/1000; // Return seconds, not milli-seconds
} else if (!isNaN(parse = Date.parse(strTmp))) {
return (parse/1000);
} else if (now) {
now = new Date(now*1000); // Accept PHP-style seconds
} else {
now = new Date();
}

strTmp = strTmp.toLowerCase();

var __is =
{
day:
{
'sun': 0,
'mon': 1,
'tue': 2,
'wed': 3,
'thu': 4,
'fri': 5,
'sat': 6
},
mon:
{
'jan': 0,
'feb': 1,
'mar': 2,
'apr': 3,
'may': 4,
'jun': 5,
'jul': 6,
'aug': 7,
'sep': 8,
'oct': 9,
'nov': 10,
'dec': 11
}
};

var process = function (m) {
var ago = (m[2] && m[2] == 'ago');
var num = (num = m[0] == 'last' ? -1 : 1) * (ago ? -1 : 1);

switch (m[0]) {
case 'last':
case 'next':
switch (m[1].substring(0, 3)) {
case 'yea':
now.setFullYear(now.getFullYear() + num);
break;
case 'mon':
now.setMonth(now.getMonth() + num);
break;
case 'wee':
now.setDate(now.getDate() + (num * 7));
break;
case 'day':
now.setDate(now.getDate() + num);
break;
case 'hou':
now.setHours(now.getHours() + num);
break;
case 'min':
now.setMinutes(now.getMinutes() + num);
break;
case 'sec':
now.setSeconds(now.getSeconds() + num);
break;
default:
var day;
if (typeof (day = __is.day[m[1].substring(0, 3)]) != 'undefined') {
var diff = day - now.getDay();
if (diff == 0) {
diff = 7 * num;
} else if (diff > 0) {
if (m[0] == 'last') {diff -= 7;}
} else {
if (m[0] == 'next') {diff += 7;}
}
now.setDate(now.getDate() + diff);
}
}
break;

default:
if (/\d+/.test(m[0])) {
num *= parseInt(m[0], 10);

switch (m[1].substring(0, 3)) {
case 'yea':
now.setFullYear(now.getFullYear() + num);
break;
case 'mon':
now.setMonth(now.getMonth() + num);
break;
case 'wee':
now.setDate(now.getDate() + (num * 7));
break;
case 'day':
now.setDate(now.getDate() + num);
break;
case 'hou':
now.setHours(now.getHours() + num);
break;
case 'min':
now.setMinutes(now.getMinutes() + num);
break;
case 'sec':
now.setSeconds(now.getSeconds() + num);
break;
}
} else {
return false;
}
break;
}
return true;
};

match = strTmp.match(/^(\d{2,4}-\d{2}-\d{2})(?:\s(\d{1,2}:\d{2}(:\d{2})?)?(?:\.(\d+))?)?$/);
if (match != null) {
if (!match[2]) {
match[2] = '00:00:00';
} else if (!match[3]) {
match[2] += ':00';
}

s = match[1].split(/-/g);

for (i in __is.mon) {
if (__is.mon[i] == s[1] - 1) {
s[1] = i;
}
}
s[0] = parseInt(s[0], 10);

s[0] = (s[0] >= 0 && s[0] <= 69) ? '20'+(s[0] < 10 ? '0'+s[0] : s[0]+'') : (s[0] >= 70 && s[0] <= 99) ? '19'+s[0] : s[0]+'';
return parseInt(this.strtotime(s[2] + ' ' + s[1] + ' ' + s[0] + ' ' + match[2])+(match[4] ? match[4]/1000 : ''), 10);
}

var regex = '([+-]?\\d+\\s'+
'(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?'+
'|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday'+
'|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday)'+
'|(last|next)\\s'+
'(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?'+
'|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday'+
'|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday))'+
'(\\sago)?';

match = strTmp.match(new RegExp(regex, 'gi')); // Brett: seems should be case insensitive per docs, so added 'i'
if (match == null) {
return false;
}

for (i = 0; i < match.length; i++) {
if (!process(match[i].split(' '))) {
return false;
}
}

return (now.getTime()/1000);
}

0 comments on commit 7488afb

Please sign in to comment.