Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Merge 9580011 into 41f2031
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomassaro committed Feb 14, 2018
2 parents 41f2031 + 9580011 commit 575f313
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 67 deletions.
2 changes: 2 additions & 0 deletions lib/network/model/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule Helix.Network.Model.Connection do
@type public_ftp :: t_of_type(:public_ftp)
@type bank_login :: t_of_type(:bank_login)
@type wire_transfer :: t_of_type(:wire_transfer)
@type virus_collect :: t_of_type(:virus_collect)
@type cracker_bruteforce :: t_of_type(:cracker_bruteforce)

@type meta :: map | nil
Expand All @@ -37,6 +38,7 @@ defmodule Helix.Network.Model.Connection do
| :public_ftp
| :bank_login
| :wire_transfer
| :virus_collect
| :cracker_bruteforce

@type close_reasons :: :normal | :force
Expand Down
130 changes: 106 additions & 24 deletions lib/process/executable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Helix.Process.Executable do
alias Helix.Network.Query.Tunnel, as: TunnelQuery
alias Helix.Server.Model.Server
alias Helix.Software.Model.File
alias Helix.Universe.Bank.Model.BankAccount
alias Helix.Process.Action.Process, as: ProcessAction
alias Helix.Process.Model.Process

Expand Down Expand Up @@ -273,25 +274,65 @@ defmodule Helix.Process.Executable do

# Defaults: in case these functions were not defined, we assume the
# process is not interested on this (optional) data.

@spec get_bounce_id(Server.t, Server.t, params, meta) ::
%{bounce_id: Bounce.id | nil}
@doc false
defp get_bounce_id(_, _, _, %{bounce: bounce = %Bounce{}}),
do: %{bounce_id: bounce.bounce_id}
defp get_bounce_id(_, _, _, %{bounce: bounce_id = %Bounce.ID{}}),
do: %{bounce_id: bounce_id}
defp get_bounce_id(_, _, _, _),
do: %{bounce_id: nil}

@spec get_source_connection(Server.t, Server.t, params, meta) ::
{:create, Connection.type}
| nil
@doc false
defp get_source_connection(_, _, _, _),
do: nil

defp get_source_file(_, _, _, _),
do: %{src_file_id: nil}

@spec get_target_connection(Server.t, Server.t, params, meta) ::
{:create, Connection.type}
| :same_origin
| nil
@doc false
defp get_target_connection(_, _, _, _),
do: nil

@spec get_source_file(Server.t, Server.t, params, meta) ::
%{src_file_id: File.id | nil}
@doc false
defp get_source_file(_, _, _, _),
do: %{src_file_id: nil}

@spec get_target_file(Server.t, Server.t, params, meta) ::
%{tgt_file_id: File.id | nil}
@doc false
defp get_target_file(_, _, _, _),
do: %{tgt_file_id: nil}

@spec get_source_bank_account(Server.t, Server.t, params, meta) ::
%{
src_atm_id: Server.t | nil,
src_acc_number: BankAccount.account | nil
}
@doc false
defp get_source_bank_account(_, _, _, _),
do: %{src_atm_id: nil, src_acc_number: nil}

@spec get_target_bank_account(Server.t, Server.t, params, meta) ::
%{
tgt_atm_id: Server.t | nil,
tgt_acc_number: BankAccount.account | nil
}
@doc false
defp get_target_bank_account(_, _, _, _),
do: %{tgt_atm_id: nil, tgt_acc_number: nil}

@spec get_target_process(Server.t, Server.t, params, meta) ::
%{tgt_process_id: Process.t | nil}
@doc false
defp get_target_process(_, _, _, _),
do: %{tgt_process_id: nil}
end
Expand All @@ -317,18 +358,21 @@ defmodule Helix.Process.Executable do
resources = get_resources(unquote_splicing(args))
source_file = get_source_file(unquote_splicing(args))
target_file = get_target_file(unquote_splicing(args))
source_bank_account = get_source_bank_account(unquote_splicing(args))
target_bank_account = get_target_bank_account(unquote_splicing(args))
target_process = get_target_process(unquote_splicing(args))
bounce_id = get_bounce_id(unquote_splicing(args))
ownership = get_ownership(unquote_splicing(args))
process_type = get_process_type(unquote(meta))
network_id = get_network_id(unquote(meta))

