diff --git a/.gitignore b/.gitignore index 2c31110..9a14ded 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ nulform.sublime-workspace /ebin /deps erl_crash.dump +mix.lock diff --git a/lib/nulform.ex b/lib/nulform.ex index 6d2f7a6..131692e 100644 --- a/lib/nulform.ex +++ b/lib/nulform.ex @@ -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 diff --git a/lib/nulform/connection.ex b/lib/nulform/connection.ex new file mode 100644 index 0000000..6cebc6a --- /dev/null +++ b/lib/nulform/connection.ex @@ -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 diff --git a/mix.exs b/mix.exs index b00cdb5..6472de3 100644 --- a/mix.exs +++ b/mix.exs @@ -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 diff --git a/test/nulform_test.exs b/test/nulform_test.exs index 7607917..968c041 100644 --- a/test/nulform_test.exs +++ b/test/nulform_test.exs @@ -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