$ show openclaw/imsg
This repository is a macOS iMessage/SMS automation tool called `imsg`. It provides a CLI and JSON-RPC interface for reading, watching, and sending messages. The architecture has three layers: a Swift CLI that parses commands and outputs NDJSON or plain text; a core library (`IMsgCore`) that reads the local Messages SQLite database and manages the bridge to Messages.app; and an injected Objective-C dylib (`IMsgHelper`) that hooks into private IMCore APIs for advanced features like typing indicators, polls, stickers, rich links, and message mutations. The tool can fall back to AppleScript automation for basic sends when the bridge is unavailable (e.g., SIP enabled). The RPC server runs over stdio, making it suitable for integration with AI agents and scripts. The codebase is well-tested with unit tests for all layers, including native Objective-C tests for the injected helper. It demonstrates deep reverse-engineering of Apple's private messaging protocols and careful handling of platform-specific quirks. The primary data flow is read-only from the local `chat.db` SQLite database for history and live updates, with write operations going through either AppleScript (via `osascript`) or the injected bridge (via UUID-keyed IPC queues). External dependencies include SQLite.swift, PhoneNumberKit, ffmpeg, afconvert, and private macOS frameworks (IMCore, CNContactStore). The build system uses SwiftPM with custom scripts for universal macOS binaries and Linux static archives. The project is actively maintained with CI, releases, and a documentation site.
The LLM identified 5 files worth highlighting for reuse:
A self-contained utility that kills a child process (and its process group) after a configurable timeout. Uses Swift's Process and signal handling to ensure cleanup. Demonstrates careful handling of process groups, SIGTERM/SIGKILL escalation, and resource deallocation. Reusable in any Swift project that needs to enforce timeouts on external commands.
A utility that checks every component of a file path for symlinks using lstat, preventing symlink traversal attacks. Self-contained and platform-aware (uses #if os(macOS) for lstat). Demonstrates security-conscious file system operations. Reusable in any Swift project that handles user-supplied paths.
Decodes Apple's proprietary typed-stream binary format used in attributed body BLOBs. Handles length-prefixed UTF-16LE strings, integer types, and nested structures. Self-contained and well-documented with comments explaining the reverse-engineered format. Reusable for any project that needs to parse Apple's binary attributed body data.
A declarative command specification framework that defines CLI commands with options, flags, and usage examples using a fluent API (.make, .withFlags, .withExamples). Self-contained and independent of the rest of the codebase. Demonstrates a clean pattern for building composable CLI interfaces. Reusable in any Swift CLI project.
A thread-safe NDJSON writer that encodes Swift structs as JSON lines with proper escaping of newlines and control characters. Self-contained and uses Swift's standard library only. Demonstrates correct handling of JSON streaming for line-delimited output. Reusable in any Swift project that needs to output NDJSON.