Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

260 lines (239 sloc) 7.925 kb
% -*-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 }}
Jump to Line
Something went wrong with that request. Please try again.