initial project commit

This commit is contained in:
ewood 2022-05-25 09:40:49 -06:00
parent defdcb2f9e
commit 4686a7f57d
7 changed files with 168 additions and 2 deletions

4
.formatter.exs Normal file
View File

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

View File

@ -1,3 +1,20 @@
# smolex
# Smolex
An elixir TUI client for Gemini/Spartan and the smolnet in general.
An elixir TUI client for Gemini/Spartan and the smolnet in general.
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `smolex` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:smolex, "~> 0.1.0"}
]
end
```
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/smolex>.

87
lib/smolex.ex Normal file
View File

@ -0,0 +1,87 @@
defmodule Smolex do
use GenServer
@moduledoc """
Documentation for `Smolex`.
"""
@crlf "\r\n"
@impl true
def init(state) do
{:ok, state}
end
def start_link(initial_state) do
GenServer.start_link(__MODULE__, initial_state, name: __MODULE__)
end
@impl true
def handle_call({:connect, url}, _from, state) do
{:reply, connect(url), state}
end
@impl true
def handle_info({:ssl, sslsocket, content}, state) do
{:noreply, state <> parse_response(to_string(content))}
end
@impl true
def handle_info({:ssl_closed, sslsocket}, state) do
IO.puts(state)
{:noreply, state}
end
def prefixes do
["gemini://", "spartan://"]
end
def request(url) do
if String.starts_with?(url, prefixes()) do
GenServer.call(__MODULE__, {:connect, url}, :infinity)
else
IO.puts("Error sending request")
end
end
def set_opts do
opts = [verify: :verify_none, active: :true]
end
def extract_domain(url) do
String.replace(url, prefixes(), "")
|> String.replace(~r/\/(.*)/, "")
end
def connect(url) do
:ssl.start()
opts = set_opts()
port = 1965
domain = extract_domain(url)
{:ok, sslsocket} = :ssl.connect(String.to_charlist(domain), port, opts)
crlf_url = url <> @crlf
status = :ssl.send(sslsocket, String.to_charlist(crlf_url))
unless status === :ok do
IO.puts("Error sending request")
end
end
def parse_response(content) do
String.replace(content, "20 text/gemini\r\n", "")
|> to_string
end
def process_gemini(content) do
String.replace(content, "20 text/gemini\r\n", "")
|> to_string
end
def process_spartan(content) do
IO.puts("Not implemented!")
end
end

20
lib/smolex/application.ex Normal file
View File

@ -0,0 +1,20 @@
defmodule Smolex.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
@impl true
def start(_type, _args) do
children = [
# Starts a worker by calling: Smolex.Worker.start_link(arg)
# {Smolex.Worker, arg}
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Smolex.Supervisor]
Supervisor.start_link(children, opts)
end
end

29
mix.exs Normal file
View File

@ -0,0 +1,29 @@
defmodule Smolex.MixProject do
use Mix.Project
def project do
[
app: :smolex,
version: "0.1.0",
elixir: "~> 1.13",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger, :ssl],
mod: {Smolex.Application, []}
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end

8
test/smolex_test.exs Normal file
View File

@ -0,0 +1,8 @@
defmodule SmolexTest do
use ExUnit.Case
doctest Smolex
test "greets the world" do
assert Smolex.hello() == :world
end
end

1
test/test_helper.exs Normal file
View File

@ -0,0 +1 @@
ExUnit.start()