elixirc

Build StatusHex.pm Version


A small lightweight Elixir IRC library based off the Python IRC library aioyoyo/oyoyo. Available on Hex.

Example usage

defmodule ElixircTest do
  use Supervisor
  import Supervisor.Spec

  def start(_type, _args) do
    state = %Elixirc.State{address: 'chat.freenode.net',
                           port: 6667,
                           ssl: false,
                           nick: "henry232323",
                           name: "henry232323",
                           pinging: true}
    Elixirc.start!(state)
    Supervisor.start_link(__MODULE__, state, name: __MODULE__)
  end

  def init(:ok) do
    children = [
      worker(TestConsumer, [:ok])
    ]
    supervise(children, strategy: :one_for_one)
  end
end

defmodule TestConsumer do
  use Elixirc.Consumer
  alias Elixirc.Client

  def start_link(:ok) do
    Elixirc.Consumer.start_link(__MODULE__, :ok)
  end

  def handle_command(:welcome, [message], state) do
    Client.send(["JOIN", "#elixir-lang"])
    IO.inspect("We've been welcomed with the following message: #{message}")
    {:ok, state}
  end

  def handle_command(_command, _args, state) do
    {:ok, state}
  end

  def handle_event(:send, {request}, state) do
    IO.puts("Sent message #{request}")
    {:ok, state}
  end

  def handle_event(_event, _args, state) do
    {:ok, state}
  end
end

State

Commands will pass the client state. This is the default state. Whatever state you pass should have at least these keys.

defmodule Elixirc.State do
  defstruct address: "",
            port: "",
            ssl: false,
            nick: "",
            name: "",
            pass: "",
            pinging: false,
            reconnect: false,
            channels: {},
            users: %{},
            socket: nil,
            state: %{} # sub state
end

Handle Commands

handle_command/3 will be called with every message sent from the server. As shown in the above examples it will be called first with an atom representing the command (usually in all caps in the message as the first argument, or if a numeric command is preceded by a prefix) then a list of its arguments, and finally the current state as defined above.

A list of numeric events can be found here The commands used by the client can be found in the source (A little long to be listed here) or accessed as Elixirc.Events.events

Handle Event

handle_event/3 will be called with every action on the part of the client and certain other events. It operates in the same fashion as handle_command/3 with the first argument being the event, the second being a tuple of its arguments and the third being the state. Valid events include:

- :socket_closed    {reason}    The socket was closed for some reason
- :close            {}          The client/socket have been closed by user
- :send             {message}   A message has been sent to the server