A CLI tool to query local WeChat data with 11 commands: sessions, history, search, contacts, members, stats, export, favorites, unread, new-messages, and init. Features: - Self-contained init with key extraction (no external deps) - On-the-fly SQLCipher decryption with caching - JSON output by default for LLM/AI tool integration - Message type filtering and chat statistics - Markdown/txt export for conversations - Cross-platform: macOS, Windows, Linux
37 lines
923 B
Python
37 lines
923 B
Python
"""密钥工具 — 路径匹配、元数据剥离"""
|
|
|
|
import os
|
|
import posixpath
|
|
|
|
|
|
def strip_key_metadata(keys):
|
|
return {k: v for k, v in keys.items() if not k.startswith("_")}
|
|
|
|
|
|
def _is_safe_rel_path(path):
|
|
normalized = path.replace("\\", "/")
|
|
return ".." not in posixpath.normpath(normalized).split("/")
|
|
|
|
|
|
def key_path_variants(rel_path):
|
|
normalized = rel_path.replace("\\", "/")
|
|
variants = []
|
|
for candidate in (
|
|
rel_path,
|
|
normalized,
|
|
normalized.replace("/", "\\"),
|
|
normalized.replace("/", os.sep),
|
|
):
|
|
if candidate not in variants:
|
|
variants.append(candidate)
|
|
return variants
|
|
|
|
|
|
def get_key_info(keys, rel_path):
|
|
if not _is_safe_rel_path(rel_path):
|
|
return None
|
|
for candidate in key_path_variants(rel_path):
|
|
if candidate in keys and not candidate.startswith("_"):
|
|
return keys[candidate]
|
|
return None
|