Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes required for Elixir 1.1 #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/dkim.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ defmodule DKIM do
def check(mail) when is_binary(mail), do:
check(MimeMail.from_string(mail))
def check(%MimeMail{headers: headers,body: {:raw,body}}=mail) do
mail = decode_headers(mail)
case mail.headers[:'dkim-signature'] do
mail = decode_headers(mail)
case Dict.get(mail.headers, :"dkim-signature") do
nil -> :none
sig ->
if (sig.bh == body_hash(body,sig)) do
Expand Down Expand Up @@ -38,11 +38,11 @@ defmodule DKIM do
end

def decode_headers(%MimeMail{headers: headers}=mail) do
case headers[:'dkim-signature'] do
case Dict.get(headers, :"dkim-signature") do
{:raw,raw} ->
unquoted = MimeMail.header_value(raw)
sig = struct(DKIM,for({k,v}<-MimeMail.Params.parse_header(unquoted),do: {k,decode_field(k,v)}))
put_in(mail,[:headers,:'dkim-signature'],sig)
%MimeMail{mail | headers: put_in(mail.headers, [:"dkim-signature"], sig)}
_ -> mail
end
end
Expand Down Expand Up @@ -78,7 +78,7 @@ defmodule DKIM do
:error-> ""
end
end
defp decode_field(:h,h), do:
defp decode_field(:h,h), do:
(h|>String.downcase|>String.split(":")|>Enum.map(&String.to_atom/1))
defp decode_field(_,e), do: e

Expand All @@ -91,7 +91,7 @@ defmodule DKIM do
def canon_body(body,:simple), do:
String.replace(body,~r/(\r\n)*$/,"\r\n", global: false)
def canon_body(body,:relaxed) do
body
body
|> String.replace(~r/[\t ]+\r\n/, "\r\n")
|> String.replace(~r/[\t ]+/, " ")
|> String.replace(~r/(\r\n)*$/,"\r\n", global: false)
Expand All @@ -102,7 +102,7 @@ defmodule DKIM do
<<trunc_body::binary-size(l),_::binary>> = body
trunc_body
end

def hash(bin,:sha256), do: :crypto.hash(:sha256,bin)
def hash(bin,:sha1), do: :crypto.hash(:sha,bin)

Expand Down
17 changes: 8 additions & 9 deletions lib/mimemail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ defmodule MimeMail do
@type header :: {:raw,binary} | MimeMail.Header.t #ever the raw line or any term implementing MimeMail.Header.to_ascii
@type body :: binary | [MimeMail.t] | {:raw,binary} #ever the raw body or list of mail for multipart or binary for decoded content
@type t :: %MimeMail{headers: [{key::binary,header}], body: body}
@derive [Access]
defstruct headers: [], body: ""

