|
|
@ -14,9 +14,10 @@ use ruma_client_api::{
|
|
|
|
},
|
|
|
|
},
|
|
|
|
unversioned::get_supported_versions,
|
|
|
|
unversioned::get_supported_versions,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
use ruma_events::room::message::MessageEvent;
|
|
|
|
use ruma_events::{room::message::MessageEvent, EventResult};
|
|
|
|
use ruma_identifiers::{EventId, UserId};
|
|
|
|
use ruma_identifiers::{EventId, UserId};
|
|
|
|
use ruma_wrapper::{MatrixResult, Ruma};
|
|
|
|
use ruma_wrapper::{MatrixResult, Ruma};
|
|
|
|
|
|
|
|
use serde_json::map::Map;
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::{collections::HashMap, convert::TryInto};
|
|
|
|
use std::{collections::HashMap, convert::TryInto};
|
|
|
|
|
|
|
|
|
|
|
@ -41,6 +42,7 @@ fn register_route(
|
|
|
|
data: State<Data>,
|
|
|
|
data: State<Data>,
|
|
|
|
body: Ruma<register::Request>,
|
|
|
|
body: Ruma<register::Request>,
|
|
|
|
) -> MatrixResult<register::Response> {
|
|
|
|
) -> MatrixResult<register::Response> {
|
|
|
|
|
|
|
|
// Validate user id
|
|
|
|
let user_id: UserId = match (*format!(
|
|
|
|
let user_id: UserId = match (*format!(
|
|
|
|
"@{}:{}",
|
|
|
|
"@{}:{}",
|
|
|
|
body.username.clone().unwrap_or("randomname".to_owned()),
|
|
|
|
body.username.clone().unwrap_or("randomname".to_owned()),
|
|
|
@ -59,6 +61,7 @@ fn register_route(
|
|
|
|
Ok(user_id) => user_id,
|
|
|
|
Ok(user_id) => user_id,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check if username is creative enough
|
|
|
|
if data.user_exists(&user_id) {
|
|
|
|
if data.user_exists(&user_id) {
|
|
|
|
debug!("ID already taken");
|
|
|
|
debug!("ID already taken");
|
|
|
|
return MatrixResult(Err(Error {
|
|
|
|
return MatrixResult(Err(Error {
|
|
|
@ -68,68 +71,115 @@ fn register_route(
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data.user_add(user_id.clone(), body.password.clone());
|
|
|
|
// Create user
|
|
|
|
|
|
|
|
data.user_add(&user_id, body.password.clone());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Generate new device id if the user didn't specify one
|
|
|
|
|
|
|
|
let device_id = body
|
|
|
|
|
|
|
|
.device_id
|
|
|
|
|
|
|
|
.clone()
|
|
|
|
|
|
|
|
.unwrap_or_else(|| "TODO:randomdeviceid".to_owned());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add device
|
|
|
|
|
|
|
|
data.device_add(&user_id, &device_id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Generate new token for the device
|
|
|
|
|
|
|
|
let token = "TODO:randomtoken".to_owned();
|
|
|
|
|
|
|
|
data.token_replace(&user_id, &device_id, token.clone());
|
|
|
|
|
|
|
|
|
|
|
|
MatrixResult(Ok(register::Response {
|
|
|
|
MatrixResult(Ok(register::Response {
|
|
|
|
access_token: "randomtoken".to_owned(),
|
|
|
|
access_token: token,
|
|
|
|
home_server: data.hostname(),
|
|
|
|
home_server: data.hostname(),
|
|
|
|
user_id,
|
|
|
|
user_id,
|
|
|
|
device_id: body.device_id.clone().unwrap_or("randomid".to_owned()),
|
|
|
|
device_id,
|
|
|
|
}))
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[post("/_matrix/client/r0/login", data = "<body>")]
|
|
|
|
#[post("/_matrix/client/r0/login", data = "<body>")]
|
|
|
|
fn login_route(data: State<Data>, body: Ruma<login::Request>) -> MatrixResult<login::Response> {
|
|
|
|
fn login_route(data: State<Data>, body: Ruma<login::Request>) -> MatrixResult<login::Response> {
|
|
|
|
let username = if let login::UserInfo::MatrixId(mut username) = body.user.clone() {
|
|
|
|
// Validate login method
|
|
|
|
if !username.contains(':') {
|
|
|
|
let user_id =
|
|
|
|
username = format!("@{}:{}", username, data.hostname());
|
|
|
|
if let (login::UserInfo::MatrixId(mut username), login::LoginInfo::Password { password }) =
|
|
|
|
}
|
|
|
|
(body.user.clone(), body.login_info.clone())
|
|
|
|
if let Ok(user_id) = (*username).try_into() {
|
|
|
|
{
|
|
|
|
if !data.user_exists(&user_id) {
|
|
|
|
if !username.contains(':') {
|
|
|
|
debug!("Userid does not exist. Can't log in.");
|
|
|
|
username = format!("@{}:{}", username, data.hostname());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok(user_id) = (*username).try_into() {
|
|
|
|
|
|
|
|
if !data.user_exists(&user_id) {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check password
|
|
|
|
|
|
|
|
if let Some(correct_password) = data.password_get(&user_id) {
|
|
|
|
|
|
|
|
if password == correct_password {
|
|
|
|
|
|
|
|
// Success!
|
|
|
|
|
|
|
|
user_id
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
debug!("Invalid password.");
|
|
|
|
|
|
|
|
return MatrixResult(Err(Error {
|
|
|
|
|
|
|
|
kind: ErrorKind::Unknown,
|
|
|
|
|
|
|
|
message: "".to_owned(),
|
|
|
|
|
|
|
|
status_code: http::StatusCode::FORBIDDEN,
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
debug!("UserId does not exist (has no assigned password). Can't log in.");
|
|
|
|
|
|
|
|
return MatrixResult(Err(Error {
|
|
|
|
|
|
|
|
kind: ErrorKind::Forbidden,
|
|
|
|
|
|
|
|
message: "".to_owned(),
|
|
|
|
|
|
|
|
status_code: http::StatusCode::FORBIDDEN,
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
debug!("Invalid UserId.");
|
|
|
|
return MatrixResult(Err(Error {
|
|
|
|
return MatrixResult(Err(Error {
|
|
|
|
kind: ErrorKind::Forbidden,
|
|
|
|
kind: ErrorKind::Unknown,
|
|
|
|
message: "UserId not found.".to_owned(),
|
|
|
|
message: "Bad login type.".to_owned(),
|
|
|
|
status_code: http::StatusCode::BAD_REQUEST,
|
|
|
|
status_code: http::StatusCode::BAD_REQUEST,
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user_id
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
debug!("Invalid UserId.");
|
|
|
|
debug!("Bad login type");
|
|
|
|
return MatrixResult(Err(Error {
|
|
|
|
return MatrixResult(Err(Error {
|
|
|
|
kind: ErrorKind::Unknown,
|
|
|
|
kind: ErrorKind::Unknown,
|
|
|
|
message: "Bad login type.".to_owned(),
|
|
|
|
message: "Bad login type.".to_owned(),
|
|
|
|
status_code: http::StatusCode::BAD_REQUEST,
|
|
|
|
status_code: http::StatusCode::BAD_REQUEST,
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
|
|
|
|
debug!("Bad login type");
|
|
|
|
// Generate new device id if the user didn't specify one
|
|
|
|
return MatrixResult(Err(Error {
|
|
|
|
let device_id = body
|
|
|
|
kind: ErrorKind::Unknown,
|
|
|
|
.device_id
|
|
|
|
message: "Bad login type.".to_owned(),
|
|
|
|
.clone()
|
|
|
|
status_code: http::StatusCode::BAD_REQUEST,
|
|
|
|
.unwrap_or("TODO:randomdeviceid".to_owned());
|
|
|
|
}));
|
|
|
|
|
|
|
|
};
|
|
|
|
// Add device (TODO: We might not want to call it when using an existing device)
|
|
|
|
|
|
|
|
data.device_add(&user_id, &device_id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Generate a new token for the device
|
|
|
|
|
|
|
|
let token = "TODO:randomtoken".to_owned();
|
|
|
|
|
|
|
|
data.token_replace(&user_id, &device_id, token.clone());
|
|
|
|
|
|
|
|
|
|
|
|
return MatrixResult(Ok(login::Response {
|
|
|
|
return MatrixResult(Ok(login::Response {
|
|
|
|
user_id: username.try_into().unwrap(), // Unwrap is okay because the user is already registered
|
|
|
|
user_id,
|
|
|
|
access_token: "randomtoken".to_owned(),
|
|
|
|
access_token: token,
|
|
|
|
home_server: Some("localhost".to_owned()),
|
|
|
|
home_server: Some(data.hostname()),
|
|
|
|
device_id: body.device_id.clone().unwrap_or("randomid".to_owned()),
|
|
|
|
device_id,
|
|
|
|
well_known: None,
|
|
|
|
well_known: None,
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/_matrix/client/r0/directory/room/<room_alias>")]
|
|
|
|
#[get("/_matrix/client/r0/directory/room/<room_alias>")]
|
|
|
|
fn get_alias_route(room_alias: String) -> MatrixResult<get_alias::Response> {
|
|
|
|
fn get_alias_route(room_alias: String) -> MatrixResult<get_alias::Response> {
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
let room_id = match &*room_alias {
|
|
|
|
let room_id = match &*room_alias {
|
|
|
|
"#room:localhost" => "!xclkjvdlfj:localhost",
|
|
|
|
"#room:localhost" => "!xclkjvdlfj:localhost",
|
|
|
|
_ => {
|
|
|
|
_ => {
|
|
|
|
|
|
|
|
debug!("Room not found.");
|
|
|
|
return MatrixResult(Err(Error {
|
|
|
|
return MatrixResult(Err(Error {
|
|
|
|
kind: ErrorKind::NotFound,
|
|
|
|
kind: ErrorKind::NotFound,
|
|
|
|
message: "Room not found.".to_owned(),
|
|
|
|
message: "Room not found.".to_owned(),
|
|
|
|
status_code: http::StatusCode::NOT_FOUND,
|
|
|
|
status_code: http::StatusCode::NOT_FOUND,
|
|
|
|
}))
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.try_into()
|
|
|
|
.try_into()
|
|
|
@ -146,6 +196,7 @@ fn join_room_by_id_route(
|
|
|
|
_room_id: String,
|
|
|
|
_room_id: String,
|
|
|
|
body: Ruma<join_room_by_id::Request>,
|
|
|
|
body: Ruma<join_room_by_id::Request>,
|
|
|
|
) -> MatrixResult<join_room_by_id::Response> {
|
|
|
|
) -> MatrixResult<join_room_by_id::Response> {
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
MatrixResult(Ok(join_room_by_id::Response {
|
|
|
|
MatrixResult(Ok(join_room_by_id::Response {
|
|
|
|
room_id: body.room_id.clone(),
|
|
|
|
room_id: body.room_id.clone(),
|
|
|
|
}))
|
|
|
|
}))
|
|
|
@ -162,23 +213,34 @@ fn create_message_event_route(
|
|
|
|
_txn_id: String,
|
|
|
|
_txn_id: String,
|
|
|
|
body: Ruma<create_message_event::Request>,
|
|
|
|
body: Ruma<create_message_event::Request>,
|
|
|
|
) -> MatrixResult<create_message_event::Response> {
|
|
|
|
) -> MatrixResult<create_message_event::Response> {
|
|
|
|
dbg!(&body);
|
|
|
|
// Check if content is valid
|
|
|
|
if let Ok(content) = body.data.clone().into_result() {
|
|
|
|
let content = match body.data.clone() {
|
|
|
|
data.room_event_add(
|
|
|
|
EventResult::Ok(content) => content,
|
|
|
|
&MessageEvent {
|
|
|
|
EventResult::Err(_) => {
|
|
|
|
content,
|
|
|
|
debug!("No content.");
|
|
|
|
event_id: EventId::try_from("$randomeventid:localhost").unwrap(),
|
|
|
|
return MatrixResult(Err(Error {
|
|
|
|
origin_server_ts: utils::millis_since_unix_epoch(),
|
|
|
|
kind: ErrorKind::NotFound,
|
|
|
|
room_id: Some(body.room_id.clone()),
|
|
|
|
message: "No content.".to_owned(),
|
|
|
|
sender: UserId::try_from("@TODO:localhost").unwrap(),
|
|
|
|
status_code: http::StatusCode::BAD_REQUEST,
|
|
|
|
unsigned: None,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
};
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
let event_id = EventId::try_from("$TODOrandomeventid:localhost").unwrap();
|
|
|
|
MatrixResult(Ok(create_message_event::Response {
|
|
|
|
|
|
|
|
event_id: "$randomeventid:localhost".try_into().unwrap(),
|
|
|
|
data.room_event_add(
|
|
|
|
}))
|
|
|
|
&MessageEvent {
|
|
|
|
|
|
|
|
content,
|
|
|
|
|
|
|
|
event_id: event_id.clone(),
|
|
|
|
|
|
|
|
origin_server_ts: utils::millis_since_unix_epoch(),
|
|
|
|
|
|
|
|
room_id: Some(body.room_id.clone()),
|
|
|
|
|
|
|
|
sender: body.user_id.expect("user is authenticated"),
|
|
|
|
|
|
|
|
unsigned: Map::default(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
.into(),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MatrixResult(Ok(create_message_event::Response { event_id }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
fn main() {
|
|
|
|