Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
scrogson committed Sep 2, 2019
1 parent 9a94c93 commit 5ddfcb5
Show file tree
Hide file tree
Showing 38 changed files with 700 additions and 652 deletions.
4 changes: 0 additions & 4 deletions .envrc

This file was deleted.

4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
32 changes: 32 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI

on: [push, pull-request]

jobs:
test:
runs-on: ubuntu:latest

container:
image: elixir:latest

steps:
- uses: actions/checkout@v1
- name: Install Dependencies
run: |
mix local.rebar --force
mix local.hex --force
mix deps.get
- name: Run Tests
run: mix test

services:
ejabberd:
image: rroemhild/ejabberd
ports:
- 5222:5222
- 5269:5269
- 5280:5280
env:
XMPP_DOMAIN: localhost
EJABBERD_SKIP_MODULES_UPDATE: true
options: --name ejabberd
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
elixir 1.9.1
erlang 22.0
39 changes: 0 additions & 39 deletions .travis.yml

This file was deleted.

13 changes: 0 additions & 13 deletions config/config.exs

This file was deleted.

13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "3"

services:
ejabberd:
image: rroemhild/ejabberd
container_name: ejabberd
ports:
- 5222:5222
- 5269:5269
- 5280:5280
environment:
XMPP_DOMAIN: localhost
EJABBERD_SKIP_MODULES_UPDATE: "true"
20 changes: 0 additions & 20 deletions lib/mix/tasks/ejabberd.gen.config.ex

This file was deleted.

27 changes: 16 additions & 11 deletions lib/romeo/auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ defmodule Romeo.Auth do

defmodule Mechanism do
@doc "Authenticates using the supplied mechanism"
@callback authenticate(String.t, Romeo.Connection.t) :: Romeo.Connection.t
@callback authenticate(String.t(), Romeo.Connection.t()) :: Romeo.Connection.t()
end

defmodule Error do
defexception [:message]

