summaryrefslogtreecommitdiff
path: root/lib/exirc/exirc.ex
blob: b32af540d3308dedfbcce9951e52aac58681ed3d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
defmodule ExIrc do
  @moduledoc """
  Supervises IRC client processes

  Usage:

      # Start the supervisor (started automatically when ExIrc is run as an application)
      ExIrc.start_link

      # Start a new IRC client
      {:ok, client} = ExIrc.start_client!

      # Connect to an IRC server
      ExIrc.Client.connect! client, "localhost", 6667

      # Logon
      ExIrc.Client.logon client, "password", "nick", "user", "name"

      # Join a channel (password is optional)
      ExIrc.Client.join client, "#channel", "password"

      # Send a message
      ExIrc.Client.msg client, :privmsg, "#channel", "Hello world!"

      # Quit (message is optional)
      ExIrc.Client.quit client, "message"

      # Stop and close the client connection
      ExIrc.Client.stop! client

  """
  use Supervisor
  import Supervisor.Spec

  ##############
  # Public API
  ##############

  @doc """
  Start the ExIrc supervisor.
  """
  @spec start! :: {:ok, pid} | {:error, term}
  def start! do
    Supervisor.start_link(__MODULE__, [], name: :exirc)
  end

  @doc """
  Start a new ExIrc client
  """
  @spec start_client! :: {:ok, pid} | {:error, term}
  def start_client! do
    # Start the client worker
    Supervisor.start_child(:exirc, [[owner: self()]])
  end

  ##############
  # Supervisor API
  ##############

  @spec init(any) :: {:ok, pid} | {:error, term}
  def init(_) do
    children = [
      worker(ExIrc.Client, [], restart: :temporary)
    ]
    supervise children, strategy: :simple_one_for_one
  end

end