@gloaming/poke 0.0.0-stage → 0.2.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.
Files changed (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +136 -2
  3. package/daemon.js +175 -0
  4. package/drizzle/20261123093322_init/migration.sql +38 -0
  5. package/drizzle/20261123093322_init/snapshot.json +330 -0
  6. package/drizzle/20261123101024_poll_cursors_and_secrets/migration.sql +8 -0
  7. package/drizzle/20261123101024_poll_cursors_and_secrets/snapshot.json +382 -0
  8. package/drizzle/20261123124858_owner_trust/migration.sql +5 -0
  9. package/drizzle/20261123124858_owner_trust/snapshot.json +382 -0
  10. package/drizzle/20261123130247_looks/migration.sql +7 -0
  11. package/drizzle/20261123130247_looks/snapshot.json +413 -0
  12. package/drizzle/20261123133921_look_states/migration.sql +4 -0
  13. package/drizzle/20261123133921_look_states/snapshot.json +444 -0
  14. package/drizzle/20261123140834_pattern_states/migration.sql +7 -0
  15. package/drizzle/20261123140834_pattern_states/snapshot.json +495 -0
  16. package/drizzle/20261123141452_source_cursors/migration.sql +4 -0
  17. package/drizzle/20261123141452_source_cursors/snapshot.json +526 -0
  18. package/drizzle/20261124082939_replies/migration.sql +7 -0
  19. package/drizzle/20261124082939_replies/snapshot.json +581 -0
  20. package/package.json +24 -4
  21. package/poke-skill.md +104 -0
  22. package/poke.js +301 -0
  23. package/presets/bluesky.json +22 -0
  24. package/presets/docker-events.json +18 -0
  25. package/presets/github-releases.json +22 -0
  26. package/presets/gmail.json +27 -0
  27. package/presets/home-assistant.json +23 -0
  28. package/presets/journald-over-ssh.json +27 -0
  29. package/presets/kubectl-watch.json +22 -0
  30. package/presets/log-file.json +24 -0
  31. package/presets/mastodon.json +27 -0
  32. package/presets/modbus.json +40 -0
  33. package/presets/mqtt.json +28 -0
  34. package/presets/ntfy.json +28 -0
  35. package/presets/onvif.json +23 -0
  36. package/presets/serial.json +31 -0
  37. package/presets/status-page.json +18 -0
  38. package/presets/syslog.json +24 -0
  39. package/types/discord/main.js +35 -0
  40. package/types/discord/type.json +47 -0
  41. package/types/imap/main.js +134 -0
  42. package/types/imap/type.json +54 -0
  43. package/types/presence/main.js +35 -0
  44. package/types/presence/type.json +27 -0
  45. package/types/slack/main.js +35 -0
  46. package/types/slack/type.json +52 -0
  47. package/types/system/run.sh +19 -0
  48. package/types/system/system-events.swift +153 -0
  49. package/types/system/type.json +18 -0
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "slack",
3
+ "description": "What is written in Slack channels and your direct messages, read with your user token (no Slack app); read-only: poke never posts, reacts or marks read",
4
+ "mode": "looks",
5
+ "command": "bun main.js",
6
+ "interval": "1m",
7
+ "ownerTrustable": false,
8
+ "options": {
9
+ "properties": {
10
+ "token": {
11
+ "type": "string",
12
+ "secret": true,
13
+ "description": "A Slack user token (xoxp-) with channels:history, groups:history, im:history, mpim:history, channels:read, groups:read, im:read, mpim:read and users:read, and (optional, recommended) search:read so each look finds new messages and thread replies by search instead of reading conversations in turn: ${NAME} of a poke secret"
14
+ },
15
+ "channels": {
16
+ "type": "string",
17
+ "description": "The channels to watch, by name or id, comma-separated (at most 20); none: only direct messages"
18
+ },
19
+ "mentions": {
20
+ "type": "boolean",
21
+ "description": "In channels, only messages that mention you (<@you>)"
22
+ },
23
+ "keywords": {
24
+ "type": "string",
25
+ "description": "In channels, only messages with one of these words, comma-separated; capitals do not matter"
26
+ },
27
+ "from": {
28
+ "type": "string",
29
+ "description": "User ids whose messages always count, comma-separated"
30
+ },
31
+ "dms": {
32
+ "type": "boolean",
33
+ "description": "Your direct and group conversations count (default true)"
34
+ },
35
+ "bots": {
36
+ "type": "boolean",
37
+ "description": "Also what apps and integrations post (default false)"
38
+ },
39
+ "ignore": {
40
+ "type": "string",
41
+ "description": "User or app ids never reported, comma-separated"
42
+ },
43
+ "api_base": {
44
+ "type": "string",
45
+ "description": "A Slack-compatible Web API over https or on this machine (default https://slack.com/api)"
46
+ }
47
+ },
48
+ "required": [
49
+ "token"
50
+ ]
51
+ }
52
+ }
@@ -0,0 +1,19 @@
1
+ #!/bin/sh
2
+ # Compiles system-events.swift once per version into poke's cache, then runs it.
3
+ set -e
4
+ cache="${POKE_HOME:-${TMPDIR:-/tmp}}/cache/system"
5
+ version=$(shasum -a 256 system-events.swift | cut -c1-16)
6
+ helper="$cache/system-events-$version"
7
+
8
+ if [ ! -x "$helper" ]; then
9
+ if ! command -v swiftc >/dev/null 2>&1; then
10
+ echo '{"error":"swiftc is missing: install the Xcode command line tools (xcode-select --install)"}'
11
+ exit 1
12
+ fi
13
+ mkdir -p "$cache"
14
+ echo '{"status":"compiling the system helper"}'
15
+ swiftc -O -swift-version 5 system-events.swift -o "$helper.building" >&2
16
+ mv "$helper.building" "$helper"
17
+ fi
18
+
19
+ exec "$helper"
@@ -0,0 +1,153 @@
1
+ // The `system` Source type's helper: reports this Mac's system events as
2
+ // JSON lines (docs/source-type-protocol.md). It listens to NSWorkspace,
3
+ // the distributed screen-lock notifications, IOKit power sources and the
4
+ // Network path monitor, none of which needs a privacy prompt.
5
+ import AppKit
6
+ import Foundation
7
+ import IOKit.ps
8
+ import Network
9
+
10
+ func write(_ object: [String: Any]) {
11
+ guard let data = try? JSONSerialization.data(withJSONObject: object, options: [.sortedKeys]) else { return }
12
+ FileHandle.standardOutput.write(data)
13
+ FileHandle.standardOutput.write("\n".data(using: .utf8)!)
14
+ }
15
+
16
+ func emit(_ type: String, _ payload: [String: Any] = [:]) {
17
+ write(["event": ["type": type, "payload": payload]])
18
+ }
19
+
20
+ // The `init` line: {"init":{"source":…,"options":{…},"cursor":…}}.
21
+ let initLine = readLine() ?? "{}"
22
+ let initObject = (try? JSONSerialization.jsonObject(with: Data(initLine.utf8))) as? [String: Any]
23
+ let options = ((initObject?["init"] as? [String: Any])?["options"] as? [String: Any]) ?? [:]
24
+
25
+ /// The apps to report, by name or bundle id, lowercased; empty means every app.
26
+ let apps: Set<String> = Set(
27
+ ((options["apps"] as? String) ?? "")
28
+ .split(separator: ",")
29
+ .map { $0.trimmingCharacters(in: .whitespaces).lowercased() }
30
+ .filter { !$0.isEmpty }
31
+ )
32
+ let lowPercent = (options["low"] as? Double).map { Int($0) } ?? 20
33
+
34
+ // Apps
35
+
36
+ func appPayload(_ note: Notification) -> [String: Any]? {
37
+ guard let app = note.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication else { return nil }
38
+ let name = app.localizedName ?? ""
39
+ let bundle = app.bundleIdentifier ?? ""
40
+ if !apps.isEmpty && !apps.contains(name.lowercased()) && !apps.contains(bundle.lowercased()) { return nil }
41
+ return ["name": name, "bundle": bundle, "pid": Int(app.processIdentifier)]
42
+ }
43
+
44
+ let workspace = NSWorkspace.shared.notificationCenter
45
+
46
+ workspace.addObserver(forName: NSWorkspace.didLaunchApplicationNotification, object: nil, queue: .main) { note in
47
+ if let payload = appPayload(note) { emit("app-started", payload) }
48
+ }
49
+ workspace.addObserver(forName: NSWorkspace.didTerminateApplicationNotification, object: nil, queue: .main) { note in
50
+ if let payload = appPayload(note) { emit("app-quit", payload) }
51
+ }
52
+
53
+ // Sleep and wake
54
+
55
+ workspace.addObserver(forName: NSWorkspace.willSleepNotification, object: nil, queue: .main) { _ in emit("sleeping") }
56
+ workspace.addObserver(forName: NSWorkspace.didWakeNotification, object: nil, queue: .main) { _ in emit("woke") }
57
+
58
+ // Volumes
59
+
60
+ func volumePayload(_ note: Notification) -> [String: Any] {
61
+ let url = note.userInfo?[NSWorkspace.volumeURLUserInfoKey] as? URL
62
+ let name = note.userInfo?[NSWorkspace.localizedVolumeNameUserInfoKey] as? String
63
+ return ["path": url?.path ?? "", "name": name ?? url?.lastPathComponent ?? ""]
64
+ }
65
+
66
+ workspace.addObserver(forName: NSWorkspace.didMountNotification, object: nil, queue: .main) { note in
67
+ emit("mounted", volumePayload(note))
68
+ }
69
+ workspace.addObserver(forName: NSWorkspace.didUnmountNotification, object: nil, queue: .main) { note in
70
+ emit("unmounted", volumePayload(note))
71
+ }
72
+
73
+ // Screen lock
74
+
75
+ let distributed = DistributedNotificationCenter.default()
76
+ distributed.addObserver(forName: Notification.Name("com.apple.screenIsLocked"), object: nil, queue: .main) { _ in emit("locked") }
77
+ distributed.addObserver(forName: Notification.Name("com.apple.screenIsUnlocked"), object: nil, queue: .main) { _ in emit("unlocked") }
78
+
79
+ // Power
80
+
81
+ struct Power: Equatable {
82
+ let on: Bool
83
+ let percent: Int?
84
+ }
85
+
86
+ func readPower() -> Power {
87
+ let info = IOPSCopyPowerSourcesInfo().takeRetainedValue()
88
+ let providing = IOPSGetProvidingPowerSourceType(info)?.takeUnretainedValue() as String?
89
+ let sources = IOPSCopyPowerSourcesList(info).takeRetainedValue() as [CFTypeRef]
90
+ var percent: Int?
91
+ for source in sources {
92
+ guard let description = IOPSGetPowerSourceDescription(info, source)?.takeUnretainedValue() as? [String: Any],
93
+ let current = description[kIOPSCurrentCapacityKey] as? Int,
94
+ let max = description[kIOPSMaxCapacityKey] as? Int, max > 0 else { continue }
95
+ percent = current * 100 / max
96
+ }
97
+ return Power(on: providing == kIOPMACPowerKey, percent: percent)
98
+ }
99
+
100
+ func powerPayload(_ power: Power) -> [String: Any] {
101
+ var payload: [String: Any] = ["on": power.on]
102
+ if let percent = power.percent { payload["percent"] = percent }
103
+ return payload
104
+ }
105
+
106
+ func isLow(_ power: Power) -> Bool {
107
+ !power.on && (power.percent.map { $0 <= lowPercent } ?? false)
108
+ }
109
+
110
+ // What holds at start is the baseline: only a change from it is reported.
111
+ var lastPower = readPower()
112
+ var batteryLow = isLow(lastPower)
113
+
114
+ func powerChanged() {
115
+ let power = readPower()
116
+ if power.on != lastPower.on { emit("power", powerPayload(power)) }
117
+ let low = isLow(power)
118
+ if low && !batteryLow { emit("battery-low", ["percent": power.percent ?? 0]) }
119
+ batteryLow = low
120
+ lastPower = power
121
+ status()
122
+ }
123
+
124
+ let powerSource = IOPSNotificationCreateRunLoopSource({ _ in powerChanged() }, nil).takeRetainedValue()
125
+ CFRunLoopAddSource(CFRunLoopGetMain(), powerSource, .defaultMode)
126
+
127
+ // Network
128
+
129
+ var online: Bool?
130
+ let monitor = NWPathMonitor()
131
+ monitor.pathUpdateHandler = { path in
132
+ let now = path.status == .satisfied
133
+ if let was = online, was != now { emit("network", ["online": now]) }
134
+ online = now
135
+ status()
136
+ }
137
+ monitor.start(queue: .main)
138
+
139
+ func status() {
140
+ let power = lastPower.on ? "on power" : "on battery"
141
+ let percent = lastPower.percent.map { " \($0)%" } ?? ""
142
+ let network = online.map { $0 ? ", online" : ", offline" } ?? ""
143
+ write(["status": "watching: \(power)\(percent)\(network)"])
144
+ }
145
+
146
+ // poke keeps stdin open while it runs this; its end means poke is gone.
147
+ Thread.detachNewThread {
148
+ while readLine() != nil {}
149
+ exit(0)
150
+ }
151
+
152
+ status()
153
+ RunLoop.main.run()
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "system",
3
+ "description": "This Mac's system events: sleeping, woke, locked, unlocked, power, battery-low, network, mounted, unmounted, app-started, app-quit",
4
+ "mode": "told",
5
+ "command": "sh run.sh",
6
+ "options": {
7
+ "properties": {
8
+ "apps": {
9
+ "type": "string",
10
+ "description": "Only these apps for app-started and app-quit: names or bundle ids, comma-separated; every app when absent"
11
+ },
12
+ "low": {
13
+ "type": "number",
14
+ "description": "battery-low when the charge falls to this percent on battery (default 20)"
15
+ }
16
+ }
17
+ }
18
+ }