Initial IRC connection test

This commit is contained in:
Mikko Ahlroth 2013-06-14 00:49:31 +03:00
parent e6edcec903
commit e1609f3e28
5 changed files with 82 additions and 19 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@ nulform.sublime-workspace
/ebin
/deps
erl_crash.dump
mix.lock

View file

@ -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
View 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

32
mix.exs
View file

@ -1,20 +1,22 @@
defmodule Nulform.Mixfile do
use Mix.Project
use Mix.Project
def project do
[ app: :nulform,
version: "0.0.1",
deps: deps ]
end
def project do
[ app: :nulform,
version: "0.0.1",
deps: deps ]
end
# Configuration for the OTP application
def application do
[]
end
# Configuration for the OTP application
def application do
[]
end
# Returns the list of dependencies in the format:
# { :foobar, "0.1", git: "https://github.com/elixir-lang/foobar.git" }
defp deps do
[]
end
# 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

View file

@ -1,9 +1,9 @@
Code.require_file "test_helper.exs", __DIR__
defmodule NulformTest do
use ExUnit.Case
use ExUnit.Case
test "the truth" do
assert(true)
end
test "the truth" do
assert(true)
end
end