notes.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const fileHash = require('../../util/backup_filehash')
  2. const log = require('../../util/log')
  3. const apple_timestamp = require('../../util/apple_timestamp')
  4. const NOTES_DB = fileHash('Library/Notes/notes.sqlite')
  5. const NOTES2_DB = fileHash('NoteStore.sqlite', 'AppDomainGroup-group.com.apple.notes')
  6. module.exports = {
  7. version: 4,
  8. name: 'notes',
  9. description: `List all iOS notes`,
  10. requiresBackup: true,
  11. // Run on a v3 lib / backup object.
  12. run (lib, { backup }) {
  13. return getAllNotes(backup)
  14. },
  15. // Public facing properties
  16. output: {
  17. id: el => el.Z_PK,
  18. identifier: el => el.ZIDENTIFIER,
  19. modified: el => (el.XFORMATTEDDATESTRING || el.XFORMATTEDDATESTRING1) + '',
  20. passwordProtected: el => !!el.ZISPASSWORDPROTECTED,
  21. title: el => (el.ZTITLE || el.ZTITLE1 || el.ZTITLE2 || '').trim() || null,
  22. content: el => el.ZCONTENT || null
  23. }
  24. }
  25. function getAllNotes (backup) {
  26. return new Promise(async (resolve, reject) => {
  27. var newNotes
  28. // Try iOS 10/11 query.
  29. try {
  30. newNotes = await getNewNotesiOS10iOS11(backup)
  31. } catch (e) {
  32. log.verbose(`couldn't query notes as iOS10/11, trying iOS9`, e)
  33. }
  34. // If iOS 10/11 query fails, try iOS 9.
  35. if (newNotes == null) {
  36. try {
  37. newNotes = await getNewNotesiOS9(backup)
  38. } catch (e) {
  39. log.verbose(`couldn't query notes as iOS9`, e)
  40. }
  41. }
  42. // Try to fetch old notes database
  43. try {
  44. var oldNotes = await getOldNotes(backup)
  45. } catch (e) {
  46. log.verbose(`couldn't query old notes`, e)
  47. }
  48. // If we didn't get anything successfully, reject.
  49. if (newNotes == null && oldNotes == null) {
  50. return reject(new Error(`Couldn't find any known notes database in the system.`))
  51. }
  52. // Join the notes together.
  53. let result = [...(newNotes || []), ...(oldNotes || [])]
  54. // console.log(result)
  55. log.verbose(result)
  56. resolve(result)
  57. })
  58. }
  59. function getNewNotesiOS9 (backup) {
  60. return new Promise((resolve, reject) => {
  61. backup.openDatabase(NOTES2_DB)
  62. .then(db => {
  63. db.all(`SELECT ZICCLOUDSYNCINGOBJECT.*, ZICNOTEDATA.ZDATA as X_CONTENT_DATA, ${apple_timestamp.parse('ZCREATIONDATE')} AS XFORMATTEDDATESTRING FROM ZICCLOUDSYNCINGOBJECT LEFT JOIN ZICNOTEDATA ON ZICCLOUDSYNCINGOBJECT.ZNOTE = ZICNOTEDATA.ZNOTE`, async function (err, rows) {
  64. if (err) reject(err)
  65. resolve(rows)
  66. })
  67. })
  68. .catch(reject)
  69. })
  70. }
  71. function getOldNotes (backup) {
  72. return new Promise((resolve, reject) => {
  73. backup.openDatabase(NOTES_DB)
  74. .then(db => {
  75. db.all(`SELECT *, ${apple_timestamp.parse('ZCREATIONDATE')} AS XFORMATTEDDATESTRING from ZNOTE LEFT JOIN ZNOTEBODY ON ZBODY = ZNOTEBODY.Z_PK`, function (err, rows) {
  76. if (err) reject(err)
  77. resolve(rows)
  78. })
  79. })
  80. .catch(reject)
  81. })
  82. }
  83. function getNewNotesiOS10iOS11 (backup) {
  84. return new Promise((resolve, reject) => {
  85. backup.openDatabase(NOTES2_DB)
  86. .then(db => {
  87. db.all(`SELECT ZICCLOUDSYNCINGOBJECT.*, ZICNOTEDATA.ZDATA as X_CONTENT_DATA, ${apple_timestamp.parse('(ZCREATIONDATE')} AS XFORMATTEDDATESTRING, ${apple_timestamp.parse('ZCREATIONDATE1')} AS XFORMATTEDDATESTRING1 FROM ZICCLOUDSYNCINGOBJECT LEFT JOIN ZICNOTEDATA ON ZICCLOUDSYNCINGOBJECT.ZNOTE = ZICNOTEDATA.ZNOTE`, function (err, rows) {
  88. if (err) reject(err)
  89. resolve(rows)
  90. })
  91. })
  92. .catch(reject)
  93. })
  94. }