gmail_accounts.js 2.2 KB

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