summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/calc_handler.ex
blob: 0f74ac94a32c6a2553fc283d5de6a62cdb61425c (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
defmodule LSG.IRC.CalcHandler do
  @moduledoc """
  # calc

  * **!calc `<expression>`**: évalue l'expression mathématique `<expression>`.
  """

  def irc_doc, do: @moduledoc

  def start_link(client) do
    GenServer.start_link(__MODULE__, [client])
  end

  def init([client]) do
    ExIRC.Client.add_handler client, self
    {:ok, client}
  end

  def handle_info({:received, "!calc "<>expr, %ExIRC.SenderInfo{nick: nick}, chan}, client) do
    IO.inspect "HAZ CALC " <>inspect(expr)
    result = try do
      case Abacus.eval(expr) do
        {:ok, result} -> result
        error -> inspect(error)
      end
    rescue
      error -> "#{error.message}"
    end
    ExIRC.Client.msg(client, :privmsg, chan, "#{nick}: #{expr} = #{result}")
    {:noreply, client}
  end

  def handle_info(msg, client) do
    {:noreply, client}
  end

end