Current section
Files
Jump to
Current section
Files
src/gleambox.erl
-module(gleambox).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([get_headers/1, get_header/2, get_body/1, get_from/1, get_to/1, get_date/1, get_subject/1, get_message_id/1, get_references/1, parse/1, maildir_iterator/1]).
-export_type([m_box/0, mail/0]).
-type m_box() :: {m_box, gleam@dict:dict(binary(), binary()), binary()}.
-type mail() :: {mail,
binary(),
list(binary()),
binary(),
binary(),
birl:time(),
binary(),
gleam@dict:dict(binary(), binary())} |
invalid_mail.
-spec get_headers(m_box()) -> gleam@dict:dict(binary(), binary()).
get_headers(Mbox) ->
erlang:element(2, Mbox).
-spec get_header(m_box(), binary()) -> {ok, binary()} | {error, nil}.
get_header(Mbox, Key) ->
_pipe = erlang:element(2, Mbox),
gleam@dict:get(_pipe, Key).
-spec get_body(m_box()) -> binary().
get_body(Mbox) ->
erlang:element(3, Mbox).
-spec get_from(m_box()) -> {ok, binary()} | {error, nil}.
get_from(Mbox) ->
get_header(Mbox, <<"From"/utf8>>).
-spec get_to(m_box()) -> {ok, binary()} | {error, nil}.
get_to(Mbox) ->
get_header(Mbox, <<"To"/utf8>>).
-spec get_date(m_box()) -> {ok, binary()} | {error, nil}.
get_date(Mbox) ->
get_header(Mbox, <<"Date"/utf8>>).
-spec get_subject(m_box()) -> {ok, binary()} | {error, nil}.
get_subject(Mbox) ->
get_header(Mbox, <<"Subject"/utf8>>).
-spec get_message_id(m_box()) -> {ok, binary()} | {error, nil}.
get_message_id(Mbox) ->
get_header(Mbox, <<"Message-ID"/utf8>>).
-spec get_references(m_box()) -> list(binary()).
get_references(Mbox) ->
_pipe = get_header(Mbox, <<"References"/utf8>>),
_pipe@1 = gleam@result:unwrap(_pipe, <<"Error"/utf8>>),
gleam@string:split(_pipe@1, <<" "/utf8>>).
-spec parse_body(binary()) -> binary().
parse_body(Mboxcontents) ->
_pipe = Mboxcontents,
_pipe@1 = gleam@string:split_once(_pipe, <<"\n\n"/utf8>>),
_pipe@2 = gleam@result:unwrap(_pipe@1, {<<""/utf8>>, <<""/utf8>>}),
gleam@pair:second(_pipe@2).
-spec get_header_dict(binary()) -> {binary(), binary()}.
get_header_dict(S) ->
_pipe = S,
_pipe@1 = gleam@string:split_once(_pipe, <<": "/utf8>>),
gleam@result:unwrap(_pipe@1, {<<""/utf8>>, <<""/utf8>>}).
-spec remove_dead_space(binary(), binary()) -> binary().
remove_dead_space(Acc, Matched_content) ->
_assert_subject = gleam@regex:from_string(<<"\\s+"/utf8>>),
{ok, Dead_space} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"gleambox"/utf8>>,
function => <<"remove_dead_space"/utf8>>,
line => 115})
end,
_pipe = Dead_space,
_pipe@1 = gleam@regex:split(_pipe, Matched_content),
_pipe@2 = gleam@string:join(_pipe@1, <<" "/utf8>>),
gleam@string:replace(Acc, Matched_content, _pipe@2).
-spec fix_multiline_values(binary()) -> binary().
fix_multiline_values(S) ->
_assert_subject = gleam@regex:compile(
<<": [^\n]+\n\\s+[^\n]+$"/utf8>>,
{options, true, true}
),
{ok, Multi_line_value} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"gleambox"/utf8>>,
function => <<"fix_multiline_values"/utf8>>,
line => 103})
end,
_pipe = Multi_line_value,
_pipe@1 = gleam@regex:scan(_pipe, S),
_pipe@2 = gleam@list:map(
_pipe@1,
fun(Match) -> erlang:element(2, Match) end
),
_pipe@3 = gleam@list:scan(_pipe@2, S, fun remove_dead_space/2),
_pipe@4 = gleam@list:first(_pipe@3),
gleam@result:unwrap(_pipe@4, <<"bar"/utf8>>).
-spec parse_headers(binary()) -> gleam@dict:dict(binary(), binary()).
parse_headers(Mboxcontents) ->
_pipe = Mboxcontents,
_pipe@1 = gleam@string:split_once(_pipe, <<"\n\n"/utf8>>),
_pipe@2 = gleam@result:unwrap(_pipe@1, {<<""/utf8>>, <<""/utf8>>}),
_pipe@3 = gleam@pair:first(_pipe@2),
_pipe@4 = fix_multiline_values(_pipe@3),
_pipe@5 = gleam@string:split(_pipe@4, <<"\n"/utf8>>),
_pipe@6 = gleam@list:map(_pipe@5, fun get_header_dict/1),
maps:from_list(_pipe@6).
-spec parse(binary()) -> m_box().
parse(Mboxcontents) ->
{m_box, parse_headers(Mboxcontents), parse_body(Mboxcontents)}.
-spec mail_date(mail()) -> birl:time().
mail_date(Mail) ->
case Mail of
{mail, _, _, _, _, Date, _, _} ->
Date;
invalid_mail ->
birl:now()
end.
-spec mail_from(mail()) -> binary().
mail_from(Mail) ->
case Mail of
{mail, From, _, _, _, _, _, _} ->
From;
invalid_mail ->
<<""/utf8>>
end.
-spec mail_to(mail()) -> list(binary()).
mail_to(Mail) ->
case Mail of
{mail, _, To, _, _, _, _, _} ->
To;
invalid_mail ->
gleam@list:wrap(<<""/utf8>>)
end.
-spec mail_body(mail()) -> binary().
mail_body(Mail) ->
case Mail of
{mail, _, _, _, _, _, Body, _} ->
Body;
invalid_mail ->
<<""/utf8>>
end.
-spec read_file(binary()) -> binary().
read_file(File_path) ->
_pipe = File_path,
_pipe@1 = simplifile:read(_pipe),
gleam@result:unwrap(_pipe@1, <<""/utf8>>).
-spec maildir_iterator(binary()) -> gleam@iterator:iterator(binary()).
maildir_iterator(Mbox_path) ->
_pipe = Mbox_path,
_pipe@1 = simplifile:get_files(_pipe),
_pipe@2 = gleam@result:lazy_unwrap(_pipe@1, fun gleam@list:new/0),
_pipe@3 = gleam@iterator:from_list(_pipe@2),
gleam@iterator:map(_pipe@3, fun read_file/1).
-spec mbox_to_mail(m_box()) -> mail().
mbox_to_mail(Mbox) ->
{mail,
begin
_pipe = Mbox,
_pipe@1 = get_from(_pipe),
gleam@result:unwrap(_pipe@1, <<""/utf8>>)
end,
begin
_pipe@2 = Mbox,
_pipe@3 = get_to(_pipe@2),
_pipe@4 = gleam@result:unwrap(_pipe@3, <<""/utf8>>),
gleam@list:wrap(_pipe@4)
end,
begin
_pipe@5 = Mbox,
_pipe@6 = get_subject(_pipe@5),
gleam@result:unwrap(_pipe@6, <<""/utf8>>)
end,
begin
_pipe@7 = Mbox,
_pipe@8 = get_message_id(_pipe@7),
gleam@result:unwrap(_pipe@8, <<""/utf8>>)
end,
begin
_pipe@9 = Mbox,
_pipe@10 = get_date(_pipe@9),
_pipe@11 = gleam@result:unwrap(_pipe@10, <<""/utf8>>),
_pipe@12 = birl:parse(_pipe@11),
gleam@result:unwrap(_pipe@12, birl:now())
end,
begin
_pipe@13 = Mbox,
get_body(_pipe@13)
end,
begin
_pipe@14 = Mbox,
get_headers(_pipe@14)
end}.