aiplang 2.0.0 → 2.1.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 (53) hide show
  1. package/bin/aiplang.js +7 -7
  2. package/package.json +7 -5
  3. package/server/node_modules/.package-lock.json +9 -0
  4. package/server/node_modules/nodemailer/.gitattributes +6 -0
  5. package/server/node_modules/nodemailer/.ncurc.js +9 -0
  6. package/server/node_modules/nodemailer/.prettierignore +8 -0
  7. package/server/node_modules/nodemailer/.prettierrc +12 -0
  8. package/server/node_modules/nodemailer/.prettierrc.js +10 -0
  9. package/server/node_modules/nodemailer/.release-please-config.json +9 -0
  10. package/server/node_modules/nodemailer/CHANGELOG.md +976 -0
  11. package/server/node_modules/nodemailer/CODE_OF_CONDUCT.md +76 -0
  12. package/server/node_modules/nodemailer/LICENSE +16 -0
  13. package/server/node_modules/nodemailer/README.md +86 -0
  14. package/server/node_modules/nodemailer/SECURITY.txt +22 -0
  15. package/server/node_modules/nodemailer/eslint.config.js +88 -0
  16. package/server/node_modules/nodemailer/lib/addressparser/index.js +382 -0
  17. package/server/node_modules/nodemailer/lib/base64/index.js +140 -0
  18. package/server/node_modules/nodemailer/lib/dkim/index.js +245 -0
  19. package/server/node_modules/nodemailer/lib/dkim/message-parser.js +154 -0
  20. package/server/node_modules/nodemailer/lib/dkim/relaxed-body.js +154 -0
  21. package/server/node_modules/nodemailer/lib/dkim/sign.js +116 -0
  22. package/server/node_modules/nodemailer/lib/errors.js +58 -0
  23. package/server/node_modules/nodemailer/lib/fetch/cookies.js +276 -0
  24. package/server/node_modules/nodemailer/lib/fetch/index.js +278 -0
  25. package/server/node_modules/nodemailer/lib/json-transport/index.js +82 -0
  26. package/server/node_modules/nodemailer/lib/mail-composer/index.js +599 -0
  27. package/server/node_modules/nodemailer/lib/mailer/index.js +446 -0
  28. package/server/node_modules/nodemailer/lib/mailer/mail-message.js +312 -0
  29. package/server/node_modules/nodemailer/lib/mime-funcs/index.js +610 -0
  30. package/server/node_modules/nodemailer/lib/mime-funcs/mime-types.js +2109 -0
  31. package/server/node_modules/nodemailer/lib/mime-node/index.js +1334 -0
  32. package/server/node_modules/nodemailer/lib/mime-node/last-newline.js +33 -0
  33. package/server/node_modules/nodemailer/lib/mime-node/le-unix.js +40 -0
  34. package/server/node_modules/nodemailer/lib/mime-node/le-windows.js +49 -0
  35. package/server/node_modules/nodemailer/lib/nodemailer.js +151 -0
  36. package/server/node_modules/nodemailer/lib/punycode/index.js +460 -0
  37. package/server/node_modules/nodemailer/lib/qp/index.js +230 -0
  38. package/server/node_modules/nodemailer/lib/sendmail-transport/index.js +205 -0
  39. package/server/node_modules/nodemailer/lib/ses-transport/index.js +223 -0
  40. package/server/node_modules/nodemailer/lib/shared/index.js +698 -0
  41. package/server/node_modules/nodemailer/lib/smtp-connection/data-stream.js +105 -0
  42. package/server/node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js +144 -0
  43. package/server/node_modules/nodemailer/lib/smtp-connection/index.js +1903 -0
  44. package/server/node_modules/nodemailer/lib/smtp-pool/index.js +641 -0
  45. package/server/node_modules/nodemailer/lib/smtp-pool/pool-resource.js +256 -0
  46. package/server/node_modules/nodemailer/lib/smtp-transport/index.js +402 -0
  47. package/server/node_modules/nodemailer/lib/stream-transport/index.js +135 -0
  48. package/server/node_modules/nodemailer/lib/well-known/index.js +47 -0
  49. package/server/node_modules/nodemailer/lib/well-known/services.json +619 -0
  50. package/server/node_modules/nodemailer/lib/xoauth2/index.js +436 -0
  51. package/server/node_modules/nodemailer/package.json +48 -0
  52. package/server/server.js +686 -865
  53. /package/{FLUX-PROJECT-KNOWLEDGE.md → aiplang-knowledge.md} +0 -0
