-
Notifications
You must be signed in to change notification settings - Fork 1
Added pop3 mailer and tools #1
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
333a7cb
1978190
533953d
4333247
6a7af56
8775159
a64cff9
a2e4a6b
4a68718
549a077
f3ca092
da842f3
dd511b2
d81549f
f030468
c7a1f96
776a43d
f49c8af
18344bf
93f8865
ee29ed7
82c19b7
578bbaa
4aec18b
a2dcf0e
da46e40
6ae615c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| elixir 1.6.0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| defmodule Agala.Provider.Email.Receiver do | ||
| @moduledoc """ | ||
| Module providing email connection fetch. | ||
| """ | ||
| use Agala.Bot.Receiver | ||
| alias Agala.BotParams | ||
| @mail_proto Agala.Provider.Email.Protocol.Pop3.Mock | ||
|
|
||
| def get_updates(notify_with, bot_params = %BotParams{}) do | ||
| mail_proto = private_options(bot_params) | ||
| {:ok, client} = mail_proto.connect(mail_options(bot_params)) | ||
| {:ok, mails} = mail_proto.scan(client) | ||
|
|
||
| Enum.map(mails, fn {id, _} -> | ||
| #@todo: use poolboy and asynk tasks | ||
| {:ok, bin_message} = mail_proto.retrieve(client, id) | ||
|
|
||
| mail_proto.parse_binary(bin_message) | ||
| |> resolve_mail(notify_with) | ||
| end) | ||
| end | ||
|
|
||
| defp mail_options(%BotParams{ | ||
|
||
| provider_params: %{ | ||
| login: login, email: email, password: password, server: server, | ||
| port: port | ||
| } | ||
| } | ||
| ), do: %{email: email, password: password, server: server, port: port, login: login} | ||
|
|
||
| defp private_options(%BotParams{ | ||
| private: %{ | ||
| mail_fetcher_module: mail_fetcher_module | ||
| } | ||
| } | ||
| ), do: mail_fetcher_module | ||
|
|
||
| defp resolve_mail({h, c}, notify_with) do | ||
| notify_with.({h, c}) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| defmodule Agala.Provider.Email.Protocol.Pop3 do | ||
| @moduledoc """ | ||
| Implementation of Pop3 mail protocol for Agala framework. | ||
| Based on nico-amsterdam/pop3mail | ||
| """ | ||
|
|
||
| def connect(%{email: email, password: password, server: server, port: port, login: login}) do | ||
| :epop_client.connect(email, password, [{:addr, server},{:port, port},{:user, login},:ssl]) | ||
| end | ||
|
|
||
| def scan(client) do | ||
| :epop_client.scan(client) | ||
| end | ||
|
|
||
| def retrieve(client, id) do | ||
| :epop_client.bin_retrieve(client, id) | ||
| end | ||
|
|
||
| def parse_binary(raw_binary) do | ||
| {:message, h, c} = | ||
| raw_binary | ||
| |> :epop_message.bin_parse | ||
| |> parse_content | ||
| |> parse_headers | ||
| {h, c} | ||
| end | ||
|
|
||
| def parse_headers({:message, headers, content}) do | ||
| parsed_headers = | ||
| headers | ||
| |> Enum.reduce(%{}, fn {:header, key, val}, acc -> | ||
| Map.put(acc, key, val) | ||
| end) | ||
| {:message, parsed_headers, content} | ||
| end | ||
|
|
||
| def parse_content({:message, headers, content}) do | ||
| parsed_content = Pop3mail.decode_body_content(headers, content) | ||
| {:message, headers, parsed_content} | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| defmodule Agala.Provider.Email.Protocol.Pop3.Mock do | ||
| @moduledoc """ | ||
| Implementation of mock module for Pop3 mail protocol of Agala framework. | ||
| For testing purposes | ||
| """ | ||
| alias Agala.Provider.Email.Protocol.Pop3 | ||
|
|
||
| def connect(%{email: _, password: _, server: _, port: _, login: _}) do | ||
| {:ok, | ||
| {:sk, 'user', 'mail.example.com', | ||
| {:sslsocket, {:gen_tcp, "some_port", :tls_connection, :undefined}, | ||
| self()}, 995, false, false, true}} | ||
| end | ||
|
|
||
| def scan(_client) do | ||
| {:ok, [{1, 1948}, {2, 1923}, {3, 1123}]} | ||
| end | ||
|
|
||
| def retrieve(_client, id) do | ||
| {:ok, | ||
| "To: <[email protected]@yandex.ru>\r\nFrom: Sender <[email protected]>\r\nSubject: =?UTF-8?B?0LLQvtC/0YDQvtGBINC/0L4g0LrRgNC10LTQuNGC0YM=?=\r\nDate: Tue, 30 Jan 2018 16:22:53 +0300\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101\r\n Thunderbird/52.5.2\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=\"utf-8\"; format=flowed\r\nContent-Transfer-Encoding: 8bit\r\nContent-Language: en-US\r\n\r\nmessage number #{id}\r\n\r\n"} | ||
| end | ||
|
|
||
| def parse_binary(raw_binary) do | ||
| Pop3.parse_binary(raw_binary) | ||
| end | ||
|
|
||
| def parse_headers({:message, headers, content}) do | ||
| Pop3.parse_headers({:message, headers, content}) | ||
| end | ||
|
|
||
| def parse_content({:message, headers, content}) do | ||
| Pop3.parse_content({:message, headers, content}) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,9 @@ defmodule AgalaEmail.MixProject do | |
| [ | ||
| {:agala, "~> 2.0.2"}, | ||
| {:ex_doc, "> 0.0.0", only: :dev}, | ||
| {:credo, "~> 0.8", only: [:dev, :test]} | ||
| {:credo, "~> 0.8", only: [:dev, :test]}, | ||
| {:pop3mail, "~> 1.3"}, | ||
| {:erlpop, github: "nico-amsterdam/erlpop"} | ||
|
||
| # {:dep_from_hexpm, "~> 0.3.0"}, | ||
| # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}, | ||
| ] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| defmodule EmailReceiverTest do | ||
| use ExUnit.Case | ||
| doctest Agala.Provider.Email | ||
| alias Agala.BotParams | ||
| alias Agala.Provider.Email | ||
| alias Agala.Provider.Email.Receiver | ||
|
|
||
| @bot_configuration %BotParams{ | ||
| provider_params: %{ | ||
| login: "user", | ||
| password: "secure", | ||
| server: "mail.server.ru", | ||
| port: 995, | ||
| email: "[email protected]" | ||
| } | ||
| } | ||
|
|
||
| test "get_updates call applies handler" do | ||
| {:ok, bot_params} = Email.init(@bot_configuration) | ||
| assert [:ok, :ok, :ok] == Receiver.get_updates(fn _ -> :ok end, bot_params) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| defmodule Email.Protocol.Pop3Test do | ||
| use ExUnit.Case | ||
| doctest Agala.Provider.Email.Protocol.Pop3 | ||
| alias Agala.Provider.Email.Protocol.Pop3 | ||
|
|
||
| test "parse_headers returns map of headers" do | ||
| exp_result = %{ | ||
| "To" => "<[email protected]>", | ||
| "From" => "Sender <[email protected]>", | ||
| "Subject" => | ||
| "=?UTF-8?B?0LLQvtC/0YDQvtGBINC/0L4g0LrRgNC10LTQuNGC0YM=?=", | ||
| "Message-ID" => "<[email protected]>", | ||
| "Date" => "Tue, 30 Jan 2018 16:22:53 +0300", | ||
| "User-Agent" => | ||
| "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2", | ||
| "MIME-Version" => "1.0", | ||
| "Content-Type" => "text/plain; charset=\"utf-8\"; format=flowed", | ||
| "Content-Transfer-Encoding" => "8bit", | ||
| "Content-Language" => "en-US" | ||
| } | ||
| assert {:message, ^exp_result, _} = Pop3.parse_headers({:message, mock_headers(), mock_content()}) | ||
| end | ||
|
|
||
| test "parse_content returns list of parts" do | ||
| exp_result = [ | ||
| %Pop3mail.Part{ | ||
| boundary: "", | ||
| charset: "utf-8", | ||
| content: "пожалуйста не присылайте коллекторов\r\n\r\n", | ||
| content_id: "", | ||
| content_location: "", | ||
| filename: "", | ||
| filename_charset: "us-ascii", | ||
| index: 1, | ||
| inline: nil, | ||
| media_type: "text/plain", | ||
| path: "" | ||
| } | ||
| ] | ||
| assert {:message, _, ^exp_result} = Pop3.parse_content({:message, mock_headers(), mock_content()}) | ||
| end | ||
|
|
||
| defp mock_headers() do | ||
| [ | ||
| {:header, "To", "<[email protected]>"}, | ||
| {:header, "From", "Sender <[email protected]>"}, | ||
| {:header, "Subject", | ||
| "=?UTF-8?B?0LLQvtC/0YDQvtGBINC/0L4g0LrRgNC10LTQuNGC0YM=?="}, | ||
| {:header, "Message-ID", "<[email protected]>"}, | ||
| {:header, "Date", "Tue, 30 Jan 2018 16:22:53 +0300"}, | ||
| {:header, "User-Agent", | ||
| "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2"}, | ||
| {:header, "MIME-Version", "1.0"}, | ||
| {:header, "Content-Type", "text/plain; charset=\"utf-8\"; format=flowed"}, | ||
| {:header, "Content-Transfer-Encoding", "8bit"}, | ||
| {:header, "Content-Language", "en-US"} | ||
| ] | ||
| end | ||
|
|
||
| defp mock_content() do | ||
| "пожалуйста не присылайте коллекторов\r\n\r\n" | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&&@pop3_mock - это какой-то особый синтаксис? непонятно немного
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&&@pop3_mock -> && @pop3_mock
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Какая-то императивная магия
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вика поправила