|
|
|
@ -1,42 +1,47 @@ |
|
|
|
#![feature(proc_macro_hygiene, decl_macro)] |
|
|
|
#![feature(proc_macro_hygiene, decl_macro)] |
|
|
|
|
|
|
|
mod data; |
|
|
|
mod ruma_wrapper; |
|
|
|
mod ruma_wrapper; |
|
|
|
|
|
|
|
|
|
|
|
use { |
|
|
|
use data::Data; |
|
|
|
directories::ProjectDirs, |
|
|
|
use log::debug; |
|
|
|
log::debug, |
|
|
|
use rocket::{get, post, put, routes, State}; |
|
|
|
rocket::{get, post, put, routes, State}, |
|
|
|
use ruma_client_api::{ |
|
|
|
ruma_client_api::{ |
|
|
|
|
|
|
|
error::{Error, ErrorKind}, |
|
|
|
error::{Error, ErrorKind}, |
|
|
|
r0::{ |
|
|
|
r0::{ |
|
|
|
account::register, alias::get_alias, membership::join_room_by_id, |
|
|
|
account::register, alias::get_alias, membership::join_room_by_id, |
|
|
|
message::create_message_event, session::login, |
|
|
|
message::create_message_event, session::login, |
|
|
|
}, |
|
|
|
}, |
|
|
|
unversioned::get_supported_versions, |
|
|
|
unversioned::get_supported_versions, |
|
|
|
}, |
|
|
|
|
|
|
|
ruma_identifiers::UserId, |
|
|
|
|
|
|
|
ruma_wrapper::{MatrixResult, Ruma}, |
|
|
|
|
|
|
|
sled::Db, |
|
|
|
|
|
|
|
std::{collections::HashMap, convert::TryInto}, |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
use ruma_identifiers::UserId; |
|
|
|
|
|
|
|
use ruma_wrapper::{MatrixResult, Ruma}; |
|
|
|
|
|
|
|
use std::{collections::HashMap, convert::TryInto}; |
|
|
|
|
|
|
|
|
|
|
|
#[get("/_matrix/client/versions")] |
|
|
|
#[get("/_matrix/client/versions")] |
|
|
|
fn get_supported_versions_route() -> MatrixResult<get_supported_versions::Response> { |
|
|
|
fn get_supported_versions_route() -> MatrixResult<get_supported_versions::Response> { |
|
|
|
MatrixResult(Ok(get_supported_versions::Response { |
|
|
|
MatrixResult(Ok(get_supported_versions::Response { |
|
|
|
versions: vec!["r0.6.0".to_owned()], |
|
|
|
versions: vec![ |
|
|
|
|
|
|
|
"r0.0.1".to_owned(), |
|
|
|
|
|
|
|
"r0.1.0".to_owned(), |
|
|
|
|
|
|
|
"r0.2.0".to_owned(), |
|
|
|
|
|
|
|
"r0.3.0".to_owned(), |
|
|
|
|
|
|
|
"r0.4.0".to_owned(), |
|
|
|
|
|
|
|
"r0.5.0".to_owned(), |
|
|
|
|
|
|
|
"r0.6.0".to_owned(), |
|
|
|
|
|
|
|
], |
|
|
|
unstable_features: HashMap::new(), |
|
|
|
unstable_features: HashMap::new(), |
|
|
|
})) |
|
|
|
})) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[post("/_matrix/client/r0/register", data = "<body>")] |
|
|
|
#[post("/_matrix/client/r0/register", data = "<body>")] |
|
|
|
fn register_route( |
|
|
|
fn register_route( |
|
|
|
db: State<Db>, |
|
|
|
data: State<Data>, |
|
|
|
body: Ruma<register::Request>, |
|
|
|
body: Ruma<register::Request>, |
|
|
|
) -> MatrixResult<register::Response> { |
|
|
|
) -> MatrixResult<register::Response> { |
|
|
|
let users = db.open_tree("users").unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let user_id: UserId = match (*format!( |
|
|
|
let user_id: UserId = match (*format!( |
|
|
|
"@{}:localhost", |
|
|
|
"@{}:{}", |
|
|
|
body.username.clone().unwrap_or("randomname".to_owned()) |
|
|
|
body.username.clone().unwrap_or("randomname".to_owned()), |
|
|
|
|
|
|
|
data.hostname() |
|
|
|
)) |
|
|
|
)) |
|
|
|
.try_into() |
|
|
|
.try_into() |
|
|
|
{ |
|
|
|
{ |
|
|
|
@ -51,7 +56,7 @@ fn register_route( |
|
|
|
Ok(user_id) => user_id, |
|
|
|
Ok(user_id) => user_id, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
if users.contains_key(user_id.to_string()).unwrap() { |
|
|
|
if data.user_exists(&user_id) { |
|
|
|
debug!("ID already taken"); |
|
|
|
debug!("ID already taken"); |
|
|
|
return MatrixResult(Err(Error { |
|
|
|
return MatrixResult(Err(Error { |
|
|
|
kind: ErrorKind::UserInUse, |
|
|
|
kind: ErrorKind::UserInUse, |
|
|
|
@ -60,28 +65,25 @@ fn register_route( |
|
|
|
})); |
|
|
|
})); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
users |
|
|
|
data.user_add(user_id.clone(), body.password.clone()); |
|
|
|
.insert( |
|
|
|
|
|
|
|
user_id.to_string(), |
|
|
|
|
|
|
|
&*body.password.clone().unwrap_or_default(), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MatrixResult(Ok(register::Response { |
|
|
|
MatrixResult(Ok(register::Response { |
|
|
|
access_token: "randomtoken".to_owned(), |
|
|
|
access_token: "randomtoken".to_owned(), |
|
|
|
home_server: "localhost".to_owned(), |
|
|
|
home_server: data.hostname(), |
|
|
|
user_id, |
|
|
|
user_id, |
|
|
|
device_id: body.device_id.clone().unwrap_or("randomid".to_owned()), |
|
|
|
device_id: body.device_id.clone().unwrap_or("randomid".to_owned()), |
|
|
|
})) |
|
|
|
})) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[post("/_matrix/client/r0/login", data = "<body>")] |
|
|
|
#[post("/_matrix/client/r0/login", data = "<body>")] |
|
|
|
fn login_route(db: State<Db>, body: Ruma<login::Request>) -> MatrixResult<login::Response> { |
|
|
|
fn login_route(data: State<Data>, body: Ruma<login::Request>) -> MatrixResult<login::Response> { |
|
|
|
let user_id = if let login::UserInfo::MatrixId(username) = &body.user { |
|
|
|
let username = if let login::UserInfo::MatrixId(mut username) = body.user.clone() { |
|
|
|
let user_id = format!("@{}:localhost", username); |
|
|
|
if !username.contains(':') { |
|
|
|
let users = db.open_tree("users").unwrap(); |
|
|
|
username = format!("@{}:{}", username, data.hostname()); |
|
|
|
if !users.contains_key(user_id.clone()).unwrap() { |
|
|
|
} |
|
|
|
dbg!(); |
|
|
|
if let Ok(user_id) = (*username).try_into() { |
|
|
|
|
|
|
|
if !data.user_exists(&user_id) { |
|
|
|
|
|
|
|
debug!("Userid does not exist. Can't log in."); |
|
|
|
return MatrixResult(Err(Error { |
|
|
|
return MatrixResult(Err(Error { |
|
|
|
kind: ErrorKind::Forbidden, |
|
|
|
kind: ErrorKind::Forbidden, |
|
|
|
message: "UserId not found.".to_owned(), |
|
|
|
message: "UserId not found.".to_owned(), |
|
|
|
@ -90,7 +92,15 @@ fn login_route(db: State<Db>, body: Ruma<login::Request>) -> MatrixResult<login: |
|
|
|
} |
|
|
|
} |
|
|
|
user_id |
|
|
|
user_id |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
dbg!(); |
|
|
|
debug!("Invalid UserId."); |
|
|
|
|
|
|
|
return MatrixResult(Err(Error { |
|
|
|
|
|
|
|
kind: ErrorKind::Unknown, |
|
|
|
|
|
|
|
message: "Bad login type.".to_owned(), |
|
|
|
|
|
|
|
status_code: http::StatusCode::BAD_REQUEST, |
|
|
|
|
|
|
|
})); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
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(), |
|
|
|
@ -99,7 +109,7 @@ fn login_route(db: State<Db>, body: Ruma<login::Request>) -> MatrixResult<login: |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
return MatrixResult(Ok(login::Response { |
|
|
|
return MatrixResult(Ok(login::Response { |
|
|
|
user_id: (*user_id).try_into().unwrap(), // User id is correct because the user is already registered
|
|
|
|
user_id: username.try_into().unwrap(), // Unwrap is okay because the user is already registered
|
|
|
|
access_token: "randomtoken".to_owned(), |
|
|
|
access_token: "randomtoken".to_owned(), |
|
|
|
home_server: Some("localhost".to_owned()), |
|
|
|
home_server: Some("localhost".to_owned()), |
|
|
|
device_id: body.device_id.clone().unwrap_or("randomid".to_owned()), |
|
|
|
device_id: body.device_id.clone().unwrap_or("randomid".to_owned()), |
|
|
|
@ -148,7 +158,7 @@ fn create_message_event_route( |
|
|
|
_txn_id: String, |
|
|
|
_txn_id: String, |
|
|
|
body: Ruma<create_message_event::IncomingRequest>, |
|
|
|
body: Ruma<create_message_event::IncomingRequest>, |
|
|
|
) -> MatrixResult<create_message_event::Response> { |
|
|
|
) -> MatrixResult<create_message_event::Response> { |
|
|
|
dbg!(body.0); |
|
|
|
dbg!(body); |
|
|
|
MatrixResult(Ok(create_message_event::Response { |
|
|
|
MatrixResult(Ok(create_message_event::Response { |
|
|
|
event_id: "$randomeventid".try_into().unwrap(), |
|
|
|
event_id: "$randomeventid".try_into().unwrap(), |
|
|
|
})) |
|
|
|
})) |
|
|
|
@ -161,12 +171,8 @@ fn main() { |
|
|
|
} |
|
|
|
} |
|
|
|
pretty_env_logger::init(); |
|
|
|
pretty_env_logger::init(); |
|
|
|
|
|
|
|
|
|
|
|
let db = sled::open( |
|
|
|
let data = Data::load_or_create(); |
|
|
|
ProjectDirs::from("xyz", "koesters", "matrixserver") |
|
|
|
data.set_hostname("localhost"); |
|
|
|
.unwrap() |
|
|
|
|
|
|
|
.data_dir(), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rocket::ignite() |
|
|
|
rocket::ignite() |
|
|
|
.mount( |
|
|
|
.mount( |
|
|
|
@ -180,6 +186,6 @@ fn main() { |
|
|
|
create_message_event_route, |
|
|
|
create_message_event_route, |
|
|
|
], |
|
|
|
], |
|
|
|
) |
|
|
|
) |
|
|
|
.manage(db) |
|
|
|
.manage(data) |
|
|
|
.launch(); |
|
|
|
.launch(); |
|
|
|
} |
|
|
|
} |
|
|
|
|