Skip to content
This repository
tag: v1799
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 260 lines (239 sloc) 7.925 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
% -*-proto-*-

%
% Copyright © 2011 MLstate
%
% This file is part of OPA.
%
% OPA is free software: you can redistribute it and/or modify it under the
% terms of the GNU Affero General Public License, version 3, as published by
% the Free Software Foundation.
%
% OPA is distributed in the hope that it will be useful, but WITHOUT ANY
% WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
% FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
% more details.
%
% You should have received a copy of the GNU Affero General Public License
% along with OPA. If not, see <http://www.gnu.org/licenses/>.
%
-generate client
-debugvar PROTOCOL_DEBUG
-protocol SMTP
-open Printf

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Les types %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-type email = {
  from : string;
  dests : string list;
  body : string;
  auth : string;
  user : string;
  pass : string;
  dryrun : bool;
  sent : Buffer.t;
}

-type result =
  | Ok of string
  | Error of string
  | Error_MX
  | Delayed of int
  | Bad_Sender
  | Bad_Recipient

-type cont = result -> unit

-type imports = {
  log : int -> string -> unit ;
  elog : int -> string -> unit ;
  k : cont
}

-type payload = unit
-include "libnet/rt_proto.proto"
%-type rt_proto = {
% rt_block_size : int;
% rt_backtrace : bool;
% rt_server_write_timeout : Time.t;
%}

-type runtime = {
  rt_plim : int;
  rt_proto : rt_proto;
}

