-
Notifications
You must be signed in to change notification settings - Fork 17
/
implementation.js
208 lines (189 loc) · 6.93 KB
/
implementation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Author: John Bieling (john@thunderbird.net)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
var { ExtensionCommon } = ChromeUtils.importESModule(
"resource://gre/modules/ExtensionCommon.sys.mjs"
);
var { ExtensionUtils } = ChromeUtils.importESModule(
"resource://gre/modules/ExtensionUtils.sys.mjs"
);
var { ExtensionError } = ExtensionUtils;
ChromeUtils.defineESModuleGetters(this, {
AttachmentInfo: "resource:///modules/AttachmentInfo.sys.mjs",
});
Cu.importGlobalProperties(["File", "IOUtils", "PathUtils"]);
async function getRealFileForFile(file) {
let pathTempFile = await IOUtils.createUniqueFile(
PathUtils.tempDir,
file.name.replaceAll(/[/:*?\"<>|]/g, "_"),
0o600
);
let tempFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
tempFile.initWithPath(pathTempFile);
let extAppLauncher = Cc[
"@mozilla.org/uriloader/external-helper-app-service;1"
].getService(Ci.nsPIExternalAppLauncher);
extAppLauncher.deleteTemporaryFileOnExit(tempFile);
let buffer = await file.arrayBuffer();
await IOUtils.write(pathTempFile, new Uint8Array(buffer));
return tempFile;
}
var Attachment = class extends ExtensionCommon.ExtensionAPI {
getAPI(context) {
function getMessageWindow(tabId) {
// Get about:message from the tabId.
let { nativeTab } = context.extension.tabManager.get(tabId);
if (nativeTab instanceof Ci.nsIDOMWindow) {
return nativeTab.messageBrowser.contentWindow
} else if (nativeTab.mode && nativeTab.mode.name == "mail3PaneTab") {
return nativeTab.chromeBrowser.contentWindow.messageBrowser.contentWindow
} else if (nativeTab.mode && nativeTab.mode.name == "mailMessageTab") {
return nativeTab.chromeBrowser.contentWindow;
}
return null;
}
async function getAttachmentFromUrl(url) {
let channel = Services.io.newChannelFromURI(
Services.io.newURI(url),
null,
Services.scriptSecurityManager.getSystemPrincipal(),
null,
Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
Ci.nsIContentPolicy.TYPE_OTHER
);
let raw = await new Promise((resolve, reject) => {
let listener = Cc["@mozilla.org/network/stream-loader;1"].createInstance(
Ci.nsIStreamLoader
);
listener.init({
onStreamComplete(loader, context, status, resultLength, result) {
if (Components.isSuccessCode(status)) {
resolve(Uint8Array.from(result));
} else {
reject(
new ExtensionError(
`Failed to read attachment ${attachment.url} content: ${status}`
)
);
}
},
});
channel.asyncOpen(listener, null);
});
return raw;
}
/**
* Returns the currently displayed message in the given tab.
*
* @param {integer} tabId
* @returns {nsIMsgHdr} nsIMsgHdr
*/
function getDisplayedMessage(tabId) {
let { nativeTab } = context.extension.tabManager.get(tabId);
if (nativeTab instanceof Ci.nsIDOMWindow) {
if (nativeTab.messageBrowser) {
return nativeTab.messageBrowser.contentWindow.gMessage;
}
} else if (nativeTab.mode.name == "mail3PaneTab") {
let msgHdrs = nativeTab.chromeBrowser.contentWindow.gDBView.getSelectedMsgHdrs();
if (msgHdrs.length == 1) {
return msgHdrs[0];
}
} else if (nativeTab.mode.name == "mailMessageTab") {
return nativeTab.chromeBrowser.contentWindow.gMessage;
}
return null;
}
return {
Attachment: {
listAttachments: async function (tabId) {
let window = getMessageWindow(tabId);
if (!window) {
return
}
let attachments = [];
for (let attachmentInfo of window.currentAttachments) {
let attachment = {
contentType: attachmentInfo.contentType,
name: attachmentInfo.name,
partName: attachmentInfo.partID,
size: attachmentInfo.size,
}
attachments.push(attachment);
};
return attachments;
},
getAttachmentFile: async function (tabId, partName) {
let window = getMessageWindow(tabId);
if (!window) {
return
}
let attachmentInfo = window.currentAttachments.find(a => a.partID == partName);
if (!attachmentInfo) {
throw new ExtensionError(`Attachment with partName ${partName} not found`);
}
let bytes = await getAttachmentFromUrl(attachmentInfo.url);
return new File([bytes], attachmentInfo.name, { type: attachmentInfo.contentType });
},
addAttachments: async function (tabId, newAttachments) {
let window = getMessageWindow(tabId);
if (!window) {
return
}
let modified = false;
for (let attachment of newAttachments) {
let msgHdr = getDisplayedMessage(tabId);
if (!msgHdr) {
continue;
}
let realFile = await getRealFileForFile(attachment.file);
let url = `${Services.io.newFileURI(realFile).spec}?part=${attachment.partName}`;
let attachmentInfo = new AttachmentInfo({
contentType: attachment.contentType,
url,
name: attachment.name,
uri: msgHdr.folder.getUriForMsg(msgHdr),
isExternalAttachment: true,
message: msgHdr,
updateAttachmentsDisplayFn: window.updateAttachmentsDisplay,
});
window.currentAttachments.push(attachmentInfo);
modified = true;
}
if (!modified) {
return
}
await window.ClearAttachmentList();
window.gBuildAttachmentsForCurrentMsg = false;
await window.displayAttachmentsForExpandedView();
window.gBuildAttachmentsForCurrentMsg = true;
},
removeAttachments: async function (tabId, partNames) {
let window = getMessageWindow(tabId);
if (!window) {
return
}
let modified = false;
for (let index = window.currentAttachments.length; index > 0; index--) {
let idx = index - 1;
if (partNames.includes(window.currentAttachments[idx].partID)) {
window.currentAttachments.splice(idx);
modified = true;
}
}
if (!modified) {
return
}
await window.ClearAttachmentList();
window.gBuildAttachmentsForCurrentMsg = false;
await window.displayAttachmentsForExpandedView();
window.gBuildAttachmentsForCurrentMsg = true;
},
},
};
}
};