vcf.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /**
  2. ** VCF - Parser for the vcard format.
  3. **
  4. ** This is purely a vCard 4.0 implementation, as described in RFC 6350.
  5. **
  6. ** The generated VCard object roughly corresponds to the JSON representation
  7. ** of a hCard, as described here: http://microformats.org/wiki/jcard
  8. ** (Retrieved May 17, 2012)
  9. **
  10. **/
  11. var VCard = require( 'vcard' )
  12. var VCF;
  13. (function() {
  14. VCF = {
  15. simpleKeys: [
  16. 'VERSION',
  17. 'FN', // 6.2.1
  18. 'PHOTO', // 6.2.4 (we don't care about URIs [yet])
  19. 'GEO', // 6.5.2 (SHOULD also b a URI)
  20. 'TITLE', // 6.6.1
  21. 'ROLE', // 6.6.2
  22. 'LOGO', // 6.6.3 (also [possibly data:] URI)
  23. 'MEMBER', // 6.6.5
  24. 'NOTE', // 6.7.2
  25. 'PRODID', // 6.7.3
  26. 'SOUND', // 6.7.5
  27. 'UID', // 6.7.6
  28. ],
  29. csvKeys: [
  30. 'NICKNAME', // 6.2.3
  31. 'CATEGORIES', // 6.7.1
  32. ],
  33. dateAndOrTimeKeys: [
  34. 'BDAY', // 6.2.5
  35. 'ANNIVERSARY', // 6.2.6
  36. 'REV', // 6.7.4
  37. ],
  38. // parses the given input, constructing VCard objects.
  39. // if the input contains multiple (properly seperated) vcards,
  40. // the callback may be called multiple times, with one vcard given
  41. // each time.
  42. // The third argument specifies the context in which to evaluate
  43. // the given callback.
  44. parse: function(input, callback, context) {
  45. var vcard = null;
  46. if(! context) {
  47. context = this;
  48. }
  49. this.lex(input, function(key, value, attrs) {
  50. function setAttr(val) {
  51. if(vcard) {
  52. vcard.addAttribute(key.toLowerCase(), val);
  53. }
  54. }
  55. if(key == 'BEGIN') {
  56. vcard = new VCard();
  57. } else if(key == 'END') {
  58. if(vcard) {
  59. callback.apply(context, [vcard]);
  60. vcard = null;
  61. }
  62. } else if(this.simpleKeys.indexOf(key) != -1) {
  63. setAttr(value);
  64. } else if(this.csvKeys.indexOf(key) != -1) {
  65. setAttr(value.split(','));
  66. } else if(this.dateAndOrTimeKeys.indexOf(key) != -1) {
  67. if(attrs.VALUE == 'text') {
  68. // times can be expressed as "text" as well,
  69. // e.g. "ca 1800", "next week", ...
  70. setAttr(value);
  71. } else if(attrs.CALSCALE && attrs.CALSCALE != 'gregorian') {
  72. // gregorian calendar is the only calscale mentioned
  73. // in RFC 6350. I do not intend to support anything else
  74. // (yet).
  75. } else {
  76. // FIXME: handle TZ attribute.
  77. setAttr(this.parseDateAndOrTime(value));
  78. }
  79. } else if(key == 'N') { // 6.2.2
  80. setAttr(this.parseName(value));
  81. } else if(key == 'GENDER') { // 6.2.7
  82. setAttr(this.parseGender(value));
  83. } else if(key == 'TEL') { // 6.4.1
  84. setAttr({
  85. type: (attrs.TYPE || 'voice'),
  86. pref: attrs.PREF,
  87. value: value
  88. });
  89. } else if(key == 'EMAIL') { // 6.4.2
  90. setAttr({
  91. type: attrs.TYPE,
  92. pref: attrs.PREF,
  93. value: value
  94. });
  95. } else if(key == 'IMPP') { // 6.4.3
  96. // RFC 6350 doesn't define TYPEs for IMPP addresses.
  97. // It just seems odd to me to have multiple email addresses and phone numbers,
  98. // but not multiple IMPP addresses.
  99. setAttr({ value: value });
  100. } else if(key == 'LANG') { // 6.4.4
  101. setAttr({
  102. type: attrs.TYPE,
  103. pref: attrs.PREF,
  104. value: value
  105. });
  106. } else if(key == 'TZ') { // 6.5.1
  107. // neither hCard nor jCard mention anything about the TZ
  108. // property, except that it's singular (which it is *not* in
  109. // RFC 6350).
  110. // using compound representation.
  111. if(attrs.VALUE == 'utc-offset') {
  112. setAttr({ 'utc-offset': this.parseTimezone(value) });
  113. } else {
  114. setAttr({ name: value });
  115. }
  116. } else if(key == 'ORG') { // 6.6.4
  117. var parts = value.split(';');
  118. setAttr({
  119. 'organization-name': parts[0],
  120. 'organization-unit': parts[1]
  121. });
  122. } else if(key == 'RELATED') { // 6.6.6
  123. setAttr({
  124. type: attrs.TYPE,
  125. pref: attrs.PREF,
  126. value: attrs.VALUE
  127. });
  128. } else if(key =='ADR'){
  129. setAttr({
  130. type: attrs.TYPE,
  131. pref: attrs.PREF,
  132. value: value
  133. });
  134. //TODO: Handle 'LABEL' field.
  135. } else {
  136. console.log('WARNING: unhandled key: ', key);
  137. }
  138. });
  139. },
  140. nameParts: [
  141. 'family-name', 'given-name', 'additional-name',
  142. 'honorific-prefix', 'honorific-suffix'
  143. ],
  144. parseName: function(name) { // 6.2.2
  145. var parts = name.split(';');
  146. var n = {};
  147. for(var i in parts) {
  148. if(parts[i]) {
  149. n[this.nameParts[i]] = parts[i].split(',');
  150. }
  151. }
  152. return n;
  153. },
  154. /**
  155. * The representation of gender for hCards (and hence their JSON
  156. * representation) is undefined, as hCard is based on RFC 2436, which
  157. * doesn't define the GENDER attribute.
  158. * This method uses a compound representation.
  159. *
  160. * Examples:
  161. * "GENDER:M" -> {"sex":"male"}
  162. * "GENDER:M;man" -> {"sex":"male","identity":"man"}
  163. * "GENDER:F;girl" -> {"sex":"female","identity":"girl"}
  164. * "GENDER:M;girl" -> {"sex":"male","identity":"girl"}
  165. * "GENDER:F;boy" -> {"sex":"female","identity":"boy"}
  166. * "GENDER:N;woman" -> {"identity":"woman"}
  167. * "GENDER:O;potted plant" -> {"sex":"other","identity":"potted plant"}
  168. */
  169. parseGender: function(value) { // 6.2.7
  170. var gender = {};
  171. var parts = value.split(';');
  172. switch(parts[0]) {
  173. case 'M':
  174. gender.sex = 'male';
  175. break;
  176. case 'F':
  177. gender.sex = 'female';
  178. break;
  179. case 'O':
  180. gender.sex = 'other';
  181. }
  182. if(parts[1]) {
  183. gender.identity = parts[1];
  184. }
  185. return gender;
  186. },
  187. /** Date/Time parser.
  188. *
  189. * This implements only the parts of ISO 8601, that are
  190. * allowed by RFC 6350.
  191. * Paranthesized examples all represent (parts of):
  192. * 31st of January 1970, 23 Hours, 59 Minutes, 30 Seconds
  193. **/
  194. /** DATE **/
  195. // [ISO.8601.2004], 4.1.2.2, basic format:
  196. dateRE: /^(\d{4})(\d{2})(\d{2})$/, // (19700131)
  197. // [ISO.8601.2004], 4.1.2.3 a), basic format:
  198. dateReducedARE: /^(\d{4})\-(\d{2})$/, // (1970-01)
  199. // [ISO.8601.2004], 4.1.2.3 b), basic format:
  200. dateReducedBRE: /^(\d{4})$/, // (1970)
  201. // truncated representation from [ISO.8601.2000], 5.3.1.4.
  202. // I don't have access to that document, so relying on examples
  203. // from RFC 6350:
  204. dateTruncatedMDRE: /^\-{2}(\d{2})(\d{2})$/, // (--0131)
  205. dateTruncatedDRE: /^\-{3}(\d{2})$/, // (---31)
  206. /** TIME **/
  207. // (Note: it is unclear to me which of these are supposed to support
  208. // timezones. Allowing them for all. If timezones are ommitted,
  209. // defaulting to UTC)
  210. // [ISO.8601.2004, 4.2.2.2, basic format:
  211. timeRE: /^(\d{2})(\d{2})(\d{2})([+\-]\d+|Z|)$/, // (235930)
  212. // [ISO.8601.2004, 4.2.2.3 a), basic format:
  213. timeReducedARE: /^(\d{2})(\d{2})([+\-]\d+|Z|)$/, // (2359)
  214. // [ISO.8601.2004, 4.2.2.3 b), basic format:
  215. timeReducedBRE: /^(\d{2})([+\-]\d+|Z|)$/, // (23)
  216. // truncated representation from [ISO.8601.2000], see above.
  217. timeTruncatedMSRE: /^\-{2}(\d{2})(\d{2})([+\-]\d+|Z|)$/, // (--5930)
  218. timeTruncatedSRE: /^\-{3}(\d{2})([+\-]\d+|Z|)$/, // (---30)
  219. parseDate: function(data) {
  220. var md;
  221. var y, m, d;
  222. if((md = data.match(this.dateRE))) {
  223. y = md[1]; m = md[2]; d = md[3];
  224. } else if((md = data.match(this.dateReducedARE))) {
  225. y = md[1]; m = md[2];
  226. } else if((md = data.match(this.dateReducedBRE))) {
  227. y = md[1];
  228. } else if((md = data.match(this.dateTruncatedMDRE))) {
  229. m = md[1]; d = md[2];
  230. } else if((md = data.match(this.dateTruncatedDRE))) {
  231. d = md[1];
  232. } else if((md = data.match(/^(\d{4})-(\d{2})-(\d{2})$/))) {
  233. y = md[1]; m = md[2]; d = md[3];
  234. } else {
  235. console.error("WARNING: failed to parse date: ", data);
  236. return null;
  237. }
  238. var dt = new Date(0);
  239. if(typeof(y) != 'undefined') { dt.setUTCFullYear(y); }
  240. if(typeof(m) != 'undefined') { dt.setUTCMonth(m - 1); }
  241. if(typeof(d) != 'undefined') { dt.setUTCDate(d); }
  242. return dt;
  243. },
  244. parseTime: function(data) {
  245. var md;
  246. var h, m, s, tz;
  247. if((md = data.match(this.timeRE))) {
  248. h = md[1]; m = md[2]; s = md[3];
  249. tz = md[4];
  250. } else if((md = data.match(this.timeReducedARE))) {
  251. h = md[1]; m = md[2];
  252. tz = md[3];
  253. } else if((md = data.match(this.timeReducedBRE))) {
  254. h = md[1];
  255. tz = md[2];
  256. } else if((md = data.match(this.timeTruncatedMSRE))) {
  257. m = md[1]; s = md[2];
  258. tz = md[3];
  259. } else if((md = data.match(this.timeTruncatedSRE))) {
  260. s = md[1];
  261. tz = md[2];
  262. } else if((md = data.match(/^(\d{2})(\d{2})(\d{2})\.\d+([+\-]\d+|Z|)$/))) {
  263. y = md[1]; m = md[2]; d = md[3];
  264. tz = md[4];
  265. } else {
  266. console.error("WARNING: failed to parse time: ", data);
  267. return null;
  268. }
  269. var dt = new Date(0);
  270. if(typeof(h) != 'undefined') { dt.setUTCHours(h); }
  271. if(typeof(m) != 'undefined') { dt.setUTCMinutes(m); }
  272. if(typeof(s) != 'undefined') { dt.setUTCSeconds(s); }
  273. if(tz) {
  274. dt = this.applyTimezone(dt, tz);
  275. }
  276. return dt;
  277. },
  278. // add two dates. if addSub is false, substract instead of add.
  279. addDates: function(aDate, bDate, addSub) {
  280. if(typeof(addSub) == 'undefined') { addSub = true };
  281. if(! aDate) { return bDate; }
  282. if(! bDate) { return aDate; }
  283. var a = Number(aDate);
  284. var b = Number(bDate);
  285. var c = addSub ? a + b : a - b;
  286. return new Date(c);
  287. },
  288. parseTimezone: function(tz) {
  289. var md;
  290. if((md = tz.match(/^([+\-])(\d{2})(\d{2})?/))) {
  291. var offset = new Date(0);
  292. offset.setUTCHours(md[2]);
  293. offset.setUTCMinutes(md[3] || 0);
  294. return Number(offset) * (md[1] == '+' ? +1 : -1);
  295. } else {
  296. return null;
  297. }
  298. },
  299. applyTimezone: function(date, tz) {
  300. var offset = this.parseTimezone(tz);
  301. if(offset) {
  302. return new Date(Number(date) + offset);
  303. } else {
  304. return date;
  305. }
  306. },
  307. parseDateTime: function(data) {
  308. var parts = data.split('T');
  309. var t = this.parseDate(parts[0]);
  310. var d = this.parseTime(parts[1]);
  311. return this.addDates(t, d);
  312. },
  313. parseDateAndOrTime: function(data) {
  314. switch(data.indexOf('T')) {
  315. case 0:
  316. return this.parseTime(data.slice(1));
  317. case -1:
  318. return this.parseDate(data);
  319. default:
  320. return this.parseDateTime(data);
  321. }
  322. },
  323. lineRE: /^([^\s].*)(?:\r?\n|$)/, // spec wants CRLF, but we're on the internet. reality is chaos.
  324. foldedLineRE:/^\s(.+)(?:\r?\n|$)/,
  325. // lex the given input, calling the callback for each line, with
  326. // the following arguments:
  327. // * key - key of the statement, such as 'BEGIN', 'FN', 'N', ...
  328. // * value - value of the statement, i.e. everything after the first ':'
  329. // * attrs - object containing attributes, such as {"TYPE":"work"}
  330. lex: function(input, callback) {
  331. var md, line = null, length = 0;
  332. for(;;) {
  333. if((md = input.match(this.lineRE))) {
  334. // Unfold quoted-printables (vCard 2.1) into a single line before parsing.
  335. // "Soft" linebreaks are indicated by a '=' at the end of the line, and do
  336. // not affect the underlying data.
  337. if(line && line.indexOf('QUOTED-PRINTABLE') != -1 && line.slice(-1) == '=') {
  338. line = line.slice(0,-1) + md[1];
  339. length = md[0].length;
  340. } else {
  341. if(line) {
  342. this.lexLine(line, callback);
  343. }
  344. line = md[1];
  345. length = md[0].length;
  346. }
  347. } else if((md = input.match(this.foldedLineRE))) {
  348. if(line) {
  349. line += md[1];
  350. length = md[0].length;
  351. } else {
  352. // ignore folded junk.
  353. }
  354. } else {
  355. console.error("Unmatched line: " + line);
  356. }
  357. input = input.slice(length);
  358. if(! input) {
  359. break;
  360. }
  361. }
  362. if(line) {
  363. // last line.
  364. this.lexLine(line, callback);
  365. }
  366. line = null;
  367. },
  368. lexLine: function(line, callback) {
  369. var tmp = '';
  370. var key = null, attrs = {}, value = null, attrKey = null;
  371. //If our value is a quoted-printable (vCard 2.1), decode it and discard the encoding attribute
  372. var qp = line.indexOf('ENCODING=QUOTED-PRINTABLE');
  373. if(qp != -1){
  374. line = line.substr(0,qp) + this.decodeQP(line.substr(qp+25));
  375. }
  376. function finalizeKeyOrAttr() {
  377. if(key) {
  378. if(attrKey) {
  379. attrs[attrKey] = tmp.split(',');
  380. } else {
  381. //"Floating" attributes are probably vCard 2.1 TYPE or PREF values.
  382. if(tmp == "PREF"){
  383. attrs.PREF = 1;
  384. } else {
  385. if (attrs.TYPE) attrs.TYPE.push(tmp);
  386. else attrs.TYPE = [tmp];
  387. }
  388. }
  389. } else {
  390. key = tmp;
  391. }
  392. }
  393. for(var i in line) {
  394. var c = line[i];
  395. switch(c) {
  396. case ':':
  397. finalizeKeyOrAttr();
  398. value = line.slice(Number(i) + 1);
  399. callback.apply(
  400. this,
  401. [key, value, attrs]
  402. );
  403. return;
  404. case ';':
  405. finalizeKeyOrAttr();
  406. tmp = '';
  407. break;
  408. case '=':
  409. attrKey = tmp;
  410. tmp = '';
  411. break;
  412. default:
  413. tmp += c;
  414. }
  415. }
  416. },
  417. /** Quoted Printable Parser
  418. *
  419. * Parses quoted-printable strings, which sometimes appear in
  420. * vCard 2.1 files (usually the address field)
  421. *
  422. * Code adapted from:
  423. * https://github.com/andris9/mimelib
  424. *
  425. **/
  426. decodeQP: function(str){
  427. str = (str || "").toString();
  428. str = str.replace(/\=(?:\r?\n|$)/g, "");
  429. var str2 = "";
  430. for(var i=0, len = str.length; i<len; i++){
  431. chr = str.charAt(i);
  432. if(chr == "=" && (hex = str.substr(i+1, 2)) && /[\da-fA-F]{2}/.test(hex)){
  433. str2 += String.fromCharCode(parseInt(hex,16));
  434. i+=2;
  435. continue;
  436. }
  437. str2 += chr;
  438. }
  439. return str2;
  440. }
  441. };
  442. })();
  443. module.exports = VCF;