accounts.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_accounts'
  7. module.exports.description = 'Show Gmail account(s) 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. 'Id': el => el.id,
  20. 'Email': el => el.email,
  21. 'Avatar': el => el.avatar || null
  22. }
  23. })
  24. resolve(result)
  25. })
  26. .catch(reject)
  27. }
  28. const gmailAccountsReport = (backup) => {
  29. return new Promise((resolve, reject) => {
  30. var filename = backup.getFileName(file)
  31. try {
  32. let gmailPlist = bplist.parseBuffer(fs.readFileSync(filename))[0]
  33. let gmailAccountIds = Object.keys(gmailPlist).filter(key => key.indexOf('kIdToEmailMapKey') !== -1)
  34. let gmailAvatars = Object.keys(gmailPlist).filter(key => key.indexOf('kCurrentAvatarUrlKey') !== -1)
  35. gmailAvatars = gmailAvatars.map(avatarKey => {
  36. let id = avatarKey.split('kCurrentAvatarUrlKey')[1].split('-')
  37. id = id[id.length - 1]
  38. return {
  39. avatarKey: avatarKey,
  40. accountId: id
  41. }
  42. })
  43. gmailAccountIds = gmailAccountIds.map(key => {
  44. const split = key.split('-')
  45. let avatar = gmailAvatars.find(avatar => avatar.accountId === split[split.length - 1])
  46. return {
  47. id: split[split.length - 1],
  48. email: gmailPlist[key],
  49. avatar: gmailPlist[(avatar || {}).avatarKey]
  50. }
  51. })
  52. resolve(gmailAccountIds)
  53. } catch (e) {
  54. reject(e)
  55. }
  56. })
  57. }