shared_contacts.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const bplist = require('bplist-parser')
  2. const fs = require('fs')
  3. // Derive filenames based on domain + file path
  4. const fileHash = require('../../../util/backup_filehash')
  5. const file = fileHash('Library/Preferences/group.com.google.Gmail.plist', 'AppDomainGroup-group.com.google.Gmail')
  6. module.exports.name = 'gmail_shared_contacts'
  7. module.exports.description = 'Show Gmail account(s) shared contacts information'
  8. // Specify this reporter requires a backup.
  9. // The second parameter to func() is now a backup instead of the path to one.
  10. module.exports.requiresBackup = true
  11. // Specify this reporter supports the promises API for allowing chaining of reports.
  12. module.exports.usesPromises = true
  13. module.exports.func = function (program, backup, resolve, reject) {
  14. gmailAccountsReport(backup)
  15. .then((items) => {
  16. var result = program.formatter.format(items, {
  17. program: program,
  18. columns: {
  19. 'Account': el => el.account,
  20. 'Name': el => el.name,
  21. 'Email': el => el.email,
  22. 'Avatar': el => el.avatar
  23. }
  24. })
  25. resolve(result)
  26. })
  27. .catch(reject)
  28. }
  29. const gmailAccountsReport = (backup) => {
  30. return new Promise((resolve, reject) => {
  31. var filename = backup.getFileName(file)
  32. try {
  33. let gmailPlist = bplist.parseBuffer(fs.readFileSync(filename))[0]
  34. let gmailAccountIds = Object.keys(gmailPlist).filter(key => key.indexOf('kIdToEmailMapKey') !== -1)
  35. let gmailContactsByAccount = Object.keys(gmailPlist).filter(key => key.indexOf('kInboxSharedStorageContacts') !== -1)
  36. gmailContactsByAccount = gmailContactsByAccount.map(contactsKey => {
  37. let id = contactsKey.split('kInboxSharedStorageContacts')[1].split('_')
  38. id = id[id.length - 1]
  39. return {
  40. contactsKey: contactsKey,
  41. accountId: id
  42. }
  43. })
  44. gmailAccountIds = gmailAccountIds.map(key => {
  45. const split = key.split('kIdToEmailMapKey-')
  46. let contacts = gmailContactsByAccount.find(contacts => contacts.accountId === split[split.length - 1])
  47. return {
  48. id: split[split.length - 1],
  49. email: gmailPlist[key],
  50. contacts: gmailPlist[(contacts || {}).contactsKey]
  51. }
  52. })
  53. let contacts = []
  54. gmailAccountIds.forEach(gmailAccount => {
  55. gmailAccount.contacts = gmailAccount.contacts || []
  56. gmailAccount.contacts.forEach(contact => {
  57. contacts.push({
  58. account: gmailAccount.email,
  59. name: contact[0],
  60. email: contact[1],
  61. avatar: contact[2]
  62. })
  63. })
  64. })
  65. resolve(contacts)
  66. } catch (e) {
  67. reject(e)
  68. }
  69. })
  70. }