compose_email.anubis 13.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
/*
 * 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 xlib/net_services_protocols/logger_service.anubis
transmit xlib/mail/tools.anubis
read xlib/mail/decode_mail.anubis
read xlib/mail/encode_mail.anubis
read xlib/mail/lexers/fqa.anubis
transmit xlib/mail/types/generated/email_address.anubis
transmit xlib/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)
      }
  }.
   

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
  (
    File_ref  file, 
  )=
  with data_file = if read_from_file(file.path +"/"+file.name) is 
                   {  
                     cannot_find_file then constant_byte_array(0,0),
                     read_error(data) then data
                     ok(data)         then data
                   },
  data_file_multipart(file.name, 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_ref)      files,
    String              boundary
  )=
  if files is
  {
    []            then "",
    [file_j . t]  then 
      "--" + boundary + crlf +
      data_file_multipart(file_j) + data_files_multipart(t, boundary)
  }
.

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
  make_alternative
  (
    String  email_id,
    String  text_part_content,
    String  html_part_content
  )=
  if length(text_part_content) > 0 & length(html_part_content) > 0 then
    //multipart alternative
    with boundary = "=_alternative_"+email_id+"_=",
    "Content-Type: multipart/alternative;" + crlf + 
    " boundary=\"" + boundary + "\"" + crlf + 
    crlf +
    "--" + boundary + crlf +
    text_part_content + crlf + 
    "--" + boundary + crlf +
    html_part_content + crlf + 
    "--" + boundary + "--"
  else if length(text_part_content) > 0 then
    text_part_content
  else 
    html_part_content
    
.


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, text_content, html_content, files, send_copy, reply_to) then
    with send_date = get_rfc822_date_format,
    with  email_id = to_ascii(sha1((from, send_date))),
         data_text = if length(text_content) > 0 then 
                      "Content-Type: text/plain; charset=UTF-8" + crlf +
                      "Content-Transfer-Encoding: 8bit" + crlf +
                      crlf +
                      encode_content(text_content, false)
                     else
                      "",
         data_html = if length(html_content) > 0 then 
                      "Content-Type: text/html; charset=UTF-8" + crlf +
                      "Content-Transfer-Encoding: 8bit" + crlf +
                      crlf +
                      encode_content(html_content, true)
                     else
                      "",
         body_text = make_alternative(email_id, data_text, data_html),
         
         is_multipart = if length(files) > 0 then true else false,
//                        is
//                        {
//                          []      then false,
//                          [h . t] then true
//                        },
    
    "Message-ID: " + email_id + crlf +
    "Date: " + send_date + crlf +
   // "From: " + user.login + "<" + user.login + "@" + domain + ">" + crlf +
    "User-Agent: Anubis XLib 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 = "=_mixed_"+email_id+"_=",
      "Content-Type: multipart/mixed;" + crlf + 
      " boundary=\"" + boundary + "\"" + crlf + 
      crlf +
      "This is a multi-part message in MIME format." + crlf +  
      "--" + boundary + crlf +
      body_text + crlf + 
      
      data_files_multipart(files, boundary) + 
      
      "--" + boundary + "--"
    else
      body_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
    message_header(mail_parts)
    //"get_message_header NOT YET Implemented."
  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
           }
        }
    }.