partial =
%{}
|> Map.merge(process_data)
process_data
|> Map.merge(resources)
|> Map.merge(source_file)
|> Map.merge(target_file)
|> Map.merge(source_bank_account)
|> Map.merge(target_bank_account)
|> Map.merge(target_process)
|> Map.merge(bounce_id)
|> Map.merge(ownership)
Expand Down Expand Up @@ -396,10 +440,6 @@ defmodule Helix.Process.Executable do

quote do

@spec get_source_connection(term, term, term, term) ::
{:create, Connection.type}
| nil
@doc false
defp get_source_connection(unquote_splicing(args)) do
unquote(block)
end
Expand All @@ -421,11 +461,6 @@ defmodule Helix.Process.Executable do

quote do

@spec get_target_connection(term, term, term, term) ::
{:create, Connection.type}
| :same_origin
| nil
@doc false
defp get_target_connection(unquote_splicing(args)) do
unquote(block)
end
Expand All @@ -442,9 +477,6 @@ defmodule Helix.Process.Executable do

quote do

@spec get_source_file(term, term, term, term) ::
%{src_file_id: File.id | nil}
@doc false
defp get_source_file(unquote_splicing(args)) do
file_id = unquote(block)

Expand All @@ -463,9 +495,6 @@ defmodule Helix.Process.Executable do

quote do

@spec get_target_file(term, term, term, term) ::
%{tgt_file_id: File.id | nil}
@doc false
defp get_target_file(unquote_splicing(args)) do
file_id = unquote(block)

Expand All @@ -475,6 +504,62 @@ defmodule Helix.Process.Executable do
end
end

@doc """
Returns the process' `src_atm_id` and `src_acc_number`, as defined on the
`source_bank_account` section of the Process.Executable
"""
defmacro source_bank_account(gateway, target, params, meta, do: block) do
args = [gateway, target, params, meta]

quote do

defp get_source_bank_account(unquote_splicing(args)) do
{atm_id, account_number} =
unquote(block)
|> get_bank_data()

%{
src_atm_id: atm_id,
src_acc_number: account_number
}
end

end
end

@doc """
Returns the process' `tgt_atm_id` and `tgt_acc_number`, as defined on the
`target_bank_account` section of the Process.Executable
"""
defmacro target_bank_account(gateway, target, params, meta, do: block) do
args = [gateway, target, params, meta]

quote do

defp get_target_bank_account(unquote_splicing(args)) do
{atm_id, account_number} =
unquote(block)
|> get_bank_data()

%{
tgt_atm_id: atm_id,
tgt_acc_number: account_number
}
end

end
end

@spec get_bank_data(BankAccount.t) :: {Server.id, BankAccount.account}
@spec get_bank_data(tuple) :: tuple
@spec get_bank_data(nil) :: {nil, nil}
def get_bank_data(%BankAccount{atm_id: atm_id, account_number: number}),
do: {atm_id, number}
def get_bank_data(result = {_, _}),
do: result
def get_bank_data(nil),
do: {nil, nil}

@doc """
Returns the process' `tgt_process_id`, as defined on the `target_process`
section of the Process.Executable.
Expand All @@ -484,9 +569,6 @@ defmodule Helix.Process.Executable do

quote do

@spec get_target_process(term, term, term, term) ::
%{tgt_process_id: Process.t | nil}
@doc false
defp get_target_process(unquote_splicing(args)) do
process_id = unquote(block)

Expand All @@ -509,7 +591,7 @@ defmodule Helix.Process.Executable do

quote do

@spec get_resources(term, term, term, term) ::
@spec get_resources(Server.t, Server.t, params, meta) ::
unquote(process).resources
@doc false
defp get_resources(unquote_splicing(args)) do
Expand Down
Loading

0 comments on commit 575f313

Please sign in to comment.