Files
exchange-mcp/client/ews_client.ts
T

485 lines
14 KiB
TypeScript

import { XMLParser } from "fast-xml-parser";
import https from "node:https";
import { createRequire } from "node:module";
import { promisify } from "node:util";
import { extractEmail, type Email } from "../utils/extract_email.ts";
import { extractFolders, type Folder } from "../utils/extract_folder.ts";
import { initDataDir } from "../utils/data.ts";
import {
loadConfig,
saveConfig,
clearConfig,
setPassword,
getPassword,
hasPassword,
type LoginConfig,
} from "../utils/login.ts";
const ntlm: any = createRequire(import.meta.url)("httpntlm");
const postAsync = promisify(ntlm.post.bind(ntlm));
const SSL_OP_LEGACY_SERVER_CONNECT = 0x00000004;
const AGENT = new https.Agent({
keepAlive: true,
rejectUnauthorized: false,
secureOptions: SSL_OP_LEGACY_SERVER_CONNECT,
});
const DISTINGUISHED_FOLDERS: Record<string, string> = {
inbox: "inbox",
"входящие": "inbox",
sent: "sentitems",
"отправленные": "sentitems",
drafts: "drafts",
"черновики": "drafts",
deleted: "deleteditems",
"удаленные": "deleteditems",
junk: "junkemail",
"нежелательная почта": "junkemail",
outbox: "outbox",
"исходящие": "outbox",
calendar: "calendar",
"календарь": "calendar",
};
function buildSoapEnvelope(body: string): string {
return `<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<s:Body>
${body}
</s:Body>
</s:Envelope>`;
}
const PARSER = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: "@_",
removeNSPrefix: true,
textNodeName: "#text",
});
export class EwsClient {
private config: LoginConfig;
private ewsUrl: string;
constructor(config: LoginConfig, password?: string) {
this.config = config;
this.ewsUrl = `${config.serverUrl.replace(/\/+$/, "")}/EWS/Exchange.asmx`;
if (password) setPassword(password);
}
private get domain(): string {
return this.config.domain ?? "corp";
}
private get password(): string {
const pw = getPassword();
if (!pw) throw new Error("Not logged in. Password not found in memory.");
return pw;
}
private async soapRequest(body: string, soapAction: string): Promise<any> {
const res = await postAsync({
url: this.ewsUrl,
username: this.config.username,
domain: this.domain,
password: this.password,
agent: AGENT,
headers: {
"Content-Type": "text/xml; charset=utf-8",
SOAPAction: soapAction,
},
body,
});
if (typeof res.body !== "string") {
throw new Error(`NTLM request failed, status=${res.statusCode}`);
}
return PARSER.parse(res.body);
}
private extractResponseMessages(data: any): any[] {
const body = data?.Envelope?.Body;
if (!body) return [];
const firstKey = Object.keys(body).find((k) => k.endsWith("Response"));
if (!firstKey) return [];
const rm = body[firstKey]?.ResponseMessages;
if (!rm) return [];
const msgKey = Object.keys(rm).find((k) => k.endsWith("ResponseMessage"));
if (!msgKey) return [];
const msgs = rm[msgKey];
return Array.isArray(msgs) ? msgs : [msgs];
}
async verifyConnection(): Promise<{ ok: boolean; error?: string }> {
try {
await this.getFolderId("inbox");
return { ok: true };
} catch (error: any) {
return { ok: false, error: error.message ?? String(error) };
}
}
async getFolderId(folderName: string): Promise<string> {
const lower = folderName.toLowerCase();
const distinguished = (DISTINGUISHED_FOLDERS as Record<string, string>)[lower];
if (distinguished) {
const soap = buildSoapEnvelope(`\
<m:GetFolder>
<m:FolderShape>
<t:BaseShape>IdOnly</t:BaseShape>
</m:FolderShape>
<m:FolderIds>
<t:DistinguishedFolderId Id="${distinguished}"/>
</m:FolderIds>
</m:GetFolder>`);
const data = await this.soapRequest(
soap,
"http://schemas.microsoft.com/exchange/services/2006/messages/GetFolder",
);
for (const msg of this.extractResponseMessages(data)) {
const folder = msg?.Folders?.Folder;
if (folder?.FolderId?.["@_Id"]) {
return folder.FolderId["@_Id"];
}
}
}
const soap = buildSoapEnvelope(`\
<m:FindFolder Traversal="Shallow">
<m:FolderShape>
<t:BaseShape>Default</t:BaseShape>
</m:FolderShape>
<m:ParentFolderIds>
<t:DistinguishedFolderId Id="msgfolderroot"/>
</m:ParentFolderIds>
<m:IndexedPageFolderView MaxEntriesReturned="200" Offset="0" BasePoint="Beginning"/>
</m:FindFolder>`);
const data = await this.soapRequest(
soap,
"http://schemas.microsoft.com/exchange/services/2006/messages/FindFolder",
);
for (const msg of this.extractResponseMessages(data)) {
const folders = msg?.RootFolder?.Folders?.Folder;
if (!folders) continue;
const list = Array.isArray(folders) ? folders : [folders];
for (const f of list) {
if (f.DisplayName?.toLowerCase() === lower) {
return f.FolderId?.["@_Id"] ?? "";
}
}
}
throw new Error(`Folder '${folderName}' not found`);
}
async findItems(
folderId: string,
options: {
limit?: number;
offset?: number;
baseShape?: string;
restriction?: string;
traversal?: string;
} = {},
): Promise<any[]> {
const {
limit = 10,
offset = 0,
baseShape = "AllProperties",
restriction = "",
traversal = "Shallow",
} = options;
const isDistinguished =
(DISTINGUISHED_FOLDERS as Record<string, string>)[folderId.toLowerCase()] !== undefined;
const folderIdXml = isDistinguished
? `<t:DistinguishedFolderId Id="${folderId}"/>`
: `<t:FolderId Id="${folderId}"/>`;
const soap = buildSoapEnvelope(`\
<m:FindItem Traversal="${traversal}">
<m:ItemShape>
<t:BaseShape>${baseShape}</t:BaseShape>
</m:ItemShape>
<m:IndexedPageItemView MaxEntriesReturned="${limit}" Offset="${offset}" BasePoint="Beginning"/>
<m:ParentFolderIds>
${folderIdXml}
</m:ParentFolderIds>
<m:SortOrder>
<t:FieldOrder Order="Descending">
<t:FieldURI FieldURI="item:DateTimeReceived"/>
</t:FieldOrder>
</m:SortOrder>
${restriction}
</m:FindItem>`);
const data = await this.soapRequest(
soap,
"http://schemas.microsoft.com/exchange/services/2006/messages/FindItem",
);
for (const msg of this.extractResponseMessages(data)) {
const items = msg?.RootFolder?.Items?.Message
?? msg?.RootFolder?.Items?.CalendarItem;
if (!items) continue;
return Array.isArray(items) ? items : [items];
}
return [];
}
async getItem(itemId: string): Promise<any> {
const soap = buildSoapEnvelope(`\
<m:GetItem>
<m:ItemShape>
<t:BaseShape>AllProperties</t:BaseShape>
<t:BodyType>HTML</t:BodyType>
</m:ItemShape>
<m:ItemIds>
<t:ItemId Id="${itemId}"/>
</m:ItemIds>
</m:GetItem>`);
const data = await this.soapRequest(
soap,
"http://schemas.microsoft.com/exchange/services/2006/messages/GetItem",
);
for (const msg of this.extractResponseMessages(data)) {
const items = msg?.Items;
if (!items) continue;
const itemKey = Object.keys(items).find((k) =>
[
"Message",
"CalendarItem",
"MeetingRequest",
"MeetingResponse",
"MeetingCancellation",
].includes(k)
);
if (itemKey) {
return items[itemKey];
}
}
throw new Error(`Item '${itemId}' not found`);
}
async findFolders(
parentFolderId: string,
recursive: boolean = false,
): Promise<Folder[]> {
return extractFolders(
(body, soapAction) => this.soapRequest(body, soapAction),
parentFolderId,
recursive,
);
}
extractEmail(item: any): Email {
return extractEmail(item);
}
async updateItems(
itemIds: string[],
updates: { fieldUri: string; value: string | boolean }[],
): Promise<any[]> {
const propName = (fieldUri: string): string => {
const parts = fieldUri.split(":");
return parts[parts.length - 1];
};
const changesXml = itemIds.map((id) => {
const updatesXml = updates.map((u) => {
const val = typeof u.value === "boolean"
? (u.value ? "true" : "false")
: u.value;
return `\
<t:SetItemField>
<t:FieldURI FieldURI="${u.fieldUri}"/>
<t:Message>
<t:${propName(u.fieldUri)}>${String(val)}</t:${
propName(u.fieldUri)
}>
</t:Message>
</t:SetItemField>`;
}).join("\n");
return `\
<t:ItemChange>
<t:ItemId Id="${id}"/>
<t:Updates>
${updatesXml}
</t:Updates>
</t:ItemChange>`;
}).join("\n");
const soap = buildSoapEnvelope(`\
<m:UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AutoResolve">
<m:ItemChanges>
${changesXml}
</m:ItemChanges>
</m:UpdateItem>`);
const data = await this.soapRequest(
soap,
"http://schemas.microsoft.com/exchange/services/2006/messages/UpdateItem",
);
return this.extractResponseMessages(data);
}
async getAttachment(
attachmentId: string,
): Promise<{ content: Buffer; name: string; contentType: string }> {
const soap = buildSoapEnvelope(`\
<m:GetAttachment>
<m:AttachmentIds>
<t:AttachmentId Id="${attachmentId}"/>
</m:AttachmentIds>
</m:GetAttachment>`);
const data = await this.soapRequest(
soap,
"http://schemas.microsoft.com/exchange/services/2006/messages/GetAttachment",
);
for (const msg of this.extractResponseMessages(data)) {
const attachments = msg?.Attachments?.FileAttachment;
if (!attachments) continue;
const list = Array.isArray(attachments) ? attachments : [attachments];
for (const a of list) {
const contentBase64 = a.Content ?? "";
const content = Buffer.from(contentBase64, "base64");
return {
content,
name: a.Name ?? "attachment",
contentType: a.ContentType ?? "application/octet-stream",
};
}
}
throw new Error(`Attachment '${attachmentId}' not found`);
}
async findCalendarItems(
folderId: string,
startDate: string,
endDate: string,
): Promise<any[]> {
const soap = buildSoapEnvelope(`\
<m:FindItem Traversal="Shallow">
<m:ItemShape>
<t:BaseShape>AllProperties</t:BaseShape>
<t:BodyType>HTML</t:BodyType>
</m:ItemShape>
<m:ParentFolderIds>
<t:FolderId Id="${folderId}"/>
</m:ParentFolderIds>
<m:CalendarView MaxEntriesReturned="200" StartDate="${startDate}" EndDate="${endDate}"/>
</m:FindItem>`);
const data = await this.soapRequest(
soap,
"http://schemas.microsoft.com/exchange/services/2006/messages/FindItem",
);
for (const msg of this.extractResponseMessages(data)) {
const items = msg?.RootFolder?.Items?.CalendarItem;
if (!items) continue;
return Array.isArray(items) ? items : [items];
}
return [];
}
async getUserAvailability(
emails: string[],
startDate: string,
endDate: string,
requestedView: string = "DetailedMerged",
): Promise<any> {
const mailboxDataXml = emails.map((email) =>
`\
<t:MailboxData>
<t:Email>
<t:Address>${
email.replace(/&/g, "&amp;").replace(/</g, "&lt;")
}</t:Address>
</t:Email>
<t:AttendeeType>Required</t:AttendeeType>
</t:MailboxData>`
).join("\n");
const soap = `<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<s:Body>
<m:GetUserAvailabilityRequest>
<m:MailboxDataArray>
${mailboxDataXml}
</m:MailboxDataArray>
<m:FreeBusyViewOptions>
<t:TimeWindow>
<t:StartTime>${startDate}T00:00:00</t:StartTime>
<t:EndTime>${endDate}T23:59:59</t:EndTime>
</t:TimeWindow>
<t:MergedFreeBusyIntervalInMinutes>30</t:MergedFreeBusyIntervalInMinutes>
<t:RequestedView>${requestedView}</t:RequestedView>
</m:FreeBusyViewOptions>
</m:GetUserAvailabilityRequest>
</s:Body>
</s:Envelope>`;
const data = await this.soapRequest(
soap,
"http://schemas.microsoft.com/exchange/services/2006/messages/GetUserAvailability",
);
return data;
}
async resolveNames(
query: string,
fullContact: boolean = true,
): Promise<any[]> {
const soap = buildSoapEnvelope(`\
<m:ResolveNames ReturnFullContactData="${fullContact}" SearchScope="ActiveDirectoryContacts">
<m:UnresolvedEntry>${
query.replace(/&/g, "&amp;").replace(/</g, "&lt;")
}</m:UnresolvedEntry>
</m:ResolveNames>`);
const data = await this.soapRequest(
soap,
"http://schemas.microsoft.com/exchange/services/2006/messages/ResolveNames",
);
for (const msg of this.extractResponseMessages(data)) {
const resolutions = msg?.ResolutionSet?.Resolution;
if (resolutions) {
return Array.isArray(resolutions) ? resolutions : [resolutions];
}
}
return [];
}
}