Initial IRC connection test
This commit is contained in:
parent
e6edcec903
commit
e1609f3e28
5 changed files with 82 additions and 19 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,3 +3,4 @@ nulform.sublime-workspace
|
|||
/ebin
|
||||
/deps
|
||||
erl_crash.dump
|
||||
mix.lock
|
||||
|
|
|
@ -1,2 +1,8 @@
|
|||
defmodule Nulform do
|
||||
|
||||
def run() do
|
||||
{:ok, sock} = Nulform.Connection.connect('irc.quakenet.org', 6667)
|
||||
Nulform.Connection.wait_on_socket(sock)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
54
lib/nulform/connection.ex
Normal file
54
lib/nulform/connection.ex
Normal file
|
@ -0,0 +1,54 @@
|
|||
defmodule Nulform.Connection do
|
||||
def connect(host, port) do
|
||||
{:ok, sock} = :gen_tcp.connect host, port, [
|
||||
{:active, :true}, :binary, :inet, {:packet, :line}
|
||||
]
|
||||
end
|
||||
|
||||
def wait_on_socket(sock) do
|
||||
receive do
|
||||
{:tcp, sock, msg} ->
|
||||
stripped = String.rstrip msg
|
||||
IO.puts stripped
|
||||
|
||||
|
||||
case stripped do
|
||||
"NOTICE AUTH :*** Looking up your hostname" ->
|
||||
send sock, "NICK Nulform"
|
||||
send sock, "USER nulform 8 * :␀form"
|
||||
|
||||
_ ->
|
||||
|
||||
end
|
||||
|
||||
case String.split stripped, ":" do
|
||||
["PING " | rest]
|
||||
when is_binary rest ->
|
||||
:ok = :gen_tcp.send sock, rest <> "\r\n"
|
||||
|
||||
["PING " | rest]
|
||||
when is_list rest ->
|
||||
concat_list = fn
|
||||
f, list, sep when length(list) > 1 ->
|
||||
[now | rest] = list
|
||||
now <> sep <> f.(rest, sep)
|
||||
_, list, _ ->
|
||||
[last] = list
|
||||
last
|
||||
end
|
||||
|
||||
send sock, "PONG :" <> concat_list.(concat_list, rest, ":")
|
||||
|
||||
|
||||
_ ->
|
||||
end
|
||||
end
|
||||
|
||||
wait_on_socket sock
|
||||
end
|
||||
|
||||
def send(sock, msg) do
|
||||
:ok = :gen_tcp.send sock, msg <> "\r\n"
|
||||
IO.puts("<- " <> msg)
|
||||
end
|
||||
end
|
4
mix.exs
4
mix.exs
|
@ -15,6 +15,8 @@ defmodule Nulform.Mixfile do
|
|||
# Returns the list of dependencies in the format:
|
||||
# { :foobar, "0.1", git: "https://github.com/elixir-lang/foobar.git" }
|
||||
defp deps do
|
||||
[]
|
||||
[
|
||||
{ :socket, "0.0.1", git: "https://github.com/meh/elixir-socket" }
|
||||
]
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue