-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
154 lines (139 loc) · 4.67 KB
/
main.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
// https://developers.google.com/apps-script/reference/gmail
// RFC2822 Date and Time Format
// https://tools.ietf.org/html/rfc2822#page-14
const KEYWORDS = ['パスワード', 'password'];
const MAX_MESSAGE_LENGTH = 100;
const ZIP_CONTENT_TYPES = new Set([
'application/zip',
'application/x-zip-compressed',
'application/octet-stream',
]);
function toEpochTime(date) {
return Number(date) / 1000;
}
function extractDatePart(headerValue) {
if (!headerValue) return null;
const match = headerValue.match(/;([^;]+)$/);
if (!match) return null;
return match[1];
}
function truncate(message) {
if (message.length > MAX_MESSAGE_LENGTH) {
return `${message.slice(0, MAX_MESSAGE_LENGTH)}...`;
}
return message;
}
function getData(messageId) {
const defaultData = {
currentMessageBody: null,
currentMessageHasZip: false,
currentMessageHasAttachments: false,
passwordMessageBody: null,
query: null,
};
const currentMessage = GmailApp.getMessageById(messageId);
if (!currentMessage) {
return defaultData;
}
const currentMessageBody = currentMessage.getPlainBody();
const currentMessageHasZip =
currentMessage.getAttachments().findIndex((a) => ZIP_CONTENT_TYPES.has(a.getContentType())) !==
-1;
if (!currentMessageHasZip) {
return {
...defaultData,
currentMessageBody,
};
}
const dateReceived = new Date(
extractDatePart(
currentMessage.getHeader('X-Received') || currentMessage.getHeader('Received'),
) || currentMessage.getHeader('Date'),
);
const currentMessageEpochTime = toEpochTime(dateReceived);
const startTime = currentMessageEpochTime - 60 * 5;
const endTime = currentMessageEpochTime + 60 * 5;
const query = `${KEYWORDS.join(' OR ')} after:${startTime} before:${endTime}`;
const threads = GmailApp.search(query, 0, 1);
const messages = threads[0] && threads[0].getMessages();
const passwordMessageBody = messages && messages[0].getPlainBody();
return {
...defaultData,
passwordMessageBody,
currentMessageBody,
currentMessageHasZip,
query,
};
}
function renderNoMessage() {
return CardService.newTextParagraph().setText('メッセージをどれか一つ選択してください。');
}
function renderCurrentMessage({ currentMessageBody }) {
return CardService.newKeyValue()
.setTopLabel('選択されたメッセージ')
.setContent(truncate(currentMessageBody));
}
function renderPasswordMessage({ currentMessageHasZip, passwordMessageBody, query }) {
if (!currentMessageHasZip) {
return CardService.newKeyValue()
.setTopLabel('Zip ファイルなし')
.setContent('パスワード付き zip ファイルは添付されていません。')
.setMultiline(true);
}
if (passwordMessageBody == null) {
return CardService.newKeyValue()
.setTopLabel('パスワード通知メールなし')
.setContent(`添付ファイルのパスワードは見つかりませんでした。<br><br>検索クエリ: ${query}`)
.setMultiline(true);
}
return CardService.newKeyValue()
.setTopLabel('🔑パスワード通知メールあり')
.setContent(passwordMessageBody)
.setMultiline(true);
}
function renderMessageCard({
passwordMessageBody,
currentMessageBody,
currentMessageHasZip,
query,
}) {
const section = CardService.newCardSection();
if (currentMessageBody == null) {
section.addWidget(renderNoMessage());
} else {
section.addWidget(renderCurrentMessage({ currentMessageBody })).addWidget(
renderPasswordMessage({
currentMessageHasZip,
passwordMessageBody,
query,
}),
);
}
const card = CardService.newCardBuilder().addSection(section);
return card.build();
}
function renderNotSupported() {
const section = CardService.newCardSection();
const paragraph = CardService.newTextParagraph();
paragraph.setText('Password Zip アドオンは Gmail のブラウザ版でのみ使用することができます。');
section.addWidget(paragraph);
const card = CardService.newCardBuilder().addSection(section);
return card.build();
}
// eslint-disable-next-line no-unused-vars
function onGmailMessage(e) {
if (!e.gmail) {
return renderNotSupported();
}
const messageId = e.gmail.messageId;
return renderMessageCard(getData(messageId));
}
// eslint-disable-next-line no-unused-vars
function onHomepage() {
const textParagraph = CardService.newTextParagraph().setText(
'Password Zip アドオンへようこそ。使用するには Gmail のメッセージを選択してください。',
);
const section = CardService.newCardSection().addWidget(textParagraph);
const card = CardService.newCardBuilder().addSection(section);
return card.build();
}