commit
0ff0403a1b
@ -0,0 +1,4 @@ |
|||||||
|
# Used by "mix format" |
||||||
|
[ |
||||||
|
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] |
||||||
|
] |
@ -0,0 +1,26 @@ |
|||||||
|
# The directory Mix will write compiled artifacts to. |
||||||
|
/_build/ |
||||||
|
|
||||||
|
# If you run "mix test --cover", coverage assets end up here. |
||||||
|
/cover/ |
||||||
|
|
||||||
|
# The directory Mix downloads your dependencies sources to. |
||||||
|
/deps/ |
||||||
|
|
||||||
|
# Where third-party dependencies like ExDoc output generated docs. |
||||||
|
/doc/ |
||||||
|
|
||||||
|
# Ignore .fetch files in case you like to edit your project deps locally. |
||||||
|
/.fetch |
||||||
|
|
||||||
|
# If the VM crashes, it generates a dump, let's ignore it too. |
||||||
|
erl_crash.dump |
||||||
|
|
||||||
|
# Also ignore archive artifacts (built via "mix archive.build"). |
||||||
|
*.ez |
||||||
|
|
||||||
|
# Ignore package tarball (built via "mix hex.build"). |
||||||
|
ircked_elixir-*.tar |
||||||
|
|
||||||
|
# Temporary files, for example, from tests. |
||||||
|
/tmp/ |
@ -0,0 +1,21 @@ |
|||||||
|
# IrckedElixir |
||||||
|
|
||||||
|
**TODO: Add description** |
||||||
|
|
||||||
|
## Installation |
||||||
|
|
||||||
|
If [available in Hex](https://hex.pm/docs/publish), the package can be installed |
||||||
|
by adding `ircked_elixir` to your list of dependencies in `mix.exs`: |
||||||
|
|
||||||
|
```elixir |
||||||
|
def deps do |
||||||
|
[ |
||||||
|
{:ircked_elixir, "~> 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/ircked_elixir>. |
||||||
|
|
@ -0,0 +1,9 @@ |
|||||||
|
defmodule IrckedElixir do |
||||||
|
use Application |
||||||
|
|
||||||
|
@impl true |
||||||
|
def start(_type, _args) do |
||||||
|
IO.puts("starting~") |
||||||
|
IrckedElixir.Overseer.start_link(name: IrckedElixir.Overseer) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,29 @@ |
|||||||
|
defmodule IrckedElixir.Chatter do |
||||||
|
alias IrckedElixir.Chatter.Message, as: Message |
||||||
|
alias IrckedElixir.Chatter.Privmsg, as: Privmsg |
||||||
|
def start(server, port, nick) do |
||||||
|
{:ok, socket} = :gen_tcp.connect(to_charlist(server), port, [:binary, active: false]) |
||||||
|
|
||||||
|
:gen_tcp.send(socket, ["USER ", nick, " 0 * :", nick, "\r\n"]) |
||||||
|
:gen_tcp.send(socket, ["NICK ", nick, "\r\n"]) |
||||||
|
|
||||||
|
run(socket) |
||||||
|
end |
||||||
|
def run(socket) do |
||||||
|
{:ok, data} = :gen_tcp.recv(socket, 0) |
||||||
|
String.split(data, "\r\n") |> Enum.each(fn raw -> handle(Message.parse(raw), socket) end) |
||||||
|
run(socket) |
||||||
|
end |
||||||
|
def handle(msg, socket) do |
||||||
|
case msg.command do |
||||||
|
"PING" -> Message.construct("", "PONG", msg.parameters) |> Message.send(socket) |
||||||
|
"001" -> Message.construct("", "JOIN", ["#qrs"]) |> Message.send(socket) |
||||||
|
"PRIVMSG" -> |
||||||
|
pm = Privmsg.parse(msg) |
||||||
|
IO.puts(inspect pm.message) |
||||||
|
Privmsg.construct("functism", pm.to, "hai").message |> Message.send(socket) |
||||||
|
"" -> "" |
||||||
|
_ -> IO.puts(inspect msg) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,20 @@ |
|||||||
|
defmodule IrckedElixir.Chatter.Message do |
||||||
|
defstruct prefix: "", command: "", parameters: [] |
||||||
|
def parse(raw) do |
||||||
|
parts = String.split(raw, " ") |
||||||
|
|
||||||
|
is_prefix = String.starts_with?(Enum.at(parts, 0), ":") |
||||||
|
|
||||||
|
pre = if(is_prefix, [do: Enum.at(parts, 0), else: ""]) |
||||||
|
comm = if(is_prefix, [do: Enum.at(parts, 1), else: Enum.at(parts, 0)]) |
||||||
|
param = if(is_prefix, [do: Enum.slice(parts, 2..999), else: Enum.slice(parts, 1..999)]) |
||||||
|
%IrckedElixir.Chatter.Message{prefix: pre, command: comm, parameters: param} |
||||||
|
end |
||||||
|
def construct(prefix, command, parameters) do |
||||||
|
%IrckedElixir.Chatter.Message{prefix: prefix, command: command, parameters: parameters} |
||||||
|
end |
||||||
|
def send(msg, socket) do |
||||||
|
:gen_tcp.send(socket, [if(msg.prefix == "", [do: "", else: msg.prefix<>" "]), msg.command, " ", Enum.join(msg.parameters, " "), "\r\n"]) |
||||||
|
IO.puts([if(msg.prefix == "", [do: "", else: msg.prefix<>" "]), msg.command, " ", Enum.join(msg.parameters, " "), "\r\n"]) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,9 @@ |
|||||||
|
defmodule IrckedElixir.Chatter.Privmsg do |
||||||
|
defstruct message: %IrckedElixir.Chatter.Message{}, from: "", to: "", body: "" |
||||||
|
def parse(message) do |
||||||
|
%IrckedElixir.Chatter.Privmsg{message: message, from: String.slice(message.prefix, 1..999), to: Enum.at(message.parameters, 0), body: Enum.join(Enum.slice(message.parameters, 1..999), " ")} |
||||||
|
end |
||||||
|
def construct(from, to, body) do |
||||||
|
%IrckedElixir.Chatter.Privmsg{message: %IrckedElixir.Chatter.Message{prefix: ":"<>from, command: "PRIVMSG", parameters: ([] |> List.insert_at(0, to)) ++ String.split(":"<>body, " ")}, from: from, to: to, body: body} |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,15 @@ |
|||||||
|
defmodule IrckedElixir.Overseer do |
||||||
|
use Supervisor |
||||||
|
|
||||||
|
def start_link(opts) do |
||||||
|
Supervisor.start_link(__MODULE__, :ok, opts) |
||||||
|
end |
||||||
|
|
||||||
|
@impl true |
||||||
|
def init(:ok) do |
||||||
|
children = [ |
||||||
|
{Task, fn -> IrckedElixir.Chatter.start("localhost", 6667, "functism") end} |
||||||
|
] |
||||||
|
Supervisor.init(children, strategy: :one_for_one) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,29 @@ |
|||||||
|
defmodule IrckedElixir.MixProject do |
||||||
|
use Mix.Project |
||||||
|
|
||||||
|
def project do |
||||||
|
[ |
||||||
|
app: :ircked_elixir, |
||||||
|
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 |
||||||
|
[ |
||||||
|
mod: {IrckedElixir, []}, |
||||||
|
extra_applications: [:logger] |
||||||
|
] |
||||||
|
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 |
@ -0,0 +1,4 @@ |
|||||||
|
defmodule IrckedElixirTest do |
||||||
|
use ExUnit.Case |
||||||
|
doctest IrckedElixir |
||||||
|
end |
@ -0,0 +1 @@ |
|||||||
|
ExUnit.start() |
Loading…
Reference in new issue