-
Notifications
You must be signed in to change notification settings - Fork 0
/
Message.js
305 lines (265 loc) · 11 KB
/
Message.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
Message object.
Create and send a compiled message.
*/
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Soup = imports.gi.Soup;
const Settings = imports.object.Settings;
const time = imports.lib.time;
const Data = imports.object.Data;
const myTemplate = imports.object.Template;
const secret = imports.lib.secret;
const tpllib = imports.lib.template;
const Template = new myTemplate.Template();
const GObject = imports.gi.GObject;
const appData = new Data.Data();
const Config = new Settings.Settings();
var Message = GObject.registerClass( // eslint-disable-line
{
GTypeName: 'Message',
Signals: {
'Logger': {
param_types: [GObject.TYPE_STRING],
},
'Sent': {
param_types: [GObject.TYPE_BOOLEAN],
},
},
},
class Message extends GObject.Object {
_init () {
super._init();
// eslint-disable-next-line max-len
this.mixedBoundary = [...Array(16)].map(() => Math.random().toString(36)[2]).join('');
this.relatedBoundary = [...Array(16)].map(() => Math.random().toString(36)[2]).join('');
this.alternativeBoundary = [...Array(16)].map(() => Math.random().toString(36)[2]).join('');
}
// METHODS
async compile () {
return await Template.compile();
}
rule79 (str) {
return str.match(/.{1,72}/g).join("\n");
}
success (str) {
let res = 'ok';
const lines = str.split("\n");
lines.forEach(line => {
if (line[0] == '<' && line.substring(2,5) > 400) {
// < 250 2.0.0 Ok: queued
res = line;
}
});
return res;
}
sleep (milliseconds) {
const timeStart = new Date().getTime();
// eslint-disable-next-line
while (true) {
const elapsedTime = new Date().getTime() - timeStart;
if (elapsedTime > milliseconds) {
break;
}
}
}
sendAll () {
let res = [];
this.results = [];
this.App = Gio.Application.get_default();
this.curConn = Config.getConnection(appData.get('CONN'));
const delay = this.curConn.DELAY;
appData.get('MAILINGS').forEach((mailing) => {
// eslint-disable-next-line max-len
const mobj = this.build(mailing);
try {
res.push(this.send(mobj, mailing.to));
} catch (error) {
logError(error);
}
this.sleep(delay);
});
appData.set('SENT', time.now());
this.App.emit('Sent', true);
return res;
}
build (mailing) {
const inline = [];
const attach = [];
const blocks = [
{
type: 'plain',
name: 'text',
content: mailing.text,
parent: 'message'
},
{
type: 'html',
name: 'html',
content: mailing.html,
parent: 'message'
}
];
// const mObj = {};
let payload = tpllib.payload.replace('{{subject}}', appData.get('SUBJECT'));
payload = payload.replace('{{to}}', mailing.to);
payload = payload.replace('{{from}}', mailing.from);
payload = payload.replace('{{date}}', GLib.DateTime.new_now_utc().format('%d %b %Y %H:%M:%S %:z'));
payload = payload.replace(/{{mixedBoundary}}/g, this.mixedBoundary);
payload = payload.replace(/{{relatedBoundary}}/g, this.relatedBoundary);
payload = payload.replace(/{{alternativeBoundary}}/g, this.alternativeBoundary);
blocks.forEach(block => {
let str = tpllib.partBlock;
// {{boundary}}, {{contentType}}, {{contentExtra}}, {{content}}, {{dispositionHeader}}
// Content-Disposition: inline; filename="image001.png";
str = str.replace('{{boundary}}', this.alternativeBoundary);
str = str.replace('{{encoding}}', '7bit');
str = str.replace('{{contentId}}', '');
if (block.type == 'plain') {
str = str.replace('{{contentType}}', 'text/plain');
str = str.replace('{{contentExtra}}', 'charset="us-ascii"');
str = str.replace('{{content}}', block.content);
str = str.replace('{{dispositionHeader}}', '');
payload = payload.replace('{{partText}}', str);
}
if (block.type == 'html') {
str = str.replace('{{contentType}}', 'text/html');
str = str.replace('{{contentExtra}}', 'charset="us-ascii"');
str = str.replace('{{content}}', block.content);
str = str.replace('{{dispositionHeader}}', '');
payload = payload.replace('{{partHtml}}', str);
}
});
const attachments = appData.get('ATTACHMENTS');
attachments.forEach(attachment => {
let str = tpllib.partBlock;
// str = str.replace('{{contentType}}', attachment.type);
str = str.replace('{{contentExtra}}', `name="${attachment.fileName}";`);
str = str.replace('{{encoding}}', 'base64');
if (attachment.inline) {
str = str.replace('{{contentType}}', attachment.type);
str = str.replace('{{boundary}}', this.relatedBoundary);
str = str.replace('{{content}}', this.rule79(attachment.contents));
str = str.replace('{{contentId}}', `Content-ID: <${attachment.id}>`);
str = str.replace('{{dispositionHeader}}', `Content-Disposition: inline; filename="${attachment.fileName}";`);
inline.push(str);
} else {
str = str.replace('{{contentType}}', 'application/octet-stream');
str = str.replace('{{boundary}}', this.mixedBoundary);
const rid = Math.random().toString(36).slice(2, 7);
str = str.replace('{{contentId}}', `Content-ID: "${rid}"`);
str = str.replace('{{content}}', this.rule79(attachment.contents));
str = str.replace('{{dispositionHeader}}', `Content-Disposition: attachment; filename="${attachment.fileName}";`);
attach.push(str);
}
});
payload = payload.replace('{{partsInline}}', inline.join("\n\n"));
payload = payload.replace('{{partsAttachment}}', attach.join("\n\n"));
return payload;
}
preview () {
return true;
}
async soupSend(msgObj, to) {
this.App = Gio.Application.get_default();
// const ipv4 = Config.getIpv4();
let httpSession = new Soup.Session();
// httpSession.user_agent = 'blah'
let authUri = new Soup.URI(url);
authUri.set_user(this.handle);
authUri.set_password(this.token);
let message = new Soup.Message({method: 'GET', uri: authUri});
let authManager = new Soup.AuthManager();
let auth = new Soup.AuthBasic({host: 'api.github.com', realm: 'Github Api'});
Soup.Session.prototype.add_feature.call(httpSession, authManager);
httpSession.queue_message(message, function() {});
}
//
// https://stackoverflow.com/questions/47533683/writing-a-native-messaging-host-in-gjs
// https://www.mailjet.com/feature/smtp-relay/
// https://stackoverflow.com/questions/44728855/curl-send-html-email-with-embedded-image-and-attachment
//
async send (msgObj, to, cancellable = null) {
this.App = Gio.Application.get_default();
const ipv4 = Config.getIpv4();
const pass = secret.connPasswordGet(this.curConn.ID);
let flagStr = '-svk';
if (ipv4) {
flagStr = '-svk4';
}
const argv = ['curl',
flagStr,
// Option switches and values are separate args
'--mail-from', this.curConn.FROM,
'--url', this.curConn.HOST,
'--mail-rcpt', to,
'-T', '-',
'--user', `${this.curConn.USER}:${pass}`,
];
if (this.curConn.HOST.toLowerCase().includes('smtps')) {
argv.push('--ssl-reqd');
}
try {
const proc = new Gio.Subprocess({
argv,
flags: Gio.SubprocessFlags.STDIN_PIPE |
Gio.SubprocessFlags.STDOUT_PIPE |
Gio.SubprocessFlags.STDERR_MERGE,
});
// Classes that implement GInitable must be initialized before use,
// but you could use Gio.Subprocess.new(argv, flags)
// which will call this for you
proc.init(cancellable);
// We're going to wrap a GLib async function in a Promise so we can
// use it like a native JavaScript async function.
//
// You could alternatively return this Promise instead of awaiting it
// here, but that's up to you.
const stdout = await new Promise((resolve, reject) => {
// communicate_utf8() returns a string, communicate() returns a
// a GLib.Bytes and there are "headless" functions available as well
proc.communicate_utf8_async(
// This is your stdin, which can just be a JS string
msgObj,
// we've been passing this around from the function args; you
// can create a Gio.Cancellable and call `cancellable.cancel()`
// to stop the command or any other operation you've passed it
// to at any time, which will throw an
// "Operation Cancelled" error.
cancellable,
// This is the GAsyncReady callback, which works like any other
// callback, but we need to ensure we catch errors so we can
// propagate them with `reject()` to make the Promise work
// properly
(proc, res) => {
try {
// eslint-disable-next-line
let [ok, stdout, stderr] = proc.communicate_utf8_finish(res);
// Because we used the STDERR_MERGE flag stderr will be
// included in stdout. Obviously you could also call
// `resolve([stdout, stderr])` if you wanted to keep both
// and separate them.
//
// This won't affect whether the proc actually return non-
// zero causing the Promise to reject()
resolve(stdout);
} catch (e) {
this.App.emit('Logger', `>>> ERR >>> : ${e}`);
reject(e);
}
},
);
});
// log(`>>> RES >>> : ${stdout}`);
this.results.push([to, this.success(stdout)]);
this.App.emit('Logger', `>>> RES >>> : ${stdout}`);
return Date.now().toLocaleString();
} catch (e) {
// This could be any number of errors, but probably it will be a
// GError in which case it will have `code` property carrying a
// GIOErrorEnum you could use to programmatically respond to.
logError(e);
}
}
},
);