compose_email.anubis
14.1 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 12/07/2017
* Time: 01:28
* © David RENÉ
*/
read cipher/base64.anubis
read tools/basis.anubis
read tools/findstring.anubis
read tools/streams.anubis
read system/string.anubis
read system/logger.anubis
read system/data_io.anubis
read web/mime.anubis
read calexium_lib/net_services_protocols/logger_service.anubis
read calexium_lib/mail/tools.anubis
read calexium_lib/mail/decode_mail.anubis
read calexium_lib/mail/encode_mail.anubis
read calexium_lib/mail/lexers/fqa.anubis
transmit calexium_lib/mail/types/generated/email_address.anubis
transmit calexium_lib/mail/types/generated/email_to_send.anubis
public type Mail_Structure:
mail_header_content(String header, String content).
public define Mail_Structure
no_data_mail =
mail_header_content("","").
//public type Email_Address:
email_address(String name, String address).
public type File_Attached :
attached_file(String filename). //, RStream filestream).
//public type Email_to_send :
data_to_send(
Email_Address from,
List(Email_Address) to,
List(Email_Address) cc,
List(Email_Address) bcc,
String subject,
String content,
List(File_Attached) files,
Bool send_copy, //send copy to sender by bcc (useful when the email is sent by an automat)
// String from,
String reply_to
).
define Bool
is_element
(
String element,
List(String) list
)=
if list is
{
[] then false,
[h . t] then
if h = element then
true
else
is_element(element, t)
}.
define Maybe(List($T2))
_do_to_list
(
List($T1) list,
$T1 -> Maybe($T2) to_do,
List($T2) new_list
)=
if list is
{
[] then success(new_list),
[h . t] then
with is_done = to_do(h),
if is_done is
{
failure then failure,
success(done) then
_do_to_list(t, to_do, [done . new_list])
}
}.
public define Maybe(List($T2))
do_to_list
(
List($T1) list,
$T1 -> Maybe($T2) to_do,
)=
with result = _do_to_list(list, to_do, []),
if result is
{
failure then failure,
success(new_list) then success(reverse(new_list))
}.
define Maybe($T)
last_element
(
List($T) list
)=
if list is
{
[] then failure,
[h . t] then
if t is
{
[] then success(h),
[h1 . t1] then last_element(t)
}
}.
define Maybe(String)
day_name
(
Int day_num
)=
if day_num = 1 then success("Mon")
else if day_num = 2 then success("Tue")
else if day_num = 3 then success("Wed")
else if day_num = 4 then success("Thu")
else if day_num = 5 then success("Fri")
else if day_num = 6 then success("Sat")
else if day_num = 7 then success("Sun")
else failure
.
define Maybe(String)
month_name
(
Int month_num
)=
if month_num = 1 then success("Jan")
else if month_num = 2 then success("Feb")
else if month_num = 3 then success("Mar")
else if month_num = 4 then success("Apr")
else if month_num = 5 then success("May")
else if month_num = 6 then success("Jun")
else if month_num = 7 then success("Jul")
else if month_num = 8 then success("Aug")
else if month_num = 9 then success("Sep")
else if month_num = 10 then success("Oct")
else if month_num = 11 then success("Nov")
else if month_num = 12 then success("Dec")
else failure
.
public define String
address_to_string
(
Email_Address mail
)=
(if mail.name = "" then "" else to_MIME_text("UTF-8", mail.name) + " ") +
"<" + mail.address + ">"
.
define String
get_mime_of_extensions
(
String ext,
List(MIME) mime_list
)=
if mime_list is
{
[] then "application/octet-stream",
[h . t] then
if h is mime(_,_, exts) then
if is_element(ext, exts) then
to_String(h)
else
get_mime_of_extensions(ext, t)
}.
define String
get_mime
(
String filename
)=
if last_element(split_by_token(filename, '.')) is
{
failure then "application/octet-stream",
success(ext)then get_mime_of_extensions("." + ext, known_mime_types)
}.
define String
limit_line_length
(
String source,
Int limit,
String so_far
) =
with l = length(source),
if l > limit then
if sub_string(source, 0, limit) is
{
failure then so_far, // impossible
success(s) then
if sub_string(source, limit, l - limit) is
{
failure then so_far + s, // impossible
success(new_source) then
limit_line_length(new_source, limit, so_far + s + crlf)
}
}
else
so_far + source.
public define String
limit_line_length
(
String source,
Int limit
) =
limit_line_length(source, limit, "").
public define String
data_file_multipart
(
String filename,
ByteArray data,
) =
with data_file = limit_line_length(fast_base64_encode(data, false), 76),
"Content-Type: " + get_mime(filename) + ";" + crlf +
" name=\"" + filename + "\"" + crlf +
//"Content-ID :" + crlf +
"Content-Transfer-Encoding: base64" + crlf +
"Content-Disposition: attachment;" + crlf +
" filename=\"" + filename + "\"" + crlf +
crlf +
data_file + crlf
.
define String
data_file_multipart
(
String attached_file,
)=
if (Maybe(RStream))file(attached_file, read) is
{
failure then
data_file_multipart(attached_file, constant_byte_array(0,0))
success(fd) then
with data_file = if reliable_read(fd, file_size(attached_file), 100) is
{
failure then constant_byte_array(0,0),
success(data) then data
},
data_file_multipart(attached_file, data_file)
}
.
if file(fullpath, read) is
{
failure then
logError(debug_log, "Can't open mail '" + file_name + "'.");failure
success(f) then success(f)
}
define String
data_files_multipart
(
//List(File_Attached) files,
List(String) files,
String boundary
)=
if files is
{
[] then "",
[file_j . t] then
"--" + boundary + crlf +
data_file_multipart(file_j) + data_files_multipart(t, boundary)
}.
public define String
str_utime_send_mail
(
UTime t
) =
with date_send = convert_time(t.seconds),
if day_name(date_send.week_day) is
{
failure then "",
success(the_day_name) then the_day_name + ", "
}+
date_send.day + " " +
if month_name(date_send.month) is
{
failure then to_decimal(date_send.month),
success(the_month_name) then the_month_name
}+ " " +
date_send.year + " " +
zero_pad_n(2, date_send.hour) + ":" + zero_pad_n(2, date_send.minute) + ":" + zero_pad_n(2, date_send.second)
//+ " +0000"
.
public define String
get_rfc822_date_format
=
str_utime_send_mail(unow).
if capture_shell_command("date", ["-R"]) is
{
failure then str_utime_send_mail(unow),
success(s) then
if split_lines(s) is [h . t] then h else str_utime_send_mail(unow)
}.
define Int
_next_ws_pos
(
List(Word8) line,
Int pos
) =
if line is
{
[] then pos,
[h . t] then
if h = 32 | h = 9 then pos
else _next_ws_pos(t, pos + 1)
}.
define String
encode_plain_text_content
(
List(String) lines,
List(String) output,
) =
if lines is
{
[] then join(crlf, reverse(output)),
[line . t] then
with l = length(line),
pos = 75 - _next_ws_pos(reverse(explode(force(sub_string(line, 0, 75), line))), 1),
if l > 76 then
if sub_string(line, 0, pos) is
{
failure then
(if pos < 0 then unique else println("sub_string 1 failure into encode_plain_text_content()"));
encode_plain_text_content(t, [line . output]),
success(l1) then
if sub_string(line, pos + 1, l - pos - 1) is
{
failure then
println("sub_string 2 failure into encode_plain_text_content()");
encode_plain_text_content(t, [line . output]),
success(l2) then
encode_plain_text_content([l2 . t], [l1 + " " . output])
}
}
else
encode_plain_text_content(t, [line . output])
}.
public define String
encode_content
(
String content,
Bool is_html,
) =
if is_html then
join(crlf, split_lines(content)) + crlf
else
encode_plain_text_content(split_lines(content), []) + crlf.
public define String
compose_mail
(
Email_to_send send
) =
//with header_to_name = "To: ",
if send is data_to_send(from, to_list, cc_list, bcc_list, subject, content, files, send_copy, reply_to) then
with send_date = get_rfc822_date_format,
text_type = if find("<html",content,0) is
{
failure then "plain",
success(_) then "html"
},
data_text = "Content-Type: text/" + text_type + "; charset=UTF-8" + crlf +
"Content-Transfer-Encoding: 8bit" + crlf +
crlf +
encode_content(content, text_type = "html"),
is_multipart = if files is
{
[] then false,
[h . t] then true
},
"Message-ID: " + to_ascii(sha1((from, send_date))) + crlf +
"Date: " + send_date + crlf +
// "From: " + user.login + "<" + user.login + "@" + domain + ">" + crlf +
"User-Agent: Calexium lib email sender" + crlf +
"From: " + "<" + from.address + ">" + crlf +
(if reply_to = "" then
""
else
"Reply-To: <" + reply_to +">" + crlf
)+
"MIME-Version: 1.0" + crlf +
//header_to_name + folding(to, 68 - length(header_to_name)) + crlf +
"To: " + folding(to_list, address_to_string) + crlf +
(
// with header_cc_name = "Cc: ",
// header_cc_name + folding(cc_list, 68 - length(header_cc_name)) + crlf
if cc_list is [] then ""
else "Cc: " + folding(cc_list, address_to_string) + crlf
) +
(if subject = "" then
""
else
"Subject: " + to_folded_base64_text("UTF-8", subject) + crlf
) +
if is_multipart then
with boundary = to_ascii(sha1((from, send_date))),
"Content-Type: multipart/mixed;" + crlf +
" boundary=\"" + boundary + "\"" + crlf +
crlf +
"This is a multi-part message in MIME format." + crlf +
"--" + boundary + crlf +
data_text + crlf +
data_files_multipart(files, boundary) +
"--" + boundary + "--"
else
data_text
.
define String
message_header
(
Data_IO d_io
) =
if (Maybe(String))read_line(d_io) is
{
failure then "",
success(line) then
if length(line) = 0 then
""
else
line + message_header(d_io)
}.
public define String
get_message_header
(
String file_name
)=
//open the message file from the drive
if (Maybe(RStream))file(file_name, read) is
{
failure then println("get_message_header: can't open mail ["+file_name+"] from disk."); "", //logError(debug_log, "get_message_header: can't open mail ["+file_name+"] from disk."); "",
success(source) then message_header(make_data_io(source))
}.
public define String
get_message_header
(
List(Data_IO) mail_parts
)=
//open the message file from the drive
if rewind(mail_parts) then
"get_message_header NOT YET Implemented. Please report that error to Calexium"
else
"".
public define Mail_Structure
separate_data_mail
(
String mail
)=
if find(crlfcrlf, mail, 0) is
{
failure then mail_header_content("", mail),
success(position) then mail_header_content(
if sub_string(mmake_directoriesail, 0, position) is
{
failure then "",
success(substring) then substring
},
if sub_string(mail, position + 4 , length(mail) - (position + 4)) is
{
failure then mail,
success(substring) then substring
}
)
}.
public define List(String)
list_of_mails
(
String string_list,
List(String) mails_list
)=
if find(",", string_list, 0) is
{
failure then [string_list . mails_list],
success(separator) then
if sub_string(string_list, 0, separator) is
{
failure then [],
success(mail) then
if sub_string(string_list, separator + 1, length(string_list) - (separator + 1)) is
{
failure then [],
success(rest) then list_of_mails(rest, [mail . mails_list])
}
}
}
.
public define Maybe(Email_Address)
parse_address
(
String mail,
)=
if find("<", mail, 0) is
{
failure then
with result_mail = trim(mail),
if test_fqa(result_mail) then
success(email_address("", result_mail))
else
failure,
success(separator_b) then
if find(">", mail, separator_b + 1) is
{
failure then
failure,
success(separator_e) then
with name_string = sub_string(mail, 0, separator_b),
address_string = sub_string(mail, separator_b + 1, separator_e - separator_b - 1),
if address_string is
{
failure then failure,
success(add_str) then
with name = if name_string is
{
failure then "",
success(name_str) then trim(name_str)
},
if test_fqa(add_str) then
success(email_address(name, add_str))
else
failure
}
}
}.