langmail-mcp 0.1.0 → 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 (137) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +89 -169
  3. package/dist/chunk-0fxfqny0.js +12642 -0
  4. package/dist/langmail-mcp.js +7 -0
  5. package/package.json +93 -98
  6. package/THIRD_PARTY_NOTICES.md +0 -388
  7. package/dist/attachments/download.js +0 -69
  8. package/dist/attachments/routes.js +0 -42
  9. package/dist/attachments/tickets.js +0 -45
  10. package/dist/attachments/upload-tickets.js +0 -43
  11. package/dist/attachments/upload.js +0 -136
  12. package/dist/caldav/account-settings.js +0 -92
  13. package/dist/caldav/agenda.js +0 -277
  14. package/dist/caldav/alarm.js +0 -97
  15. package/dist/caldav/client.js +0 -408
  16. package/dist/caldav/ical.js +0 -248
  17. package/dist/caldav/occurrence.js +0 -534
  18. package/dist/caldav/recurrence.js +0 -313
  19. package/dist/caldav/rsvp.js +0 -107
  20. package/dist/caldav/scheduling-handover.js +0 -22
  21. package/dist/caldav/timezone.js +0 -103
  22. package/dist/caldav/types.js +0 -5
  23. package/dist/config.js +0 -219
  24. package/dist/contacts/address-class.js +0 -79
  25. package/dist/contacts/birthdays.js +0 -81
  26. package/dist/contacts/card.js +0 -58
  27. package/dist/contacts/constants.js +0 -46
  28. package/dist/contacts/fold.js +0 -309
  29. package/dist/contacts/index.js +0 -10
  30. package/dist/contacts/merge.js +0 -388
  31. package/dist/contacts/observations.js +0 -115
  32. package/dist/contacts/profile.js +0 -402
  33. package/dist/contacts/scoring.js +0 -146
  34. package/dist/contacts/types.js +0 -7
  35. package/dist/contacts/upsert.js +0 -131
  36. package/dist/drafts/attachments.js +0 -254
  37. package/dist/drafts/idempotency.js +0 -60
  38. package/dist/drafts/send-guard.js +0 -184
  39. package/dist/email-render/email-viewer.html +0 -143
  40. package/dist/email-render/image-proxy.js +0 -389
  41. package/dist/email-render/meta.js +0 -48
  42. package/dist/email-render/sanitize.js +0 -93
  43. package/dist/email-render/viewer-resource.js +0 -104
  44. package/dist/http/bearer.js +0 -30
  45. package/dist/http/guard.js +0 -57
  46. package/dist/http/hosts.js +0 -10
  47. package/dist/http/listener.js +0 -170
  48. package/dist/http/routes.js +0 -72
  49. package/dist/ical/ical-parse.js +0 -357
  50. package/dist/ical/index.js +0 -4
  51. package/dist/ical/timezone.js +0 -22
  52. package/dist/ical/windows-zones.js +0 -155
  53. package/dist/index.js +0 -79
  54. package/dist/jmap/blob.js +0 -49
  55. package/dist/jmap/contacts.js +0 -226
  56. package/dist/jmap/drafts.js +0 -480
  57. package/dist/jmap/email.js +0 -668
  58. package/dist/jmap/index.js +0 -12
  59. package/dist/jmap/keywords.js +0 -22
  60. package/dist/jmap/live.js +0 -21
  61. package/dist/jmap/origin.js +0 -113
  62. package/dist/jmap/patch.js +0 -29
  63. package/dist/jmap/request.js +0 -37
  64. package/dist/jmap/session.js +0 -89
  65. package/dist/jmap/sieve.js +0 -71
  66. package/dist/jmap/submission.js +0 -107
  67. package/dist/jmap/timeout.js +0 -30
  68. package/dist/jmap/types.js +0 -6
  69. package/dist/logger.js +0 -48
  70. package/dist/mcp/handler.js +0 -255
  71. package/dist/mcp/http.js +0 -29
  72. package/dist/mcp/instructions.js +0 -44
  73. package/dist/mcp/links.js +0 -64
  74. package/dist/mcp/tool-logging.js +0 -67
  75. package/dist/observability/errors.js +0 -41
  76. package/dist/rules/apply.js +0 -236
  77. package/dist/rules/rule.js +0 -280
  78. package/dist/rules/store.js +0 -92
  79. package/dist/tools/annotations.js +0 -71
  80. package/dist/tools/archive-emails.js +0 -40
  81. package/dist/tools/calendar-lookup.js +0 -64
  82. package/dist/tools/contact-common.js +0 -146
  83. package/dist/tools/contact-format.js +0 -72
  84. package/dist/tools/contact-write-through.js +0 -37
  85. package/dist/tools/create-calendar-event.js +0 -231
  86. package/dist/tools/create-calendar.js +0 -54
  87. package/dist/tools/create-contact.js +0 -172
  88. package/dist/tools/create-draft.js +0 -369
  89. package/dist/tools/create-mail-rule.js +0 -101
  90. package/dist/tools/delete-calendar-event.js +0 -165
  91. package/dist/tools/delete-contact.js +0 -60
  92. package/dist/tools/delete-draft.js +0 -129
  93. package/dist/tools/delete-mail-rule.js +0 -38
  94. package/dist/tools/download-attachment.js +0 -90
  95. package/dist/tools/download-email.js +0 -112
  96. package/dist/tools/draft-common.js +0 -600
  97. package/dist/tools/email-types.js +0 -58
  98. package/dist/tools/error-detail.js +0 -10
  99. package/dist/tools/file-emails-common.js +0 -75
  100. package/dist/tools/format.js +0 -131
  101. package/dist/tools/forward-message.js +0 -237
  102. package/dist/tools/forward-quote.js +0 -179
  103. package/dist/tools/get-draft.js +0 -61
  104. package/dist/tools/get-email.js +0 -164
  105. package/dist/tools/get-thread.js +0 -67
  106. package/dist/tools/keywords.js +0 -43
  107. package/dist/tools/list-calendar-events.js +0 -180
  108. package/dist/tools/list-calendars.js +0 -44
  109. package/dist/tools/list-drafts.js +0 -134
  110. package/dist/tools/list-mail-rules.js +0 -28
  111. package/dist/tools/list-unsubscribe.js +0 -152
  112. package/dist/tools/list-upcoming-birthdays.js +0 -165
  113. package/dist/tools/mail-rule-common.js +0 -43
  114. package/dist/tools/mail-rule-format.js +0 -79
  115. package/dist/tools/mailbox-lookup.js +0 -13
  116. package/dist/tools/mark-emails.js +0 -78
  117. package/dist/tools/merge-contacts.js +0 -145
  118. package/dist/tools/move-calendar-events.js +0 -340
  119. package/dist/tools/move-emails.js +0 -64
  120. package/dist/tools/one-click-post.js +0 -275
  121. package/dist/tools/preview-email.js +0 -204
  122. package/dist/tools/preview-mail-rule.js +0 -79
  123. package/dist/tools/reply-quote.js +0 -426
  124. package/dist/tools/reply-to-message.js +0 -292
  125. package/dist/tools/respond-to-event.js +0 -110
  126. package/dist/tools/result.js +0 -9
  127. package/dist/tools/search-contacts.js +0 -431
  128. package/dist/tools/search-emails.js +0 -570
  129. package/dist/tools/send-draft.js +0 -308
  130. package/dist/tools/trash-emails.js +0 -36
  131. package/dist/tools/unsubscribe-from-list.js +0 -249
  132. package/dist/tools/update-calendar-event.js +0 -973
  133. package/dist/tools/update-calendar.js +0 -100
  134. package/dist/tools/update-contact.js +0 -382
  135. package/dist/tools/update-draft.js +0 -714
  136. package/dist/tools/update-mail-rule.js +0 -103
  137. package/dist/tools/upload-attachments.js +0 -126
