@digitalpresence/cliclaw 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.
- package/dist/__tests__/calendar.integration.test.d.ts +2 -0
- package/dist/__tests__/calendar.integration.test.d.ts.map +1 -0
- package/dist/__tests__/calendar.integration.test.js +98 -0
- package/dist/__tests__/calendar.integration.test.js.map +1 -0
- package/dist/__tests__/forms.integration.test.d.ts +2 -0
- package/dist/__tests__/forms.integration.test.d.ts.map +1 -0
- package/dist/__tests__/forms.integration.test.js +243 -0
- package/dist/__tests__/forms.integration.test.js.map +1 -0
- package/dist/__tests__/gdrive.integration.test.d.ts +2 -0
- package/dist/__tests__/gdrive.integration.test.d.ts.map +1 -0
- package/dist/__tests__/gdrive.integration.test.js +186 -0
- package/dist/__tests__/gdrive.integration.test.js.map +1 -0
- package/dist/__tests__/gmail.integration.test.d.ts +2 -0
- package/dist/__tests__/gmail.integration.test.d.ts.map +1 -0
- package/dist/__tests__/gmail.integration.test.js +197 -0
- package/dist/__tests__/gmail.integration.test.js.map +1 -0
- package/dist/__tests__/gslides.integration.test.d.ts +2 -0
- package/dist/__tests__/gslides.integration.test.d.ts.map +1 -0
- package/dist/__tests__/gslides.integration.test.js +124 -0
- package/dist/__tests__/gslides.integration.test.js.map +1 -0
- package/dist/__tests__/sheets.integration.test.d.ts +2 -0
- package/dist/__tests__/sheets.integration.test.d.ts.map +1 -0
- package/dist/__tests__/sheets.integration.test.js +150 -0
- package/dist/__tests__/sheets.integration.test.js.map +1 -0
- package/dist/agent/crud.d.ts +6 -0
- package/dist/agent/crud.d.ts.map +1 -0
- package/dist/agent/crud.js +41 -0
- package/dist/agent/crud.js.map +1 -0
- package/dist/agent/memory.d.ts +14 -0
- package/dist/agent/memory.d.ts.map +1 -0
- package/dist/agent/memory.js +66 -0
- package/dist/agent/memory.js.map +1 -0
- package/dist/agent/permissions.d.ts +4 -0
- package/dist/agent/permissions.d.ts.map +1 -0
- package/dist/agent/permissions.js +32 -0
- package/dist/agent/permissions.js.map +1 -0
- package/dist/calendar/accounts.d.ts +3 -0
- package/dist/calendar/accounts.d.ts.map +1 -0
- package/dist/calendar/accounts.js +21 -0
- package/dist/calendar/accounts.js.map +1 -0
- package/dist/calendar/auth.d.ts +3 -0
- package/dist/calendar/auth.d.ts.map +1 -0
- package/dist/calendar/auth.js +41 -0
- package/dist/calendar/auth.js.map +1 -0
- package/dist/calendar/calendars.d.ts +3 -0
- package/dist/calendar/calendars.d.ts.map +1 -0
- package/dist/calendar/calendars.js +28 -0
- package/dist/calendar/calendars.js.map +1 -0
- package/dist/calendar/create.d.ts +3 -0
- package/dist/calendar/create.d.ts.map +1 -0
- package/dist/calendar/create.js +31 -0
- package/dist/calendar/create.js.map +1 -0
- package/dist/calendar/delete.d.ts +3 -0
- package/dist/calendar/delete.d.ts.map +1 -0
- package/dist/calendar/delete.js +21 -0
- package/dist/calendar/delete.js.map +1 -0
- package/dist/calendar/events.d.ts +3 -0
- package/dist/calendar/events.d.ts.map +1 -0
- package/dist/calendar/events.js +37 -0
- package/dist/calendar/events.js.map +1 -0
- package/dist/calendar/get.d.ts +3 -0
- package/dist/calendar/get.d.ts.map +1 -0
- package/dist/calendar/get.js +21 -0
- package/dist/calendar/get.js.map +1 -0
- package/dist/calendar/update.d.ts +3 -0
- package/dist/calendar/update.d.ts.map +1 -0
- package/dist/calendar/update.js +31 -0
- package/dist/calendar/update.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +37 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/agent.d.ts +6 -0
- package/dist/commands/agent.d.ts.map +1 -0
- package/dist/commands/agent.js +107 -0
- package/dist/commands/agent.js.map +1 -0
- package/dist/commands/calendar.d.ts +9 -0
- package/dist/commands/calendar.d.ts.map +1 -0
- package/dist/commands/calendar.js +104 -0
- package/dist/commands/calendar.js.map +1 -0
- package/dist/commands/cron.d.ts +6 -0
- package/dist/commands/cron.d.ts.map +1 -0
- package/dist/commands/cron.js +103 -0
- package/dist/commands/cron.js.map +1 -0
- package/dist/commands/forms.d.ts +9 -0
- package/dist/commands/forms.d.ts.map +1 -0
- package/dist/commands/forms.js +139 -0
- package/dist/commands/forms.js.map +1 -0
- package/dist/commands/gdrive.d.ts +9 -0
- package/dist/commands/gdrive.d.ts.map +1 -0
- package/dist/commands/gdrive.js +198 -0
- package/dist/commands/gdrive.js.map +1 -0
- package/dist/commands/gmail.d.ts +9 -0
- package/dist/commands/gmail.d.ts.map +1 -0
- package/dist/commands/gmail.js +231 -0
- package/dist/commands/gmail.js.map +1 -0
- package/dist/commands/gslides.d.ts +9 -0
- package/dist/commands/gslides.d.ts.map +1 -0
- package/dist/commands/gslides.js +167 -0
- package/dist/commands/gslides.js.map +1 -0
- package/dist/commands/sheets.d.ts +9 -0
- package/dist/commands/sheets.d.ts.map +1 -0
- package/dist/commands/sheets.js +174 -0
- package/dist/commands/sheets.js.map +1 -0
- package/dist/cron/daemon.d.ts +3 -0
- package/dist/cron/daemon.d.ts.map +1 -0
- package/dist/cron/daemon.js +127 -0
- package/dist/cron/daemon.js.map +1 -0
- package/dist/cron/handlers.d.ts +6 -0
- package/dist/cron/handlers.d.ts.map +1 -0
- package/dist/cron/handlers.js +58 -0
- package/dist/cron/handlers.js.map +1 -0
- package/dist/cron/logger.d.ts +4 -0
- package/dist/cron/logger.d.ts.map +1 -0
- package/dist/cron/logger.js +11 -0
- package/dist/cron/logger.js.map +1 -0
- package/dist/cron/progress.d.ts +25 -0
- package/dist/cron/progress.d.ts.map +1 -0
- package/dist/cron/progress.js +71 -0
- package/dist/cron/progress.js.map +1 -0
- package/dist/cron/ralph-wiggum.d.ts +18 -0
- package/dist/cron/ralph-wiggum.d.ts.map +1 -0
- package/dist/cron/ralph-wiggum.js +143 -0
- package/dist/cron/ralph-wiggum.js.map +1 -0
- package/dist/forms/accounts.d.ts +3 -0
- package/dist/forms/accounts.d.ts.map +1 -0
- package/dist/forms/accounts.js +20 -0
- package/dist/forms/accounts.js.map +1 -0
- package/dist/forms/auth.d.ts +3 -0
- package/dist/forms/auth.d.ts.map +1 -0
- package/dist/forms/auth.js +41 -0
- package/dist/forms/auth.js.map +1 -0
- package/dist/forms/create.d.ts +3 -0
- package/dist/forms/create.d.ts.map +1 -0
- package/dist/forms/create.js +28 -0
- package/dist/forms/create.js.map +1 -0
- package/dist/forms/get.d.ts +3 -0
- package/dist/forms/get.d.ts.map +1 -0
- package/dist/forms/get.js +21 -0
- package/dist/forms/get.js.map +1 -0
- package/dist/forms/list.d.ts +3 -0
- package/dist/forms/list.d.ts.map +1 -0
- package/dist/forms/list.js +33 -0
- package/dist/forms/list.js.map +1 -0
- package/dist/forms/questions.d.ts +6 -0
- package/dist/forms/questions.d.ts.map +1 -0
- package/dist/forms/questions.js +179 -0
- package/dist/forms/questions.js.map +1 -0
- package/dist/forms/responses.d.ts +4 -0
- package/dist/forms/responses.d.ts.map +1 -0
- package/dist/forms/responses.js +40 -0
- package/dist/forms/responses.js.map +1 -0
- package/dist/forms/update.d.ts +3 -0
- package/dist/forms/update.d.ts.map +1 -0
- package/dist/forms/update.js +44 -0
- package/dist/forms/update.js.map +1 -0
- package/dist/gdrive/about.d.ts +3 -0
- package/dist/gdrive/about.d.ts.map +1 -0
- package/dist/gdrive/about.js +31 -0
- package/dist/gdrive/about.js.map +1 -0
- package/dist/gdrive/accounts.d.ts +3 -0
- package/dist/gdrive/accounts.d.ts.map +1 -0
- package/dist/gdrive/accounts.js +20 -0
- package/dist/gdrive/accounts.js.map +1 -0
- package/dist/gdrive/auth.d.ts +3 -0
- package/dist/gdrive/auth.d.ts.map +1 -0
- package/dist/gdrive/auth.js +51 -0
- package/dist/gdrive/auth.js.map +1 -0
- package/dist/gdrive/files.d.ts +12 -0
- package/dist/gdrive/files.d.ts.map +1 -0
- package/dist/gdrive/files.js +174 -0
- package/dist/gdrive/files.js.map +1 -0
- package/dist/gdrive/folders.d.ts +4 -0
- package/dist/gdrive/folders.d.ts.map +1 -0
- package/dist/gdrive/folders.js +46 -0
- package/dist/gdrive/folders.js.map +1 -0
- package/dist/gdrive/search.d.ts +3 -0
- package/dist/gdrive/search.d.ts.map +1 -0
- package/dist/gdrive/search.js +23 -0
- package/dist/gdrive/search.js.map +1 -0
- package/dist/gdrive/sharing.d.ts +5 -0
- package/dist/gdrive/sharing.d.ts.map +1 -0
- package/dist/gdrive/sharing.js +54 -0
- package/dist/gdrive/sharing.js.map +1 -0
- package/dist/gmail/accounts.d.ts +3 -0
- package/dist/gmail/accounts.d.ts.map +1 -0
- package/dist/gmail/accounts.js +18 -0
- package/dist/gmail/accounts.js.map +1 -0
- package/dist/gmail/auth.d.ts +3 -0
- package/dist/gmail/auth.d.ts.map +1 -0
- package/dist/gmail/auth.js +50 -0
- package/dist/gmail/auth.js.map +1 -0
- package/dist/gmail/drafts.d.ts +7 -0
- package/dist/gmail/drafts.d.ts.map +1 -0
- package/dist/gmail/drafts.js +103 -0
- package/dist/gmail/drafts.js.map +1 -0
- package/dist/gmail/get.d.ts +4 -0
- package/dist/gmail/get.d.ts.map +1 -0
- package/dist/gmail/get.js +140 -0
- package/dist/gmail/get.js.map +1 -0
- package/dist/gmail/inbox.d.ts +4 -0
- package/dist/gmail/inbox.d.ts.map +1 -0
- package/dist/gmail/inbox.js +45 -0
- package/dist/gmail/inbox.js.map +1 -0
- package/dist/gmail/labels.d.ts +5 -0
- package/dist/gmail/labels.d.ts.map +1 -0
- package/dist/gmail/labels.js +45 -0
- package/dist/gmail/labels.js.map +1 -0
- package/dist/gmail/modify.d.ts +5 -0
- package/dist/gmail/modify.d.ts.map +1 -0
- package/dist/gmail/modify.js +68 -0
- package/dist/gmail/modify.js.map +1 -0
- package/dist/gmail/send.d.ts +5 -0
- package/dist/gmail/send.d.ts.map +1 -0
- package/dist/gmail/send.js +310 -0
- package/dist/gmail/send.js.map +1 -0
- package/dist/gmail/threads.d.ts +4 -0
- package/dist/gmail/threads.d.ts.map +1 -0
- package/dist/gmail/threads.js +47 -0
- package/dist/gmail/threads.js.map +1 -0
- package/dist/gslides/accounts.d.ts +3 -0
- package/dist/gslides/accounts.d.ts.map +1 -0
- package/dist/gslides/accounts.js +20 -0
- package/dist/gslides/accounts.js.map +1 -0
- package/dist/gslides/auth.d.ts +3 -0
- package/dist/gslides/auth.d.ts.map +1 -0
- package/dist/gslides/auth.js +50 -0
- package/dist/gslides/auth.js.map +1 -0
- package/dist/gslides/presentations.d.ts +29 -0
- package/dist/gslides/presentations.d.ts.map +1 -0
- package/dist/gslides/presentations.js +320 -0
- package/dist/gslides/presentations.js.map +1 -0
- package/dist/lib/config.d.ts +6 -0
- package/dist/lib/config.d.ts.map +1 -0
- package/dist/lib/config.js +12 -0
- package/dist/lib/config.js.map +1 -0
- package/dist/lib/media-utils.d.ts +5 -0
- package/dist/lib/media-utils.d.ts.map +1 -0
- package/dist/lib/media-utils.js +79 -0
- package/dist/lib/media-utils.js.map +1 -0
- package/dist/lib/output.d.ts +4 -0
- package/dist/lib/output.d.ts.map +1 -0
- package/dist/lib/output.js +12 -0
- package/dist/lib/output.js.map +1 -0
- package/dist/sheets/accounts.d.ts +3 -0
- package/dist/sheets/accounts.d.ts.map +1 -0
- package/dist/sheets/accounts.js +20 -0
- package/dist/sheets/accounts.js.map +1 -0
- package/dist/sheets/auth.d.ts +3 -0
- package/dist/sheets/auth.d.ts.map +1 -0
- package/dist/sheets/auth.js +56 -0
- package/dist/sheets/auth.js.map +1 -0
- package/dist/sheets/cells.d.ts +6 -0
- package/dist/sheets/cells.d.ts.map +1 -0
- package/dist/sheets/cells.js +89 -0
- package/dist/sheets/cells.js.map +1 -0
- package/dist/sheets/format.d.ts +8 -0
- package/dist/sheets/format.d.ts.map +1 -0
- package/dist/sheets/format.js +97 -0
- package/dist/sheets/format.js.map +1 -0
- package/dist/sheets/sheets-tab.d.ts +6 -0
- package/dist/sheets/sheets-tab.d.ts.map +1 -0
- package/dist/sheets/sheets-tab.js +88 -0
- package/dist/sheets/sheets-tab.js.map +1 -0
- package/dist/sheets/spreadsheets.d.ts +6 -0
- package/dist/sheets/spreadsheets.d.ts.map +1 -0
- package/dist/sheets/spreadsheets.js +88 -0
- package/dist/sheets/spreadsheets.js.map +1 -0
- package/package.json +33 -0
- package/src/__tests__/calendar.integration.test.ts +152 -0
- package/src/__tests__/forms.integration.test.ts +403 -0
- package/src/__tests__/gdrive.integration.test.ts +253 -0
- package/src/__tests__/gmail.integration.test.ts +294 -0
- package/src/__tests__/gslides.integration.test.ts +195 -0
- package/src/__tests__/sheets.integration.test.ts +234 -0
- package/src/agent/crud.ts +54 -0
- package/src/agent/memory.ts +95 -0
- package/src/agent/permissions.ts +54 -0
- package/src/calendar/accounts.ts +25 -0
- package/src/calendar/auth.ts +45 -0
- package/src/calendar/calendars.ts +32 -0
- package/src/calendar/create.ts +44 -0
- package/src/calendar/delete.ts +27 -0
- package/src/calendar/events.ts +45 -0
- package/src/calendar/get.ts +27 -0
- package/src/calendar/update.ts +44 -0
- package/src/cli.ts +41 -0
- package/src/commands/agent.ts +128 -0
- package/src/commands/calendar.ts +127 -0
- package/src/commands/cron.ts +126 -0
- package/src/commands/forms.ts +158 -0
- package/src/commands/gdrive.ts +225 -0
- package/src/commands/gmail.ts +290 -0
- package/src/commands/gslides.ts +188 -0
- package/src/commands/sheets.ts +193 -0
- package/src/cron/daemon.ts +143 -0
- package/src/cron/handlers.ts +78 -0
- package/src/cron/logger.ts +20 -0
- package/src/cron/progress.ts +90 -0
- package/src/cron/ralph-wiggum.ts +172 -0
- package/src/forms/accounts.ts +24 -0
- package/src/forms/auth.ts +45 -0
- package/src/forms/create.ts +34 -0
- package/src/forms/get.ts +26 -0
- package/src/forms/list.ts +38 -0
- package/src/forms/questions.ts +209 -0
- package/src/forms/responses.ts +50 -0
- package/src/forms/update.ts +57 -0
- package/src/gdrive/about.ts +36 -0
- package/src/gdrive/accounts.ts +24 -0
- package/src/gdrive/auth.ts +55 -0
- package/src/gdrive/files.ts +237 -0
- package/src/gdrive/folders.ts +58 -0
- package/src/gdrive/search.ts +30 -0
- package/src/gdrive/sharing.ts +72 -0
- package/src/gmail/accounts.ts +22 -0
- package/src/gmail/auth.ts +53 -0
- package/src/gmail/drafts.ts +166 -0
- package/src/gmail/get.ts +195 -0
- package/src/gmail/inbox.ts +78 -0
- package/src/gmail/labels.ts +69 -0
- package/src/gmail/modify.ts +89 -0
- package/src/gmail/send.ts +424 -0
- package/src/gmail/threads.ts +68 -0
- package/src/gslides/accounts.ts +24 -0
- package/src/gslides/auth.ts +54 -0
- package/src/gslides/presentations.ts +384 -0
- package/src/lib/config.ts +14 -0
- package/src/lib/media-utils.ts +82 -0
- package/src/lib/output.ts +13 -0
- package/src/sheets/accounts.ts +24 -0
- package/src/sheets/auth.ts +60 -0
- package/src/sheets/cells.ts +112 -0
- package/src/sheets/format.ts +114 -0
- package/src/sheets/sheets-tab.ts +109 -0
- package/src/sheets/spreadsheets.ts +106 -0
- package/tsconfig.json +8 -0
- package/vitest.config.ts +7 -0
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import { readFileSync } from "fs";
|
|
2
|
+
import { basename, extname } from "path";
|
|
3
|
+
import { google } from "googleapis";
|
|
4
|
+
import type { OAuthClientManager } from "@digitalpresence/cliclaw-auth";
|
|
5
|
+
import { extToMime } from "../lib/media-utils.js";
|
|
6
|
+
import { outputJson, outputAuthRequired } from "../lib/output.js";
|
|
7
|
+
|
|
8
|
+
type OAuth2Client = InstanceType<typeof google.auth.OAuth2>;
|
|
9
|
+
|
|
10
|
+
interface AttachmentInput {
|
|
11
|
+
file_path: string;
|
|
12
|
+
filename?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface InMemoryAttachment {
|
|
16
|
+
filename: string;
|
|
17
|
+
mimeType: string;
|
|
18
|
+
data: Buffer;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function encodeMessage(headers: Record<string, string>, body: string): string {
|
|
22
|
+
const headerLines = Object.entries(headers)
|
|
23
|
+
.map(([k, v]) => `${k}: ${v}`)
|
|
24
|
+
.join("\r\n");
|
|
25
|
+
const raw = `${headerLines}\r\n\r\n${body}`;
|
|
26
|
+
return Buffer.from(raw).toString("base64url");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function buildAlternativePart(boundary: string, body: string, htmlBody: string): string {
|
|
30
|
+
const altBoundary = `----=_Alt_${Date.now()}`;
|
|
31
|
+
const altParts = [
|
|
32
|
+
[
|
|
33
|
+
`--${altBoundary}`,
|
|
34
|
+
"Content-Type: text/plain; charset=utf-8",
|
|
35
|
+
"Content-Transfer-Encoding: quoted-printable",
|
|
36
|
+
"",
|
|
37
|
+
body,
|
|
38
|
+
].join("\r\n"),
|
|
39
|
+
[
|
|
40
|
+
`--${altBoundary}`,
|
|
41
|
+
"Content-Type: text/html; charset=utf-8",
|
|
42
|
+
"Content-Transfer-Encoding: quoted-printable",
|
|
43
|
+
"",
|
|
44
|
+
htmlBody,
|
|
45
|
+
].join("\r\n"),
|
|
46
|
+
];
|
|
47
|
+
return [
|
|
48
|
+
`--${boundary}`,
|
|
49
|
+
`Content-Type: multipart/alternative; boundary="${altBoundary}"`,
|
|
50
|
+
"",
|
|
51
|
+
altParts.join("\r\n") + `\r\n--${altBoundary}--`,
|
|
52
|
+
].join("\r\n");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function encodeMultipartMessage(
|
|
56
|
+
headers: Record<string, string>,
|
|
57
|
+
body: string,
|
|
58
|
+
attachments: AttachmentInput[] | undefined,
|
|
59
|
+
htmlBody?: string,
|
|
60
|
+
inMemoryAttachments?: InMemoryAttachment[],
|
|
61
|
+
): string {
|
|
62
|
+
const hasAttachments =
|
|
63
|
+
(attachments && attachments.length > 0) ||
|
|
64
|
+
(inMemoryAttachments && inMemoryAttachments.length > 0);
|
|
65
|
+
|
|
66
|
+
if (!hasAttachments && !htmlBody) {
|
|
67
|
+
return encodeMessage(headers, body);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (!hasAttachments && htmlBody) {
|
|
71
|
+
const altBoundary = `----=_Alt_${Date.now()}`;
|
|
72
|
+
const multipartHeaders: Record<string, string> = {
|
|
73
|
+
...headers,
|
|
74
|
+
"Content-Type": `multipart/alternative; boundary="${altBoundary}"`,
|
|
75
|
+
};
|
|
76
|
+
const headerLines = Object.entries(multipartHeaders)
|
|
77
|
+
.map(([k, v]) => `${k}: ${v}`)
|
|
78
|
+
.join("\r\n");
|
|
79
|
+
const parts = [
|
|
80
|
+
[
|
|
81
|
+
`--${altBoundary}`,
|
|
82
|
+
"Content-Type: text/plain; charset=utf-8",
|
|
83
|
+
"Content-Transfer-Encoding: quoted-printable",
|
|
84
|
+
"",
|
|
85
|
+
body,
|
|
86
|
+
].join("\r\n"),
|
|
87
|
+
[
|
|
88
|
+
`--${altBoundary}`,
|
|
89
|
+
"Content-Type: text/html; charset=utf-8",
|
|
90
|
+
"Content-Transfer-Encoding: quoted-printable",
|
|
91
|
+
"",
|
|
92
|
+
htmlBody,
|
|
93
|
+
].join("\r\n"),
|
|
94
|
+
];
|
|
95
|
+
const rawBody = parts.join("\r\n") + `\r\n--${altBoundary}--`;
|
|
96
|
+
const raw = `${headerLines}\r\n\r\n${rawBody}`;
|
|
97
|
+
return Buffer.from(raw).toString("base64url");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const boundary = `----=_Part_${Date.now()}`;
|
|
101
|
+
const multipartHeaders: Record<string, string> = {
|
|
102
|
+
...headers,
|
|
103
|
+
"Content-Type": `multipart/mixed; boundary="${boundary}"`,
|
|
104
|
+
};
|
|
105
|
+
const headerLines = Object.entries(multipartHeaders)
|
|
106
|
+
.map(([k, v]) => `${k}: ${v}`)
|
|
107
|
+
.join("\r\n");
|
|
108
|
+
|
|
109
|
+
const parts: string[] = [];
|
|
110
|
+
|
|
111
|
+
if (htmlBody) {
|
|
112
|
+
parts.push(buildAlternativePart(boundary, body, htmlBody));
|
|
113
|
+
} else {
|
|
114
|
+
parts.push(
|
|
115
|
+
[
|
|
116
|
+
`--${boundary}`,
|
|
117
|
+
"Content-Type: text/plain; charset=utf-8",
|
|
118
|
+
"Content-Transfer-Encoding: quoted-printable",
|
|
119
|
+
"",
|
|
120
|
+
body,
|
|
121
|
+
].join("\r\n"),
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
for (const att of attachments ?? []) {
|
|
126
|
+
const fileBuffer = readFileSync(att.file_path);
|
|
127
|
+
const name = att.filename ?? basename(att.file_path);
|
|
128
|
+
const ext = extname(name);
|
|
129
|
+
const mimeType = extToMime(ext) || "application/octet-stream";
|
|
130
|
+
const encoded = fileBuffer.toString("base64");
|
|
131
|
+
|
|
132
|
+
parts.push(
|
|
133
|
+
[
|
|
134
|
+
`--${boundary}`,
|
|
135
|
+
`Content-Type: ${mimeType}; name="${name}"`,
|
|
136
|
+
"Content-Transfer-Encoding: base64",
|
|
137
|
+
`Content-Disposition: attachment; filename="${name}"`,
|
|
138
|
+
"",
|
|
139
|
+
encoded,
|
|
140
|
+
].join("\r\n"),
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
for (const att of inMemoryAttachments ?? []) {
|
|
145
|
+
const encoded = att.data.toString("base64");
|
|
146
|
+
parts.push(
|
|
147
|
+
[
|
|
148
|
+
`--${boundary}`,
|
|
149
|
+
`Content-Type: ${att.mimeType}; name="${att.filename}"`,
|
|
150
|
+
"Content-Transfer-Encoding: base64",
|
|
151
|
+
`Content-Disposition: attachment; filename="${att.filename}"`,
|
|
152
|
+
"",
|
|
153
|
+
encoded,
|
|
154
|
+
].join("\r\n"),
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const rawBody = parts.join("\r\n") + `\r\n--${boundary}--`;
|
|
159
|
+
const raw = `${headerLines}\r\n\r\n${rawBody}`;
|
|
160
|
+
return Buffer.from(raw).toString("base64url");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function getSenderEmail(client: OAuth2Client): Promise<string> {
|
|
164
|
+
const gmail = google.gmail({ version: "v1", auth: client });
|
|
165
|
+
const res = await gmail.users.getProfile({ userId: "me" });
|
|
166
|
+
return res.data.emailAddress ?? "me";
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
interface MessagePart {
|
|
170
|
+
mimeType?: string | null;
|
|
171
|
+
filename?: string | null;
|
|
172
|
+
body?: { data?: string | null; attachmentId?: string | null } | null;
|
|
173
|
+
parts?: MessagePart[] | null;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
interface AttachmentMeta {
|
|
177
|
+
filename: string;
|
|
178
|
+
mimeType: string;
|
|
179
|
+
attachmentId: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function extractPlainText(parts: MessagePart[] | null | undefined): string {
|
|
183
|
+
let text = "";
|
|
184
|
+
function traverse(part: MessagePart) {
|
|
185
|
+
if (part.mimeType === "text/plain" && part.body?.data) {
|
|
186
|
+
text += Buffer.from(part.body.data, "base64").toString("utf-8");
|
|
187
|
+
}
|
|
188
|
+
if (part.parts) {
|
|
189
|
+
for (const child of part.parts) traverse(child);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (parts) {
|
|
193
|
+
for (const part of parts) traverse(part);
|
|
194
|
+
}
|
|
195
|
+
return text;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function extractAttachmentMeta(parts: MessagePart[] | null | undefined): AttachmentMeta[] {
|
|
199
|
+
const results: AttachmentMeta[] = [];
|
|
200
|
+
function traverse(part: MessagePart) {
|
|
201
|
+
const attachmentId = part.body?.attachmentId ?? null;
|
|
202
|
+
const filename = part.filename ?? null;
|
|
203
|
+
if (attachmentId && filename) {
|
|
204
|
+
results.push({
|
|
205
|
+
filename,
|
|
206
|
+
mimeType: part.mimeType ?? "application/octet-stream",
|
|
207
|
+
attachmentId,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
if (part.parts) {
|
|
211
|
+
for (const child of part.parts) traverse(child);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (parts) {
|
|
215
|
+
for (const part of parts) traverse(part);
|
|
216
|
+
}
|
|
217
|
+
return results;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export async function handleSend(
|
|
221
|
+
clientManager: OAuthClientManager,
|
|
222
|
+
account: string,
|
|
223
|
+
to: string,
|
|
224
|
+
subject: string,
|
|
225
|
+
body: string,
|
|
226
|
+
cc?: string,
|
|
227
|
+
bcc?: string,
|
|
228
|
+
htmlBody?: string,
|
|
229
|
+
attachmentPaths?: string[],
|
|
230
|
+
): Promise<void> {
|
|
231
|
+
if (!clientManager.listAccounts().includes(account)) {
|
|
232
|
+
outputAuthRequired();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const client = clientManager.getClient(account);
|
|
236
|
+
const gmail = google.gmail({ version: "v1", auth: client });
|
|
237
|
+
const from = await getSenderEmail(client);
|
|
238
|
+
|
|
239
|
+
const messageHeaders: Record<string, string> = {
|
|
240
|
+
From: from,
|
|
241
|
+
To: to,
|
|
242
|
+
Subject: subject,
|
|
243
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
244
|
+
"MIME-Version": "1.0",
|
|
245
|
+
};
|
|
246
|
+
if (cc) messageHeaders["Cc"] = cc;
|
|
247
|
+
if (bcc) messageHeaders["Bcc"] = bcc;
|
|
248
|
+
|
|
249
|
+
const attachments: AttachmentInput[] | undefined = attachmentPaths?.map((p) => ({ file_path: p }));
|
|
250
|
+
|
|
251
|
+
const raw = encodeMultipartMessage(messageHeaders, body, attachments, htmlBody);
|
|
252
|
+
|
|
253
|
+
const res = await gmail.users.messages.send({ userId: "me", requestBody: { raw } });
|
|
254
|
+
|
|
255
|
+
outputJson({ id: res.data.id, threadId: res.data.threadId, success: true });
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export async function handleReply(
|
|
259
|
+
clientManager: OAuthClientManager,
|
|
260
|
+
account: string,
|
|
261
|
+
id: string,
|
|
262
|
+
body: string,
|
|
263
|
+
replyAll?: boolean,
|
|
264
|
+
htmlBody?: string,
|
|
265
|
+
attachmentPaths?: string[],
|
|
266
|
+
): Promise<void> {
|
|
267
|
+
if (!clientManager.listAccounts().includes(account)) {
|
|
268
|
+
outputAuthRequired();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const client = clientManager.getClient(account);
|
|
272
|
+
const gmail = google.gmail({ version: "v1", auth: client });
|
|
273
|
+
|
|
274
|
+
const orig = await gmail.users.messages.get({
|
|
275
|
+
userId: "me",
|
|
276
|
+
id,
|
|
277
|
+
format: "metadata",
|
|
278
|
+
metadataHeaders: ["Subject", "From", "To", "Cc", "Message-ID", "References"],
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
const headers = orig.data.payload?.headers ?? [];
|
|
282
|
+
const get = (name: string) =>
|
|
283
|
+
headers.find((h) => h.name?.toLowerCase() === name.toLowerCase())?.value ?? "";
|
|
284
|
+
|
|
285
|
+
const origSubject = get("Subject");
|
|
286
|
+
const origFrom = get("From");
|
|
287
|
+
const origTo = get("To");
|
|
288
|
+
const origCc = get("Cc");
|
|
289
|
+
const origMessageId = get("Message-ID");
|
|
290
|
+
const origReferences = get("References");
|
|
291
|
+
const threadId = orig.data.threadId ?? undefined;
|
|
292
|
+
|
|
293
|
+
const replySubject = origSubject.startsWith("Re:") ? origSubject : `Re: ${origSubject}`;
|
|
294
|
+
const references = origReferences ? `${origReferences} ${origMessageId}` : origMessageId;
|
|
295
|
+
|
|
296
|
+
const from = await getSenderEmail(client);
|
|
297
|
+
|
|
298
|
+
const replyHeaders: Record<string, string> = {
|
|
299
|
+
From: from,
|
|
300
|
+
To: origFrom,
|
|
301
|
+
Subject: replySubject,
|
|
302
|
+
"In-Reply-To": origMessageId,
|
|
303
|
+
References: references,
|
|
304
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
305
|
+
"MIME-Version": "1.0",
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
if (replyAll) {
|
|
309
|
+
const allCcAddresses = [origTo, origCc]
|
|
310
|
+
.filter(Boolean)
|
|
311
|
+
.join(", ")
|
|
312
|
+
.split(",")
|
|
313
|
+
.map((a) => a.trim())
|
|
314
|
+
.filter((a) => a && !a.toLowerCase().includes(from.toLowerCase()));
|
|
315
|
+
if (allCcAddresses.length > 0) {
|
|
316
|
+
replyHeaders["Cc"] = allCcAddresses.join(", ");
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const attachments: AttachmentInput[] | undefined = attachmentPaths?.map((p) => ({ file_path: p }));
|
|
321
|
+
|
|
322
|
+
const raw = encodeMultipartMessage(replyHeaders, body, attachments, htmlBody);
|
|
323
|
+
|
|
324
|
+
const res = await gmail.users.messages.send({
|
|
325
|
+
userId: "me",
|
|
326
|
+
requestBody: { raw, threadId },
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
outputJson({ id: res.data.id, threadId: res.data.threadId, success: true });
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export async function handleForward(
|
|
333
|
+
clientManager: OAuthClientManager,
|
|
334
|
+
account: string,
|
|
335
|
+
id: string,
|
|
336
|
+
to: string,
|
|
337
|
+
body?: string,
|
|
338
|
+
attachmentPaths?: string[],
|
|
339
|
+
): Promise<void> {
|
|
340
|
+
if (!clientManager.listAccounts().includes(account)) {
|
|
341
|
+
outputAuthRequired();
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const client = clientManager.getClient(account);
|
|
345
|
+
const gmail = google.gmail({ version: "v1", auth: client });
|
|
346
|
+
|
|
347
|
+
const orig = await gmail.users.messages.get({
|
|
348
|
+
userId: "me",
|
|
349
|
+
id,
|
|
350
|
+
format: "full",
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
const headers = orig.data.payload?.headers ?? [];
|
|
354
|
+
const get = (name: string) =>
|
|
355
|
+
headers.find((h) => h.name?.toLowerCase() === name.toLowerCase())?.value ?? "";
|
|
356
|
+
|
|
357
|
+
const origSubject = get("Subject");
|
|
358
|
+
const origFrom = get("From");
|
|
359
|
+
const origDate = get("Date");
|
|
360
|
+
|
|
361
|
+
const fwdSubject = origSubject.startsWith("Fwd:") ? origSubject : `Fwd: ${origSubject}`;
|
|
362
|
+
|
|
363
|
+
let origBody = "";
|
|
364
|
+
if (orig.data.payload?.body?.data) {
|
|
365
|
+
origBody = Buffer.from(orig.data.payload.body.data, "base64").toString("utf-8");
|
|
366
|
+
} else {
|
|
367
|
+
origBody = extractPlainText(orig.data.payload?.parts as MessagePart[] | undefined);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const quotedBody = [
|
|
371
|
+
body ?? "",
|
|
372
|
+
"",
|
|
373
|
+
"---------- Forwarded message ---------",
|
|
374
|
+
`From: ${origFrom}`,
|
|
375
|
+
`Date: ${origDate}`,
|
|
376
|
+
`Subject: ${origSubject}`,
|
|
377
|
+
"",
|
|
378
|
+
origBody,
|
|
379
|
+
]
|
|
380
|
+
.join("\r\n")
|
|
381
|
+
.trimStart();
|
|
382
|
+
|
|
383
|
+
// Fetch original attachments
|
|
384
|
+
const origAttachmentMetas = extractAttachmentMeta(
|
|
385
|
+
orig.data.payload?.parts as MessagePart[] | undefined,
|
|
386
|
+
);
|
|
387
|
+
const origInMemoryAttachments: InMemoryAttachment[] = [];
|
|
388
|
+
for (const meta of origAttachmentMetas) {
|
|
389
|
+
const attRes = await gmail.users.messages.attachments.get({
|
|
390
|
+
userId: "me",
|
|
391
|
+
messageId: id,
|
|
392
|
+
id: meta.attachmentId,
|
|
393
|
+
});
|
|
394
|
+
const data = attRes.data.data;
|
|
395
|
+
if (data) {
|
|
396
|
+
origInMemoryAttachments.push({
|
|
397
|
+
filename: meta.filename,
|
|
398
|
+
mimeType: meta.mimeType,
|
|
399
|
+
data: Buffer.from(data, "base64url"),
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const from = await getSenderEmail(client);
|
|
405
|
+
const fileAttachments: AttachmentInput[] | undefined = attachmentPaths?.map((p) => ({ file_path: p }));
|
|
406
|
+
|
|
407
|
+
const raw = encodeMultipartMessage(
|
|
408
|
+
{
|
|
409
|
+
From: from,
|
|
410
|
+
To: to,
|
|
411
|
+
Subject: fwdSubject,
|
|
412
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
413
|
+
"MIME-Version": "1.0",
|
|
414
|
+
},
|
|
415
|
+
quotedBody,
|
|
416
|
+
fileAttachments,
|
|
417
|
+
undefined,
|
|
418
|
+
origInMemoryAttachments.length > 0 ? origInMemoryAttachments : undefined,
|
|
419
|
+
);
|
|
420
|
+
|
|
421
|
+
const res = await gmail.users.messages.send({ userId: "me", requestBody: { raw } });
|
|
422
|
+
|
|
423
|
+
outputJson({ id: res.data.id, threadId: res.data.threadId, success: true });
|
|
424
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { google } from "googleapis";
|
|
2
|
+
import type { OAuthClientManager } from "@digitalpresence/cliclaw-auth";
|
|
3
|
+
import { outputJson, outputAuthRequired } from "../lib/output.js";
|
|
4
|
+
|
|
5
|
+
export async function handleThreadList(
|
|
6
|
+
clientManager: OAuthClientManager,
|
|
7
|
+
account: string,
|
|
8
|
+
maxResults: number,
|
|
9
|
+
query?: string,
|
|
10
|
+
): Promise<void> {
|
|
11
|
+
if (!clientManager.listAccounts().includes(account)) {
|
|
12
|
+
outputAuthRequired();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const client = clientManager.getClient(account);
|
|
16
|
+
const gmail = google.gmail({ version: "v1", auth: client });
|
|
17
|
+
|
|
18
|
+
const res = await gmail.users.threads.list({
|
|
19
|
+
userId: "me",
|
|
20
|
+
maxResults,
|
|
21
|
+
...(query ? { q: query } : {}),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const threads = res.data.threads ?? [];
|
|
25
|
+
|
|
26
|
+
const result = threads.map((thread) => ({
|
|
27
|
+
id: thread.id ?? "",
|
|
28
|
+
snippet: thread.snippet ?? "",
|
|
29
|
+
historyId: thread.historyId ?? "",
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
outputJson(result);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function handleThreadGet(
|
|
36
|
+
clientManager: OAuthClientManager,
|
|
37
|
+
account: string,
|
|
38
|
+
threadId: string,
|
|
39
|
+
): Promise<void> {
|
|
40
|
+
if (!clientManager.listAccounts().includes(account)) {
|
|
41
|
+
outputAuthRequired();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const client = clientManager.getClient(account);
|
|
45
|
+
const gmail = google.gmail({ version: "v1", auth: client });
|
|
46
|
+
|
|
47
|
+
const res = await gmail.users.threads.get({
|
|
48
|
+
userId: "me",
|
|
49
|
+
id: threadId,
|
|
50
|
+
format: "metadata",
|
|
51
|
+
metadataHeaders: ["Subject", "From", "Date"],
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const messages = (res.data.messages ?? []).map((msg) => {
|
|
55
|
+
const headers = msg.payload?.headers ?? [];
|
|
56
|
+
const get = (name: string) =>
|
|
57
|
+
headers.find((h) => h.name?.toLowerCase() === name.toLowerCase())?.value ?? "";
|
|
58
|
+
return {
|
|
59
|
+
id: msg.id ?? "",
|
|
60
|
+
subject: get("Subject"),
|
|
61
|
+
from: get("From"),
|
|
62
|
+
date: get("Date"),
|
|
63
|
+
snippet: msg.snippet ?? "",
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
outputJson({ id: res.data.id ?? threadId, messages });
|
|
68
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { google } from "googleapis";
|
|
2
|
+
import type { OAuthClientManager } from "@digitalpresence/cliclaw-auth";
|
|
3
|
+
import { outputJson } from "../lib/output.js";
|
|
4
|
+
|
|
5
|
+
export async function handleAccounts(clientManager: OAuthClientManager): Promise<void> {
|
|
6
|
+
const allAccounts = clientManager.listAccounts();
|
|
7
|
+
const gslidesAccounts = allAccounts.filter((a) => a.startsWith("gslides:"));
|
|
8
|
+
|
|
9
|
+
const accounts = await Promise.all(
|
|
10
|
+
gslidesAccounts.map(async (tokenKey) => {
|
|
11
|
+
const account = tokenKey.replace("gslides:", "");
|
|
12
|
+
try {
|
|
13
|
+
const client = clientManager.getClient(tokenKey);
|
|
14
|
+
const drive = google.drive({ version: "v3", auth: client });
|
|
15
|
+
const about = await drive.about.get({ fields: "user" });
|
|
16
|
+
return { account, email: about.data.user?.emailAddress ?? null };
|
|
17
|
+
} catch {
|
|
18
|
+
return { account, email: null };
|
|
19
|
+
}
|
|
20
|
+
}),
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
outputJson({ accounts });
|
|
24
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { google } from "googleapis";
|
|
2
|
+
import type { OAuthClientManager } from "@digitalpresence/cliclaw-auth";
|
|
3
|
+
import { waitForOAuthCallback, getGSlidesAuthUrl } from "@digitalpresence/cliclaw-auth";
|
|
4
|
+
import { outputJson, outputError } from "../lib/output.js";
|
|
5
|
+
|
|
6
|
+
export async function handleAuth(clientManager: OAuthClientManager, port: number, account: string): Promise<void> {
|
|
7
|
+
const tokenKey = `gslides:${account}`;
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
const client = clientManager.getClient(tokenKey);
|
|
11
|
+
const creds = client.credentials;
|
|
12
|
+
if (creds && (creds.refresh_token || creds.access_token)) {
|
|
13
|
+
const drive = google.drive({ version: "v3", auth: client });
|
|
14
|
+
const about = await drive.about.get({ fields: "user" });
|
|
15
|
+
outputJson({
|
|
16
|
+
status: "already_authenticated",
|
|
17
|
+
account,
|
|
18
|
+
email: about.data.user?.emailAddress ?? "unknown",
|
|
19
|
+
message: "Existing session is still valid. No re-authentication needed.",
|
|
20
|
+
});
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
} catch {
|
|
24
|
+
// Tokens invalid — proceed with re-auth
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const open = (await import("open")).default;
|
|
29
|
+
const rawClient = clientManager.getRawClient();
|
|
30
|
+
|
|
31
|
+
const tokens = await waitForOAuthCallback(rawClient, port, (url) => {
|
|
32
|
+
console.error(`Opening browser for Google Slides authentication...`);
|
|
33
|
+
console.error(url);
|
|
34
|
+
open(url).catch(() => {
|
|
35
|
+
console.error("Could not open browser. Please visit the URL above manually.");
|
|
36
|
+
});
|
|
37
|
+
}, getGSlidesAuthUrl);
|
|
38
|
+
|
|
39
|
+
const client = clientManager.setCredentials(tokenKey, tokens);
|
|
40
|
+
|
|
41
|
+
const drive = google.drive({ version: "v3", auth: client });
|
|
42
|
+
let email = "unknown";
|
|
43
|
+
try {
|
|
44
|
+
const about = await drive.about.get({ fields: "user" });
|
|
45
|
+
email = about.data.user?.emailAddress ?? "unknown";
|
|
46
|
+
} catch {
|
|
47
|
+
// Non-fatal
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
outputJson({ status: "authenticated", account, email });
|
|
51
|
+
} catch (err) {
|
|
52
|
+
outputError("auth_failed", err instanceof Error ? err.message : String(err));
|
|
53
|
+
}
|
|
54
|
+
}
|