def from_string(data) do
Expand Down Expand Up @@ -39,7 +38,7 @@ defmodule MimeMail do
_ -> body
end
body = case headers[:'content-type'] do
{"multipart/"<>_,%{boundary: bound}}->
{"multipart/"<>_,%{boundary: bound}}->
body |> String.split(~r"\s*--#{bound}\s*") |> Enum.slice(1..-2) |> Enum.map(&from_string/1) |> Enum.map(&decode_body/1)
{"text/"<>_,%{charset: charset}} ->
body |> Iconv.conv(charset,"utf8") |> ok_or(ensure_ascii(body)) |> ensure_utf8
Expand All @@ -53,7 +52,7 @@ defmodule MimeMail do
def encode_body(%MimeMail{body: body}=mail) when is_binary(body) do
mail = MimeMail.CTParams.decode_headers(mail)
case mail.headers[:'content-type'] do
{"text/"<>_=type,params}->
{"text/"<>_=type,params}->
headers = Dict.drop(mail.headers,[:'content-type',:'content-transfer-encoding']) ++[
'content-type': {type,Dict.put(params,:charset,"utf-8")},
'content-transfer-encoding': "quoted-printable"
Expand Down Expand Up @@ -93,19 +92,19 @@ defmodule MimeMail do
defp chunk_line(<<vline::size(74)-binary,?=,rest::binary>>), do: (vline<>"=\r\n"<>chunk_line("="<>rest))
defp chunk_line(<<vline::size(75)-binary,rest::binary>>), do: (vline<>"=\r\n"<>chunk_line(rest))
defp chunk_line(other), do: other
def qp_to_binary(str), do:

def qp_to_binary(str), do:
(str |> String.rstrip |> String.rstrip(?=) |> qp_to_binary([]))
def qp_to_binary("=\r\n"<>rest,acc), do:
def qp_to_binary("=\r\n"<>rest,acc), do:
qp_to_binary(rest,acc)
def qp_to_binary(<<?=,x1,x2>><>rest,acc), do:
def qp_to_binary(<<?=,x1,x2>><>rest,acc), do:
qp_to_binary(rest,[<<x1,x2>> |> String.upcase |> Base.decode16! | acc])
def qp_to_binary(<<c,rest::binary>>,acc), do:
qp_to_binary(rest,[c | acc])
def qp_to_binary("",acc), do:
(acc |> Enum.reverse |> IO.iodata_to_binary)

def unfold_header(value), do:
def unfold_header(value), do:
String.replace(value,~r/\r\n([\t ])/,"\\1")

def fold_header(header), do:
Expand All @@ -130,7 +129,7 @@ defmodule MimeMail do
def ensure_ascii(bin), do:
Kernel.to_string(for(<<c<-bin>>, (c<127 and c>31) or c in [?\t,?\r,?\n], do: c))
def ensure_utf8(bin) do
bin
bin
|> String.chunk(:printable)
|> Enum.filter(&String.printable?/1)
|> Kernel.to_string
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ defmodule Mailibex.Mixfile do

def project do
[app: :mailibex,
version: "0.0.1",
elixir: "~> 1.0.0",
version: "0.1.0",
elixir: "~> 1.1",
description: description,
package: package,
compilers: [:iconv, :elixir, :app],
Expand Down
2 changes: 1 addition & 1 deletion test/dkim_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule DKIMTest do

test "DKIM signature round trip" do
[rsaentry] = :public_key.pem_decode(File.read!("test/mails/key.pem"))
assert {:pass,_} =
assert {:pass,_} =
File.read!("test/mails/valid_dkim_relaxed_canon.eml")
|> MimeMail.from_string
|> DKIM.sign(:public_key.pem_entry_decode(rsaentry), d: "order.brendy.fr", s: "cobrason")
Expand Down
4 changes: 2 additions & 2 deletions test/flat_mail_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule FlatMailTest do
assert {"text/plain",%{}} = text.headers[:'content-type']
assert {"text/plain",%{name: ct_txt_name}} = text_attached.headers[:'content-type']
assert {"application/x-7z-compressed",%{name: ct_7z_name}} = archive.headers[:'content-type']
assert nil = text.headers[:'content-disposition']
assert nil == text.headers[:'content-disposition']
assert {"attachment",%{filename: cd_txt_name}} = text_attached.headers[:'content-disposition']
assert {"attachment",%{filename: cd_7z_name}} = archive.headers[:'content-disposition']
assert String.contains?(ct_txt_name,".txt")
Expand All @@ -34,7 +34,7 @@ defmodule FlatMailTest do
assert ".txt" = MimeTypes.bin2ext(txt)
assert ".html" = MimeTypes.bin2ext(html)
assert ".png" = MimeTypes.bin2ext(png)
flat = flat
flat = flat
|> MimeMail.Flat.to_mail
|> MimeMail.to_string
|> MimeMail.from_string
Expand Down
8 changes: 4 additions & 4 deletions test/mime_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule MimeMailTest do
test "qp encoding => no line > 76 char && only ascii && no space at end of lines" do
res = MimeMail.string_to_qp(@qp_test)
Enum.each String.split(res,"\r\n"), fn line->
assert false = Regex.match? ~r/[\t ]+$/, line
assert false == Regex.match? ~r/[\t ]+$/, line
assert String.length(line) < 77
assert [] = Enum.filter('#{line}',&(&1 < 32 or &1 > 127))
end
Expand Down Expand Up @@ -85,9 +85,9 @@ defmodule MimeMailTest do
|> MimeMail.from_string
|> MimeMail.decode_headers([DKIM,MimeMail.Emails,MimeMail.Words,MimeMail.CTParams])
|> MimeMail.decode_body
roundtrip = decoded
|> MimeMail.to_string
|> MimeMail.from_string
roundtrip = decoded
|> MimeMail.to_string
|> MimeMail.from_string
|> MimeMail.decode_body
assert String.rstrip(decoded.body) == String.rstrip(roundtrip.body)
end
Expand Down