2.1 KiB · text History 4b6fe3b
defmodule Mix.Tasks.Opers.Grant do
  @shortdoc "Grant IRC operator status to a user account"

  @moduledoc """
  Flip a user's `is_oper` flag. After this they can `OPER` over IRC
  with no arguments (assuming they SASL-authenticate when connecting).

      mix opers.grant alice@example.com
  """

  use Mix.Task

  @impl Mix.Task
  def run([email]) do
    Mix.Task.run("app.start")
    do_set(email, true)
  end

  def run(_), do: Mix.raise("Usage: mix opers.grant <email>")

  defp do_set(email, flag) do
    case OhMyExOhIrcd.Accounts.get_user_by_email(email) do
      nil ->
        Mix.raise("No account with email #{email}")

      user ->
        case OhMyExOhIrcd.Accounts.set_oper(user, flag) do
          {:ok, updated} ->
            Mix.shell().info(
              "#{updated.email}: is_oper=#{updated.is_oper}"
            )

          {:error, cs} ->
            Mix.raise("Update failed: #{inspect(cs.errors)}")
        end
    end
  end
end

defmodule Mix.Tasks.Opers.Revoke do
  @shortdoc "Revoke IRC operator status from a user account"
  @moduledoc "See `mix help opers.grant` — this is the inverse."

  use Mix.Task

  @impl Mix.Task
  def run([email]) do
    Mix.Task.run("app.start")

    case OhMyExOhIrcd.Accounts.get_user_by_email(email) do
      nil ->
        Mix.raise("No account with email #{email}")

      user ->
        case OhMyExOhIrcd.Accounts.set_oper(user, false) do
          {:ok, updated} -> Mix.shell().info("#{updated.email}: is_oper=false")
          {:error, cs} -> Mix.raise("Update failed: #{inspect(cs.errors)}")
        end
    end
  end

  def run(_), do: Mix.raise("Usage: mix opers.revoke <email>")
end

defmodule Mix.Tasks.Opers.List do
  @shortdoc "List accounts flagged as IRC operators"

  use Mix.Task

  @impl Mix.Task
  def run(_) do
    Mix.Task.run("app.start")

    import Ecto.Query

    OhMyExOhIrcd.Repo.all(
      from u in OhMyExOhIrcd.Accounts.User,
        where: u.is_oper == true,
        order_by: [asc: u.email]
    )
    |> case do
      [] ->
        Mix.shell().info("(no IRC operators flagged)")

      users ->
        Enum.each(users, fn u -> Mix.shell().info(u.email) end)
    end
  end
end