Compare commits

...

2 Commits

Author SHA1 Message Date
JustTestingV 53f218c58f [irc] send 502 if not sender tries to change mode for other users (#4)
Reviewed-on: lavina/lavina#4
Co-authored-by: JustTestingV <JustTestingV@gmail.com>
Co-committed-by: JustTestingV <JustTestingV@gmail.com>
2023-09-06 18:43:07 +00:00
Nikita Vilunov 377d9c32d2 fix message sending 2023-09-06 16:34:20 +02:00
4 changed files with 29 additions and 4 deletions

View File

@ -78,7 +78,7 @@ impl Storage {
pub async fn insert_message(&mut self, room_id: u32, id: u32, content: &str) -> Result<()> {
let mut executor = self.conn.lock().await;
let (_,): (u32,) = sqlx::query_as(
sqlx::query(
"insert into messages(room_id, id, content)
values (?, ?, ?);
update rooms set message_count = message_count + 1 where id = ?;",
@ -87,7 +87,7 @@ impl Storage {
.bind(id)
.bind(content)
.bind(room_id)
.fetch_one(&mut *executor)
.execute(&mut *executor)
.await?;
Ok(())

View File

@ -140,7 +140,10 @@ impl RoomHandle {
pub async fn send_message(&self, player_id: PlayerId, body: Str) {
let mut lock = self.0.write().await;
lock.send_message(player_id, body).await;
let res = lock.send_message(player_id, body).await;
if let Err(err) = res {
log::warn!("Failed to send message: {err:?}");
}
}
pub async fn get_room_info(&self) -> RoomInfo {
@ -200,6 +203,7 @@ impl Room {
}
async fn broadcast_update(&self, update: Updates, except: &PlayerId) {
tracing::debug!("Broadcasting an update to {} subs", self.subscriptions.len());
for (player_id, sub) in &self.subscriptions {
if player_id == except {
continue;

View File

@ -528,7 +528,17 @@ async fn handle_incoming_message(
.await?;
writer.flush().await?;
} else {
// TODO send 502 (not 401) if the user is not the sender
ServerMessage {
tags: vec![],
sender: Some(config.server_name.clone()),
body: ServerMessageBody::N502UsersDontMatch {
client: user.nickname.clone(),
message: "Cant change mode for other users".into(),
},
}
.write_async(writer)
.await?;
writer.flush().await?;
}
}
Recipient::Chan(_) => {

View File

@ -132,6 +132,10 @@ pub enum ServerMessageBody {
chan: Chan,
message: Str,
},
N502UsersDontMatch {
client: Str,
message: Str,
},
}
impl ServerMessageBody {
@ -301,6 +305,13 @@ impl ServerMessageBody {
writer.write_all(b" :").await?;
writer.write_all(message.as_bytes()).await?;
}
ServerMessageBody::N502UsersDontMatch { client, message } => {
writer.write_all(b"502 ").await?;
writer.write_all(client.as_bytes()).await?;
writer.write_all(b" ").await?;
writer.write_all(b" :").await?;
writer.write_all(message.as_bytes()).await?;
}
}
Ok(())
}