elelem-macos 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stringio"
4
+ require "zlib"
5
+
6
+ module Elelem
7
+ module Macos
8
+ module Notes
9
+ class Body
10
+ GZIP_MAGIC = "\x1f\x8b".b
11
+
12
+ def self.extract(blob)
13
+ return nil unless blob
14
+
15
+ blob = blob.b
16
+ return nil unless blob.start_with?(GZIP_MAGIC)
17
+
18
+ data = Zlib::Inflate.new(Zlib::MAX_WBITS + 16).inflate(blob)
19
+ note = field(field(data, 2), 3)
20
+ field(note, 2)&.force_encoding(Encoding::UTF_8)
21
+ rescue StandardError
22
+ nil
23
+ end
24
+
25
+ def self.field(bytes, number)
26
+ return nil unless bytes
27
+
28
+ io = StringIO.new(bytes)
29
+ until io.eof?
30
+ key = varint(io)
31
+ field_number = key >> 3
32
+
33
+ value = case key & 0x7
34
+ when 0 then varint(io)
35
+ when 1 then io.read(8)
36
+ when 2 then io.read(varint(io))
37
+ when 5 then io.read(4)
38
+ else raise "unsupported wire type #{key & 0x7}"
39
+ end
40
+
41
+ return value if field_number == number
42
+ end
43
+
44
+ nil
45
+ end
46
+ private_class_method :field
47
+
48
+ def self.varint(io)
49
+ result = 0
50
+ shift = 0
51
+ loop do
52
+ byte = io.readbyte
53
+ result |= (byte & 0x7f) << shift
54
+ return result if byte < 0x80
55
+
56
+ shift += 7
57
+ end
58
+ end
59
+ private_class_method :varint
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+
5
+ require_relative "../sqlite"
6
+ require_relative "body"
7
+
8
+ module Elelem
9
+ module Macos
10
+ module Notes
11
+ class Database
12
+ DEFAULT_LIMIT = 50
13
+ MAX_LIMIT = 200
14
+ DEFAULT_DB_FILE = "~/Library/Group Containers/group.com.apple.notes/NoteStore.sqlite"
15
+ CORE_DATA_EPOCH = 978_307_200
16
+ NOTE_ENTITY = 12
17
+
18
+ NOTES_SQL = <<~SQL.freeze
19
+ SELECT n.Z_PK AS id, n.ZTITLE1 AS title, n.ZSNIPPET AS snippet,
20
+ n.ZCREATIONDATE3 AS created_at, n.ZMODIFICATIONDATE1 AS modified_at,
21
+ f.ZTITLE2 AS folder, hex(d.ZDATA) AS body_hex
22
+ FROM ZICCLOUDSYNCINGOBJECT n
23
+ LEFT JOIN ZICCLOUDSYNCINGOBJECT f ON f.Z_PK = n.ZFOLDER
24
+ LEFT JOIN ZICNOTEDATA d ON d.ZNOTE = n.Z_PK
25
+ WHERE n.Z_ENT = #{NOTE_ENTITY} AND (n.ZMARKEDFORDELETION IS NULL OR n.ZMARKEDFORDELETION = 0);
26
+ SQL
27
+
28
+ def initialize(db_file: DEFAULT_DB_FILE)
29
+ @db_file = db_file
30
+ end
31
+
32
+ def call(args)
33
+ notes(search: args["search"], limit: args["limit"])
34
+ end
35
+
36
+ private
37
+
38
+ def notes(search:, limit:)
39
+ rows = sqlite.dump(NOTES_SQL).sort_by { |n| n[:modified_at] || 0 }.reverse.map { |n| shape(n) }
40
+
41
+ if search
42
+ needle = search.downcase
43
+ rows = rows.select do |n|
44
+ n[:title].to_s.downcase.include?(needle) || n[:snippet].to_s.downcase.include?(needle) ||
45
+ n[:body].to_s.downcase.include?(needle)
46
+ end
47
+ end
48
+
49
+ rows.first(bounded_limit(limit))
50
+ end
51
+
52
+ def shape(row)
53
+ {
54
+ id: row[:id],
55
+ title: row[:title],
56
+ snippet: row[:snippet],
57
+ folder: row[:folder],
58
+ body: body(row[:body_hex]),
59
+ created_at: timestamp(row[:created_at]),
60
+ modified_at: timestamp(row[:modified_at])
61
+ }.compact
62
+ end
63
+
64
+ def body(hex)
65
+ return nil unless hex
66
+
67
+ Body.extract([hex].pack("H*"))
68
+ end
69
+
70
+ def timestamp(core_data_time)
71
+ return nil unless core_data_time
72
+
73
+ Time.at(core_data_time + CORE_DATA_EPOCH).iso8601(3)
74
+ end
75
+
76
+ def bounded_limit(limit)
77
+ (limit || DEFAULT_LIMIT).to_i.clamp(0, MAX_LIMIT)
78
+ end
79
+
80
+ def sqlite
81
+ @sqlite ||= Sqlite.new(db_file: @db_file)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "elelem"
4
+
5
+ require_relative "notes/database"
6
+
7
+ Elelem.configure do |config|
8
+ config.setup(:notes) do |agent|
9
+ agent.toolbox.add("notes",
10
+ description: "Query local macOS Notes.app: title, full body, folder, and dates, " \
11
+ "optionally filtered by a search substring. Password-protected notes " \
12
+ "return no body.",
13
+ params: {
14
+ search: { type: "string", description: "filter by title, snippet, or body substring" },
15
+ limit: { type: "integer",
16
+ description: "max results, most recently modified first (default 50, max 200)" }
17
+ }) do |args|
18
+ { results: Elelem::Macos::Notes::Database.new.call(args) }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "open3"
5
+
6
+ module Elelem
7
+ module Macos
8
+ class Sqlite
9
+ def initialize(db_file:)
10
+ @db_file = File.expand_path(db_file)
11
+ end
12
+
13
+ def dump(sql)
14
+ output, error, status = Open3.capture3(self.class.driver, @db_file, stdin_data: ".mode json\n#{sql}")
15
+ raise "sqlite3 failed to read #{@db_file}: #{error.strip}" unless status.success?
16
+ return [] if output.strip.empty?
17
+
18
+ JSON.parse(output, symbolize_names: true)
19
+ end
20
+
21
+ def self.driver
22
+ @driver ||= `which sqlite3`.strip.tap do |path|
23
+ raise "sqlite3 not found. Install it (e.g. brew/apt install sqlite3)." if path.empty?
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ module Macos
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "macos/version"
4
+ require_relative "macos/imessage"
5
+ require_relative "macos/contacts"
6
+ require_relative "macos/notes"
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elelem-macos
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mo khan
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: elelem
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0.13'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0.13'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '1'
32
+ - !ruby/object:Gem::Dependency
33
+ name: fiddle
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - "~>"
37
+ - !ruby/object:Gem::Version
38
+ version: '1.1'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - "~>"
44
+ - !ruby/object:Gem::Version
45
+ version: '1.1'
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '3.0'
53
+ type: :runtime
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '3.0'
60
+ description: Adds tools for reading local macOS app data
61
+ email:
62
+ - mo@mokhan.ca
63
+ executables: []
64
+ extensions:
65
+ - ext/elelem/macos/imessage/extconf.rb
66
+ extra_rdoc_files: []
67
+ files:
68
+ - LICENSE.txt
69
+ - ext/elelem/macos/imessage/crate/Cargo.lock
70
+ - ext/elelem/macos/imessage/crate/Cargo.toml
71
+ - ext/elelem/macos/imessage/crate/src/lib.rs
72
+ - ext/elelem/macos/imessage/extconf.rb
73
+ - lib/elelem/macos.rb
74
+ - lib/elelem/macos/contacts.rb
75
+ - lib/elelem/macos/contacts/database.rb
76
+ - lib/elelem/macos/imessage.rb
77
+ - lib/elelem/macos/imessage/database.rb
78
+ - lib/elelem/macos/imessage/native.rb
79
+ - lib/elelem/macos/notes.rb
80
+ - lib/elelem/macos/notes/body.rb
81
+ - lib/elelem/macos/notes/database.rb
82
+ - lib/elelem/macos/sqlite.rb
83
+ - lib/elelem/macos/version.rb
84
+ homepage: https://elelem.sh
85
+ licenses:
86
+ - GPL-3.0-or-later
87
+ metadata:
88
+ allowed_push_host: https://rubygems.org
89
+ homepage_uri: https://elelem.sh
90
+ source_code_uri: https://github.com/elelem-sh/macos
91
+ rubygems_mfa_required: 'true'
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 4.0.0
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubygems_version: 4.0.20
107
+ specification_version: 4
108
+ summary: macOS integration tools for elelem.
109
+ test_files: []