def exception(mechanism) do
msg = "Failed to authenticate using mechanism: #{inspect mechanism}"
msg = "Failed to authenticate using mechanism: #{inspect(mechanism)}"
%Romeo.Auth.Error{message: msg}
end
end
Expand All @@ -26,7 +26,7 @@ defmodule Romeo.Auth do
If the preferred mechanism is not supported it will choose PLAIN.
"""
def authenticate!(conn) do
preferred = conn.preferred_auth_mechanisms
preferred = conn.preferred_auth_mechanisms
mechanisms = conn.features.mechanisms
preferred_mechanism(preferred, mechanisms) |> do_authenticate(conn)
end
Expand All @@ -42,20 +42,21 @@ defmodule Romeo.Auth do
|> mod.recv(fn
conn, xmlel(name: "handshake") ->
conn

_conn, xmlel(name: "stream:error") ->
raise Romeo.Auth.Error, "handshake error"
end)
end


defp do_authenticate(mechanism, conn) do
{:ok, conn} =
case mechanism do
{name, mod} ->
Logger.info fn -> "Authenticating with extension #{name} implemented by #{mod}" end
Logger.info(fn -> "Authenticating with extension #{name} implemented by #{mod}" end)
mod.authenticate(name, conn)

_ ->
Logger.info fn -> "Authenticating with #{mechanism}" end
Logger.info(fn -> "Authenticating with #{mechanism}" end)
authenticate_with(mechanism, conn)
end

Expand All @@ -71,17 +72,18 @@ defmodule Romeo.Auth do
mod.send(conn, Romeo.Stanza.auth("PLAIN", Romeo.Stanza.base64_cdata(payload)))
end


defp authenticate_with("ANONYMOUS", %{transport: mod} = conn) do
conn |> mod.send(Romeo.Stanza.auth("ANONYMOUS"))
end

defp authenticate_with(mechanism_name, _conn) do
raise """
Romeo does not include an implementation for authentication mechanism #{inspect mechanism_name}.
Romeo does not include an implementation for authentication mechanism #{
inspect(mechanism_name)
}.
Please provide an implementation such as
Romeo.Connection.start_link(preferred_auth_mechanisms: [{#{inspect mechanism_name}, SomeModule}])
Romeo.Connection.start_link(preferred_auth_mechanisms: [{#{inspect(mechanism_name)}, SomeModule}])
where `SomeModule` implements the Romeo.Auth.Mechanism behaviour.
"""
Expand All @@ -91,8 +93,9 @@ defmodule Romeo.Auth do
mod.recv(conn, fn conn, xmlel(name: name) ->
case name do
"success" ->
Logger.info fn -> "Authenticated successfully" end
Logger.info(fn -> "Authenticated successfully" end)
{:ok, conn}

"failure" ->
{:error, conn}
end
Expand All @@ -104,15 +107,17 @@ defmodule Romeo.Auth do
end

defp preferred_mechanism([], _), do: "PLAIN"

defp preferred_mechanism([mechanism | tail], mechanisms) do
case acceptable_mechanism?(mechanism, mechanisms) do
true -> mechanism
true -> mechanism
false -> preferred_mechanism(tail, mechanisms)
end
end

defp acceptable_mechanism?({name, _mod}, mechanisms),
do: acceptable_mechanism?(name, mechanisms)

defp acceptable_mechanism?(mechanism, mechanisms),
do: Enum.member?(mechanisms, mechanism)
end
14 changes: 11 additions & 3 deletions lib/romeo/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ defmodule Romeo.Connection do
* `:timeout` - Call timeout (default: `#{@timeout}`)
"""
@spec close(pid, Keyword.t) :: :ok
@spec close(pid, Keyword.t()) :: :ok
def close(pid, opts \\ []) do
Connection.call(pid, :close, opts[:timeout] || @timeout)
end
Expand All @@ -90,6 +90,7 @@ defmodule Romeo.Connection do
case transport.connect(conn) do
{:ok, conn} ->
{:ok, conn}

{:error, _} ->
{:backoff, timeout, conn}
end
Expand All @@ -107,14 +108,17 @@ defmodule Romeo.Connection do
def handle_call(_, _, %{socket: nil} = conn) do
{:reply, {:error, :closed}, conn}
end

def handle_call({:send, data}, _, %{transport: transport} = conn) do
case transport.send(conn, data) do
{:ok, conn} ->
{:reply, :ok, conn}

{:error, _} = error ->
{:disconnect, error, error, conn}
end
end

def handle_call(:close, from, %{socket: socket, transport: transport} = conn) do
transport.disconnect({:close, from}, socket)
{:reply, :ok, conn}
Expand All @@ -124,16 +128,20 @@ defmodule Romeo.Connection do
case transport.handle_message(info, conn) do
{:ok, conn, :more} ->
{:noreply, conn}

{:ok, conn, stanza} ->
stanza = Romeo.Stanza.Parser.parse(stanza)
Kernel.send(owner, {:stanza, stanza})
{:noreply, conn}

{:error, _} = error ->
{:disconnect, error, conn}

:unknown ->
Logger.debug fn ->
Logger.debug(fn ->
[inspect(__MODULE__), ?\s, inspect(self()), " received message: " | inspect(info)]
end
end)

{:noreply, conn}
end
end
Expand Down
25 changes: 14 additions & 11 deletions lib/romeo/connection/features.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ defmodule Romeo.Connection.Features do
use Romeo.XML

@type t :: %__MODULE__{}
defstruct [
amp?: false,
compression?: false,
registration?: false,
stream_management?: false,
tls?: false,
mechanisms: []
]
defstruct amp?: false,
compression?: false,
registration?: false,
stream_management?: false,
tls?: false,
mechanisms: []

def parse_stream_features(features) do
%__MODULE__{
Expand All @@ -31,7 +29,9 @@ defmodule Romeo.Connection.Features do
xml when Record.is_record(xml, :xmlel) ->
mechanisms = xmlel(xml, :children)
for mechanism <- mechanisms, into: [], do: Romeo.XML.cdata(mechanism)
nil -> []

nil ->
[]
end
end

Expand All @@ -40,13 +40,16 @@ defmodule Romeo.Connection.Features do
xml when Record.is_record(xml, :xmlel) ->
methods = xmlel(xml, :children)
for method <- methods, into: [], do: Romeo.XML.cdata(method)
_ -> false

_ ->
false
end
end

def supports?(features, feature) do
case Romeo.XML.subelement(features, feature) do
nil -> false
_ -> true
_ -> true
end
end
end
1 change: 1 addition & 0 deletions lib/romeo/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Romeo.Error do
secs = ms / 1_000
"Failed to #{step} after #{secs} seconds."
end

defp translate_message(message), do: inspect(message)

defp translate_connection_step(atom) do
Expand Down
Loading

0 comments on commit 5ddfcb5

Please sign in to comment.