@@ -0,0 +1,610 @@
1
+ /* eslint no-control-regex:0 */
2
+
3
+ 'use strict';
4
+
5
+ const base64 = require('../base64');
6
+ const qp = require('../qp');
7
+ const mimeTypes = require('./mime-types');
8
+
9
+ module.exports = {
10
+ /**
11
+ * Checks if a value is plaintext string (uses only printable 7bit chars)
12
+ *
13
+ * @param {String} value String to be tested
14
+ * @returns {Boolean} true if it is a plaintext string
15
+ */
16
+ isPlainText(value, isParam) {
17
+ const re = isParam ? /[\x00-\x08\x0b\x0c\x0e-\x1f"\u0080-\uFFFF]/ : /[\x00-\x08\x0b\x0c\x0e-\x1f\u0080-\uFFFF]/;
18
+ return typeof value === 'string' && !re.test(value);
19
+ },
20
+
21
+ /**
22
+ * Checks if a multi line string containes lines longer than the selected value.
23
+ *
24
+ * Useful when detecting if a mail message needs any processing at all –
25
+ * if only plaintext characters are used and lines are short, then there is
26
+ * no need to encode the values in any way. If the value is plaintext but has
27
+ * longer lines then allowed, then use format=flowed
28
+ *
29
+ * @param {Number} lineLength Max line length to check for
30
+ * @returns {Boolean} Returns true if there is at least one line longer than lineLength chars
31
+ */
32
+ hasLongerLines(str, lineLength) {
33
+ if (str.length > 128 * 1024) {
34
+ // do not test strings longer than 128kB
35
+ return true;
36
+ }
37
+ return new RegExp('^.{' + (lineLength + 1) + ',}', 'm').test(str);
38
+ },
39
+
40
+ /**
41
+ * Encodes a string or an Buffer to an UTF-8 MIME Word (rfc2047)
42
+ *
43
+ * @param {String|Buffer} data String to be encoded
44
+ * @param {String} mimeWordEncoding='Q' Encoding for the mime word, either Q or B
45
+ * @param {Number} [maxLength=0] If set, split mime words into several chunks if needed
46
+ * @return {String} Single or several mime words joined together
47
+ */
48
+ encodeWord(data, mimeWordEncoding, maxLength) {
49
+ mimeWordEncoding = (mimeWordEncoding || 'Q').toString().toUpperCase().trim().charAt(0);
50
+ maxLength = maxLength || 0;
51
+
52
+ let encodedStr;
53
+ const toCharset = 'UTF-8';
54
+
55
+ if (maxLength && maxLength > 7 + toCharset.length) {
56
+ maxLength -= 7 + toCharset.length;
57
+ }
58
+
59
+ if (mimeWordEncoding === 'Q') {
60
+ // https://tools.ietf.org/html/rfc2047#section-5 rule (3)
61
+ encodedStr = qp.encode(data).replace(/[^a-z0-9!*+\-/=]/gi, chr => {
62
+ const ord = chr.charCodeAt(0).toString(16).toUpperCase();
63
+ if (chr === ' ') {
64
+ return '_';
65
+ }
66
+ return '=' + (ord.length === 1 ? '0' + ord : ord);
67
+ });
68
+ } else if (mimeWordEncoding === 'B') {
69
+ encodedStr = typeof data === 'string' ? data : base64.encode(data);
70
+ maxLength = maxLength ? Math.max(3, ((maxLength - (maxLength % 4)) / 4) * 3) : 0;
71
+ }
72
+
73
+ if (maxLength && (mimeWordEncoding !== 'B' ? encodedStr : base64.encode(data)).length > maxLength) {
74
+ if (mimeWordEncoding === 'Q') {
75
+ encodedStr = this.splitMimeEncodedString(encodedStr, maxLength).join('?= =?' + toCharset + '?' + mimeWordEncoding + '?');
76
+ } else {
77
+ // RFC2047 6.3 (2) states that encoded-word must include an integral number of characters, so no chopping unicode sequences
78
+ const parts = [];
79
+ let lpart = '';
80
+ for (let i = 0, len = encodedStr.length; i < len; i++) {
81
+ let chr = encodedStr.charAt(i);
82
+
83
+ if (/[\ud83c\ud83d\ud83e]/.test(chr) && i < len - 1) {
84
+ // composite emoji byte, so add the next byte as well
85
+ chr += encodedStr.charAt(++i);
86
+ }
87
+
88
+ // check if we can add this character to the existing string
89
+ // without breaking byte length limit
90
+ if (Buffer.byteLength(lpart + chr) <= maxLength || i === 0) {
91
+ lpart += chr;
92
+ } else {
93
+ // we hit the length limit, so push the existing string and start over
94
+ parts.push(base64.encode(lpart));
95
+ lpart = chr;
96
+ }
97
+ }
98
+ if (lpart) {
99
+ parts.push(base64.encode(lpart));
100
+ }
101
+
102
+ if (parts.length > 1) {
103
+ encodedStr = parts.join('?= =?' + toCharset + '?' + mimeWordEncoding + '?');
104
+ } else {
105
+ encodedStr = parts.join('');
106
+ }
107
+ }
108
+ } else if (mimeWordEncoding === 'B') {
109
+ encodedStr = base64.encode(data);
110
+ }
111
+
112
+ return '=?' + toCharset + '?' + mimeWordEncoding + '?' + encodedStr + (encodedStr.substr(-2) === '?=' ? '' : '?=');
113
+ },
114
+
115
+ /**
116
+ * Finds word sequences with non ascii text and converts these to mime words
117
+ *
118
+ * @param {String} value String to be encoded
119
+ * @param {String} mimeWordEncoding='Q' Encoding for the mime word, either Q or B
120
+ * @param {Number} [maxLength=0] If set, split mime words into several chunks if needed
121
+ * @param {Boolean} [encodeAll=false] If true and the value needs encoding then encodes entire string, not just the smallest match
122
+ * @return {String} String with possible mime words
123
+ */
124
+ encodeWords(value, mimeWordEncoding, maxLength, encodeAll) {
125
+ maxLength = maxLength || 0;
126
+
127
+ // find first word with a non-printable ascii or special symbol in it
128
+ const firstMatch = value.match(/(?:^|\s)([^\s]*["\u0080-\uFFFF])/);
129
+ if (!firstMatch) {
130
+ return value;
131
+ }
132
+
133
+ if (encodeAll) {
134
+ // if it is requested to encode everything or the string contains something that resebles encoded word, then encode everything
135
+ return this.encodeWord(value, mimeWordEncoding, maxLength);
136
+ }
137
+
138
+ // find the last word with a non-printable ascii in it
139
+ const lastMatch = value.match(/(["\u0080-\uFFFF][^\s]*)[^"\u0080-\uFFFF]*$/);
140
+ if (!lastMatch) {
141
+ // should not happen
142
+ return value;
143
+ }
144
+
145
+ const startIndex =
146
+ firstMatch.index +
147
+ (
148
+ firstMatch[0].match(/[^\s]/) || {
149
+ index: 0
150
+ }
151
+ ).index;
152
+ const endIndex = lastMatch.index + (lastMatch[1] || '').length;
153
+
154
+ return (
155
+ (startIndex ? value.substr(0, startIndex) : '') +
156
+ this.encodeWord(value.substring(startIndex, endIndex), mimeWordEncoding || 'Q', maxLength) +
157
+ (endIndex < value.length ? value.substr(endIndex) : '')
158
+ );
159
+ },
160
+
161
+ /**
162
+ * Joins parsed header value together as 'value; param1=value1; param2=value2'
163
+ * PS: We are following RFC 822 for the list of special characters that we need to keep in quotes.
164
+ * Refer: https://www.w3.org/Protocols/rfc1341/4_Content-Type.html
165
+ * @param {Object} structured Parsed header value
166
+ * @return {String} joined header value
167
+ */
168
+ buildHeaderValue(structured) {
169
+ const paramsArray = [];
170
+
171
+ Object.keys(structured.params || {}).forEach(param => {
172
+ // filename might include unicode characters so it is a special case
173
+ // other values probably do not
174
+ const value = structured.params[param];
175
+ if (!this.isPlainText(value, true) || value.length >= 75) {
176
+ this.buildHeaderParam(param, value, 50).forEach(encodedParam => {
177
+ if (!/[\s"\\;:/=(),<>@[\]?]|^[-']|'$/.test(encodedParam.value) || encodedParam.key.substr(-1) === '*') {
178
+ paramsArray.push(encodedParam.key + '=' + encodedParam.value);
179
+ } else {
180
+ paramsArray.push(encodedParam.key + '=' + JSON.stringify(encodedParam.value));
181
+ }
182
+ });
183
+ } else if (/[\s'"\\;:/=(),<>@[\]?]|^-/.test(value)) {
184
+ paramsArray.push(param + '=' + JSON.stringify(value));
185
+ } else {
186
+ paramsArray.push(param + '=' + value);
187
+ }
188
+ });
189
+
190
+ return structured.value + (paramsArray.length ? '; ' + paramsArray.join('; ') : '');
191
+ },
192
+
193
+ /**
194
+ * Encodes a string or an Buffer to an UTF-8 Parameter Value Continuation encoding (rfc2231)
195
+ * Useful for splitting long parameter values.
196
+ *
197
+ * For example
198
+ * title="unicode string"
199
+ * becomes
200
+ * title*0*=utf-8''unicode
201
+ * title*1*=%20string
202
+ *
203
+ * @param {String|Buffer} data String to be encoded
204
+ * @param {Number} [maxLength=50] Max length for generated chunks
205
+ * @param {String} [fromCharset='UTF-8'] Source sharacter set
206
+ * @return {Array} A list of encoded keys and headers
207
+ */
208
+ buildHeaderParam(key, data, maxLength) {
209
+ const list = [];
210
+ let encodedStr = typeof data === 'string' ? data : (data || '').toString();
211
+ let chr, ord;
212
+ let line;
213
+ let startPos = 0;
214
+ let i, len;
215
+
216
+ maxLength = maxLength || 50;
217
+
218
+ // process ascii only text
219
+ if (this.isPlainText(data, true)) {
220
+ // check if conversion is even needed
221
+ if (encodedStr.length <= maxLength) {
222
+ return [
223
+ {
224
+ key,
225
+ value: encodedStr
226
+ }
227
+ ];
228
+ }
229
+
230
+ encodedStr = encodedStr.replace(new RegExp('.{' + maxLength + '}', 'g'), str => {
231
+ list.push({
232
+ line: str
233
+ });
234
+ return '';
235
+ });
236
+
237
+ if (encodedStr) {
238
+ list.push({
239
+ line: encodedStr
240
+ });
241
+ }
242
+ } else {
243
+ if (/[\uD800-\uDBFF]/.test(encodedStr)) {
244
+ // string containts surrogate pairs, so normalize it to an array of bytes
245
+ const encodedStrArr = [];
246
+ for (i = 0, len = encodedStr.length; i < len; i++) {
247
+ chr = encodedStr.charAt(i);
248
+ ord = chr.charCodeAt(0);
249
+ if (ord >= 0xd800 && ord <= 0xdbff && i < len - 1) {
250
+ chr += encodedStr.charAt(i + 1);
251
+ encodedStrArr.push(chr);
252
+ i++;
253
+ } else {
254
+ encodedStrArr.push(chr);
255
+ }
256
+ }
257
+ encodedStr = encodedStrArr;
258
+ }
259
+
260
+ // first line includes the charset and language info and needs to be encoded
261
+ // even if it does not contain any unicode characters
262
+ line = "utf-8''";
263
+ let encoded = true;
264
+ startPos = 0;
265
+
266
+ // process text with unicode or special chars
267
+ for (i = 0, len = encodedStr.length; i < len; i++) {
268
+ chr = encodedStr[i];
269
+
270
+ if (encoded) {
271
+ chr = this.safeEncodeURIComponent(chr);
272
+ } else {
273
+ // try to urlencode current char
274
+ chr = chr === ' ' ? chr : this.safeEncodeURIComponent(chr);
275
+ // By default it is not required to encode a line, the need
276
+ // only appears when the string contains unicode or special chars
277
+ // in this case we start processing the line over and encode all chars
278
+ if (chr !== encodedStr[i]) {
279
+ // Check if it is even possible to add the encoded char to the line
280
+ // If not, there is no reason to use this line, just push it to the list
281
+ // and start a new line with the char that needs encoding
282
+ if ((this.safeEncodeURIComponent(line) + chr).length >= maxLength) {
283
+ list.push({
284
+ line,
285
+ encoded
286
+ });
287
+ line = '';
288
+ startPos = i - 1;
289
+ } else {
290
+ encoded = true;
291
+ i = startPos;
292
+ line = '';
293
+ continue;
294
+ }
295
+ }
296
+ }
297
+
298
+ // if the line is already too long, push it to the list and start a new one
299
+ if ((line + chr).length >= maxLength) {
300
+ list.push({
301
+ line,
302
+ encoded
303
+ });
304
+ line = chr = encodedStr[i] === ' ' ? ' ' : this.safeEncodeURIComponent(encodedStr[i]);
305
+ if (chr === encodedStr[i]) {
306
+ encoded = false;
307
+ startPos = i - 1;
308
+ } else {
309
+ encoded = true;
310
+ }
311
+ } else {
312
+ line += chr;
313
+ }
314
+ }
315
+
316
+ if (line) {
317
+ list.push({
318
+ line,
319
+ encoded
320
+ });
321
+ }
322
+ }
323
+
324
+ return list.map((item, i) => ({
325
+ // encoded lines: {name}*{part}*
326
+ // unencoded lines: {name}*{part}
327
+ // if any line needs to be encoded then the first line (part==0) is always encoded
328
+ key: key + '*' + i + (item.encoded ? '*' : ''),
329
+ value: item.line
330
+ }));
331
+ },
332
+
333
+ /**
334
+ * Parses a header value with key=value arguments into a structured
335
+ * object.
336
+ *
337
+ * parseHeaderValue('content-type: text/plain; CHARSET='UTF-8'') ->
338
+ * {
339
+ * 'value': 'text/plain',
340
+ * 'params': {
341
+ * 'charset': 'UTF-8'
342
+ * }
343
+ * }
344
+ *
345
+ * @param {String} str Header value
346
+ * @return {Object} Header value as a parsed structure
347
+ */
348
+ parseHeaderValue(str) {
349
+ const response = {
350
+ value: false,
351
+ params: {}
352
+ };
353
+ let key = false;
354
+ let value = '';
355
+ let type = 'value';
356
+ let quote = false;
357
+ let escaped = false;
358
+ let chr;
359
+
360
+ for (let i = 0, len = str.length; i < len; i++) {
361
+ chr = str.charAt(i);
362
+ if (type === 'key') {
363
+ if (chr === '=') {
364
+ key = value.trim().toLowerCase();
365
+ type = 'value';
366
+ value = '';
367
+ continue;
368
+ }
369
+ value += chr;
370
+ } else {
371
+ if (escaped) {
372
+ value += chr;
373
+ } else if (chr === '\\') {
374
+ escaped = true;
375
+ continue;
376
+ } else if (quote && chr === quote) {
377
+ quote = false;
378
+ } else if (!quote && chr === '"') {
379
+ quote = chr;
380
+ } else if (!quote && chr === ';') {
381
+ if (key === false) {
382
+ response.value = value.trim();
383
+ } else {
384
+ response.params[key] = value.trim();
385
+ }
386
+ type = 'key';
387
+ value = '';
388
+ } else {
389
+ value += chr;
390
+ }
391
+ escaped = false;
392
+ }
393
+ }
394
+
395
+ if (type === 'value') {
396
+ if (key === false) {
397
+ response.value = value.trim();
398
+ } else {
399
+ response.params[key] = value.trim();
400
+ }
401
+ } else if (value.trim()) {
402
+ response.params[value.trim().toLowerCase()] = '';
403
+ }
404
+
405
+ // handle parameter value continuations
406
+ // https://tools.ietf.org/html/rfc2231#section-3
407
+
408
+ // preprocess values
409
+ Object.keys(response.params).forEach(key => {
410
+ let actualKey, nr, match, value;
411
+ if ((match = key.match(/(\*(\d+)|\*(\d+)\*|\*)$/))) {
412
+ actualKey = key.substr(0, match.index);
413
+ nr = Number(match[2] || match[3]) || 0;
414
+
415
+ if (!response.params[actualKey] || typeof response.params[actualKey] !== 'object') {
416
+ response.params[actualKey] = {
417
+ charset: false,
418
+ values: []
419
+ };
420
+ }
421
+
422
+ value = response.params[key];
423
+
424
+ if (nr === 0 && match[0].substr(-1) === '*' && (match = value.match(/^([^']*)'[^']*'(.*)$/))) {
425
+ response.params[actualKey].charset = match[1] || 'iso-8859-1';
426
+ value = match[2];
427
+ }
428
+
429
+ response.params[actualKey].values[nr] = value;
430
+
431
+ // remove the old reference
432
+ delete response.params[key];
433
+ }
434
+ });
435
+
436
+ // concatenate split rfc2231 strings and convert encoded strings to mime encoded words
437
+ Object.keys(response.params).forEach(key => {
438
+ let value;
439
+ if (response.params[key] && Array.isArray(response.params[key].values)) {
440
+ value = response.params[key].values.map(val => val || '').join('');
441
+
442
+ if (response.params[key].charset) {
443
+ // convert "%AB" to "=?charset?Q?=AB?="
444
+ response.params[key] =
445
+ '=?' +
446
+ response.params[key].charset +
447
+ '?Q?' +
448
+ value
449
+ // fix invalidly encoded chars
450
+ .replace(/[=?_\s]/g, s => {
451
+ const c = s.charCodeAt(0).toString(16);
452
+ if (s === ' ') {
453
+ return '_';
454
+ }
455
+ return '%' + (c.length < 2 ? '0' : '') + c;
456
+ })
457
+ // change from urlencoding to percent encoding
458
+ .replace(/%/g, '=') +
459
+ '?=';
460
+ } else {
461
+ response.params[key] = value;
462
+ }
463
+ }
464
+ });
465
+
466
+ return response;
467
+ },
468
+
469
+ /**
470
+ * Returns file extension for a content type string. If no suitable extensions
471
+ * are found, 'bin' is used as the default extension
472
+ *
473
+ * @param {String} mimeType Content type to be checked for
474
+ * @return {String} File extension
475
+ */
476
+ detectExtension: mimeType => mimeTypes.detectExtension(mimeType),
477
+
478
+ /**
479
+ * Returns content type for a file extension. If no suitable content types
480
+ * are found, 'application/octet-stream' is used as the default content type
481
+ *
482
+ * @param {String} extension Extension to be checked for
483
+ * @return {String} File extension
484
+ */
485
+ detectMimeType: extension => mimeTypes.detectMimeType(extension),
486
+
487
+ /**
488
+ * Folds long lines, useful for folding header lines (afterSpace=false) and
489
+ * flowed text (afterSpace=true)
490
+ *
491
+ * @param {String} str String to be folded
492
+ * @param {Number} [lineLength=76] Maximum length of a line
493
+ * @param {Boolean} afterSpace If true, leave a space in th end of a line
494
+ * @return {String} String with folded lines
495
+ */
496
+ foldLines(str, lineLength, afterSpace) {
497
+ str = (str || '').toString();
498
+ lineLength = lineLength || 76;
499
+
500
+ let pos = 0;
501
+ const len = str.length;
502
+ let result = '';
503
+ let line, match;
504
+
505
+ while (pos < len) {
506
+ line = str.substr(pos, lineLength);
507
+ if (line.length < lineLength) {
508
+ result += line;
509
+ break;
510
+ }
511
+ if ((match = line.match(/^[^\n\r]*(\r?\n|\r)/))) {
512
+ line = match[0];
513
+ result += line;
514
+ pos += line.length;
515
+ continue;
516
+ } else if ((match = line.match(/(\s+)[^\s]*$/)) && match[0].length - (afterSpace ? (match[1] || '').length : 0) < line.length) {
517
+ line = line.substr(0, line.length - (match[0].length - (afterSpace ? (match[1] || '').length : 0)));
518
+ } else if ((match = str.substr(pos + line.length).match(/^[^\s]+(\s*)/))) {
519
+ line = line + match[0].substr(0, match[0].length - (!afterSpace ? (match[1] || '').length : 0));
520
+ }
521
+
522
+ result += line;
523
+ pos += line.length;
524
+ if (pos < len) {
525
+ result += '\r\n';
526
+ }
527
+ }
528
+
529
+ return result;
530
+ },
531
+
532
+ /**
533
+ * Splits a mime encoded string. Needed for dividing mime words into smaller chunks
534
+ *
535
+ * @param {String} str Mime encoded string to be split up
536
+ * @param {Number} maxlen Maximum length of characters for one part (minimum 12)
537
+ * @return {Array} Split string
538
+ */
539
+ splitMimeEncodedString: (str, maxlen) => {
540
+ const lines = [];
541
+ let curLine, match, chr, done;
542
+
543
+ // require at least 12 symbols to fit possible 4 octet UTF-8 sequences
544
+ maxlen = Math.max(maxlen || 0, 12);
545
+
546
+ while (str.length) {
547
+ curLine = str.substr(0, maxlen);
548
+
549
+ // move incomplete escaped char back to main
550
+ if ((match = curLine.match(/[=][0-9A-F]?$/i))) {
551
+ curLine = curLine.substr(0, match.index);
552
+ }
553
+
554
+ done = false;
555
+ while (!done) {
556
+ done = true;
557
+ // check if not middle of a unicode char sequence
558
+ if ((match = str.substr(curLine.length).match(/^[=]([0-9A-F]{2})/i))) {
559
+ chr = parseInt(match[1], 16);
560
+ // invalid sequence, move one char back anc recheck
561
+ if (chr < 0xc2 && chr > 0x7f) {
562
+ curLine = curLine.substr(0, curLine.length - 3);
563
+ done = false;
564
+ }
565
+ }
566
+ }
567
+
568
+ if (curLine.length) {
569
+ lines.push(curLine);
570
+ }
571
+ str = str.substr(curLine.length);
572
+ }
573
+
574
+ return lines;
575
+ },
576
+
577
+ encodeURICharComponent: chr => {
578
+ let res = '';
579
+ let ord = chr.charCodeAt(0).toString(16).toUpperCase();
580
+
581
+ if (ord.length % 2) {
582
+ ord = '0' + ord;
583
+ }
584
+
585
+ if (ord.length > 2) {
586
+ for (let i = 0, len = ord.length / 2; i < len; i++) {
587
+ res += '%' + ord.substr(i, 2);
588
+ }
589
+ } else {
590
+ res += '%' + ord;
591
+ }
592
+
593
+ return res;
594
+ },
595
+
596
+ safeEncodeURIComponent(str) {
597
+ str = (str || '').toString();
598
+
599
+ try {
600
+ // might throw if we try to encode invalid sequences, eg. partial emoji
601
+ str = encodeURIComponent(str);
602
+ } catch (_E) {
603
+ // should never run
604
+ return str.replace(/[^\x00-\x1F *'()<>@,;:\\"[\]?=\u007F-\uFFFF]+/g, '');
605
+ }
606
+
607
+ // ensure chars that are not handled by encodeURICompent are converted as well
608
+ return str.replace(/[\x00-\x1F *'()<>@,;:\\"[\]?=\u007F-\uFFFF]/g, chr => this.encodeURICharComponent(chr));
609
+ }
610
+ };