{{
let encode_plain user pass = String.base64encode(sprintf "\000%s\000%s" user pass)
}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Messages envoyés/reçus %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-define (AuthPlain userpass) = "AUTH PLAIN " userpass "\r\n"
-define (AuthLogin user) = "AUTH LOGIN " user "\r\n"
-define (Ehlo host) = "EHLO " host "\r\n"
-define (Helo host) = "HELO " host "\r\n"
-define (From str) = "MAIL FROM:<" str ">\r\n"
-define (To str) = "RCPT TO:<" str ">\r\n"
-define Data = "DATA\r\n"
-define Dot = ".\r\n"
-define EndData = "\r\n.\r\n"
-define Quit = "QUIT\r\n"
-define (ENs (num : int, str)) = num "-" str "\r\n"
-define (Ns (num : int, str)) = num " " str "\r\n"
-define RawInput str = str

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% L'automate %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ehlo(mail : email, domain, tools : imports):
  debug {{ Printexc.record_backtrace true }}
  debug {{ eprintf "smtpClientCore: from=%s to=%s\n" mail.from (String.concat ", " mail.dests) }}
  receive
  | ENs (220, _msg) ->
    debug {{ eprintf "ehlo received ENs: %d %s\n" 220 _msg }}
    ehlo(mail, domain, tools)
  | Ns (220, _msg) ->
    debug {{ eprintf "received Ns: %d %s\n" 220 _msg }}
    if {{ mail.auth = "plain" }}
    then
      auth_plain(mail, domain, tools)
    else
      send (Ehlo domain);
      from(mail, tools)
  | ENs (a, b) ->
    debug {{ eprintf "received ENs(error): %d %s\n" a b }}
    {{ tools.elog a b }}
    finish_error(tools)
  | Ns (a, b) ->
    debug {{ eprintf "received Ns(error): %d %s\n" a b }}
    {{ tools.log a b }}
    handle_error(tools, a, b)
  | err ->
    debug {{ eprintf "received err: %s\n" (string_of_msg err) }}
    error({{ string_of_msg err }}, tools)
  catch
  | exn ->
    {{ eprintf "SmtpClientCore.ehlo: exn=%s\n" (Printexc.to_string exn) }}
    debug {{ Printexc.print_backtrace stderr; Pervasives.flush stderr }}
    {{ tools.k Error_MX }}

auth_plain(mail, domain, tools):
  %debug {{ eprintf "auth plain: %s\n%!" (encode_plain mail.user mail.pass)}}
  send(AuthPlain (encode_plain mail.user mail.pass));
  receive
  | Ns (235, _msg) ->
    debug {{ eprintf "auth_plain received Ns: %d %s\n" 235 _msg }}
    send (Ehlo domain);
    from(mail, tools)
  | ENs (a, b) ->
    debug {{ eprintf "auth_plain received ENs(error): %d %s\n" a b }}
    {{ tools.elog a b }}
    finish_error(tools)
  | Ns (a, b) ->
    debug {{ eprintf "auth_plain received Ns(error): %d %s\n" a b }}
    {{ tools.log a b }}
    handle_error(tools, a, b)
  | err ->
    debug {{ eprintf "auth_plain received err: %s\n" (string_of_msg err) }}
    error({{ string_of_msg err }}, tools)
  catch
  | exn ->
    {{ eprintf "SmtpClientCore.auth_plain: exn=%s\n" (Printexc.to_string exn) }}
    debug {{ Printexc.print_backtrace stderr; Pervasives.flush stderr }}
    {{ tools.k Error_MX }}

finish_error(tools : imports):
  receive
  | ENs (code, _msg) ->
    debug {{ eprintf "finish_error received ENs: %d %s\n" code _msg }}
    {{ tools.elog code _msg }}
    finish_error(tools)
  | Ns (code, _msg) ->
    {{ tools.log code _msg }}
    handle_error(tools, code, _msg)
  | err ->
    debug {{ eprintf "received err: %s\n" (string_of_msg err) }}
    error({{ string_of_msg err }}, tools)
  catch
  | exn ->
    {{ eprintf "SmtpClientCore.finish_error: exn=%s\n" (Printexc.to_string exn) }}
    debug {{ Printexc.print_backtrace stderr; Pervasives.flush stderr }}
    {{ tools.k Error_MX }}

from(mail, tools):
  receive
  | ENs (250, _) -> from(mail, tools)
  | Ns (250, _) ->
      if {{ mail.dryrun }}
      then
        quit(mail, tools)
      else
        send (From mail.from);
        to(mail, tools)
  | ENs (a, b) ->
    debug {{ eprintf "from received ENs: %d %s\n" a b }}
    {{ tools.elog a b }} finish_error(tools)
  | Ns (a, b) -> {{ tools.log a b }} handle_error(tools, a, b)
  | err -> error({{ string_of_msg err }}, tools)
  catch
  | _ -> {{ tools.k Error_MX }}

to(mail, tools):
  receive
  | ENs (250, _) -> to(mail, tools)
  | Ns (250, _) ->
      if {{ List.is_empty mail.dests }} then
        send Data;
        data(mail, tools)
      else
        send (To (List.hd mail.dests));
        let new_mail = {{ { mail with dests = List.tl mail.dests } }}
        to(new_mail, tools)
  | ENs (a, b) ->
    debug {{ eprintf "to received ENs: %d %s\n" a b }}
    {{ tools.elog a b }} finish_error(tools)
  | Ns (a, b) -> {{ tools.log a b }} handle_error(tools, a, b)
  | err -> error({{ string_of_msg err }}, tools)
  catch
  | _ -> {{ tools.k Error_MX }}

data(mail, tools):
  receive
  | Ns (354, _) ->
      % TODO: more efficient than Str?
      let dot_stuff = {{ Str.global_replace (Str.regexp_string "\r\n.") "\r\n.." mail.body }}
      {{ Buffer.add_string mail.sent dot_stuff }}
      send (RawInput dot_stuff);
      enddata(mail, tools, {{ String.is_suffix "\r\n" dot_stuff }})
  | ENs (a, b) ->
    debug {{ eprintf "data received ENs: %d %s\n" a b }}
    {{ tools.elog a b }} handle_error(tools, a,b)
  | Ns (a, b) -> {{ tools.log a b }} handle_error(tools, a, b)
  | err -> error({{ string_of_msg err }}, tools)
  catch
  | _ -> {{ tools.k Error_MX }}

enddata(mail, tools, has_crlf):
  if {{ has_crlf }}
  then
    {{ Buffer.add_string mail.sent ".\r\n" }}
    send(Dot);
    wait_for_ack(mail, tools)
  else
    {{ Buffer.add_string mail.sent "\r\n.\r\n" }}
    send(EndData);
    wait_for_ack(mail, tools)

wait_for_ack(mail, tools):
  receive
  | Ns (250, _) ->
    quit(mail, tools)
  | ENs (a, b) ->
    debug {{ eprintf "wait_for_ack received ENs: %d %s\n" a b }}
    {{ tools.elog a b }} finish_error(tools)
  | Ns (a, b) ->
    {{ tools.log a b }} handle_error(tools, a, b)
  | err ->
    error({{ string_of_msg err }}, tools)
  catch
  | _ -> {{ tools.k Error_MX }}

quit(mail, tools):
  send Quit; -!- {{ tools.k (Ok (Buffer.contents mail.sent)) }}

error(_msg : string, tools : imports):
  debug {{ eprintf "error: %s\n" _msg; Pervasives.flush stderr }}
  -!-
  {{ Logger.error "Error: %s" _msg;
  tools.k (Error _msg) }}

handle_error(tools, code, err):
  debug {{ eprintf "handle_error: %s\n" err; Pervasives.flush stderr }}
  if {{ code = 450 }} then
      error(err, tools)
  else
      {{ tools.k Error_MX }}
Something went wrong with that request. Please try again.