gmail_shared_contacts.js 2.7 KB

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