send_mail_type.anubis 6.34 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ 
 * Date: 16/05/2015
 * Time: 22:50
 * © David RENÉ
 */

read system/string.anubis
read system/data_io.anubis
read tools/basis.anubis
read web/mime.anubis
read xlib/mail/smtp_client.anubis
transmit xlib/mail/lexers/fqa.anubis
transmit types/generated/email_address.anubis
transmit types/generated/email_to_send.anubis

public type Str_HostName: 
  str_host_name(String str).

public type Str_Sender: 
  str_sender(String str).
  
public type Str_Recipient:
  str_recipient(String str).

public type Str_UID:
  str_UID(String str).

public type Send_Tries:
  send_tries(Int number).
  
public type Send_Status:
  ready,                              //ready to send 
  finished,                           //the mail is delivered correctly
  timeout,                            //the mail was not sent in enough time, this is a permanent error
  general_error,
  error(Int code, String enhanced_status, String text),     //this is SMTP permanent error  5xx, we can't deliver the mail
  warning(Int code, String enhanced_status, String text),   //this is SMTP temporary error  4xx, we can retry with same mail later
  in_progress.                        //We are in sending process, it's like a lock

public define String
  to_String
  (
    Send_Status snd_st
  )=
  if snd_st is
  {
     ready         then "READY",
     finished      then "FINISHED",
     timeout       then "TIMEOUT",
     general_error then "GENERAL_ERROR",
     error(_,_,_)  then "ERROR",
     warning(_,_,_)then "WARNING",
     in_progress   then "IN_PROGRESS"  
  }.

public define (String, List((SQLite3Bind)))
  to_SQL_query_binds
  (
    Send_Status snd_st
  )=
  with     sql_query = "send_status = :status ",
       sql_query_ext = ", smtp_r_code = :smtp_r_code, smtp_r_enhanced_code = :smtp_r_enhanced_code, smtp_r_code_text = :smtp_r_code_text",
               binds = (List(SQLite3Bind)) [ bind_String(":status",      to_String(snd_st))],

  if snd_st is
  {
     ready         then (sql_query, binds),
     finished      then (sql_query, binds),
     timeout       then (sql_query, binds),
     general_error then (sql_query, binds),
     error(code,enhanced,text)  then  (sql_query + sql_query_ext,                
                                       binds + 
                                       [ bind_Int(":smtp_r_code", code),
                                         bind_String(":smtp_r_enhanced_code", enhanced),
                                         bind_String(":smtp_r_code_text", text)]),
     warning(code,enhanced,text)  then  (sql_query + sql_query_ext,                
                                       binds + 
                                       [ bind_Int(":smtp_r_code", code),
                                         bind_String(":smtp_r_enhanced_code", enhanced),
                                         bind_String(":smtp_r_code_text", text)]),
     in_progress   then (sql_query, binds)  
  }.  
  


                                     bind_Int(":smtp_r_code", code),
                                     bind_String(":smtp_r_enhanced_code", enhanced),
                                     binf_String(":smtp_r_code_text", text),
                                   ],
                                   
public define Send_Status 
  to_Send_Status
  (
    String  status,
    Int     code,
    String  enhanced_status,
    String  message,
  )=
  if status = "READY"               then ready
  else if status = "FINISHED"       then finished
  else if status = "TIMEOUT"        then timeout
  else if status = "GENERAL_ERROR"  then general_error
  else if status = "ERROR"          then error(code, enhanced_status, message)
  else if status = "WARNING"        then warning(code, enhanced_status, message)
  else if status = "IN_PROGRESS"    then in_progress
  else ready.
  
public define String
  to_image
  (
    Send_Status  status,
  )=
  if status is
  {
     ready          then "/icons/16x16/status_ready.png",
     finished       then "/icons/16x16/status_valid.png",
     timeout        then "/icons/16x16/status_sandglass.png",
     general_error  then "/icons/16x16/status_error.png",
     error(_,_,_)   then "/icons/16x16/status_error.png",
     warning(_,_,_) then "/icons/16x16/status_warning.png",
     in_progress    then "/icons/16x16/status_processing_16x16.gif"  
  }.
           
public define Send_Status
  to_Send_Status
  (
    Result(SmtpClientResult, One) result
  )=
  if result is
  {
    error(smtp_result) then
      if smtp_result is
      {
        error   then warning(440, "", "Communication error"),
        timeout then warning(441, "", "Communication timeout"),
        no_auth_method then error(500, "", "Can't find any suitable authentication method"),
        bad_reply then error(500, "", "Bad reply from server"),
        reply(code, enhanced_st, lines)  then 
          with join_lines = join(implode([13,10]),lines),
          if code >= 400 & code < 500 then
            warning(code, enhanced_st, join_lines)
          else
            error(code, enhanced_st, join_lines)
      },
    ok(_) then
      finished
  }.
  
  /***** Email_Address utilities *****/

public define Email_Address
  email_address
  (
    String  address_only
  )=
  email_address("", address_only)
.

public define String
  to_String
  (
    Email_Address email_addr
  )=
  with  string_name =
        if email_addr.name = "" then 
          ""
        else
          to_MIME_text("UTF-8", email_addr.name) + " ",
          
   string_name + "<" + email_addr.address + ">"
.

public define Email_Address
  to_Email_Address
  (
    String  header_address  //email header in decoded version
  )=
  if split_by_token(header_address, '<') is 
  {
    []            then email_address("",""),
    [first . t]   then
      if t is 
      {
        []            then email_address("",    trim_tokens(first,  ['<','>',' '])),
        [second . _]  then email_address(first, trim_tokens(second, ['<','>',' ']))
      }
  }
.

public define String
  String s + Email_Address email_addr =
  s + to_String(email_addr)
.

public define String
  Email_Address email_addr +String s =
  to_String(email_addr) + s
.

public define Maybe((String, String))
  extract_user_domain
  (
    Email_Address email_addr
  )=
  extract_user_domain(email_addr.address)
.

public define Maybe(String)
  extract_domain_name
  (
    Email_Address email_addr
  )=
  extract_domain_name(email_addr.address)
.