Skip to content
This repository
tag: v685
Fetching contributors…

Cannot retrieve contributors at this time

file 193 lines (177 sloc) 7.745 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
% -*-erlang-*-

%
% 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 server
-debugvar PROTOCOL_DEBUG
-protocol SMTP

-open Printf
-open Rcontent

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Les types %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-type email = { from : string ; dests : string list ; body : content }

-type state = {
  server_domain: string;
  server_port: int;
  hello_message: string;
  client_domain: string;
  callback: email -> int * string;
  verify: string -> int * string;
  expand: string -> (int * string) list;
  extended: bool
}

-type payload = unit
-include "libnet/rt_proto.proto"

-type runtime = {
    rt_plim : int;
    rt_dialog_name : string;
    rt_on_close : Scheduler.t -> unit;
    rt_proto : rt_proto;
}

{{
let rec msglst = function
| [] -> []
| [(c,s)] -> [Ns (c,s)]
| ((c,s)::rest) -> (ENs (c,s))::(msglst rest)
}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Messages envoyés/reçus %%
%% Different structure between server and client %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-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 DotDot = "..\r\n"
-define Crlf = "\r\n"
-define (Vrfy str) = "VRFY "~ str "\r\n"
-define (Expn str) = "EXPN "~ str "\r\n"
-define Noop = "NOOP\r\n"~
-define Rset = "RSET\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) = !".\r\n" !"..\r\n" str

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% L'automate %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+on_connection(state : state):
  {{ eprintf "on_connection\n"; Pervasives.flush stderr }}
  let str = {{ sprintf "%s %s" state.server_domain state.hello_message }}
  send(Ns (220,str));
  wait_for_hello(state)

wait_for_hello(state : state):
  receive
  | Ehlo domain -> send_greeting({{ { state with client_domain = domain; extended = true } }})
  | Helo domain -> send_greeting({{ { state with client_domain = domain; extended = false } }})
  | Vrfy str -> let resp = {{ state.verify str }} send(Ns resp); wait_for_hello(state)
  | Expn str -> let resp = {{ msglst (state.expand str) }} send_all(resp); wait_for_hello(state)
  | Noop -> send(Ns(250,"Ok")); wait_for_hello(state)
  | Rset -> send(Ns(250,"Ok")); wait_for_hello(state)
  | Quit -> send(Ns(221,"Bye")); -!-
  | RawInput _ -> send(Ns(500,"Command not recognised")); wait_for_hello(state)
  | _ -> send(Ns(502,"Command not implemented")); wait_for_hello(state)
  catch
  | exn -> error({{ sprintf "wait_for_hello: exn=%s" (Printexc.to_string exn) }}, state)

send_greeting(state : state):
  let str = {{ sprintf "Hello %s" state.client_domain }}
  send(Ns (250,str));
  wait_for_message(state, {{ { from=""; dests=[]; body=ContentNone } }})

wait_for_message(state : state, email : email):
  receive
  | From sender ->
    if {{ email.from <> "" }}
    then
      send(Ns(503,"Bad sequence of commands"));
      wait_for_message(state, email)
    else
      let email = {{ { email with from = sender } }}
      send(Ns (250,"Ok"));
      wait_for_message(state, email)
  | To recipient ->
    let email = {{ { email with dests = recipient::email.dests } }}
    send(Ns (250,"Ok"));
    wait_for_message(state, email)
  | Data ->
    send(Ns (354,"End data with <CR><LF>.<CR><LF>"));
    get_data(state, email, false)
  | Vrfy str -> let resp = {{ state.verify str }} send(Ns resp); wait_for_message(state,email)
  | Expn str -> let resp = {{ msglst (state.expand str) }} send_all(resp); wait_for_message(state,email)
  | Noop -> send(Ns(250,"Ok")); wait_for_message(state,email)
  | Rset -> send(Ns(250,"Ok")); wait_for_hello(state)
  | Quit -> send(Ns(221,"Bye")); -!-
  | RawInput _ -> send(Ns(500,"Command not recognised")); wait_for_hello(state)
  | _ -> send(Ns(502,"Command not implemented")); wait_for_hello(state)
  catch
  | exn -> error({{ sprintf "exn: %s" (Printexc.to_string exn) }}, state)

get_data(state : state, email : email, last_was_crlf : bool):
%% upto data = "\r\n.\r\n";
%% debug {{ eprintf "data='%s'\n" (String.escaped data) }}
%% let email = {{ { email with body = content_add (data^"\r\n") email.body } }}
%% process_email(state, email)
  receive
  | RawInput str ->
    debug {{ eprintf "get_data: RawInput='%s'\n" (String.escaped str); Pervasives.flush stderr }}
    let email = {{ { email with body = content_add str email.body } }}
    get_data(state, email, {{ String.is_suffix "\r\n" str }})
  | Crlf ->
    debug {{ eprintf "get_data: Crlf\n"; Pervasives.flush stderr }}
    let email = {{ { email with body = content_add "\r\n" email.body } }}
    get_data(state, email, true)
  | Dot ->
    debug {{ eprintf "get_data: Dot last_was_crlf:%b\n" last_was_crlf; Pervasives.flush stderr }}
    if {{ last_was_crlf }}
    then process_email(state, email)
    else
      let email = {{ { email with body = content_add ".\r\n" email.body } }}
      get_data(state, email, true)
  | DotDot ->
    debug {{ eprintf "get_data: DotDot\n"; Pervasives.flush stderr }}
    let email = {{ { email with body = content_add ".\r\n" email.body } }}
    get_data(state, email, true)
  | Vrfy str -> let resp = {{ state.verify str }} send(Ns resp); get_data(state,email,last_was_crlf)
  | Expn str -> let resp = {{ msglst (state.expand str) }} send_all(resp); get_data(state,email,last_was_crlf)
  | Noop -> send(Ns(250,"Ok")); get_data(state,email,last_was_crlf)
  | Rset -> send(Ns(250,"Ok")); wait_for_hello(state)
  | Quit -> send(Ns(221,"Bye")); -!-
  | _ -> send(Ns(502,"Command not implemented")); wait_for_hello(state)
  catch
  | exn -> error({{ sprintf "exn: %s" (Printexc.to_string exn) }}, state)

process_email(state:state, email:email):
  debug {{ eprintf "process_email\n"; Pervasives.flush stderr }}
  let reply = {{ Ns (state.callback email) }}
  send(reply);
  wait_for_quit(state)

wait_for_quit(state):
  receive
  | Vrfy str -> let resp = {{ state.verify str }} send(Ns resp); wait_for_quit(state)
  | Expn str -> let resp = {{ msglst (state.expand str) }} send_all(resp); wait_for_quit(state)
  | Noop -> send(Ns(250,"Ok")); wait_for_quit(state)
  | Rset -> send(Ns(250,"Ok")); wait_for_hello(state)
  | Quit -> send(Ns(221,"Bye")); -!-
  | RawInput _ -> send(Ns(500,"Command not recognised")); wait_for_hello(state)
  | _ -> send(Ns(502,"Command not implemented")); wait_for_hello(state)
  catch
  | exn -> error({{ sprintf "exn: %s" (Printexc.to_string exn) }}, state)

error(msg : string, _state : state):
  send(Ns(451,"Server error"));
  debug {{ eprintf "error %s\n" msg; Pervasives.flush stderr }}
  {{ Logger.error "Error: %s" msg }}
  -!-
Something went wrong with that request. Please try again.