Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/henry232323/elixirc
A small lightweight Elixir IRC library
https://github.com/henry232323/elixirc
Last synced: 25 days ago
JSON representation
A small lightweight Elixir IRC library
- Host: GitHub
- URL: https://github.com/henry232323/elixirc
- Owner: henry232323
- License: mit
- Created: 2017-06-03T01:05:41.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-07-30T00:11:13.000Z (over 7 years ago)
- Last Synced: 2024-03-15T06:20:15.547Z (8 months ago)
- Language: Elixir
- Size: 330 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# elixirc
[![Build Status](https://travis-ci.org/henry232323/elixirc.svg?branch=master)](https://travis-ci.org/henry232323/elixirc)
[![Hex.pm Version](http://img.shields.io/hexpm/v/exirc.svg?style=flat)](https://hex.pm/packages/elixirc)
A small lightweight Elixir IRC library based off the Python IRC library [aioyoyo](https://github.com/henry232323/aioyoyo)/oyoyo. Available on Hex.# Example usage
```elixir
defmodule ElixircTest do
use Supervisor
import Supervisor.Specdef 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__)
enddef init(:ok) do
children = [
worker(TestConsumer, [:ok])
]
supervise(children, strategy: :one_for_one)
end
enddefmodule TestConsumer do
use Elixirc.Consumer
alias Elixirc.Clientdef start_link(:ok) do
Elixirc.Consumer.start_link(__MODULE__, :ok)
enddef handle_command(:welcome, [message], state) do
Client.send(["JOIN", "#elixir-lang"])
IO.inspect("We've been welcomed with the following message: #{message}")
{:ok, state}
enddef handle_command(_command, _args, state) do
{:ok, state}
enddef handle_event(:send, {request}, state) do
IO.puts("Sent message #{request}")
{:ok, state}
enddef 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.
```elixir
defmodule Elixirc.State do
defstruct address: "",
port: "",
ssl: false,
nick: "",
name: "",
pass: "",
pinging: false,
reconnect: false,
channels: {},
users: %{},
socket: nil,
state: %{} # sub state
end
```
- Pinging: If this is enabled the client will automatically handle PINGs for you
- Reconnect: If this is enabled the client will reconnect upon a closed connection by the server
- SSL: If this is enabled the client will connect via SSL
- Socket: This is the socket that the client will read from, this is automatically overwritten
- State: A simple 'sub-state' available within the Client's state, can be modified without breaking anything
- Address: The server address
- Port: The server port
- Channels: The channel list
- Users: The user list
- Nick: The nick that will be used
- Name: The name that will be used
- Pass: The password that will be used with SASL/NickServ# 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](https://www.alien.net.au/irc/irc2numerics.html)
The commands used by the client can be found in the [source](https://github.com/henry232323/elixirc/blob/master/lib/elixirc/events.ex)
(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 currently 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