@@ -1,22 +0,0 @@
1
- // Wall-clock time helpers the VEVENT parser needs. Only the pure time
2
- // arithmetic lives here — the zod input schemas that validate tool
3
- // arguments (timeZoneSchema, dateTimeWithOptionalOffsetSchema, …) stay in
4
- // src/caldav/timezone.ts, which owns request validation, and re-export from
5
- // here so there is a single implementation.
6
- import { TZDate } from "@date-fns/tz";
7
- import { ianaTimeZone } from "./windows-zones.js";
8
- // Exported so the tools' eventTimeSchema admits exactly what this reads.
9
- export const DATE_TIME_RE = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))?(?:Z|[+-]\d{2}:\d{2})?$/iu;
10
- // Reads the clock digits of `value` as wall-clock time *in* `timeZone`,
11
- // ignoring any offset the string carries. A stored RECURRENCE-ID or DTSTART
12
- // with a TZID means "10:00 in Europe/Berlin", not "10:00 UTC", so resolving
13
- // it needs the zone rather than Date's local-time default. A Windows zone name
14
- // (`W. Europe Standard Time`) reads as its IANA zone.
15
- export const parseWallClockInZone = (value, timeZone) => {
16
- const match = DATE_TIME_RE.exec(value);
17
- if (!match) {
18
- throw new Error(`invalid date-time: ${value}`);
19
- }
20
- const [, year, month, day, hour, minute, second, millis = "0"] = match;
21
- return TZDate.tz(ianaTimeZone(timeZone), Number(year), Number(month) - 1, Number(day), Number(hour), Number(minute), Number(second), Number(millis.padEnd(3, "0")));
22
- };
@@ -1,155 +0,0 @@
1
- // Windows time zone names → IANA zones, as Outlook and Exchange write them in
2
- // TZID (`DTSTART;TZID=W. Europe Standard Time:…`). Generated from CLDR
3
- // release 49, common/supplemental/windowsZones.xml, territory `001` (each
4
- // Windows name's golden zone), with two edits: CLDR's deprecated
5
- // America/Indianapolis and America/Buenos_Aires are written as their canonical
6
- // names, which the VTIMEZONE generator src/caldav writes with knows; and
7
- // Windows `UTC` is left out, since `UTC` already names that zone. To refresh,
8
- // regenerate from a newer CLDR release, reapply the edits, and update the
9
- // release named here.
10
- export const WINDOWS_ZONES = {
11
- "Dateline Standard Time": "Etc/GMT+12",
12
- "UTC-11": "Etc/GMT+11",
13
- "Aleutian Standard Time": "America/Adak",
14
- "Hawaiian Standard Time": "Pacific/Honolulu",
15
- "Marquesas Standard Time": "Pacific/Marquesas",
16
- "Alaskan Standard Time": "America/Anchorage",
17
- "UTC-09": "Etc/GMT+9",
18
- "Pacific Standard Time (Mexico)": "America/Tijuana",
19
- "UTC-08": "Etc/GMT+8",
20
- "Pacific Standard Time": "America/Los_Angeles",
21
- "US Mountain Standard Time": "America/Phoenix",
22
- "Mountain Standard Time (Mexico)": "America/Mazatlan",
23
- "Mountain Standard Time": "America/Denver",
24
- "British Columbia Standard Time": "America/Vancouver",
25
- "Yukon Standard Time": "America/Whitehorse",
26
- "Alberta Standard Time": "America/Edmonton",
27
- "Central America Standard Time": "America/Guatemala",
28
- "Central Standard Time": "America/Chicago",
29
- "Easter Island Standard Time": "Pacific/Easter",
30
- "Central Standard Time (Mexico)": "America/Mexico_City",
31
- "Canada Central Standard Time": "America/Regina",
32
- "Manitoba Standard Time": "America/Winnipeg",
33
- "SA Pacific Standard Time": "America/Bogota",
34
- "Eastern Standard Time (Mexico)": "America/Cancun",
35
- "Eastern Standard Time": "America/New_York",
36
- "Haiti Standard Time": "America/Port-au-Prince",
37
- "Cuba Standard Time": "America/Havana",
38
- "US Eastern Standard Time": "America/Indiana/Indianapolis",
39
- "Turks And Caicos Standard Time": "America/Grand_Turk",
40
- "Paraguay Standard Time": "America/Asuncion",
41
- "Atlantic Standard Time": "America/Halifax",
42
- "Venezuela Standard Time": "America/Caracas",
43
- "Central Brazilian Standard Time": "America/Cuiaba",
44
- "SA Western Standard Time": "America/La_Paz",
45
- "Pacific SA Standard Time": "America/Santiago",
46
- "Newfoundland Standard Time": "America/St_Johns",
47
- "Tocantins Standard Time": "America/Araguaina",
48
- "E. South America Standard Time": "America/Sao_Paulo",
49
- "SA Eastern Standard Time": "America/Cayenne",
50
- "Argentina Standard Time": "America/Argentina/Buenos_Aires",
51
- "Greenland Standard Time": "America/Godthab",
52
- "Montevideo Standard Time": "America/Montevideo",
53
- "Magallanes Standard Time": "America/Punta_Arenas",
54
- "Saint Pierre Standard Time": "America/Miquelon",
55
- "Bahia Standard Time": "America/Bahia",
56
- "UTC-02": "Etc/GMT+2",
57
- "Azores Standard Time": "Atlantic/Azores",
58
- "Cape Verde Standard Time": "Atlantic/Cape_Verde",
59
- "GMT Standard Time": "Europe/London",
60
- "Greenwich Standard Time": "Atlantic/Reykjavik",
61
- "Sao Tome Standard Time": "Africa/Sao_Tome",
62
- "Morocco Standard Time": "Africa/Casablanca",
63
- "W. Europe Standard Time": "Europe/Berlin",
64
- "Central Europe Standard Time": "Europe/Budapest",
65
- "Romance Standard Time": "Europe/Paris",
66
- "Central European Standard Time": "Europe/Warsaw",
67
- "W. Central Africa Standard Time": "Africa/Lagos",
68
- "Jordan Standard Time": "Asia/Amman",
69
- "GTB Standard Time": "Europe/Bucharest",
70
- "Middle East Standard Time": "Asia/Beirut",
71
- "Egypt Standard Time": "Africa/Cairo",
72
- "E. Europe Standard Time": "Europe/Chisinau",
73
- "Syria Standard Time": "Asia/Damascus",
74
- "West Bank Standard Time": "Asia/Hebron",
75
- "South Africa Standard Time": "Africa/Johannesburg",
76
- "FLE Standard Time": "Europe/Kiev",
77
- "Israel Standard Time": "Asia/Jerusalem",
78
- "South Sudan Standard Time": "Africa/Juba",
79
- "Kaliningrad Standard Time": "Europe/Kaliningrad",
80
- "Sudan Standard Time": "Africa/Khartoum",
81
- "Libya Standard Time": "Africa/Tripoli",
82
- "Namibia Standard Time": "Africa/Windhoek",
83
- "Arabic Standard Time": "Asia/Baghdad",
84
- "Turkey Standard Time": "Europe/Istanbul",
85
- "Arab Standard Time": "Asia/Riyadh",
86
- "Belarus Standard Time": "Europe/Minsk",
87
- "Russian Standard Time": "Europe/Moscow",
88
- "E. Africa Standard Time": "Africa/Nairobi",
89
- "Iran Standard Time": "Asia/Tehran",
90
- "Arabian Standard Time": "Asia/Dubai",
91
- "Astrakhan Standard Time": "Europe/Astrakhan",
92
- "Azerbaijan Standard Time": "Asia/Baku",
93
- "Russia Time Zone 3": "Europe/Samara",
94
- "Mauritius Standard Time": "Indian/Mauritius",
95
- "Saratov Standard Time": "Europe/Saratov",
96
- "Georgian Standard Time": "Asia/Tbilisi",
97
- "Volgograd Standard Time": "Europe/Volgograd",
98
- "Caucasus Standard Time": "Asia/Yerevan",
99
- "Afghanistan Standard Time": "Asia/Kabul",
100
- "West Asia Standard Time": "Asia/Tashkent",
101
- "Ekaterinburg Standard Time": "Asia/Yekaterinburg",
102
- "Pakistan Standard Time": "Asia/Karachi",
103
- "Qyzylorda Standard Time": "Asia/Qyzylorda",
104
- "India Standard Time": "Asia/Calcutta",
105
- "Sri Lanka Standard Time": "Asia/Colombo",
106
- "Nepal Standard Time": "Asia/Katmandu",
107
- "Central Asia Standard Time": "Asia/Bishkek",
108
- "Bangladesh Standard Time": "Asia/Dhaka",
109
- "Omsk Standard Time": "Asia/Omsk",
110
- "Myanmar Standard Time": "Asia/Rangoon",
111
- "SE Asia Standard Time": "Asia/Bangkok",
112
- "Altai Standard Time": "Asia/Barnaul",
113
- "W. Mongolia Standard Time": "Asia/Hovd",
114
- "North Asia Standard Time": "Asia/Krasnoyarsk",
115
- "N. Central Asia Standard Time": "Asia/Novosibirsk",
116
- "Tomsk Standard Time": "Asia/Tomsk",
117
- "China Standard Time": "Asia/Shanghai",
118
- "North Asia East Standard Time": "Asia/Irkutsk",
119
- "Singapore Standard Time": "Asia/Singapore",
120
- "W. Australia Standard Time": "Australia/Perth",
121
- "Taipei Standard Time": "Asia/Taipei",
122
- "Ulaanbaatar Standard Time": "Asia/Ulaanbaatar",
123
- "Aus Central W. Standard Time": "Australia/Eucla",
124
- "Transbaikal Standard Time": "Asia/Chita",
125
- "Tokyo Standard Time": "Asia/Tokyo",
126
- "North Korea Standard Time": "Asia/Pyongyang",
127
- "Korea Standard Time": "Asia/Seoul",
128
- "Yakutsk Standard Time": "Asia/Yakutsk",
129
- "Cen. Australia Standard Time": "Australia/Adelaide",
130
- "AUS Central Standard Time": "Australia/Darwin",
131
- "E. Australia Standard Time": "Australia/Brisbane",
132
- "AUS Eastern Standard Time": "Australia/Sydney",
133
- "West Pacific Standard Time": "Pacific/Port_Moresby",
134
- "Tasmania Standard Time": "Australia/Hobart",
135
- "Vladivostok Standard Time": "Asia/Vladivostok",
136
- "Lord Howe Standard Time": "Australia/Lord_Howe",
137
- "Bougainville Standard Time": "Pacific/Bougainville",
138
- "Russia Time Zone 10": "Asia/Srednekolymsk",
139
- "Magadan Standard Time": "Asia/Magadan",
140
- "Norfolk Standard Time": "Pacific/Norfolk",
141
- "Sakhalin Standard Time": "Asia/Sakhalin",
142
- "Central Pacific Standard Time": "Pacific/Guadalcanal",
143
- "Russia Time Zone 11": "Asia/Kamchatka",
144
- "New Zealand Standard Time": "Pacific/Auckland",
145
- "UTC+12": "Etc/GMT-12",
146
- "Fiji Standard Time": "Pacific/Fiji",
147
- "Chatham Islands Standard Time": "Pacific/Chatham",
148
- "UTC+13": "Etc/GMT-13",
149
- "Tonga Standard Time": "Pacific/Tongatapu",
150
- "Samoa Standard Time": "Pacific/Apia",
151
- "Line Islands Standard Time": "Pacific/Kiritimati",
152
- };
153
- // The IANA zone a TZID names: a Windows name maps through CLDR's table, and
154
- // anything else (an IANA zone, or a name neither knows) comes back unchanged.
155
- export const ianaTimeZone = (tzid) => WINDOWS_ZONES[tzid] ?? tzid;
package/dist/index.js DELETED
@@ -1,79 +0,0 @@
1
- #!/usr/bin/env node
2
- import { StdioServerTransport } from "@modelcontextprotocol/server/stdio";
3
- import { ConfigError, loadConfig, } from "./config.js";
4
- import { createRequestGuard } from "./http/guard.js";
5
- import { startHttpListener } from "./http/listener.js";
6
- import { createFetchHandler, healthRoute } from "./http/routes.js";
7
- import { createRuntime, createServer } from "./mcp/handler.js";
8
- import { MCP_PATH, mcpRoutes } from "./mcp/http.js";
9
- import { linkRoutes } from "./mcp/links.js";
10
- const EXIT_MISCONFIGURED = 1;
11
- // stdout belongs to the MCP transport: everything a person should read goes
12
- // to stderr.
13
- const say = (message) => {
14
- process.stderr.write(`${message}\n`);
15
- };
16
- // Serves MCP over Streamable HTTP until the process is told to stop.
17
- const serveHttp = async (config, transport) => {
18
- const runtime = createRuntime(config);
19
- const listener = await startHttpListener({
20
- fetch: createFetchHandler([
21
- healthRoute,
22
- // File links (attachment downloads and uploads): the ticket in the
23
- // path is the credential, so these do not ask for the bearer token.
24
- ...linkRoutes(runtime),
25
- ...mcpRoutes(config, transport.authToken, runtime),
26
- ], {
27
- guard: createRequestGuard({
28
- host: transport.host,
29
- publicUrl: transport.publicUrl,
30
- }),
31
- }),
32
- host: transport.host,
33
- port: transport.port,
34
- });
35
- say(`langmail-mcp: listening on ${listener.origin}${MCP_PATH}`);
36
- // Stop accepting, let requests in flight finish, then leave. A second
37
- // signal while that is under way exits at once.
38
- let stopping = false;
39
- const stop = () => {
40
- if (stopping) {
41
- process.exit(EXIT_MISCONFIGURED);
42
- }
43
- stopping = true;
44
- listener
45
- .close()
46
- .then(() => process.exit(0))
47
- .catch(() => process.exit(EXIT_MISCONFIGURED));
48
- };
49
- process.on("SIGINT", stop);
50
- process.on("SIGTERM", stop);
51
- };
52
- const main = async () => {
53
- const { config, warnings } = loadConfig(process.env, process.argv.slice(2));
54
- for (const warning of warnings) {
55
- say(`langmail-mcp: warning: ${warning}`);
56
- }
57
- if (config.transport.kind === "http") {
58
- await serveHttp(config, config.transport);
59
- return;
60
- }
61
- // No request goes to Stalwart here: the MCP handshake must not wait on, or
62
- // fail with, the mail server. The credential is checked on the first tool
63
- // call.
64
- // Nothing listens over stdio until a tool hands out a file link: the
65
- // runtime then starts a loopback listener for the links alone.
66
- await createServer(config).connect(new StdioServerTransport());
67
- };
68
- try {
69
- await main();
70
- }
71
- catch (error) {
72
- if (error instanceof ConfigError) {
73
- say(error.message);
74
- }
75
- else {
76
- say(`langmail-mcp: failed to start: ${error instanceof Error ? error.message : String(error)}`);
77
- }
78
- process.exitCode = EXIT_MISCONFIGURED;
79
- }
package/dist/jmap/blob.js DELETED
@@ -1,49 +0,0 @@
1
- // Blob download via the session's RFC 8620 §6.2 `downloadUrl` URI template.
2
- //
3
- // The template carries `{accountId}`, `{blobId}`, `{name}`, and `{type}`
4
- // placeholders (Stalwart: `/jmap/download/{accountId}/{blobId}/{name}?accept={type}`).
5
- // Expansion URI-encodes every value so filenames with spaces, slashes, or
6
- // unicode and media types with `/` stay one path/query segment.
7
- import { jmapFetch } from "./origin.js";
8
- import { JmapError } from "./session.js";
9
- import { fetchWithHeadersDeadline, jmapTimeoutSignal } from "./timeout.js";
10
- export const blobDownloadUrl = (template, p) => template
11
- .replaceAll("{accountId}", encodeURIComponent(p.accountId))
12
- .replaceAll("{blobId}", encodeURIComponent(p.blobId))
13
- .replaceAll("{name}", encodeURIComponent(p.name))
14
- .replaceAll("{type}", encodeURIComponent(p.type));
15
- // Fetch the blob and hand back the *streamed* Response — the body is never
16
- // buffered here, so a large attachment can be piped straight through to the
17
- // consumer (the MCP `/attachments/:token` route) without memory pressure.
18
- export const blobDownload = async (p) => {
19
- const url = blobDownloadUrl(p.downloadUrl, p);
20
- // Headers-only deadline: the body is piped straight through to the consumer,
21
- // so a whole-exchange timeout would cut a slow attachment off mid-transfer.
22
- const res = await fetchWithHeadersDeadline(url, {
23
- headers: { authorization: p.authorization },
24
- });
25
- if (res.status !== 200) {
26
- throw new JmapError(res.status, `jmap blob download returned ${res.status}`);
27
- }
28
- return res;
29
- };
30
- export const blobUpload = async (p) => {
31
- const url = p.uploadUrl.replaceAll("{accountId}", encodeURIComponent(p.accountId));
32
- const res = await jmapFetch(url, {
33
- // Node's fetch types want an ArrayBuffer-backed view; any Uint8Array
34
- // is sent as its bytes.
35
- body: p.content,
36
- headers: { authorization: p.authorization, "content-type": p.type },
37
- method: "POST",
38
- signal: jmapTimeoutSignal(),
39
- });
40
- // RFC 8620 leaves the success code to the server; Stalwart answers 200.
41
- if (res.status !== 200 && res.status !== 201) {
42
- throw new JmapError(res.status, `jmap blob upload returned ${res.status}`);
43
- }
44
- const body = (await res.json());
45
- if (typeof body.blobId !== "string") {
46
- throw new JmapError(res.status, "malformed jmap blob upload response");
47
- }
48
- return body.blobId;
49
- };
@@ -1,226 +0,0 @@
1
- // Typed JMAP wrappers for the contacts capability (RFC 9610) — AddressBook
2
- // and ContactCard methods on the same endpoint and Authorization seam as the
3
- // mail surface. Cards are JSContact (RFC 9553) narrowed to the properties
4
- // Langmail reads; vendor extension properties (the collected-contacts
5
- // payload) ride along untyped and are interpreted by `src/contacts`.
6
- import { jmapCall, methodResponse } from "./request.js";
7
- import { JmapError } from "./session.js";
8
- const USING = ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:contacts"];
9
- // `type` is a JMAP error code (`invalidProperties`, `notFound`, …), not free
10
- // text, so it is safe to surface. The server's `description` stays out: it is
11
- // free-form and can quote the value it rejected — a name, a note, a phone
12
- // number, a postal address — and these messages travel to off-box error
13
- // tracking, whose redactor only strips bearer tokens and addresses. Same rule
14
- // as the EmailSubmission wrapper, for the same reason.
15
- const setErrorType = (err) => err?.type ?? "unknown";
16
- // An account holds a handful of books at most (personal + collected), so the
17
- // read side is a single AddressBook/get with `ids: null` (fetch all).
18
- export const addressBookList = async (p) => {
19
- const resp = await jmapCall(p, {
20
- methodCalls: [
21
- ["AddressBook/get", { accountId: p.accountId, ids: null }, "0"],
22
- ],
23
- using: USING,
24
- });
25
- const out = methodResponse(resp, "AddressBook/get", "0");
26
- return out.list ?? [];
27
- };
28
- // Create an address book and return the server-assigned id. Callers that
29
- // provision lazily should list first — this throws if the server rejects.
30
- export const addressBookCreate = async (p) => {
31
- const resp = await jmapCall(p, {
32
- methodCalls: [
33
- [
34
- "AddressBook/set",
35
- { accountId: p.accountId, create: { b0: { name: p.name } } },
36
- "0",
37
- ],
38
- ],
39
- using: USING,
40
- });
41
- const out = methodResponse(resp, "AddressBook/set", "0");
42
- const id = out.created?.b0?.id;
43
- if (!id) {
44
- const reason = out.notCreated?.b0;
45
- throw new JmapError(200, `AddressBook/set did not create a book: ${setErrorType(reason)}`, reason?.type);
46
- }
47
- return id;
48
- };
49
- export const contactCardGet = async (p) => {
50
- if (p.ids.length === 0) {
51
- return [];
52
- }
53
- const resp = await jmapCall(p, {
54
- methodCalls: [
55
- ["ContactCard/get", { accountId: p.accountId, ids: p.ids }, "0"],
56
- ],
57
- using: USING,
58
- });
59
- const out = methodResponse(resp, "ContactCard/get", "0");
60
- return out.list ?? [];
61
- };
62
- export const contactCardQuery = async (p) => {
63
- const args = {
64
- accountId: p.accountId,
65
- calculateTotal: false,
66
- };
67
- if (p.filter !== undefined) {
68
- args.filter = p.filter;
69
- }
70
- if (p.limit !== undefined) {
71
- args.limit = p.limit;
72
- }
73
- if (p.position !== undefined && p.position > 0) {
74
- args.position = p.position;
75
- }
76
- const resp = await jmapCall(p, {
77
- methodCalls: [["ContactCard/query", args, "0"]],
78
- using: USING,
79
- });
80
- const out = methodResponse(resp, "ContactCard/query", "0");
81
- return out.ids ?? [];
82
- };
83
- // One page of every card in the account, in a single request: an unfiltered
84
- // ContactCard/query and a ContactCard/get of its ids through a `#ids`
85
- // back-reference, so a sweep of the whole account costs one round trip per
86
- // page. Unfiltered, the query reads the store rather than the search index,
87
- // so a card is in it from the moment it is written.
88
- export const contactCardPage = async (p) => {
89
- const resp = await jmapCall(p, {
90
- methodCalls: [
91
- [
92
- "ContactCard/query",
93
- {
94
- accountId: p.accountId,
95
- calculateTotal: true,
96
- limit: p.limit,
97
- ...(p.position > 0 ? { position: p.position } : {}),
98
- },
99
- "0",
100
- ],
101
- [
102
- "ContactCard/get",
103
- {
104
- "#ids": { name: "ContactCard/query", path: "/ids", resultOf: "0" },
105
- accountId: p.accountId,
106
- properties: p.properties,
107
- },
108
- "1",
109
- ],
110
- ],
111
- using: USING,
112
- });
113
- const query = methodResponse(resp, "ContactCard/query", "0");
114
- const got = methodResponse(resp, "ContactCard/get", "1");
115
- const ids = query.ids ?? [];
116
- const byId = new Map((got.list ?? []).map((card) => [card.id, card]));
117
- const cards = ids.flatMap((id) => {
118
- const card = byId.get(id);
119
- return card ? [card] : [];
120
- });
121
- return {
122
- cards,
123
- returned: ids.length,
124
- ...(query.total === undefined ? {} : { total: query.total }),
125
- };
126
- };
127
- // The card's `uid` is already held in its address book. Stalwart 1.0 answers
128
- // that as `invalidProperties` on `uid` (assert_is_unique_uid in
129
- // crates/jmap/src/contact/mod.rs), not with a type of its own, so this is the
130
- // one shape callers can tell apart from a malformed card.
131
- export class ContactUidTakenError extends JmapError {
132
- constructor() {
133
- super(200, "ContactCard/set did not create card: uid already in the address book", "invalidProperties");
134
- this.name = "ContactUidTakenError";
135
- }
136
- }
137
- export const contactCardCreate = async (p) => {
138
- const resp = await jmapCall(p, {
139
- methodCalls: [
140
- [
141
- "ContactCard/set",
142
- { accountId: p.accountId, create: { c0: p.card } },
143
- "0",
144
- ],
145
- ],
146
- using: USING,
147
- });
148
- const out = methodResponse(resp, "ContactCard/set", "0");
149
- const id = out.created?.c0?.id;
150
- if (!id) {
151
- const reason = out.notCreated?.c0;
152
- if (reason?.type === "invalidProperties" &&
153
- reason.properties?.includes("uid")) {
154
- throw new ContactUidTakenError();
155
- }
156
- throw new JmapError(200, `ContactCard/set did not create card: ${setErrorType(reason)}`, reason?.type);
157
- }
158
- return id;
159
- };
160
- export const contactCardUpdate = async (p) => {
161
- const resp = await jmapCall(p, {
162
- methodCalls: [
163
- [
164
- "ContactCard/set",
165
- { accountId: p.accountId, update: { [p.cardId]: p.patch } },
166
- "0",
167
- ],
168
- ],
169
- using: USING,
170
- });
171
- const out = methodResponse(resp, "ContactCard/set", "0");
172
- if (!(out.updated && p.cardId in out.updated)) {
173
- const reason = out.notUpdated?.[p.cardId];
174
- throw new JmapError(200, `ContactCard/set did not update ${p.cardId}: ${setErrorType(reason)}`, reason?.type);
175
- }
176
- };
177
- // Removes the card from every address book it is in — RFC 9610 has no
178
- // per-book unlink, so `destroy` is the only deletion primitive.
179
- export const contactCardDestroy = async (p) => {
180
- const resp = await jmapCall(p, {
181
- methodCalls: [
182
- ["ContactCard/set", { accountId: p.accountId, destroy: [p.cardId] }, "0"],
183
- ],
184
- using: USING,
185
- });
186
- const out = methodResponse(resp, "ContactCard/set", "0");
187
- if (!out.destroyed?.includes(p.cardId)) {
188
- const reason = out.notDestroyed?.[p.cardId];
189
- throw new JmapError(200, `ContactCard/set did not destroy ${p.cardId}: ${setErrorType(reason)}`, reason?.type);
190
- }
191
- };
192
- // One ContactCard/set carrying the kept card's update and the merged cards'
193
- // destroys, so a CardDAV client syncs the merge as one change rather than
194
- // seeing a half-merged book between two calls. RFC 8620 §5.3 has the server
195
- // apply the update before the destroys within the call.
196
- export const contactCardMerge = async (p) => {
197
- const resp = await jmapCall(p, {
198
- methodCalls: [
199
- [
200
- "ContactCard/set",
201
- {
202
- accountId: p.accountId,
203
- destroy: p.destroy,
204
- update: { [p.cardId]: p.patch },
205
- },
206
- "0",
207
- ],
208
- ],
209
- using: USING,
210
- });
211
- const out = methodResponse(resp, "ContactCard/set", "0");
212
- const updated = out.updated !== undefined &&
213
- out.updated !== null &&
214
- p.cardId in out.updated;
215
- const destroyed = out.destroyed ?? [];
216
- const notDestroyed = Object.fromEntries(p.destroy
217
- .filter((id) => !destroyed.includes(id))
218
- .map((id) => [id, setErrorType(out.notDestroyed?.[id])]));
219
- return {
220
- destroyed,
221
- notDestroyed,
222
- ...(updated
223
- ? {}
224
- : { updateError: setErrorType(out.notUpdated?.[p.cardId]) }),
225
- };
226
- };