mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Perform validation on all kinds of chat message
This commit is contained in:
parent
4e103c433f
commit
602de267b1
@ -124,6 +124,28 @@ impl<G> ChatType<G> {
|
|||||||
message: msg.into(),
|
message: msg.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn uid(&self) -> Option<Uid> {
|
||||||
|
match self {
|
||||||
|
ChatType::Online(_) => None,
|
||||||
|
ChatType::Offline(_) => None,
|
||||||
|
ChatType::CommandInfo => None,
|
||||||
|
ChatType::CommandError => None,
|
||||||
|
ChatType::FactionMeta(_) => None,
|
||||||
|
ChatType::GroupMeta(_) => None,
|
||||||
|
ChatType::Kill(_, _) => None,
|
||||||
|
ChatType::Tell(u, _t) => Some(*u),
|
||||||
|
ChatType::Say(u) => Some(*u),
|
||||||
|
ChatType::Group(u, _s) => Some(*u),
|
||||||
|
ChatType::Faction(u, _s) => Some(*u),
|
||||||
|
ChatType::Region(u) => Some(*u),
|
||||||
|
ChatType::World(u) => Some(*u),
|
||||||
|
ChatType::Npc(u, _r) => Some(*u),
|
||||||
|
ChatType::NpcSay(u, _r) => Some(*u),
|
||||||
|
ChatType::NpcTell(u, _t, _r) => Some(*u),
|
||||||
|
ChatType::Meta => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stores chat text, type
|
// Stores chat text, type
|
||||||
@ -226,27 +248,7 @@ impl<G> GenericChatMsg<G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uid(&self) -> Option<Uid> {
|
pub fn uid(&self) -> Option<Uid> { self.chat_type.uid() }
|
||||||
match &self.chat_type {
|
|
||||||
ChatType::Online(_) => None,
|
|
||||||
ChatType::Offline(_) => None,
|
|
||||||
ChatType::CommandInfo => None,
|
|
||||||
ChatType::CommandError => None,
|
|
||||||
ChatType::FactionMeta(_) => None,
|
|
||||||
ChatType::GroupMeta(_) => None,
|
|
||||||
ChatType::Kill(_, _) => None,
|
|
||||||
ChatType::Tell(u, _t) => Some(*u),
|
|
||||||
ChatType::Say(u) => Some(*u),
|
|
||||||
ChatType::Group(u, _s) => Some(*u),
|
|
||||||
ChatType::Faction(u, _s) => Some(*u),
|
|
||||||
ChatType::Region(u) => Some(*u),
|
|
||||||
ChatType::World(u) => Some(*u),
|
|
||||||
ChatType::Npc(u, _r) => Some(*u),
|
|
||||||
ChatType::NpcSay(u, _r) => Some(*u),
|
|
||||||
ChatType::NpcTell(u, _t, _r) => Some(*u),
|
|
||||||
ChatType::Meta => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Player factions are used to coordinate pvp vs hostile factions or segment
|
/// Player factions are used to coordinate pvp vs hostile factions or segment
|
||||||
|
@ -4,6 +4,7 @@ use censor::Censor;
|
|||||||
use common::comp::AdminRole;
|
use common::comp::AdminRole;
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
use std::{
|
use std::{
|
||||||
|
fmt,
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
@ -15,12 +16,46 @@ pub enum ActionNote {
|
|||||||
SpamWarn,
|
SpamWarn,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for ActionNote {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
ActionNote::SpamWarn => write!(
|
||||||
|
f,
|
||||||
|
"You've sent a lot of messages recently. Make sure to reduce the rate of messages \
|
||||||
|
or you will be automatically muted."
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub enum ActionErr {
|
pub enum ActionErr {
|
||||||
BannedWord,
|
BannedWord,
|
||||||
TooLong,
|
TooLong,
|
||||||
SpamMuted(Duration),
|
SpamMuted(Duration),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for ActionErr {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
ActionErr::BannedWord => write!(
|
||||||
|
f,
|
||||||
|
"Your message contained a banned word. If you think this is a mistake, please let \
|
||||||
|
a moderator know."
|
||||||
|
),
|
||||||
|
ActionErr::TooLong => write!(
|
||||||
|
f,
|
||||||
|
"Your message was too long, no more than {} characters are permitted.",
|
||||||
|
MAX_BYTES_CHAT_MSG
|
||||||
|
),
|
||||||
|
ActionErr::SpamMuted(dur) => write!(
|
||||||
|
f,
|
||||||
|
"You have sent too many messages and are muted for {} seconds.",
|
||||||
|
dur.as_secs_f32() as u64
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct AutoMod {
|
pub struct AutoMod {
|
||||||
settings: ModerationSettings,
|
settings: ModerationSettings,
|
||||||
censor: Arc<Censor>,
|
censor: Arc<Censor>,
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
|
automod::AutoMod,
|
||||||
client::Client,
|
client::Client,
|
||||||
events::update_map_markers,
|
events::update_map_markers,
|
||||||
persistence::PersistedComponents,
|
persistence::PersistedComponents,
|
||||||
@ -17,7 +18,7 @@ use common::{
|
|||||||
self,
|
self,
|
||||||
item::MaterialStatManifest,
|
item::MaterialStatManifest,
|
||||||
skills::{GeneralSkill, Skill},
|
skills::{GeneralSkill, Skill},
|
||||||
Group, Inventory, Item, Poise,
|
ChatType, Group, Inventory, Item, Player, Poise,
|
||||||
},
|
},
|
||||||
effect::Effect,
|
effect::Effect,
|
||||||
link::{Link, LinkHandle},
|
link::{Link, LinkHandle},
|
||||||
@ -36,7 +37,7 @@ use specs::{
|
|||||||
saveload::MarkerAllocator, Builder, Entity as EcsEntity, EntityBuilder as EcsEntityBuilder,
|
saveload::MarkerAllocator, Builder, Entity as EcsEntity, EntityBuilder as EcsEntityBuilder,
|
||||||
Join, WorldExt,
|
Join, WorldExt,
|
||||||
};
|
};
|
||||||
use std::time::Duration;
|
use std::time::{Duration, Instant};
|
||||||
use tracing::{trace, warn};
|
use tracing::{trace, warn};
|
||||||
use vek::*;
|
use vek::*;
|
||||||
|
|
||||||
@ -113,6 +114,7 @@ pub trait StateExt {
|
|||||||
/// Performed after loading component data from the database
|
/// Performed after loading component data from the database
|
||||||
fn update_character_data(&mut self, entity: EcsEntity, components: PersistedComponents);
|
fn update_character_data(&mut self, entity: EcsEntity, components: PersistedComponents);
|
||||||
/// Iterates over registered clients and send each `ServerMsg`
|
/// Iterates over registered clients and send each `ServerMsg`
|
||||||
|
fn validate_chat_msg(&self, player: EcsEntity, msg: &str) -> bool;
|
||||||
fn send_chat(&self, msg: comp::UnresolvedChatMsg);
|
fn send_chat(&self, msg: comp::UnresolvedChatMsg);
|
||||||
fn notify_players(&self, msg: ServerGeneral);
|
fn notify_players(&self, msg: ServerGeneral);
|
||||||
fn notify_in_game_clients(&self, msg: ServerGeneral);
|
fn notify_in_game_clients(&self, msg: ServerGeneral);
|
||||||
@ -674,6 +676,39 @@ impl StateExt for State {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn validate_chat_msg(&self, entity: EcsEntity, msg: &str) -> bool {
|
||||||
|
let mut automod = self.ecs().write_resource::<AutoMod>();
|
||||||
|
let Some(client) = self.ecs().read_storage::<Client>().get(entity) else { return true };
|
||||||
|
let Some(player) = self.ecs().read_storage::<Player>().get(entity) else { return true };
|
||||||
|
|
||||||
|
match automod.validate_chat_msg(
|
||||||
|
player.uuid(),
|
||||||
|
self.ecs()
|
||||||
|
.read_storage::<comp::Admin>()
|
||||||
|
.get(entity)
|
||||||
|
.map(|a| a.0),
|
||||||
|
Instant::now(),
|
||||||
|
msg,
|
||||||
|
) {
|
||||||
|
Ok(note) => {
|
||||||
|
if let Some(note) = note {
|
||||||
|
let _ = client.send(ServerGeneral::server_msg(
|
||||||
|
ChatType::CommandInfo,
|
||||||
|
format!("{}", note),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
true
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
let _ = client.send(ServerGeneral::server_msg(
|
||||||
|
ChatType::CommandError,
|
||||||
|
format!("{}", err),
|
||||||
|
));
|
||||||
|
false
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Send the chat message to the proper players. Say and region are limited
|
/// Send the chat message to the proper players. Say and region are limited
|
||||||
/// by location. Faction and group are limited by component.
|
/// by location. Faction and group are limited by component.
|
||||||
fn send_chat(&self, msg: comp::UnresolvedChatMsg) {
|
fn send_chat(&self, msg: comp::UnresolvedChatMsg) {
|
||||||
@ -689,165 +724,183 @@ impl StateExt for State {
|
|||||||
.clone()
|
.clone()
|
||||||
.map_group(|_| group_info.map_or_else(|| "???".to_string(), |i| i.name.clone()));
|
.map_group(|_| group_info.map_or_else(|| "???".to_string(), |i| i.name.clone()));
|
||||||
|
|
||||||
match &msg.chat_type {
|
if msg.chat_type.uid().map_or(true, |sender| {
|
||||||
comp::ChatType::Offline(_)
|
(*ecs.read_resource::<UidAllocator>())
|
||||||
| comp::ChatType::CommandInfo
|
.retrieve_entity_internal(sender.0)
|
||||||
| comp::ChatType::CommandError
|
.map_or(false, |e| self.validate_chat_msg(e, &msg.message))
|
||||||
| comp::ChatType::Meta
|
}) {
|
||||||
| comp::ChatType::World(_) => self.notify_players(ServerGeneral::ChatMsg(resolved_msg)),
|
match &msg.chat_type {
|
||||||
comp::ChatType::Online(u) => {
|
comp::ChatType::Offline(_)
|
||||||
for (client, uid) in
|
| comp::ChatType::CommandInfo
|
||||||
(&ecs.read_storage::<Client>(), &ecs.read_storage::<Uid>()).join()
|
| comp::ChatType::CommandError
|
||||||
{
|
| comp::ChatType::Meta
|
||||||
if uid != u {
|
| comp::ChatType::World(_) => {
|
||||||
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
self.notify_players(ServerGeneral::ChatMsg(resolved_msg))
|
||||||
|
},
|
||||||
|
comp::ChatType::Online(u) => {
|
||||||
|
for (client, uid) in
|
||||||
|
(&ecs.read_storage::<Client>(), &ecs.read_storage::<Uid>()).join()
|
||||||
|
{
|
||||||
|
if uid != u {
|
||||||
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
comp::ChatType::Tell(from, to) => {
|
||||||
comp::ChatType::Tell(u, t) => {
|
for (client, uid) in
|
||||||
for (client, uid) in
|
(&ecs.read_storage::<Client>(), &ecs.read_storage::<Uid>()).join()
|
||||||
(&ecs.read_storage::<Client>(), &ecs.read_storage::<Uid>()).join()
|
{
|
||||||
{
|
if uid == from || uid == to {
|
||||||
if uid == u || uid == t {
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
||||||
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
comp::ChatType::Kill(kill_source, uid) => {
|
|
||||||
let comp::chat::GenericChatMsg { message, .. } = msg;
|
|
||||||
let clients = ecs.read_storage::<Client>();
|
|
||||||
let clients_count = clients.count();
|
|
||||||
// Avoid chat spam, send kill message only to group or nearby players if a
|
|
||||||
// certain amount of clients are online
|
|
||||||
if clients_count
|
|
||||||
> ecs
|
|
||||||
.fetch::<Settings>()
|
|
||||||
.max_player_for_kill_broadcast
|
|
||||||
.unwrap_or_default()
|
|
||||||
{
|
|
||||||
// Send kill message to the dead player's group
|
|
||||||
let killed_entity =
|
|
||||||
(*ecs.read_resource::<UidAllocator>()).retrieve_entity_internal(uid.0);
|
|
||||||
let groups = ecs.read_storage::<Group>();
|
|
||||||
let killed_group = killed_entity.and_then(|e| groups.get(e));
|
|
||||||
if let Some(g) = &killed_group {
|
|
||||||
send_to_group(g, ecs, &resolved_msg);
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
comp::ChatType::Kill(kill_source, uid) => {
|
||||||
|
let comp::chat::GenericChatMsg { message, .. } = msg;
|
||||||
|
let clients = ecs.read_storage::<Client>();
|
||||||
|
let clients_count = clients.count();
|
||||||
|
// Avoid chat spam, send kill message only to group or nearby players if a
|
||||||
|
// certain amount of clients are online
|
||||||
|
if clients_count
|
||||||
|
> ecs
|
||||||
|
.fetch::<Settings>()
|
||||||
|
.max_player_for_kill_broadcast
|
||||||
|
.unwrap_or_default()
|
||||||
|
{
|
||||||
|
// Send kill message to the dead player's group
|
||||||
|
let killed_entity =
|
||||||
|
(*ecs.read_resource::<UidAllocator>()).retrieve_entity_internal(uid.0);
|
||||||
|
let groups = ecs.read_storage::<Group>();
|
||||||
|
let killed_group = killed_entity.and_then(|e| groups.get(e));
|
||||||
|
if let Some(g) = &killed_group {
|
||||||
|
send_to_group(g, ecs, &resolved_msg);
|
||||||
|
}
|
||||||
|
|
||||||
// Send kill message to nearby players that aren't part of the deceased's group
|
// Send kill message to nearby players that aren't part of the deceased's
|
||||||
let positions = ecs.read_storage::<comp::Pos>();
|
// group
|
||||||
if let Some(died_player_pos) = killed_entity.and_then(|e| positions.get(e)) {
|
let positions = ecs.read_storage::<comp::Pos>();
|
||||||
for (ent, client, pos) in (&*ecs.entities(), &clients, &positions).join() {
|
if let Some(died_player_pos) = killed_entity.and_then(|e| positions.get(e))
|
||||||
let client_group = groups.get(ent);
|
{
|
||||||
let is_different_group =
|
for (ent, client, pos) in
|
||||||
!(killed_group == client_group && client_group.is_some());
|
(&*ecs.entities(), &clients, &positions).join()
|
||||||
if is_within(comp::ChatMsg::SAY_DISTANCE, pos, died_player_pos)
|
|
||||||
&& is_different_group
|
|
||||||
{
|
{
|
||||||
|
let client_group = groups.get(ent);
|
||||||
|
let is_different_group =
|
||||||
|
!(killed_group == client_group && client_group.is_some());
|
||||||
|
if is_within(comp::ChatMsg::SAY_DISTANCE, pos, died_player_pos)
|
||||||
|
&& is_different_group
|
||||||
|
{
|
||||||
|
client.send_fallible(ServerGeneral::ChatMsg(
|
||||||
|
resolved_msg.clone(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.notify_players(ServerGeneral::server_msg(
|
||||||
|
comp::ChatType::Kill(kill_source.clone(), *uid),
|
||||||
|
message,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
comp::ChatType::Say(uid) => {
|
||||||
|
let entity_opt =
|
||||||
|
(*ecs.read_resource::<UidAllocator>()).retrieve_entity_internal(uid.0);
|
||||||
|
|
||||||
|
let positions = ecs.read_storage::<comp::Pos>();
|
||||||
|
if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
|
||||||
|
for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
|
||||||
|
if is_within(comp::ChatMsg::SAY_DISTANCE, pos, speaker_pos) {
|
||||||
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
},
|
||||||
self.notify_players(ServerGeneral::server_msg(
|
comp::ChatType::Region(uid) => {
|
||||||
comp::ChatType::Kill(kill_source.clone(), *uid),
|
let entity_opt =
|
||||||
message,
|
(*ecs.read_resource::<UidAllocator>()).retrieve_entity_internal(uid.0);
|
||||||
))
|
|
||||||
}
|
|
||||||
},
|
|
||||||
comp::ChatType::Say(uid) => {
|
|
||||||
let entity_opt =
|
|
||||||
(*ecs.read_resource::<UidAllocator>()).retrieve_entity_internal(uid.0);
|
|
||||||
let positions = ecs.read_storage::<comp::Pos>();
|
|
||||||
if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
|
|
||||||
for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
|
|
||||||
if is_within(comp::ChatMsg::SAY_DISTANCE, pos, speaker_pos) {
|
|
||||||
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
comp::ChatType::Region(uid) => {
|
|
||||||
let entity_opt =
|
|
||||||
(*ecs.read_resource::<UidAllocator>()).retrieve_entity_internal(uid.0);
|
|
||||||
let positions = ecs.read_storage::<comp::Pos>();
|
|
||||||
if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
|
|
||||||
for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
|
|
||||||
if is_within(comp::ChatMsg::REGION_DISTANCE, pos, speaker_pos) {
|
|
||||||
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
comp::ChatType::Npc(uid, _r) => {
|
|
||||||
let entity_opt =
|
|
||||||
(*ecs.read_resource::<UidAllocator>()).retrieve_entity_internal(uid.0);
|
|
||||||
let positions = ecs.read_storage::<comp::Pos>();
|
|
||||||
if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
|
|
||||||
for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
|
|
||||||
if is_within(comp::ChatMsg::NPC_DISTANCE, pos, speaker_pos) {
|
|
||||||
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
comp::ChatType::NpcSay(uid, _r) => {
|
|
||||||
let entity_opt =
|
|
||||||
(*ecs.read_resource::<UidAllocator>()).retrieve_entity_internal(uid.0);
|
|
||||||
let positions = ecs.read_storage::<comp::Pos>();
|
|
||||||
if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
|
|
||||||
for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
|
|
||||||
if is_within(comp::ChatMsg::NPC_SAY_DISTANCE, pos, speaker_pos) {
|
|
||||||
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
comp::ChatType::NpcTell(from, to, _r) => {
|
|
||||||
for (client, uid) in
|
|
||||||
(&ecs.read_storage::<Client>(), &ecs.read_storage::<Uid>()).join()
|
|
||||||
{
|
|
||||||
if uid == from || uid == to {
|
|
||||||
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
comp::ChatType::FactionMeta(s) | comp::ChatType::Faction(_, s) => {
|
|
||||||
for (client, faction) in (
|
|
||||||
&ecs.read_storage::<Client>(),
|
|
||||||
&ecs.read_storage::<comp::Faction>(),
|
|
||||||
)
|
|
||||||
.join()
|
|
||||||
{
|
|
||||||
if s == &faction.0 {
|
|
||||||
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
comp::ChatType::Group(from, g) => {
|
|
||||||
if group_info.is_none() {
|
|
||||||
// group not found, reply with command error
|
|
||||||
let reply = comp::ChatMsg {
|
|
||||||
chat_type: comp::ChatType::CommandError,
|
|
||||||
message: "You are using group chat but do not belong to a group. Use \
|
|
||||||
/world or /region to change chat."
|
|
||||||
.into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some((client, _)) =
|
let positions = ecs.read_storage::<comp::Pos>();
|
||||||
(&ecs.read_storage::<Client>(), &ecs.read_storage::<Uid>())
|
if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
|
||||||
.join()
|
for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
|
||||||
.find(|(_, uid)| *uid == from)
|
if is_within(comp::ChatMsg::REGION_DISTANCE, pos, speaker_pos) {
|
||||||
{
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
||||||
client.send_fallible(ServerGeneral::ChatMsg(reply));
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return;
|
},
|
||||||
}
|
comp::ChatType::Npc(uid, _r) => {
|
||||||
send_to_group(g, ecs, &resolved_msg);
|
let entity_opt =
|
||||||
},
|
(*ecs.read_resource::<UidAllocator>()).retrieve_entity_internal(uid.0);
|
||||||
comp::ChatType::GroupMeta(g) => {
|
|
||||||
send_to_group(g, ecs, &resolved_msg);
|
let positions = ecs.read_storage::<comp::Pos>();
|
||||||
},
|
if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
|
||||||
|
for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
|
||||||
|
if is_within(comp::ChatMsg::NPC_DISTANCE, pos, speaker_pos) {
|
||||||
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
comp::ChatType::NpcSay(uid, _r) => {
|
||||||
|
let entity_opt =
|
||||||
|
(*ecs.read_resource::<UidAllocator>()).retrieve_entity_internal(uid.0);
|
||||||
|
|
||||||
|
let positions = ecs.read_storage::<comp::Pos>();
|
||||||
|
if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
|
||||||
|
for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
|
||||||
|
if is_within(comp::ChatMsg::NPC_SAY_DISTANCE, pos, speaker_pos) {
|
||||||
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
comp::ChatType::NpcTell(from, to, _r) => {
|
||||||
|
for (client, uid) in
|
||||||
|
(&ecs.read_storage::<Client>(), &ecs.read_storage::<Uid>()).join()
|
||||||
|
{
|
||||||
|
if uid == from || uid == to {
|
||||||
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
comp::ChatType::FactionMeta(s) | comp::ChatType::Faction(_, s) => {
|
||||||
|
for (client, faction) in (
|
||||||
|
&ecs.read_storage::<Client>(),
|
||||||
|
&ecs.read_storage::<comp::Faction>(),
|
||||||
|
)
|
||||||
|
.join()
|
||||||
|
{
|
||||||
|
if s == &faction.0 {
|
||||||
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
comp::ChatType::Group(from, g) => {
|
||||||
|
if group_info.is_none() {
|
||||||
|
// group not found, reply with command error
|
||||||
|
let reply = comp::ChatMsg {
|
||||||
|
chat_type: comp::ChatType::CommandError,
|
||||||
|
message: "You are using group chat but do not belong to a group. Use \
|
||||||
|
/world or /region to change chat."
|
||||||
|
.into(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some((client, _)) =
|
||||||
|
(&ecs.read_storage::<Client>(), &ecs.read_storage::<Uid>())
|
||||||
|
.join()
|
||||||
|
.find(|(_, uid)| *uid == from)
|
||||||
|
{
|
||||||
|
client.send_fallible(ServerGeneral::ChatMsg(reply));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
send_to_group(g, ecs, &resolved_msg);
|
||||||
|
},
|
||||||
|
comp::ChatType::GroupMeta(g) => {
|
||||||
|
send_to_group(g, ecs, &resolved_msg);
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,79 +1,35 @@
|
|||||||
use crate::{
|
use crate::client::Client;
|
||||||
automod::{self, AutoMod},
|
|
||||||
client::Client,
|
|
||||||
};
|
|
||||||
use common::{
|
use common::{
|
||||||
comp::{Admin, AdminRole, ChatMode, ChatType, Player},
|
comp::{ChatMode, Player},
|
||||||
event::{EventBus, ServerEvent},
|
event::{EventBus, ServerEvent},
|
||||||
resources::Time,
|
resources::Time,
|
||||||
uid::Uid,
|
uid::Uid,
|
||||||
};
|
};
|
||||||
use common_ecs::{Job, Origin, Phase, System};
|
use common_ecs::{Job, Origin, Phase, System};
|
||||||
use common_net::msg::{ClientGeneral, ServerGeneral};
|
use common_net::msg::ClientGeneral;
|
||||||
use specs::{Entities, Join, Read, ReadStorage, WriteExpect};
|
use specs::{Entities, Join, Read, ReadStorage};
|
||||||
use std::time::Instant;
|
|
||||||
use tracing::{debug, error, warn};
|
use tracing::{debug, error, warn};
|
||||||
|
|
||||||
impl Sys {
|
impl Sys {
|
||||||
fn handle_general_msg(
|
fn handle_general_msg(
|
||||||
server_emitter: &mut common::event::Emitter<'_, ServerEvent>,
|
server_emitter: &mut common::event::Emitter<'_, ServerEvent>,
|
||||||
entity: specs::Entity,
|
entity: specs::Entity,
|
||||||
client: &Client,
|
_client: &Client,
|
||||||
player: Option<&Player>,
|
player: Option<&Player>,
|
||||||
admin_role: Option<AdminRole>,
|
|
||||||
uids: &ReadStorage<'_, Uid>,
|
uids: &ReadStorage<'_, Uid>,
|
||||||
chat_modes: &ReadStorage<'_, ChatMode>,
|
chat_modes: &ReadStorage<'_, ChatMode>,
|
||||||
msg: ClientGeneral,
|
msg: ClientGeneral,
|
||||||
now: Instant,
|
|
||||||
automod: &mut AutoMod,
|
|
||||||
) -> Result<(), crate::error::Error> {
|
) -> Result<(), crate::error::Error> {
|
||||||
match msg {
|
match msg {
|
||||||
ClientGeneral::ChatMsg(message) => {
|
ClientGeneral::ChatMsg(message) => {
|
||||||
if let Some(player) = player {
|
if player.is_some() {
|
||||||
match automod.validate_chat_msg(player.uuid(), admin_role, now, &message) {
|
if let Some(from) = uids.get(entity) {
|
||||||
Ok(note) => {
|
const CHAT_MODE_DEFAULT: &ChatMode = &ChatMode::default();
|
||||||
if let Some(from) = uids.get(entity) {
|
let mode = chat_modes.get(entity).unwrap_or(CHAT_MODE_DEFAULT);
|
||||||
const CHAT_MODE_DEFAULT: &ChatMode = &ChatMode::default();
|
// Send chat message
|
||||||
let mode = chat_modes.get(entity).unwrap_or(CHAT_MODE_DEFAULT);
|
server_emitter.emit(ServerEvent::Chat(mode.new_message(*from, message)));
|
||||||
// Send chat message
|
} else {
|
||||||
server_emitter
|
error!("Could not send message. Missing player uid");
|
||||||
.emit(ServerEvent::Chat(mode.new_message(*from, message)));
|
|
||||||
} else {
|
|
||||||
error!("Could not send message. Missing player uid");
|
|
||||||
}
|
|
||||||
|
|
||||||
match note {
|
|
||||||
None => {},
|
|
||||||
Some(automod::ActionNote::SpamWarn) => {
|
|
||||||
let _ = client.send(ServerGeneral::server_msg(
|
|
||||||
ChatType::CommandError,
|
|
||||||
"You've sent a lot of messages recently. Make sure to \
|
|
||||||
reduce the rate of messages or you will be automatically \
|
|
||||||
muted.",
|
|
||||||
));
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(automod::ActionErr::TooLong) => {
|
|
||||||
let len = message.len();
|
|
||||||
warn!(?len, "Received a chat message that's too long");
|
|
||||||
},
|
|
||||||
Err(automod::ActionErr::BannedWord) => {
|
|
||||||
let _ = client.send(ServerGeneral::server_msg(
|
|
||||||
ChatType::CommandError,
|
|
||||||
"Your message contained a banned word. If you think this is a \
|
|
||||||
false positive, please open a bug report.",
|
|
||||||
));
|
|
||||||
},
|
|
||||||
Err(automod::ActionErr::SpamMuted(dur)) => {
|
|
||||||
let _ = client.send(ServerGeneral::server_msg(
|
|
||||||
ChatType::CommandError,
|
|
||||||
format!(
|
|
||||||
"You have sent too many messages and are muted for {} seconds.",
|
|
||||||
dur.as_secs_f32() as u64
|
|
||||||
),
|
|
||||||
));
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
warn!("Received a chat message from an unregistered client");
|
warn!("Received a chat message from an unregistered client");
|
||||||
@ -115,8 +71,6 @@ impl<'a> System<'a> for Sys {
|
|||||||
ReadStorage<'a, ChatMode>,
|
ReadStorage<'a, ChatMode>,
|
||||||
ReadStorage<'a, Player>,
|
ReadStorage<'a, Player>,
|
||||||
ReadStorage<'a, Client>,
|
ReadStorage<'a, Client>,
|
||||||
ReadStorage<'a, Admin>,
|
|
||||||
WriteExpect<'a, AutoMod>,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const NAME: &'static str = "msg::general";
|
const NAME: &'static str = "msg::general";
|
||||||
@ -125,26 +79,20 @@ impl<'a> System<'a> for Sys {
|
|||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
_job: &mut Job<Self>,
|
_job: &mut Job<Self>,
|
||||||
(entities, server_event_bus, time, uids, chat_modes, players, clients, admins, mut automod): Self::SystemData,
|
(entities, server_event_bus, time, uids, chat_modes, players, clients): Self::SystemData,
|
||||||
) {
|
) {
|
||||||
let mut server_emitter = server_event_bus.emitter();
|
let mut server_emitter = server_event_bus.emitter();
|
||||||
|
|
||||||
let now = Instant::now();
|
for (entity, client, player) in (&entities, &clients, players.maybe()).join() {
|
||||||
for (entity, client, player, admin) in
|
|
||||||
(&entities, &clients, players.maybe(), admins.maybe()).join()
|
|
||||||
{
|
|
||||||
let res = super::try_recv_all(client, 3, |client, msg| {
|
let res = super::try_recv_all(client, 3, |client, msg| {
|
||||||
Self::handle_general_msg(
|
Self::handle_general_msg(
|
||||||
&mut server_emitter,
|
&mut server_emitter,
|
||||||
entity,
|
entity,
|
||||||
client,
|
client,
|
||||||
player,
|
player,
|
||||||
admin.map(|a| a.0),
|
|
||||||
&uids,
|
&uids,
|
||||||
&chat_modes,
|
&chat_modes,
|
||||||
msg,
|
msg,
|
||||||
now,
|
|
||||||
&mut automod,
|
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user