Permalink
Browse files

[cleanup] stdlib, mail: partial revert 3ed1990, keep only smtp client

  • Loading branch information...
1 parent cb5d7ef commit b62865707e4eead527a5a2a096ef3670f0f9c2a7 @BourgerieQuentin BourgerieQuentin committed Feb 26, 2013
@@ -0,0 +1,3 @@
+# -*- conf -*- (for emacs)
+
+<mail.opa_plugin>: use_libbase
@@ -0,0 +1,111 @@
+var simplesmtp = require("simplesmtp");
+
+var debug = false
+
+/** @externType continuation('a) */
+/** @externType caml_list('a) */
+/** @externType caml_tuple_2('a,'b) */
+/** @externType caml_tuple_4('a,'b,'c,'d) */
+/** @externType SSL.secure_type */
+
+/** @opaType list('a) */
+/** @opaType tuple_2('a, 'b) */
+//'
+
+/** @module SmtpClient */
+
+ /** @opaType Email.send_status */
+
+ /**
+ * @register {string, string, \
+ caml_list(string), \
+ string, string, string, \
+ string, string, string, \
+ caml_list(caml_tuple_4(string,string,string,string)), \
+ caml_list(caml_tuple_2(string,string)), \
+ opa[option(string)], opa[option(string)], opa[option(int)],\
+ opa[option(string)], opa[option(string)], opa[option(string)],\
+ opa[bool], opa[option(SSL.secure_type)], \
+ (opa[email_send_status], continuation(opa[void]) -> void), \
+ continuation(opa[void]) -> void}
+ * @cpsBypass
+ */
+ function mail_send_fun(mfrom,mfrom_address_only,mdst,mto,mcc,mbcc,subject,mdata,html,files,custom_headers,via,addr,port,auth,user,pass,dryrun,secure_type,callback,k) {
+
+ var attachments = files.map(function(file) {
+ if (file.f3 == "base64") contents = new Buffer(file.f4, 'base64')
+ else contents = file.f4
+ return {
+ fileName: file.f1,
+ contents: contents,
+ contentType: file.f2
+ }
+ });
+
+ var headers = empty_constructor();
+ custom_headers.forEach(function(header) {
+ if (header.f1 != "size")
+ add_field(headers, header.f1, header.f2);
+ })
+
+ var mailOptions = {
+ from: mfrom, // sender address
+ to: mto, // list of receivers
+ cc: mcc,
+ bcc: mbcc,
+ subject: subject, // Subject line
+ text: mdata, // plaintext body
+ html: html, // html body
+ headers: headers,
+ attachments: attachments
+ };
+
+ var smtpTransport;
+ if (addr.none) {
+ // Create a Sendmail transport object
+ smtpTransport = nodemailer.createTransport("Sendmail", {
+ args: ["-f "+mfrom_address_only]
+ });
+ } else {
+ var addr = option2default(via, addr.some)
+ var port = option2default(port, 25)
+ var secure = !(secure_type.none);
+ var user = option2default(user, "");
+ var pass = option2default(pass, "")
+ smtpTransport = nodemailer.createTransport("SMTP", {
+ host: addr,
+ port: port,
+ secureConnection: secure,
+ debug: debug,
+ auth: {
+ user:user,
+ pass:pass
+ }
+ });
+ }
+
+ if (dryrun) {
+ if (smtpTransport) callback({ok:""}, ccont(k, function(){}))
+ else callback({error:""}, ccont(k, function(){}))
+ } else {
+ // send mail with defined transport object
+ smtpTransport.sendMail(mailOptions, function(error, response){
+ if (error) {
+ console.log(error);
+ callback({error:error}, ccont(k, function(){}))
+ } else {
+ console.log("Message sent: " + response.message);
+ callback({ok:""}, ccont(k, function(){}))
+ }
+ smtpTransport.close();
+ });
+ }
+
+ return_(k, js_void);
+ return;
+
+ } // End register
+
+
+/** @endModule */
+
@@ -0,0 +1 @@
+bslSmtp.nodejs
@@ -0,0 +1,117 @@
+/*
+ Copyright © 2011, 2012, 2013 MLstate
+
+ This file is part of Opa.
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+import-plugin mail
+
+import stdlib.web.mail
+import stdlib.crypto
+
+// type Email.attachment = {
+// filename: string
+// mimetype: string
+// encoding: string
+// content: string
+// }
+
+SmtpClient = {{
+
+ /**
+ * {1} Sending Email.
+ */
+
+ string_of_send_status(s : Email.send_status) =
+ match s with
+ | { bad_sender } -> "bad sender"
+ | { bad_recipient } -> "bad recipient"
+ | { sending } -> "sending"
+ | { ok=s } -> "ok : {s}"
+ | { error=e } -> "error : {e}"
+
+ emails_list_to_string(emails) =
+ List.map(Email.to_string, emails)
+ |> List.to_string_using("", "", ", ", _)
+
+ @private
+ send_async(
+ from : Email.email,
+ to : Email.email,
+ subject : string,
+ mail_content : Email.content,
+ options : Email.options,
+ k : (Email.send_status -> void)
+ ) : void =
+ (text, html) =
+ match mail_content with
+ | {~text} -> (text, "")
+ | {~text ~html} -> (text, Xhtml.serialize_as_standalone_html(html))
+ | {~html} -> (Xhtml.to_readable_string(html), Xhtml.serialize_as_standalone_html(html))
+
+ files =
+ List.filter_map(x ->
+ match x with
+ | ~{filename content mime_type} ->
+ if String.has_prefix("text/", mime_type)
+ then some((filename,mime_type,"8bit",content))
+ else some((filename,mime_type,"base64",
+ Crypto.Base64.encode(binary_of_string(content))))
+ | ~{filename content mime_type encoding} ->
+ some((filename,mime_type,encoding,content))
+ | ~{filename resource} ->
+ match Resource.export_data(resource) with
+ | {some=~{data mimetype}} ->
+ if String.has_prefix("text/", mimetype)
+ then some((filename, mimetype, "8bit", string_of_binary(data)))
+ else some((filename, mimetype, "base64", Crypto.Base64.encode(data)))
+ | {none} -> {none}
+ end
+ , options.files)
+
+ f = %% BslNativeLib.ocaml_tuple_4 %%
+ files = %% BslNativeLib.opa_list_to_ocaml_list %%(f, files)
+ f = %% BslNativeLib.ocaml_tuple_2 %%
+ custom_headers = %% BslNativeLib.opa_list_to_ocaml_list %%(f, options.custom_headers)
+ mto =
+ if List.is_empty(options.to) then Email.to_string(to)
+ else emails_list_to_string(options.to)
+ mcc = emails_list_to_string(options.cc)
+ mbcc = emails_list_to_string(options.bcc)
+ dst = [to] ++ options.to ++ options.cc ++ options.bcc
+ dst = List.map(Email.to_string_only_address, List.unique_list_of(dst))
+ dst = %% BslNativeLib.opa_list_to_ocaml_list %%(identity, dst)
+ %% BslSmtp.SmtpClient.mail_send_fun %%(
+ Email.to_string(from), Email.to_string_only_address(from),
+ dst, mto, mcc, mbcc,
+ subject, text, html,
+ files, custom_headers,
+ options.via,
+ options.server_addr, options.server_port,
+ options.auth, options.user, options.pass,
+ options.dryrun,
+ options.secure_type, k
+ )
+
+ /**
+ * Try to send a mail {b synchronously}
+ */
+ try_send(from : Email.email, to : Email.email, subject : string, content : Email.content, options : Email.options) : Email.send_status =
+ k(cont) =
+ f(r) = Continuation.return(cont, r)
+ send_async(from, to, subject, content, options, f)
+ @callcc(k)
+
+ /**
+ * Try to send a mail {b asynchronously}
+ */
+ try_send_async(from : Email.email, to : Email.email, subject : string, content : Email.content, options : Email.options, continuation : (Email.send_status -> void)) : void =
+ send_async(from, to, subject, content, options, continuation)
+
+}}

0 comments on commit b628657

Please sign in to comment.