-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]). -export_type([m_box/0]). -type m_box() :: {m_box, gleam@dict:dict(binary(), binary()), binary()}. -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 => 98}) end, _pipe = Matched_content, _pipe@1 = gleam@regex:split(Dead_space, _pipe), _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 => 86}) end, _pipe = S, _pipe@1 = gleam@regex:scan(Multi_line_value, _pipe), _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)}.