summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorPaul Schoenfelder <paulschoenfelder@gmail.com>2016-02-27 21:59:42 -0600
committerPaul Schoenfelder <paulschoenfelder@gmail.com>2016-02-27 21:59:42 -0600
commit17c2b56ed9565cb05f72d7a6ff212027da24d2a9 (patch)
treebb39a6215c3e1dc41dd863e0ada88d21d60c8933 /examples
parentAdd earmark dep for ex_doc (diff)
Add new example. Closes #38
Diffstat (limited to 'examples')
-rw-r--r--examples/README.md7
-rw-r--r--examples/bot/.gitignore5
-rw-r--r--examples/bot/config/config.exs36
-rw-r--r--examples/bot/lib/bot.ex107
-rw-r--r--examples/bot/lib/example.ex19
-rw-r--r--examples/bot/mix.exs33
-rw-r--r--examples/bot/mix.lock1
-rw-r--r--examples/bot/test/exirc_example_test.exs8
-rw-r--r--examples/bot/test/test_helper.exs1
9 files changed, 217 insertions, 0 deletions
diff --git a/examples/README.md b/examples/README.md
new file mode 100644
index 0000000..d51b9a2
--- /dev/null
+++ b/examples/README.md
@@ -0,0 +1,7 @@
+## Example Bot
+
+Add bot configuration or change pre-existing one in `config/config.exs`
+
+Run using `mix run --no-halt` or `iex -S mix`, some basic info will be output to the console.
+
+Default app connects to a random room on Freenode, should just work out of the box.
diff --git a/examples/bot/.gitignore b/examples/bot/.gitignore
new file mode 100644
index 0000000..755b605
--- /dev/null
+++ b/examples/bot/.gitignore
@@ -0,0 +1,5 @@
+/_build
+/cover
+/deps
+erl_crash.dump
+*.ez
diff --git a/examples/bot/config/config.exs b/examples/bot/config/config.exs
new file mode 100644
index 0000000..e0d35fe
--- /dev/null
+++ b/examples/bot/config/config.exs
@@ -0,0 +1,36 @@
+# This file is responsible for configuring your application
+# and its dependencies with the aid of the Mix.Config module.
+use Mix.Config
+
+config :exirc_example, bots: [
+ %{:server => "chat.freenode.net", :port => 6667,
+ :nick => "exirc-example", :user => "exirc-example", :name => "ExIrc Example Bot",
+ :channel => "##exirc-test"}
+]
+
+# This configuration is loaded before any dependency and is restricted
+# to this project. If another project depends on this project, this
+# file won't be loaded nor affect the parent project. For this reason,
+# if you want to provide default values for your application for
+# 3rd-party users, it should be done in your "mix.exs" file.
+
+# You can configure for your application as:
+#
+# config :exirc_example, key: :value
+#
+# And access this configuration in your application as:
+#
+# Application.get_env(:exirc_example, :key)
+#
+# Or configure a 3rd-party app:
+#
+# config :logger, level: :info
+#
+
+# It is also possible to import configuration files, relative to this
+# directory. For example, you can emulate configuration per environment
+# by uncommenting the line below and defining dev.exs, test.exs and such.
+# Configuration from the imported file will override the ones defined
+# here (which is why it is important to import them last).
+#
+# import_config "#{Mix.env}.exs"
diff --git a/examples/bot/lib/bot.ex b/examples/bot/lib/bot.ex
new file mode 100644
index 0000000..e73bb4c
--- /dev/null
+++ b/examples/bot/lib/bot.ex
@@ -0,0 +1,107 @@
+defmodule Example.Bot do
+ use GenServer
+ require Logger
+
+ defmodule Config do
+ defstruct server: nil,
+ port: nil,
+ pass: nil,
+ nick: nil,
+ user: nil,
+ name: nil,
+ channel: nil,
+ client: nil
+
+ def from_params(params) when is_map(params) do
+ Enum.reduce(params, %Config{}, fn {k, v}, acc ->
+ case Map.has_key?(acc, k) do
+ true -> Map.put(acc, k, v)
+ false -> acc
+ end
+ end)
+ end
+ end
+
+ alias ExIrc.Client
+
+ def start_link(%{:nick => nick} = params) when is_map(params) do
+ config = Config.from_params(params)
+ GenServer.start_link(__MODULE__, [config], name: String.to_atom(nick))
+ end
+
+ def init([config]) do
+ # Start the client and handler processes, the ExIrc supervisor is automatically started when your app runs
+ {:ok, client} = ExIrc.start_client!()
+
+ # Register the event handler with ExIrc
+ Client.add_handler client, self()
+
+ # Connect and logon to a server, join a channel and send a simple message
+ Logger.debug "Connecting to #{server}:#{port}"
+ Client.connect! client, config.server, config.port
+
+ {:ok, %Config{config | :client => client}}
+ end
+
+ def handle_info({:connected, server, port}, config) do
+ Logger.debug "Connected to #{server}:#{port}"
+ Logger.debug "Logging to #{server}:#{port} as #{config.nick}.."
+ Client.logon config.client, config.pass, config.nick, config.user, config.name
+ {:noreply, config}
+ end
+ def handle_info(:logged_in, config) do
+ Logger.debug "Logged in to #{config.server}:#{config.port}"
+ Logger.debug "Joining #{config.channel}.."
+ Client.join config.client, config.channel
+ {:noreply, config}
+ end
+ def handle_info(:disconnected, config) do
+ Logger.debug "Disconnected from #{config.server}:#{config.port}"
+ {:stop, :normal, config}
+ end
+ def handle_info({:joined, channel}, config) do
+ Logger.debug "Joined #{channel}"
+ Client.msg config.client, :privmsg, config.channel, "Hello world!"
+ {:noreply, config}
+ end
+ def handle_info({:names_list, channel, names_list}, config) do
+ names = String.split(names_list, " ", trim: true)
+ |> Enum.map(fn name -> " #{name}\n" end)
+ Logger.info "Users logged in to #{channel}:\n#{names}"
+ {:noreply, config}
+ end
+ def handle_info({:received, msg, nick, channel}, config) do
+ Logger.info "#{nick} from #{channel}: #{msg}"
+ {:noreply, config}
+ end
+ def handle_info({:mentioned, msg, nick, channel}, config) do
+ Logger.warn "#{nick} mentioned you in #{channel}"
+ case String.contains?(msg, "hi") do
+ true ->
+ reply = "Hi #{nick}!"
+ Client.msg config.client, :privmsg, config.channel, reply
+ Logger.info "Sent #{reply} to #{config.channel}"
+ false ->
+ :ok
+ end
+ {:noreply, config}
+ end
+ def handle_info({:received, msg, nick}, config) do
+ Logger.warn "#{nick}: #{msg}"
+ reply = "Hi!"
+ Client.msg config.client, :privmsg, nick, reply
+ Logger.info "Sent #{reply} to #{nick}"
+ {:noreply, config}
+ end
+ # Catch-all for messages you don't care about
+ def handle_info(_msg, config) do
+ {:noreply, config}
+ end
+
+ def terminate(_, state) do
+ # Quit the channel and close the underlying client connection when the process is terminating
+ Client.quit state.client, "Goodbye, cruel world."
+ Client.stop! state.client
+ :ok
+ end
+end
diff --git a/examples/bot/lib/example.ex b/examples/bot/lib/example.ex
new file mode 100644
index 0000000..42a71a9
--- /dev/null
+++ b/examples/bot/lib/example.ex
@@ -0,0 +1,19 @@
+defmodule Example do
+ use Application
+
+ alias Example.Bot
+
+ # See http://elixir-lang.org/docs/stable/elixir/Application.html
+ # for more information on OTP Applications
+ def start(_type, _args) do
+ import Supervisor.Spec, warn: false
+
+ children = Application.get_env(:exirc_example, :bots)
+ |> Enum.map(fn bot -> worker(Bot, [bot]) end)
+
+ # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
+ # for other strategies and supported options
+ opts = [strategy: :one_for_one, name: Example.Supervisor]
+ Supervisor.start_link(children, opts)
+ end
+end
diff --git a/examples/bot/mix.exs b/examples/bot/mix.exs
new file mode 100644
index 0000000..eca670f
--- /dev/null
+++ b/examples/bot/mix.exs
@@ -0,0 +1,33 @@
+defmodule Example.Mixfile do
+ use Mix.Project
+
+ def project do
+ [app: :exirc_example,
+ version: "0.0.1",
+ elixir: "~> 1.2",
+ build_embedded: Mix.env == :prod,
+ start_permanent: Mix.env == :prod,
+ deps: deps]
+ end
+
+ # Configuration for the OTP application
+ #
+ # Type "mix help compile.app" for more information
+ def application do
+ [applications: [:logger, :exirc],
+ mod: {Example, []}]
+ end
+
+ # Dependencies can be Hex packages:
+ #
+ # {:mydep, "~> 0.3.0"}
+ #
+ # Or git/path repositories:
+ #
+ # {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
+ #
+ # Type "mix help deps" for more examples and options
+ defp deps do
+ [{:exirc, ">= 0.0.0"}]
+ end
+end
diff --git a/examples/bot/mix.lock b/examples/bot/mix.lock
new file mode 100644
index 0000000..5f28bb5
--- /dev/null
+++ b/examples/bot/mix.lock
@@ -0,0 +1 @@
+%{"exirc": {:hex, :exirc, "0.10.0"}}
diff --git a/examples/bot/test/exirc_example_test.exs b/examples/bot/test/exirc_example_test.exs
new file mode 100644
index 0000000..4f58a9d
--- /dev/null
+++ b/examples/bot/test/exirc_example_test.exs
@@ -0,0 +1,8 @@
+defmodule ExircExampleTest do
+ use ExUnit.Case
+ doctest ExircExample
+
+ test "the truth" do
+ assert 1 + 1 == 2
+ end
+end
diff --git a/examples/bot/test/test_helper.exs b/examples/bot/test/test_helper.exs
new file mode 100644
index 0000000..869559e
--- /dev/null
+++ b/examples/bot/test/test_helper.exs
@@ -0,0 +1 @@
+ExUnit.start()