shared_contacts.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const plist = require('../../../util/plist')
  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 = {
  7. version: 4,
  8. name: 'gmail_shared_contacts',
  9. description: `Show Gmail account(s) shared contacts information`,
  10. requiresBackup: true,
  11. // Run on a v3 lib / backup object.
  12. run (lib, { backup }) {
  13. return gmailAccountsReport(backup)
  14. },
  15. // Fields for apps report
  16. output: {
  17. 'Account': el => el.account,
  18. 'Name': el => el.name,
  19. 'Email': el => el.email,
  20. 'Avatar': el => el.avatar
  21. }
  22. }
  23. const gmailAccountsReport = (backup) => {
  24. return new Promise((resolve, reject) => {
  25. var filename = backup.getFileName(file)
  26. try {
  27. let gmailPlist = plist.parseFile(filename)
  28. let gmailAccountIds = Object.keys(gmailPlist).filter(key => key.indexOf('kIdToEmailMapKey') !== -1)
  29. let gmailContactsByAccount = Object.keys(gmailPlist).filter(key => key.indexOf('kInboxSharedStorageContacts') !== -1)
  30. gmailContactsByAccount = gmailContactsByAccount.map(contactsKey => {
  31. let id = contactsKey.split('kInboxSharedStorageContacts')[1].split('_')
  32. id = id[id.length - 1]
  33. return {
  34. contactsKey: contactsKey,
  35. accountId: id
  36. }
  37. })
  38. gmailAccountIds = gmailAccountIds.map(key => {
  39. const split = key.split('kIdToEmailMapKey-')
  40. let contacts = gmailContactsByAccount.find(contacts => contacts.accountId === split[split.length - 1])
  41. return {
  42. id: split[split.length - 1],
  43. email: gmailPlist[key],
  44. contacts: gmailPlist[(contacts || {}).contactsKey]
  45. }
  46. })
  47. let contacts = []
  48. gmailAccountIds.forEach(gmailAccount => {
  49. gmailAccount.contacts = gmailAccount.contacts || []
  50. gmailAccount.contacts.forEach(contact => {
  51. contacts.push({
  52. account: gmailAccount.email,
  53. name: contact[0],
  54. email: contact[1],
  55. avatar: contact[2]
  56. })
  57. })
  58. })
  59. resolve(contacts)
  60. } catch (e) {
  61. reject(e)
  62. }
  63. })
  64. }