sideora 0.0.1-alpha.0 → 0.1.0-alpha.2
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.
- package/LICENSE.md +9 -0
- package/README.md +57 -6
- package/assets/sounds/freesound_community-mail-40275.mp3 +0 -0
- package/assets/sounds/mixkit-clear-announce-tones-2861.wav +0 -0
- package/assets/sounds/mixkit-happy-bells-notification-937.wav +0 -0
- package/assets/sounds/mixkit-musical-alert-notification-2309.wav +0 -0
- package/assets/sounds/nice-chime.mp3 +0 -0
- package/bin/sideora.js +2430 -12
- package/extension/dist/assets/background.ts-NW8YqAlO.js +1 -0
- package/extension/dist/assets/fillCode-Ci8IkBvY.js +1 -0
- package/extension/dist/assets/index.html-DrrPha1M.js +2 -0
- package/extension/dist/assets/modulepreload-polyfill-DcjICKwh.js +1 -0
- package/extension/dist/assets/shell-82MNMljp.css +1 -0
- package/extension/dist/assets/shell-QYBN1bx1.js +1 -0
- package/extension/dist/icons/icon-128.png +0 -0
- package/extension/dist/icons/icon-16.png +0 -0
- package/extension/dist/icons/icon-32.png +0 -0
- package/extension/dist/icons/icon-48.png +0 -0
- package/extension/dist/manifest.json +40 -0
- package/extension/dist/service-worker-loader.js +1 -0
- package/extension/dist/src/newtab/index.html +14 -0
- package/extension/dist/src/panel/index.html +15 -0
- package/extension/dist-web/assets/abnfDiagram-WEYOIA3C-CoXOc872.js +1 -0
- package/extension/dist-web/assets/apple-touch-icon-DSgRQASq.png +0 -0
- package/extension/dist-web/assets/architecture-WOLXFQ4H-BkJ4AerO.js +1 -0
- package/extension/dist-web/assets/architectureDiagram-ZHVYKT53-aPkSDlxA.js +36 -0
- package/extension/dist-web/assets/blockDiagram-RJAE7QA3-CH1G4NBZ.js +140 -0
- package/extension/dist-web/assets/c4Diagram-XDPPPE54-Baum1zDu.js +38 -0
- package/extension/dist-web/assets/channel-PsQg-mnK.js +1 -0
- package/extension/dist-web/assets/chunk-2E7WHVMD-CKvLrWRa.js +88 -0
- package/extension/dist-web/assets/chunk-2Q5K7J3B-C1jixKkw.js +1 -0
- package/extension/dist-web/assets/chunk-4EMNKR2T-6fjnroCZ.js +1 -0
- package/extension/dist-web/assets/chunk-55XCU76K-Ygs3sNpi.js +1 -0
- package/extension/dist-web/assets/chunk-5VM5RSS4-ZNzvKenW.js +15 -0
- package/extension/dist-web/assets/chunk-7S6XSTFO-iX5xYzfP.js +2 -0
- package/extension/dist-web/assets/chunk-AQDTSKZB-D9zwhFI8.js +72 -0
- package/extension/dist-web/assets/chunk-DSNLSHJZ-siGS-4qu.js +129 -0
- package/extension/dist-web/assets/chunk-DUW6YSOI-Dgx8z5s3.js +1 -0
- package/extension/dist-web/assets/chunk-HAGFOTED-_3dtnWXn.js +62 -0
- package/extension/dist-web/assets/chunk-HNE4Q36P-BPzRHi72.js +1 -0
- package/extension/dist-web/assets/chunk-J5ZVWO5B-oDCd-gWO.js +1 -0
- package/extension/dist-web/assets/chunk-JWPE2WC7-DVXcaiue.js +1 -0
- package/extension/dist-web/assets/chunk-NGNAAXSQ-DI_UYJZ4.js +136 -0
- package/extension/dist-web/assets/chunk-NQOKRVIF-AXbmOBjs.js +213 -0
- package/extension/dist-web/assets/chunk-UHW7PFBP-B3xrZ8sD.js +1 -0
- package/extension/dist-web/assets/chunk-VQ5W62QI-D_k7Pa6b.js +1 -0
- package/extension/dist-web/assets/chunk-WQQL4CDK-BBIrWwoZ.js +10 -0
- package/extension/dist-web/assets/chunk-XXDRQBXY-i7MKP1NY.js +1 -0
- package/extension/dist-web/assets/chunk-Y2CYZVJY-DsF7k-Jl.js +1 -0
- package/extension/dist-web/assets/chunk-YZO67HFG-Z7qp5QAI.js +1 -0
- package/extension/dist-web/assets/chunk-ZAUX2LTX-CSt3c0Rb.js +1 -0
- package/extension/dist-web/assets/chunk-ZCHMQSOR-g8XRABGK.js +2 -0
- package/extension/dist-web/assets/classDiagram-v2-4MYWR3UA-BL706wzc.js +217 -0
- package/extension/dist-web/assets/cose-bilkent-JH36ORCC-C6J2IoHg.js +1 -0
- package/extension/dist-web/assets/cynefin-EF2NZ3EQ-DLhQOiAy.js +1 -0
- package/extension/dist-web/assets/cynefinDiagram-WF24UDRP-Cv9Lx6_l.js +62 -0
- package/extension/dist-web/assets/cytoscape.esm-nTrd5EVq.js +321 -0
- package/extension/dist-web/assets/d3-scale-CElPUUud.js +1 -0
- package/extension/dist-web/assets/d3-selection-COWvaQNI.js +1 -0
- package/extension/dist-web/assets/dagre-NK4GIZVX-ngM-w2EC.js +4 -0
- package/extension/dist-web/assets/diagram-CBK3MWTC-kp8sayNG.js +24 -0
- package/extension/dist-web/assets/diagram-I5XIIV2D-BZ_Dgs6K.js +24 -0
- package/extension/dist-web/assets/diagram-ODIL2UCW-DWhUyArG.js +30 -0
- package/extension/dist-web/assets/diagram-PFBEJUXE-CBAdEK7l.js +3 -0
- package/extension/dist-web/assets/diagram-RJA3I4VV-kOdIr_3K.js +41 -0
- package/extension/dist-web/assets/diagram-TYUNFSNR-BrWGa0x5.js +200 -0
- package/extension/dist-web/assets/dist-DdXDFcej.js +1 -0
- package/extension/dist-web/assets/docx-preview-D1zbEiLy.js +20 -0
- package/extension/dist-web/assets/earth-Ce3xs8Cb.webp +0 -0
- package/extension/dist-web/assets/ebnfDiagram-V4P3TTPT-0r3weWmj.js +1 -0
- package/extension/dist-web/assets/elk-QONTBCTQ-D3oElRZ3.js +27 -0
- package/extension/dist-web/assets/erDiagram-KYQIGQAA-B9oU7_j_.js +99 -0
- package/extension/dist-web/assets/eventmodeling-K75KTNOO-CMKgc1nP.js +1 -0
- package/extension/dist-web/assets/favicon-D_X6tUVf.svg +5 -0
- package/extension/dist-web/assets/flowDiagram-IUGLLWBU-yRwks0Mw.js +1 -0
- package/extension/dist-web/assets/ganttDiagram-R7GPNTJY-cJqD1qwQ.js +292 -0
- package/extension/dist-web/assets/gitGraph-VSP46ZUC-CuBQqkbr.js +1 -0
- package/extension/dist-web/assets/gitGraphDiagram-PDYL7XHY-D7bHnQXC.js +106 -0
- package/extension/dist-web/assets/graphlib-DS17s2tU.js +1 -0
- package/extension/dist-web/assets/home-CV7-8Trs.js +1 -0
- package/extension/dist-web/assets/index-CQvw7Wy_.js +7 -0
- package/extension/dist-web/assets/index-CVKnZ21i.css +1 -0
- package/extension/dist-web/assets/info-OHQRW6UA-B8kyzV4O.js +1 -0
- package/extension/dist-web/assets/infoDiagram-MVDRKAX5-CEm6lezX.js +2 -0
- package/extension/dist-web/assets/ishikawaDiagram-HPEHV2GE-Ce3uv2wr.js +70 -0
- package/extension/dist-web/assets/journeyDiagram-BOSHW7O7-QgdahHCP.js +139 -0
- package/extension/dist-web/assets/jszip.min-3XXygzvy.js +2 -0
- package/extension/dist-web/assets/kanban-definition-FNSA4HC5-DPuTtWgR.js +89 -0
- package/extension/dist-web/assets/katex-MEuSR36T.js +257 -0
- package/extension/dist-web/assets/mermaid-parser.core-h7U7YGn_.js +7 -0
- package/extension/dist-web/assets/mermaid.core-DkGhophj.js +44 -0
- package/extension/dist-web/assets/mindmap-definition-6OY57UG3-D7VQxbda.js +98 -0
- package/extension/dist-web/assets/mount-CBzzf6KQ.js +195 -0
- package/extension/dist-web/assets/mount-DtvYnQXt.css +1 -0
- package/extension/dist-web/assets/officeparser.browser.slim-kEjhTrQ1.js +1989 -0
- package/extension/dist-web/assets/packet-JDAUHWVQ-oafHgw6X.js +1 -0
- package/extension/dist-web/assets/pegDiagram-ECIUJNBX-C3zo1naj.js +1 -0
- package/extension/dist-web/assets/peoria-B5TBlKDG.jpg +0 -0
- package/extension/dist-web/assets/peoria-thumb-CN8upAdZ.jpg +0 -0
- package/extension/dist-web/assets/pie-XZMESJXO-DXzeBQqF.js +1 -0
- package/extension/dist-web/assets/pieDiagram-GNMPY74T-S5NzPthl.js +39 -0
- package/extension/dist-web/assets/quadrantDiagram-D57K4MC3-C9glHQmk.js +7 -0
- package/extension/dist-web/assets/radar-ABXABTNO-CWTNqtbu.js +1 -0
- package/extension/dist-web/assets/railroad-I3PHUGI6-BTpD5_Bu.js +1 -0
- package/extension/dist-web/assets/railroad-abnf-LNEFI6M7-bWyxoXQG.js +1 -0
- package/extension/dist-web/assets/railroad-ebnf-DWYD2UWJ-ri5733DQ.js +1 -0
- package/extension/dist-web/assets/railroad-peg-JR7BVNR7-CmGfTubP.js +1 -0
- package/extension/dist-web/assets/railroadDiagram-Y72NRBL7-DTfe6Dh1.js +1 -0
- package/extension/dist-web/assets/requirementDiagram-EQYSW5RA-DQDzcS3L.js +84 -0
- package/extension/dist-web/assets/rolldown-runtime-COiEfhsA.js +1 -0
- package/extension/dist-web/assets/rough.esm-Dy-Kn_BL.js +1 -0
- package/extension/dist-web/assets/sankeyDiagram-SY24JHO5-DyMKWxdB.js +40 -0
- package/extension/dist-web/assets/sequenceDiagram-BLEHNHQU-dIkB1mCn.js +169 -0
- package/extension/dist-web/assets/sizeCapture-INFHLROL-3uHXrFqA.js +1 -0
- package/extension/dist-web/assets/stateDiagram-v2-A6IESKWZ-Bj_vxEVF.js +291 -0
- package/extension/dist-web/assets/swimlanes-B3ENRKFQ-BSkWEwHR.js +1 -0
- package/extension/dist-web/assets/swimlanesDiagram-AYQRV4AK-BTzSDlSM.js +8 -0
- package/extension/dist-web/assets/timeline-definition-JXYKGLGB-TwfWS0pw.js +120 -0
- package/extension/dist-web/assets/treeView-D4JQ5SDB-BzA_8YYb.js +1 -0
- package/extension/dist-web/assets/treemap-SAJKECNS-BI9d6--2.js +1 -0
- package/extension/dist-web/assets/usecaseDiagram-WPOLBNNX-BOVbAUPk.js +328 -0
- package/extension/dist-web/assets/vennDiagram-Q3Y3IDCW-X2kYAWaj.js +34 -0
- package/extension/dist-web/assets/wardley-7MLQ67FV-CItxqJ8G.js +1 -0
- package/extension/dist-web/assets/wardleyDiagram-IVAIDKWA-BnUXvKD_.js +78 -0
- package/extension/dist-web/assets/xychartDiagram-4URNHDU4-ysvBXvyE.js +7 -0
- package/extension/dist-web/icon-192.png +0 -0
- package/extension/dist-web/icon-512.png +0 -0
- package/extension/dist-web/index.html +25 -0
- package/extension/dist-web/manifest.webmanifest +14 -0
- package/package.json +64 -12
- package/plugin/.claude-plugin/icon.png +0 -0
- package/plugin/.claude-plugin/plugin.json +6 -0
- package/plugin/.mcp.json +8 -0
- package/plugin/README.md +72 -0
- package/plugin/mcp-launcher.mjs +48 -0
- package/src/account-probe.js +45 -0
- package/src/accounts.js +687 -0
- package/src/ai.js +269 -0
- package/src/assistant-runs.js +32 -0
- package/src/attached-mail.js +76 -0
- package/src/auth-gate.js +70 -0
- package/src/backup-cli.js +158 -0
- package/src/backup.js +329 -0
- package/src/bimi.js +233 -0
- package/src/bridge.js +101 -0
- package/src/calendar-alerts.js +56 -0
- package/src/chats.js +27 -0
- package/src/compose-editor.js +58 -0
- package/src/config.js +420 -0
- package/src/connections.d.ts +24 -0
- package/src/connections.js +37 -0
- package/src/core/agent.js +92 -0
- package/src/core/assistant.d.ts +13 -0
- package/src/core/assistant.js +790 -0
- package/src/core/chats.js +323 -0
- package/src/core/markdown.d.ts +8 -0
- package/src/core/markdown.js +239 -0
- package/src/core/notes.js +445 -0
- package/src/core/otp.js +152 -0
- package/src/core/rules.js +196 -0
- package/src/core/runs.d.ts +9 -0
- package/src/core/runs.js +211 -0
- package/src/core/thread.js +526 -0
- package/src/core/write.js +247 -0
- package/src/core-db.js +131 -0
- package/src/core.js +174 -0
- package/src/credentials.js +320 -0
- package/src/daemon.js +564 -0
- package/src/events.js +213 -0
- package/src/extension-id.d.ts +2 -0
- package/src/extension-id.js +5 -0
- package/src/file-lock.js +27 -0
- package/src/files-index.js +154 -0
- package/src/google-contacts.js +315 -0
- package/src/google-signin.js +306 -0
- package/src/host/assistant-html.js +44 -0
- package/src/host/chats-files.js +137 -0
- package/src/host/file-prep.js +122 -0
- package/src/host/notes-files.js +158 -0
- package/src/host/office-text.js +224 -0
- package/src/host/office-worker.js +8 -0
- package/src/host/rules-file.js +20 -0
- package/src/host/secrets-file.js +91 -0
- package/src/host/systemd.js +90 -0
- package/src/hub.js +245 -0
- package/src/identity.js +316 -0
- package/src/labels.js +5 -0
- package/src/mail-port.d.ts +79 -0
- package/src/mail-port.js +148 -0
- package/src/mcp-server.js +747 -0
- package/src/microsoft-app.js +20 -0
- package/src/microsoft-signin.js +194 -0
- package/src/migrate.js +327 -0
- package/src/notes-html.js +450 -0
- package/src/notes.js +19 -0
- package/src/notify.js +105 -0
- package/src/onepassword.js +223 -0
- package/src/op-registry.d.ts +14 -0
- package/src/op-registry.js +150 -0
- package/src/ops.js +1251 -0
- package/src/otp.js +6 -0
- package/src/panel-ops.js +273 -0
- package/src/pictures.js +735 -0
- package/src/postinstall.js +25 -0
- package/src/relay-passkey.js +122 -0
- package/src/relay-request.js +155 -0
- package/src/relay-seal.js +56 -0
- package/src/relay-settings.js +139 -0
- package/src/relay.js +876 -0
- package/src/render.js +82 -0
- package/src/rules-store.js +8 -0
- package/src/safe-fetch.js +215 -0
- package/src/sender-auth.js +270 -0
- package/src/site-match.js +77 -0
- package/src/tui-agenda.js +266 -0
- package/src/tui.js +670 -0
- package/src/vmc.js +376 -0
- package/src/wake.js +96 -0
- package/src/web-access.js +185 -0
- package/src/web-server.js +241 -0
- package/src/web-service.js +152 -0
package/src/events.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// Local event bus. The daemon publishes mail activity two ways:
|
|
2
|
+
//
|
|
3
|
+
// 1. events.sock — live NDJSON stream (hello, newmail, match, wake, stream);
|
|
4
|
+
// fire-and-forget, nothing is replayed.
|
|
5
|
+
// 2. http.sock — HTTP over a Unix socket with a cursor-backed queue of
|
|
6
|
+
// mail and calendar events (newmail / wake / calendar / reminder) and a
|
|
7
|
+
// LONG-POLL endpoint:
|
|
8
|
+
//
|
|
9
|
+
// GET /events?after=<cursor>&timeout=<s>&match=rules|all&account=<a>&rule=<r>&types=<t,t>
|
|
10
|
+
// → { events: [...], cursor } immediately when events exist past the
|
|
11
|
+
// cursor, else when the first matching one lands, else on timeout;
|
|
12
|
+
// plus { gap: true, oldest, lost } when `after` predates the oldest
|
|
13
|
+
// retained event (the queue keeps the last 500): the client should
|
|
14
|
+
// resync (list_unread / the agenda) rather than trust the stream
|
|
15
|
+
// (events: []). match=rules (default) = mail that hit a rule.
|
|
16
|
+
// Calendar events (calendar = created/updated/cancelled/deleted,
|
|
17
|
+
// reminder = an event starts soon) are delivered only when `types`
|
|
18
|
+
// names them, e.g. types=newmail,reminder.
|
|
19
|
+
// GET /cursor → { cursor } the latest sequence number
|
|
20
|
+
//
|
|
21
|
+
// Sequence numbers are monotonic and persisted (events-log.json), so a
|
|
22
|
+
// client can re-arm with its last cursor across daemon restarts and
|
|
23
|
+
// miss nothing. From a shell: curl --unix-socket ~/.sideora/http.sock
|
|
24
|
+
// 'http://mail/events?after=42&timeout=600'. The CLI wraps it as
|
|
25
|
+
// `sideora events --wait`, the MCP server as `next_event`.
|
|
26
|
+
|
|
27
|
+
import net from 'node:net';
|
|
28
|
+
import http from 'node:http';
|
|
29
|
+
import fs from 'node:fs';
|
|
30
|
+
import path from 'node:path';
|
|
31
|
+
import { HOME_DIR } from './config.js';
|
|
32
|
+
|
|
33
|
+
export const EVENTS_SOCK = path.join(HOME_DIR, 'events.sock');
|
|
34
|
+
export const HTTP_SOCK = path.join(HOME_DIR, 'http.sock');
|
|
35
|
+
const LOG_PATH = path.join(HOME_DIR, 'events-log.json');
|
|
36
|
+
const QUEUE_MAX = 500;
|
|
37
|
+
const QUEUED_TYPES = new Set(['newmail', 'wake', 'calendar', 'reminder']);
|
|
38
|
+
const MAIL_TYPES = new Set(['newmail', 'wake']);
|
|
39
|
+
const MAX_WAIT_S = 3600;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A cursor older than the oldest event still retained means events in
|
|
43
|
+
* between are gone (the queue is bounded): { gap: true, oldest, lost }, else {}.
|
|
44
|
+
*/
|
|
45
|
+
export function gapInfo(after, queue, seq) {
|
|
46
|
+
const oldest = queue.length ? queue[0].seq : seq + 1;
|
|
47
|
+
if (after >= oldest - 1) return {};
|
|
48
|
+
return { gap: true, oldest: queue.length ? oldest : null, lost: Math.min(seq, oldest - 1) - after };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function startEventServer(log = () => {}) {
|
|
52
|
+
const clients = new Set();
|
|
53
|
+
|
|
54
|
+
// --- cursor-backed queue --------------------------------------------------
|
|
55
|
+
let seq = 0;
|
|
56
|
+
let queue = [];
|
|
57
|
+
try {
|
|
58
|
+
const saved = JSON.parse(fs.readFileSync(LOG_PATH, 'utf8'));
|
|
59
|
+
seq = Number(saved.seq) || 0;
|
|
60
|
+
queue = Array.isArray(saved.events) ? saved.events.slice(-QUEUE_MAX) : [];
|
|
61
|
+
} catch {}
|
|
62
|
+
const persist = () => {
|
|
63
|
+
try { fs.writeFileSync(LOG_PATH, JSON.stringify({ seq, events: queue }), { mode: 0o600 }); } catch {}
|
|
64
|
+
};
|
|
65
|
+
const waiters = new Set(); // { filter, resolve, timer }
|
|
66
|
+
|
|
67
|
+
const matches = (e, f) => {
|
|
68
|
+
if (f.types && !f.types.includes(e.type)) return false;
|
|
69
|
+
if (MAIL_TYPES.has(e.type)) {
|
|
70
|
+
if (f.match !== 'all' && !e.rule) return false;
|
|
71
|
+
if (f.rule && e.rule !== f.rule) return false;
|
|
72
|
+
} else if (!f.types) {
|
|
73
|
+
return false; // calendar/reminder: only when asked for via types=
|
|
74
|
+
}
|
|
75
|
+
if (f.account && e.account !== f.account) return false;
|
|
76
|
+
return true;
|
|
77
|
+
};
|
|
78
|
+
const since = (after, f) => queue.filter((e) => e.seq > after && matches(e, f));
|
|
79
|
+
|
|
80
|
+
// --- live NDJSON socket ---------------------------------------------------
|
|
81
|
+
const server = net.createServer((sock) => {
|
|
82
|
+
clients.add(sock);
|
|
83
|
+
sock.on('close', () => clients.delete(sock));
|
|
84
|
+
sock.on('error', () => clients.delete(sock));
|
|
85
|
+
sock.write(line({ type: 'hello', service: 'sideora', pid: process.pid, cursor: seq }));
|
|
86
|
+
});
|
|
87
|
+
server.on('error', (e) => log('event server error: ' + e.message));
|
|
88
|
+
|
|
89
|
+
// --- HTTP long-poll over a Unix socket ------------------------------------
|
|
90
|
+
const httpServer = http.createServer((req, res) => {
|
|
91
|
+
const url = new URL(req.url, 'http://mail');
|
|
92
|
+
const json = (code, body) => { res.writeHead(code, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(body) + '\n'); };
|
|
93
|
+
if (req.method !== 'GET') return json(405, { error: 'GET only' });
|
|
94
|
+
if (url.pathname === '/cursor') return json(200, { cursor: seq });
|
|
95
|
+
if (url.pathname !== '/events') return json(404, { error: 'unknown path — use /events or /cursor' });
|
|
96
|
+
const q = url.searchParams;
|
|
97
|
+
const types = String(q.get('types') || '').split(',').map((t) => t.trim()).filter(Boolean);
|
|
98
|
+
const filter = { match: q.get('match') === 'all' ? 'all' : 'rules', account: q.get('account') || null, rule: q.get('rule') || null, types: types.length ? types : null };
|
|
99
|
+
const afterRaw = q.get('after');
|
|
100
|
+
const after = afterRaw == null || afterRaw === 'now' ? seq : Math.max(0, Number(afterRaw) || 0);
|
|
101
|
+
const timeout = Math.min(MAX_WAIT_S, Math.max(0, Number(q.get('timeout') ?? 90) || 0));
|
|
102
|
+
const limit = Math.min(100, Math.max(1, Number(q.get('limit')) || 50));
|
|
103
|
+
|
|
104
|
+
const gap = afterRaw != null && afterRaw !== 'now' ? gapInfo(after, queue, seq) : {};
|
|
105
|
+
|
|
106
|
+
const ready = since(after, filter);
|
|
107
|
+
if (ready.length || timeout === 0) return json(200, { events: ready.slice(0, limit), cursor: ready.length ? ready[Math.min(limit, ready.length) - 1].seq : seq, ...gap });
|
|
108
|
+
|
|
109
|
+
const w = { filter, resolve: (events) => json(200, { events, cursor: events.length ? events[events.length - 1].seq : seq, ...gap }), timer: null };
|
|
110
|
+
w.timer = setTimeout(() => { waiters.delete(w); w.resolve([]); }, timeout * 1000);
|
|
111
|
+
waiters.add(w);
|
|
112
|
+
req.on('close', () => { clearTimeout(w.timer); waiters.delete(w); });
|
|
113
|
+
});
|
|
114
|
+
httpServer.on('error', (e) => log('event http error: ' + e.message));
|
|
115
|
+
|
|
116
|
+
const inos = { events: null, http: null };
|
|
117
|
+
const listen = () => {
|
|
118
|
+
try { fs.unlinkSync(EVENTS_SOCK); } catch {}
|
|
119
|
+
try { fs.unlinkSync(HTTP_SOCK); } catch {}
|
|
120
|
+
server.listen(EVENTS_SOCK, () => {
|
|
121
|
+
try { inos.events = fs.statSync(EVENTS_SOCK).ino; } catch {}
|
|
122
|
+
log('event socket listening at ' + EVENTS_SOCK);
|
|
123
|
+
});
|
|
124
|
+
httpServer.listen(HTTP_SOCK, () => {
|
|
125
|
+
try { inos.http = fs.statSync(HTTP_SOCK).ino; } catch {}
|
|
126
|
+
log(`event long-poll at ${HTTP_SOCK} (cursor ${seq})`);
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
listen();
|
|
130
|
+
|
|
131
|
+
// If another process (a foreground `sideora watch`, say) replaced the
|
|
132
|
+
// socket files and left, clients would get ECONNREFUSED forever: re-bind.
|
|
133
|
+
const guard = setInterval(() => {
|
|
134
|
+
const cur = (p) => { try { return fs.statSync(p).ino; } catch { return null; } };
|
|
135
|
+
if ((inos.events !== null && cur(EVENTS_SOCK) !== inos.events) || (inos.http !== null && cur(HTTP_SOCK) !== inos.http)) {
|
|
136
|
+
log('event socket path was replaced — re-binding');
|
|
137
|
+
let pending = 2;
|
|
138
|
+
const done = () => { if (--pending === 0) listen(); };
|
|
139
|
+
server.close(done);
|
|
140
|
+
httpServer.close(done);
|
|
141
|
+
}
|
|
142
|
+
}, 30000);
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
emit(type, data = {}) {
|
|
146
|
+
const event = { ts: new Date().toISOString(), type, ...data };
|
|
147
|
+
if (QUEUED_TYPES.has(type)) {
|
|
148
|
+
event.seq = ++seq;
|
|
149
|
+
queue.push(event);
|
|
150
|
+
if (queue.length > QUEUE_MAX) queue = queue.slice(-QUEUE_MAX);
|
|
151
|
+
persist();
|
|
152
|
+
for (const w of Array.from(waiters)) {
|
|
153
|
+
if (matches(event, w.filter)) {
|
|
154
|
+
clearTimeout(w.timer);
|
|
155
|
+
waiters.delete(w);
|
|
156
|
+
w.resolve([event]);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const msg = JSON.stringify(event) + '\n';
|
|
161
|
+
for (const c of clients) {
|
|
162
|
+
try { c.write(msg); } catch { clients.delete(c); }
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
cursor: () => seq,
|
|
166
|
+
clientCount: () => clients.size,
|
|
167
|
+
close() {
|
|
168
|
+
clearInterval(guard);
|
|
169
|
+
for (const w of waiters) { clearTimeout(w.timer); w.resolve([]); }
|
|
170
|
+
server.close();
|
|
171
|
+
httpServer.close();
|
|
172
|
+
for (const c of clients) c.destroy();
|
|
173
|
+
try { fs.unlinkSync(EVENTS_SOCK); } catch {}
|
|
174
|
+
try { fs.unlinkSync(HTTP_SOCK); } catch {}
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Client side of the long-poll (CLI `events --wait`, MCP `next_event`).
|
|
181
|
+
* Resolves { events, cursor }; rejects if the daemon is not running.
|
|
182
|
+
*/
|
|
183
|
+
export function pollEvents({ after = 'now', timeout = 90, match = 'rules', account, rule, types, limit, signal } = {}) {
|
|
184
|
+
const q = new URLSearchParams({ after: String(after), timeout: String(timeout), match });
|
|
185
|
+
if (account) q.set('account', account);
|
|
186
|
+
if (rule) q.set('rule', rule);
|
|
187
|
+
if (types && types.length) q.set('types', Array.isArray(types) ? types.join(',') : String(types));
|
|
188
|
+
if (limit) q.set('limit', String(limit));
|
|
189
|
+
return httpGet('/events?' + q.toString(), Number(timeout) + 30, signal);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function currentCursor(signal) {
|
|
193
|
+
return httpGet('/cursor', 10, signal).then((r) => r.cursor);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function httpGet(pathname, timeoutS, signal) {
|
|
197
|
+
return new Promise((resolve, reject) => {
|
|
198
|
+
const req = http.request({ socketPath: HTTP_SOCK, path: pathname, method: 'GET', timeout: timeoutS * 1000, signal }, (res) => { // (signal: whoever asked lets go)
|
|
199
|
+
let body = '';
|
|
200
|
+
res.on('data', (c) => (body += c));
|
|
201
|
+
res.on('end', () => {
|
|
202
|
+
try { resolve(JSON.parse(body)); } catch { reject(new Error('bad response from the daemon: ' + body.slice(0, 200))); }
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
req.on('timeout', () => { req.destroy(new Error('long-poll timed out without a response')); });
|
|
206
|
+
req.on('error', (e) => reject(new Error(`cannot reach the daemon at ${HTTP_SOCK} (${e.code || e.message}) — is it running? (sideora status)`)));
|
|
207
|
+
req.end();
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function line(obj) {
|
|
212
|
+
return JSON.stringify({ ts: new Date().toISOString(), ...obj }) + '\n';
|
|
213
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// The Chrome extension's id, pinned by the key in its manifest (extension/manifest.config.ts): the only extension
|
|
2
|
+
// the native host lets connect (bin/sideora.js, install-extension), and the only page the web app may be shown
|
|
3
|
+
// in a frame of (src/web-server.js).
|
|
4
|
+
export const EXTENSION_ID = 'oeknfneghipheiginkflkbfadcgjpoak';
|
|
5
|
+
export const EXTENSION_ORIGIN = `chrome-extension://${EXTENSION_ID}`;
|
package/src/file-lock.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// One change at a time to a file that several processes read and write whole (the paired browsers, the relay's
|
|
2
|
+
// devices): each reads what is there and writes it back, so two at once would undo one another (a device forgotten
|
|
3
|
+
// coming back, a code spent good again). A folder made beside the file is the lock: making it either works or does
|
|
4
|
+
// not, whoever else tries. One left by a process that died is taken after a few seconds.
|
|
5
|
+
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
|
|
9
|
+
const STALE_MS = 5000;
|
|
10
|
+
const pause = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
11
|
+
|
|
12
|
+
/** Run `change` (synchronous: read, modify, write) while holding `<file>.lock`. Returns what it returns. */
|
|
13
|
+
export function withFileLock(file, change) {
|
|
14
|
+
const lock = `${file}.lock`;
|
|
15
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
16
|
+
for (let tries = 0; ; tries++) {
|
|
17
|
+
try { fs.mkdirSync(lock); break; } catch (e) {
|
|
18
|
+
if (e.code !== 'EEXIST') throw e;
|
|
19
|
+
let age = 0;
|
|
20
|
+
try { age = Date.now() - fs.statSync(lock).mtimeMs; } catch { continue; } // gone meanwhile: try again at once
|
|
21
|
+
if (age > STALE_MS) { try { fs.rmdirSync(lock); } catch { /* another took it first */ } continue; }
|
|
22
|
+
if (tries > 400) throw new Error('that list is being changed by another Sideora: try again');
|
|
23
|
+
pause(10);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
try { return change(); } finally { try { fs.rmdirSync(lock); } catch { /* taken as stale meanwhile */ } }
|
|
27
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// The files of the mail, as this Mac found them the last times it looked: which messages of a folder carry
|
|
2
|
+
// attachments, and what each is (name, size, type). The Files page asks for them through `files` (src/ops.js),
|
|
3
|
+
// which looks at the mail servers each time (a search, then each message's structure: ten seconds and more); what
|
|
4
|
+
// it finds is kept here, so that the same page, or a search by name, is answered at once from what is kept
|
|
5
|
+
// (`quick`), while the servers are asked again.
|
|
6
|
+
//
|
|
7
|
+
// Kept in the core's encrypted database (src/core-db.js), in memory when there is none, never in a
|
|
8
|
+
// plain file: names of files, and the list rows of their mail. Nothing here is the truth: each look at the servers
|
|
9
|
+
// replaces what is kept for the stretch of time it covered (a mail deleted since is let go), and a file is always
|
|
10
|
+
// read from its mail server.
|
|
11
|
+
|
|
12
|
+
const SCHEMA = 'CREATE TABLE IF NOT EXISTS mw_files (folder TEXT NOT NULL, id TEXT NOT NULL, account TEXT NOT NULL, received INTEGER NOT NULL, data BLOB NOT NULL, PRIMARY KEY (folder, id))';
|
|
13
|
+
const INDEX = 'CREATE INDEX IF NOT EXISTS mw_files_when ON mw_files (folder, received)';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Where they are kept: the database `open()` returns (the encrypted cache), else memory.
|
|
17
|
+
* An entry: { account, owner, id, received (ms), message (its list row), files: [{ id, name, size, contentType }] }.
|
|
18
|
+
* `owner`: the address the account had when this was found. What is read is only of accounts as they are now
|
|
19
|
+
* (`owners`: account → its address now): an account removed, or set up anew under the same name for another
|
|
20
|
+
* mailbox, has nothing of the old one shown; and only what is no older than each account's cache keeps (`since`).
|
|
21
|
+
*/
|
|
22
|
+
export function filesStore({ open = () => null } = {}) {
|
|
23
|
+
const mem = new Map(); // `${folder}\n${id}` → entry
|
|
24
|
+
const ready = new WeakSet();
|
|
25
|
+
const db = () => {
|
|
26
|
+
try {
|
|
27
|
+
const d = open();
|
|
28
|
+
if (d && !ready.has(d)) { d.exec(SCHEMA); d.exec(INDEX); ready.add(d); }
|
|
29
|
+
return d || null;
|
|
30
|
+
} catch { return null; }
|
|
31
|
+
};
|
|
32
|
+
const entryOf = (r) => { try { return { account: r.account, id: r.id, received: Number(r.received), ...JSON.parse(Buffer.from(r.data).toString('utf8')) }; } catch { return null; } };
|
|
33
|
+
const all = (folder) => {
|
|
34
|
+
const d = db();
|
|
35
|
+
if (d) { try { return d.prepare('SELECT account, id, received, data FROM mw_files WHERE folder = ? ORDER BY received DESC').all(folder).map(entryOf).filter(Boolean); } catch { /* the memory's */ } }
|
|
36
|
+
return [...mem.entries()].filter(([k]) => k.startsWith(`${folder}\n`)).map(([, e]) => e).sort((a, b) => b.received - a.received);
|
|
37
|
+
};
|
|
38
|
+
/** Whether an entry is of an account as it is now, and no older than what is kept of it. */
|
|
39
|
+
const mine = (e, owners, since) => (!owners || (owners.has(e.account) && owners.get(e.account) === String(e.owner || '')))
|
|
40
|
+
&& (!since || !(e.received < (since.get(e.account) ?? -Infinity)));
|
|
41
|
+
return {
|
|
42
|
+
/** Keep these (each replaces what was kept of its message). */
|
|
43
|
+
put(folder, entries) {
|
|
44
|
+
const d = db();
|
|
45
|
+
for (const e of entries) {
|
|
46
|
+
if (!e?.id || !e.account || !Number.isFinite(e.received) || !Array.isArray(e.files) || !e.files.length) continue;
|
|
47
|
+
const data = { owner: String(e.owner || ''), message: e.message, files: e.files };
|
|
48
|
+
if (d) {
|
|
49
|
+
try {
|
|
50
|
+
d.prepare('INSERT INTO mw_files (folder, id, account, received, data) VALUES (?, ?, ?, ?, ?) ON CONFLICT(folder, id) DO UPDATE SET account = excluded.account, received = excluded.received, data = excluded.data')
|
|
51
|
+
.run(folder, e.id, e.account, Math.round(e.received), Buffer.from(JSON.stringify(data), 'utf8'));
|
|
52
|
+
continue;
|
|
53
|
+
} catch { /* kept in memory */ }
|
|
54
|
+
}
|
|
55
|
+
mem.set(`${folder}\n${e.id}`, { account: e.account, id: e.id, received: Math.round(e.received), ...data });
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
/** Let go of these messages' files. */
|
|
59
|
+
remove(folder, ids) {
|
|
60
|
+
const d = db();
|
|
61
|
+
for (const id of ids) {
|
|
62
|
+
mem.delete(`${folder}\n${id}`);
|
|
63
|
+
if (d) { try { d.prepare('DELETE FROM mw_files WHERE folder = ? AND id = ?').run(folder, id); } catch { /* next time */ } }
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
/** The messages kept of a folder, newest first: of the accounts in `owners` (as they are now), no older than `since` says for each, received at `until` or before, matching every word of `q` (file names, subject, who). At most `limit`. */
|
|
67
|
+
page(folder, { owners, since, until, q, limit = 30 } = {}) {
|
|
68
|
+
// (every word; a "quoted phrase" is one, as the servers take it)
|
|
69
|
+
const words = [...String(q || '').toLowerCase().matchAll(/"([^"]+)"|(\S+)/g)].map((m) => (m[1] ?? m[2]).trim()).filter(Boolean);
|
|
70
|
+
const out = [];
|
|
71
|
+
for (const e of all(folder)) {
|
|
72
|
+
if (!mine(e, owners, since)) continue;
|
|
73
|
+
if (until !== undefined && e.received > until) continue;
|
|
74
|
+
if (words.length) {
|
|
75
|
+
const m = e.message || {};
|
|
76
|
+
const hay = `${e.files.map((f) => f.name).join(' ')} ${m.subject || ''} ${m.fromName || ''} ${m.fromEmail || ''} ${m.displayTo || ''}`.toLowerCase();
|
|
77
|
+
if (!words.every((w) => hay.includes(w))) continue;
|
|
78
|
+
}
|
|
79
|
+
out.push(e);
|
|
80
|
+
if (out.length >= limit) break;
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
},
|
|
84
|
+
/** What is kept of a folder for these accounts: [{ id, account, received }]. */
|
|
85
|
+
rows(folder, { owners } = {}) {
|
|
86
|
+
return all(folder).filter((e) => mine(e, owners)).map((e) => ({ id: e.id, account: e.account, received: e.received }));
|
|
87
|
+
},
|
|
88
|
+
/** Let go of what is not of an account as it is now (removed, another mailbox under its name, not cached any more), and of what is older than its cache keeps. */
|
|
89
|
+
prune({ owners, since } = {}) {
|
|
90
|
+
for (const folder of ['inbox', 'sent']) this.remove(folder, all(folder).filter((e) => !mine(e, owners, since)).map((e) => e.id));
|
|
91
|
+
},
|
|
92
|
+
/** An account's, all let go (it was removed, or is another now). */
|
|
93
|
+
drop(account) {
|
|
94
|
+
for (const [k, e] of mem) if (e.account === account) mem.delete(k);
|
|
95
|
+
const d = db();
|
|
96
|
+
if (d) { try { d.prepare('DELETE FROM mw_files WHERE account = ?').run(account); } catch { /* next time */ } }
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* What a look at the servers says of what is kept. The look asked each account for its newest messages with
|
|
103
|
+
* attachments received at `until` or before, and got `messages` (all accounts together, the newest of them) from
|
|
104
|
+
* the accounts in `answered` (one that failed is not in it: nothing of its is touched). Each account was read from
|
|
105
|
+
* its newest down, without a gap: so, for each, what lies strictly between its oldest message found and `until`
|
|
106
|
+
* was looked at, and what is kept of that stretch and was not found is gone. Nothing is said of what is older
|
|
107
|
+
* (a look reads only so far), of an account with nothing found, or of the very moments at either end (several
|
|
108
|
+
* messages of one second may have been cut apart by the page's size).
|
|
109
|
+
* `kept`: [{ id, account, received }] (the store's rows). → the ids to let go of.
|
|
110
|
+
*/
|
|
111
|
+
export function gone(kept, { messages, until, answered }) {
|
|
112
|
+
const found = new Set(messages.map((m) => m.id));
|
|
113
|
+
const oldest = new Map(); // account → its oldest message found (ms)
|
|
114
|
+
for (const m of messages) {
|
|
115
|
+
const at = Date.parse(m.received);
|
|
116
|
+
if (!answered.includes(m.account) || !Number.isFinite(at)) continue;
|
|
117
|
+
oldest.set(m.account, Math.min(oldest.get(m.account) ?? Infinity, at));
|
|
118
|
+
}
|
|
119
|
+
const to = until === undefined ? Infinity : until;
|
|
120
|
+
return kept.filter((e) => oldest.has(e.account) && e.received > oldest.get(e.account) && e.received < to && !found.has(e.id)).map((e) => e.id);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Whose files are kept, and how far back: the accounts whose mail is cached (config.json: cache), each under the
|
|
125
|
+
* address it has now, and no older than its cache keeps. An account that is not cached has nothing kept.
|
|
126
|
+
* → { owners: Map<account, address>, since: Map<account, ms> }
|
|
127
|
+
*/
|
|
128
|
+
export async function keptAccounts() {
|
|
129
|
+
const [{ loadConfig, mailAccountNames }, core] = await Promise.all([import('./config.js'), import('./core.js')]);
|
|
130
|
+
const cfg = loadConfig();
|
|
131
|
+
let cached = [];
|
|
132
|
+
try { cached = core.cache.cachedAccounts(); } catch { /* none kept */ }
|
|
133
|
+
const names = mailAccountNames(cfg).filter((n) => cached.includes(n));
|
|
134
|
+
return {
|
|
135
|
+
// (the mailbox an account is of: its address, else who signs in where; never nothing, which every account without an address would share)
|
|
136
|
+
owners: new Map(names.map((n) => { const a = cfg.accounts?.[n] || {}; return [n, String(a.email || `${a.username || a.user || n}@${a.imap?.host || a.server || a.host || a.provider || ''}`).toLowerCase()]; })),
|
|
137
|
+
since: new Map(names.map((n) => [n, Date.now() - Number(cfg.accounts?.[n]?.cache?.days || 30) * 86400e3]))
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** After an account was removed or set up otherwise: what is kept and is not of an account as it is now is let go, at once. Never throws. */
|
|
142
|
+
export async function filesChanged() {
|
|
143
|
+
try { (await filesIndex()).prune(await keptAccounts()); } catch { /* at the next look */ }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let instance = null;
|
|
147
|
+
/** The one of this process, over the core's encrypted database (src/core-db.js). */
|
|
148
|
+
export async function filesIndex() {
|
|
149
|
+
if (!instance) {
|
|
150
|
+
const db = await (await import('./core-db.js')).theCoreDb();
|
|
151
|
+
instance = filesStore({ open: () => db.open() });
|
|
152
|
+
}
|
|
153
|
+
return instance;
|
|
154
|
+
}
|