From 7b69bebb04a3ae3ab9b59da5c0944b64094c81d1 Mon Sep 17 00:00:00 2001 From: Daniel Rebelo de Oliveira Date: Wed, 29 Feb 2012 22:13:21 +0000 Subject: [PATCH] Added handle_STARTTLS (gets called when TLS Negotiation is complete). --- src/gen_smtp_server_session.erl | 4 ++-- src/smtp_server_example.erl | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/gen_smtp_server_session.erl b/src/gen_smtp_server_session.erl index 39d25074..efc7a28e 100644 --- a/src/gen_smtp_server_session.erl +++ b/src/gen_smtp_server_session.erl @@ -567,7 +567,7 @@ handle_request({<<"VRFY">>, Address}, #state{module= Module, socket = Socket, ca socket:send(Socket, "501 Syntax: VRFY username/address\r\n"), {ok, State} end; -handle_request({<<"STARTTLS">>, <<>>}, #state{socket = Socket, tls=false, extensions = Extensions, options = Options} = State) -> +handle_request({<<"STARTTLS">>, <<>>}, #state{socket = Socket, module = Module, tls=false, extensions = Extensions, callbackstate = OldCallbackState, options = Options} = State) -> case has_extension(Extensions, "STARTTLS") of {true, _} -> socket:send(Socket, "220 OK\r\n"), @@ -592,7 +592,7 @@ handle_request({<<"STARTTLS">>, <<>>}, #state{socket = Socket, tls=false, extens %io:format("SSL negotiation sucessful~n"), {ok, State#state{socket = NewSocket, envelope=undefined, authdata=undefined, waitingauth=false, readmessage=false, - tls=true}}; + tls=true, callbackstate = Module:handle_STARTTLS(OldCallbackState)}}; {error, Reason} -> io:format("SSL handshake failed : ~p~n", [Reason]), socket:send(Socket, "454 TLS negotiation failed\r\n"), diff --git a/src/smtp_server_example.erl b/src/smtp_server_example.erl index b1ddcb5b..95af9d7a 100644 --- a/src/smtp_server_example.erl +++ b/src/smtp_server_example.erl @@ -7,7 +7,7 @@ -export([init/4, handle_HELO/2, handle_EHLO/3, handle_MAIL/2, handle_MAIL_extension/2, handle_RCPT/2, handle_RCPT_extension/2, handle_DATA/4, handle_RSET/1, handle_VRFY/2, - handle_other/3, handle_AUTH/4, code_change/3, terminate/2]). + handle_other/3, handle_AUTH/4, handle_STARTTLS/1, code_change/3, terminate/2]). -define(RELAY, true). @@ -195,6 +195,13 @@ handle_AUTH('cram-md5', <<"username">>, {Digest, Seed}, State) -> handle_AUTH(_Type, _Username, _Password, _State) -> error. +%% this callback is OPTIONAL +%% it only gets called if you add STARTTLS to your ESMTP extensions +-spec handle_STARTTLS(#state{}) -> #state{}. +handle_STARTTLS(State) -> + io:format("TLS Started~n"), + State. + -spec code_change(OldVsn :: any(), State :: #state{}, Extra :: any()) -> {ok, #state{}}. code_change(_OldVsn, State, _Extra) -> {ok, State}.