vcf.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 {
  263. console.error("WARNING: failed to parse time: ", data);
  264. return null;
  265. }
  266. var dt = new Date(0);
  267. if(typeof(h) != 'undefined') { dt.setUTCHours(h); }
  268. if(typeof(m) != 'undefined') { dt.setUTCMinutes(m); }
  269. if(typeof(s) != 'undefined') { dt.setUTCSeconds(s); }
  270. if(tz) {
  271. dt = this.applyTimezone(dt, tz);
  272. }
  273. return dt;
  274. },
  275. // add two dates. if addSub is false, substract instead of add.
  276. addDates: function(aDate, bDate, addSub) {
  277. if(typeof(addSub) == 'undefined') { addSub = true };
  278. if(! aDate) { return bDate; }
  279. if(! bDate) { return aDate; }
  280. var a = Number(aDate);
  281. var b = Number(bDate);
  282. var c = addSub ? a + b : a - b;
  283. return new Date(c);
  284. },
  285. parseTimezone: function(tz) {
  286. var md;
  287. if((md = tz.match(/^([+\-])(\d{2})(\d{2})?/))) {
  288. var offset = new Date(0);
  289. offset.setUTCHours(md[2]);
  290. offset.setUTCMinutes(md[3] || 0);
  291. return Number(offset) * (md[1] == '+' ? +1 : -1);
  292. } else {
  293. return null;
  294. }
  295. },
  296. applyTimezone: function(date, tz) {
  297. var offset = this.parseTimezone(tz);
  298. if(offset) {
  299. return new Date(Number(date) + offset);
  300. } else {
  301. return date;
  302. }
  303. },
  304. parseDateTime: function(data) {
  305. var parts = data.split('T');
  306. var t = this.parseDate(parts[0]);
  307. var d = this.parseTime(parts[1]);
  308. return this.addDates(t, d);
  309. },
  310. parseDateAndOrTime: function(data) {
  311. switch(data.indexOf('T')) {
  312. case 0:
  313. return this.parseTime(data.slice(1));
  314. case -1:
  315. return this.parseDate(data);
  316. default:
  317. return this.parseDateTime(data);
  318. }
  319. },
  320. lineRE: /^([^\s].*)(?:\r?\n|$)/, // spec wants CRLF, but we're on the internet. reality is chaos.
  321. foldedLineRE:/^\s(.+)(?:\r?\n|$)/,
  322. // lex the given input, calling the callback for each line, with
  323. // the following arguments:
  324. // * key - key of the statement, such as 'BEGIN', 'FN', 'N', ...
  325. // * value - value of the statement, i.e. everything after the first ':'
  326. // * attrs - object containing attributes, such as {"TYPE":"work"}
  327. lex: function(input, callback) {
  328. var md, line = null, length = 0;
  329. for(;;) {
  330. if((md = input.match(this.lineRE))) {
  331. // Unfold quoted-printables (vCard 2.1) into a single line before parsing.
  332. // "Soft" linebreaks are indicated by a '=' at the end of the line, and do
  333. // not affect the underlying data.
  334. if(line && line.indexOf('QUOTED-PRINTABLE') != -1 && line.slice(-1) == '=') {
  335. line = line.slice(0,-1) + md[1];
  336. length = md[0].length;
  337. } else {
  338. if(line) {
  339. this.lexLine(line, callback);
  340. }
  341. line = md[1];
  342. length = md[0].length;
  343. }
  344. } else if((md = input.match(this.foldedLineRE))) {
  345. if(line) {
  346. line += md[1];
  347. length = md[0].length;
  348. } else {
  349. // ignore folded junk.
  350. }
  351. } else {
  352. console.error("Unmatched line: " + line);
  353. }
  354. input = input.slice(length);
  355. if(! input) {
  356. break;
  357. }
  358. }
  359. if(line) {
  360. // last line.
  361. this.lexLine(line, callback);
  362. }
  363. line = null;
  364. },
  365. lexLine: function(line, callback) {
  366. var tmp = '';
  367. var key = null, attrs = {}, value = null, attrKey = null;
  368. //If our value is a quoted-printable (vCard 2.1), decode it and discard the encoding attribute
  369. var qp = line.indexOf('ENCODING=QUOTED-PRINTABLE');
  370. if(qp != -1){
  371. line = line.substr(0,qp) + this.decodeQP(line.substr(qp+25));
  372. }
  373. function finalizeKeyOrAttr() {
  374. if(key) {
  375. if(attrKey) {
  376. attrs[attrKey] = tmp.split(',');
  377. } else {
  378. //"Floating" attributes are probably vCard 2.1 TYPE or PREF values.
  379. if(tmp == "PREF"){
  380. attrs.PREF = 1;
  381. } else {
  382. if (attrs.TYPE) attrs.TYPE.push(tmp);
  383. else attrs.TYPE = [tmp];
  384. }
  385. }
  386. } else {
  387. key = tmp;
  388. }
  389. }
  390. for(var i in line) {
  391. var c = line[i];
  392. switch(c) {
  393. case ':':
  394. finalizeKeyOrAttr();
  395. value = line.slice(Number(i) + 1);
  396. callback.apply(
  397. this,
  398. [key, value, attrs]
  399. );
  400. return;
  401. case ';':
  402. finalizeKeyOrAttr();
  403. tmp = '';
  404. break;
  405. case '=':
  406. attrKey = tmp;
  407. tmp = '';
  408. break;
  409. default:
  410. tmp += c;
  411. }
  412. }
  413. },
  414. /** Quoted Printable Parser
  415. *
  416. * Parses quoted-printable strings, which sometimes appear in
  417. * vCard 2.1 files (usually the address field)
  418. *
  419. * Code adapted from:
  420. * https://github.com/andris9/mimelib
  421. *
  422. **/
  423. decodeQP: function(str){
  424. str = (str || "").toString();
  425. str = str.replace(/\=(?:\r?\n|$)/g, "");
  426. var str2 = "";
  427. for(var i=0, len = str.length; i<len; i++){
  428. chr = str.charAt(i);
  429. if(chr == "=" && (hex = str.substr(i+1, 2)) && /[\da-fA-F]{2}/.test(hex)){
  430. str2 += String.fromCharCode(parseInt(hex,16));
  431. i+=2;
  432. continue;
  433. }
  434. str2 += chr;
  435. }
  436. return str2;
  437. }
  438. };
  439. })();