Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace all joinsas with joinas spec #614

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 53 additions & 23 deletions lib/teiserver/data/cache_user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -661,35 +661,65 @@ defmodule Teiserver.CacheUser do
def send_direct_message(_, _, nil), do: :ok

def send_direct_message(from_id, to_id, message) do
if String.starts_with?(message, "!clan") do
host = Application.get_env(:teiserver, TeiserverWeb.Endpoint)[:url][:host]
website_url = "https://#{host}"
# Replace SPADS command (starting with !) with lowercase version to prevent bypassing with capitalised command names
# Ignore !# bot commands like !#JSONRPC
message =
if String.starts_with?(message, "!") and !String.starts_with?(message, "!#") do
[cmd | rest] = String.split(message, " ", parts: 2)

cmd
|> String.trim()
|> String.downcase()
|> case do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, didn't know you could do |> case do that's neat

"!cv joinas" ->
"!cv joinas spec"

"!callvote joinas spec" ->
"!callvote joinas spec"

"!joinas" ->
"!joinas spec"

"!clan" ->
clan_command(from_id)
"!clan"

processed_cmd ->
[processed_cmd | rest] |> Enum.join(" ")
end
else
message
end

Coordinator.send_to_user(
from_id,
"SPADS clans have been replaced by parties. You can access them via #{website_url}/teiserver/parties."
)
send_direct_message(from_id, to_id, [message])
end

uuid = ExULID.ULID.generate()
client = Account.get_client_by_id(from_id)
defp clan_command(from_id) do
host = Application.get_env(:teiserver, TeiserverWeb.Endpoint)[:url][:host]
website_url = "https://#{host}"

{:ok, _code} =
Account.create_code(%{
value: uuid <> "$#{client.ip}",
purpose: "one_time_login",
expires: Timex.now() |> Timex.shift(minutes: 5),
user_id: from_id
})
Coordinator.send_to_user(
from_id,
"SPADS clans have been replaced by parties. You can access them via #{website_url}/teiserver/parties."
)

url = "https://#{host}/one_time_login/#{uuid}"
uuid = ExULID.ULID.generate()
client = Account.get_client_by_id(from_id)

Coordinator.send_to_user(
from_id,
"If you have not already logged in, here is a one-time link to do so automatically - #{url}"
)
end
{:ok, _code} =
Account.create_code(%{
value: uuid <> "$#{client.ip}",
purpose: "one_time_login",
expires: Timex.now() |> Timex.shift(minutes: 5),
user_id: from_id
})

send_direct_message(from_id, to_id, [message])
url = "https://#{host}/one_time_login/#{uuid}"

Coordinator.send_to_user(
from_id,
"If you have not already logged in, here is a one-time link to do so automatically - #{url}"
)
end

@spec ring(T.userid(), T.userid()) :: :ok
Expand Down
88 changes: 58 additions & 30 deletions lib/teiserver/lobby/libs/chat_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,41 @@ defmodule Teiserver.Lobby.ChatLib do
def say(nil, _, _), do: {:error, "No userid"}
def say(_, _, nil), do: {:error, "No lobby"}

def say(userid, "!joinas spec", lobby_id), do: say(userid, "!!joinas spec", lobby_id)
def say(userid, "!joinas" <> s, lobby_id), do: say(userid, "!cv joinas" <> s, lobby_id)
def say(userid, "!joinq", lobby_id), do: say(userid, "$joinq", lobby_id)
def say(_userid, "!specafk" <> _, _lobby_id), do: :ok

def say(userid, "!clan" <> _, _lobby_id) do
client = Account.get_client_by_id(userid)

{:ok, code} =
Account.create_code(%{
value: ExULID.ULID.generate(),
purpose: "one_time_login",
expires: Timex.now() |> Timex.shift(minutes: 5),
user_id: userid,
metadata: %{
ip: client.ip,
redirect: "/teiserver/account/parties"
}
})

host = Application.get_env(:teiserver, TeiserverWeb.Endpoint)[:url][:host]
url = "https://#{host}/one_time_login/#{code.value}"

Coordinator.send_to_user(userid, [
"To access parties please use this link - #{url}",
"You can use the $explain command to see how balance is being calculated and why you are/are not being teamed with your party",
"We are working on handling it within the new client and protocol, the website is only a temporary measure."
])
end

def say(userid, msg, lobby_id) do
msg = String.replace(msg, "!!joinas spec", "!joinas spec")
# Replace SPADS command (starting with !) with lowercase version to prevent bypassing with capitalised command names
# Ignore !# bot commands like !#JSONRPC
msg =
if String.starts_with?(msg, "!") and !String.starts_with?(msg, "!#") do
[cmd | rest] = String.split(msg, " ", parts: 2)

cmd
|> String.trim()
|> String.downcase()
|> case do
"!cv joinas" ->
"!cv joinas spec"

"!callvote joinas spec" ->
"!callvote joinas spec"

"!joinas" ->
"!joinas spec"

"!clan" ->
clan_command(userid)
"!clan"

"!joinq" ->
"$joinq"

processed_cmd ->
[processed_cmd | rest] |> Enum.join(" ")
end
else
msg
end

case Teiserver.Coordinator.Parser.handle_in(userid, msg, lobby_id) do
:say -> do_say(userid, msg, lobby_id)
Expand Down Expand Up @@ -69,7 +72,7 @@ defmodule Teiserver.Lobby.ChatLib do
CacheUser.is_restricted?(user, ["All chat", "Lobby chat"]) ->
msg_allowed_when_muted?(msg)

String.slice(msg, 0..0) == "!" and CacheUser.is_restricted?(user, ["Host commands"]) ->
String.starts_with?(msg, "!") and CacheUser.is_restricted?(user, ["Host commands"]) ->
false

blacklisted ->
Expand Down Expand Up @@ -316,4 +319,29 @@ defmodule Teiserver.Lobby.ChatLib do
String.match?(msg, regex)
end)
end

defp clan_command(user_id) do
client = Account.get_client_by_id(user_id)

{:ok, code} =
Account.create_code(%{
value: ExULID.ULID.generate(),
purpose: "one_time_login",
expires: Timex.now() |> Timex.shift(minutes: 5),
user_id: user_id,
metadata: %{
ip: client.ip,
redirect: "/teiserver/account/parties"
}
})

host = Application.get_env(:teiserver, TeiserverWeb.Endpoint)[:url][:host]
url = "https://#{host}/one_time_login/#{code.value}"

Coordinator.send_to_user(user_id, [
"To access parties please use this link - #{url}",
"You can use the $explain command to see how balance is being calculated and why you are/are not being teamed with your party",
"We are working on handling it within the new client and protocol, the website is only a temporary measure."
])
end
end
Loading