diff --git a/src/handlers.rs b/src/handlers.rs index 6034f82..06e3dcc 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -65,7 +65,7 @@ async fn handle_iq_disco( ErrorType::Modify, DefinedCondition::BadRequest, "en", - format!("Unknown disco#info node"), + String::from("Unknown disco#info node"), ); let reply = Iq::from_error(iq.id, error) .with_from(iq.to.unwrap()) @@ -94,14 +94,13 @@ async fn handle_iq_ping( if let Jid::Full(participant) = to.clone() { // TODO: Reply from participant if joined and nick is correct let bare = BareJid::from(to.clone()); - if let Some(room) = rooms.get(&bare) { - if room.is_joined(&participant) && - let Some(occupant) = room.occupants.get(&participant.resource) { - if occupant.contains(&from) { - let success = success.clone().with_from(Jid::Full(participant)); - component.send_stanza(success).await?; - return Ok(()); - } + if let Some(room) = rooms.get(&bare) && + room.is_joined(&participant) && + let Some(occupant) = room.occupants.get(&participant.resource) { + if occupant.contains(&from) { + let success = success.clone().with_from(Jid::Full(participant)); + component.send_stanza(success).await?; + return Ok(()); } return Ok(()); @@ -144,7 +143,7 @@ async fn handle_iq_ping( } Err(err) => { error!("Failed to parse iq as ping: {}", err); - return Err(err.into()); + Err(err.into()) } } } @@ -237,36 +236,35 @@ async fn handle_presence( let _ = rooms.insert(roomjid, room); } } - } else if presence.type_ == PresenceType::Unavailable { - if let Jid::Full(realjid) = presence.from.unwrap() && - let Jid::Full(participant) = presence.to.unwrap() { + } else if presence.type_ == PresenceType::Unavailable && + let Jid::Full(realjid) = presence.from.unwrap() && + let Jid::Full(participant) = presence.to.unwrap() { - let roomjid = BareJid::from(participant.clone()); + let roomjid = BareJid::from(participant.clone()); - let error = Presence::new(PresenceType::Error) - .with_from(participant.clone()) - .with_to(realjid.clone()) - .with_payloads(vec![ - StanzaError::new( - ErrorType::Cancel, - DefinedCondition::ServiceUnavailable, - "en", - "Occupant does not exist", - ).into() - ]); - if let Some(mut room) = rooms.remove(&roomjid) { - match room.remove_session(component, realjid, participant.resource.clone()).await { - Ok(()) => (), - Err(Error::NonexistantSession(_)) => { - component.send_stanza(error).await.unwrap(); - }, - Err(err) => Err(err).unwrap(), - } + let error = Presence::new(PresenceType::Error) + .with_from(participant.clone()) + .with_to(realjid.clone()) + .with_payloads(vec![ + StanzaError::new( + ErrorType::Cancel, + DefinedCondition::ServiceUnavailable, + "en", + "Occupant does not exist", + ).into() + ]); + if let Some(mut room) = rooms.remove(&roomjid) { + match room.remove_session(component, realjid, participant.resource.clone()).await { + Ok(()) => (), + Err(Error::NonexistantSession(_)) => { + component.send_stanza(error).await.unwrap(); + }, + Err(err) => Err(err).unwrap(), + } - // Add the room back - if room.occupants.len() > 0 { - rooms.insert(roomjid, room); - } + // Add the room back + if !room.occupants.is_empty() { + rooms.insert(roomjid, room); } } }