feat: TEXT_MESSAGE_HANDLES

This commit is contained in:
2021-05-17 02:42:24 +08:00
parent 50780dfd3c
commit ae5169e3bd

View File

@@ -118,6 +118,9 @@ lazy_static! {
handles.push(Box::new(HandleTextMessageExit));
handles.push(Box::new(HandleTextMessageStatics));
handles.push(Box::new(HandleTextMessagePass));
handles.push(Box::new(HandleTextMessageRooms));
handles.push(Box::new(HandleTextMessageRoom));
handles.push(Box::new(HandleTextMessageVersion));
handles
};
}
@@ -174,47 +177,78 @@ impl HandleTextMessage for HandleTextMessagePass {
}
}
struct HandleTextMessageRooms;
impl HandleTextMessage for HandleTextMessageRooms {
fn is_matches(&self, _handle_context: &mut HandleContext, _tx: &Tx, _addr: SocketAddr, msg: &str) -> bool {
msg == "/rooms"
}
fn handle(&self, handle_context: &mut HandleContext, tx: &Tx, _addr: SocketAddr, _msg: &str) {
if !handle_context.is_admin {
RoomMessageDown::create_error_reply("Not admin").send(tx);
return;
}
let rooms = handle_context.room_map.lock().unwrap().keys().map(
|k| k.clone()
).collect::<Vec<_>>().join("\n");
if rooms.is_empty() {
tx.unbounded_send(Message::Text("rooms: <empty>".into())).ok();
} else {
tx.unbounded_send(Message::Text(format!("rooms:\n{}", rooms))).ok();
}
}
}
struct HandleTextMessageRoom;
impl HandleTextMessage for HandleTextMessageRoom {
fn is_matches(&self, _handle_context: &mut HandleContext, _tx: &Tx, _addr: SocketAddr, msg: &str) -> bool {
msg.starts_with("/room ")
}
fn handle(&self, handle_context: &mut HandleContext, tx: &Tx, _addr: SocketAddr, msg: &str) {
if !handle_context.is_admin {
RoomMessageDown::create_error_reply("Not admin").send(tx);
return;
}
let room_id = &msg[6..];
let room_map = handle_context.room_map.lock().unwrap();
let mut client_ids = vec![];
if let Some(client_map) = room_map.get(room_id) {
for peer_client_id in client_map.keys() {
client_ids.push(peer_client_id.clone());
}
tx.unbounded_send(Message::Text(format!("clients in room {}:\n{}", room_id, client_ids.join("\n")))).ok();
} else {
tx.unbounded_send(Message::Text(format!("room not found: {}", room_id))).ok();
}
}
}
struct HandleTextMessageVersion;
impl HandleTextMessage for HandleTextMessageVersion {
fn is_matches(&self, _handle_context: &mut HandleContext, _tx: &Tx, _addr: SocketAddr, msg: &str) -> bool {
msg == "/version"
}
fn handle(&self, handle_context: &mut HandleContext, tx: &Tx, _addr: SocketAddr, _msg: &str) {
if !handle_context.is_admin {
RoomMessageDown::create_error_reply("Not admin").send(tx);
return;
}
tx.unbounded_send(Message::Text(format!("{} - v{}", NAME, VERSION))).ok();
}
}
fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: SocketAddr, msg: String) {
// process all registered handles
for handle in &*TEXT_MESSAGE_HANDLES {
if handle.is_matches(handle_context, tx, addr, &msg) {
return handle.handle(handle_context, tx, addr, &msg);
if msg.starts_with('/') {
for handle in &*TEXT_MESSAGE_HANDLES {
if handle.is_matches(handle_context, tx, addr, &msg) {
return handle.handle(handle_context, tx, addr, &msg);
}
}
}
if handle_context.is_admin {
if msg == "/rooms" {
let rooms = handle_context.room_map.lock().unwrap().keys().map(
|k| k.clone()
).collect::<Vec<_>>().join("\n");
if rooms.is_empty() {
tx.unbounded_send(Message::Text("rooms: <empty>".into())).ok();
} else {
tx.unbounded_send(Message::Text(format!("rooms:\n{}", rooms))).ok();
}
return;
}
if msg.starts_with("/room ") {
let room_id = &msg[6..];
let room_map = handle_context.room_map.lock().unwrap();
let mut client_ids = vec![];
if let Some(client_map) = room_map.get(room_id) {
for peer_client_id in client_map.keys() {
client_ids.push(peer_client_id.clone());
}
tx.unbounded_send(Message::Text(format!("clients in room {}:\n{}", room_id, client_ids.join("\n")))).ok();
} else {
tx.unbounded_send(Message::Text(format!("room not found: {}", room_id))).ok();
}
return;
}
if msg == "/version" {
tx.unbounded_send(Message::Text(
format!("{} - v{}", NAME, VERSION)
)).ok();
return;
}
}
let room_message = match serde_json::from_str::<RoomMessage>(&msg) {
Ok(room_message) => room_message,
Err(e) => {