summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/calc_handler.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lsg_irc/calc_handler.ex')
-rw-r--r--lib/lsg_irc/calc_handler.ex38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/lsg_irc/calc_handler.ex b/lib/lsg_irc/calc_handler.ex
new file mode 100644
index 0000000..0f74ac9
--- /dev/null
+++ b/lib/lsg_irc/calc_handler.ex
@@ -0,0 +1,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
+