conduit/src/main.rs

192 lines
5.8 KiB
Rust
Raw Normal View History

2020-02-15 21:42:21 +00:00
#![feature(proc_macro_hygiene, decl_macro)]
2020-03-28 17:50:02 +00:00
mod data;
2020-02-15 21:42:21 +00:00
mod ruma_wrapper;
2020-03-28 22:08:59 +00:00
pub use data::Data;
2020-03-28 17:50:02 +00:00
use log::debug;
use rocket::{get, post, put, routes, State};
use ruma_client_api::{
error::{Error, ErrorKind},
r0::{
account::register, alias::get_alias, membership::join_room_by_id,
message::create_message_event, session::login,
2020-02-18 21:07:57 +00:00
},
2020-03-28 17:50:02 +00:00
unversioned::get_supported_versions,
2020-02-15 21:42:21 +00:00
};
2020-03-28 17:50:02 +00:00
use ruma_identifiers::UserId;
use ruma_wrapper::{MatrixResult, Ruma};
use std::{collections::HashMap, convert::TryInto};
2020-02-15 21:42:21 +00:00
2020-02-18 21:07:57 +00:00
#[get("/_matrix/client/versions")]
fn get_supported_versions_route() -> MatrixResult<get_supported_versions::Response> {
MatrixResult(Ok(get_supported_versions::Response {
2020-03-28 17:50:02 +00:00
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(),
],
2020-03-27 20:00:40 +00:00
unstable_features: HashMap::new(),
2020-02-18 21:07:57 +00:00
}))
}
2020-02-15 21:42:21 +00:00
#[post("/_matrix/client/r0/register", data = "<body>")]
2020-02-20 09:12:13 +00:00
fn register_route(
2020-03-28 17:50:02 +00:00
data: State<Data>,
2020-02-20 09:12:13 +00:00
body: Ruma<register::Request>,
) -> MatrixResult<register::Response> {
let user_id: UserId = match (*format!(
2020-03-28 17:50:02 +00:00
"@{}:{}",
body.username.clone().unwrap_or("randomname".to_owned()),
data.hostname()
2020-02-18 21:07:57 +00:00
))
.try_into()
{
Err(_) => {
2020-03-28 14:16:18 +00:00
debug!("Username invalid");
2020-02-18 21:07:57 +00:00
return MatrixResult(Err(Error {
kind: ErrorKind::InvalidUsername,
2020-02-20 09:12:13 +00:00
message: "Username was invalid.".to_owned(),
2020-02-18 21:07:57 +00:00
status_code: http::StatusCode::BAD_REQUEST,
2020-03-28 14:16:18 +00:00
}));
2020-02-18 21:07:57 +00:00
}
Ok(user_id) => user_id,
};
2020-03-28 17:50:02 +00:00
if data.user_exists(&user_id) {
2020-03-28 14:16:18 +00:00
debug!("ID already taken");
2020-02-20 09:12:13 +00:00
return MatrixResult(Err(Error {
kind: ErrorKind::UserInUse,
message: "Desired user ID is already taken.".to_owned(),
status_code: http::StatusCode::BAD_REQUEST,
}));
}
2020-03-28 17:50:02 +00:00
data.user_add(user_id.clone(), body.password.clone());
2020-02-20 09:12:13 +00:00
2020-02-18 21:07:57 +00:00
MatrixResult(Ok(register::Response {
access_token: "randomtoken".to_owned(),
2020-03-28 17:50:02 +00:00
home_server: data.hostname(),
2020-02-18 21:07:57 +00:00
user_id,
device_id: body.device_id.clone().unwrap_or("randomid".to_owned()),
}))
}
2020-02-20 09:12:13 +00:00
#[post("/_matrix/client/r0/login", data = "<body>")]
2020-03-28 17:50:02 +00:00
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() {
if !username.contains(':') {
username = format!("@{}:{}", username, data.hostname());
}
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 {
kind: ErrorKind::Forbidden,
message: "UserId not found.".to_owned(),
status_code: http::StatusCode::BAD_REQUEST,
}));
}
user_id
} else {
debug!("Invalid UserId.");
2020-02-20 09:12:13 +00:00
return MatrixResult(Err(Error {
2020-03-28 17:50:02 +00:00
kind: ErrorKind::Unknown,
message: "Bad login type.".to_owned(),
2020-02-20 09:12:13 +00:00
status_code: http::StatusCode::BAD_REQUEST,
}));
}
} else {
2020-03-28 17:50:02 +00:00
debug!("Bad login type");
2020-02-20 09:12:13 +00:00
return MatrixResult(Err(Error {
kind: ErrorKind::Unknown,
message: "Bad login type.".to_owned(),
status_code: http::StatusCode::BAD_REQUEST,
}));
};
return MatrixResult(Ok(login::Response {
2020-03-28 17:50:02 +00:00
user_id: username.try_into().unwrap(), // Unwrap is okay because the user is already registered
2020-02-20 09:12:13 +00:00
access_token: "randomtoken".to_owned(),
home_server: Some("localhost".to_owned()),
device_id: body.device_id.clone().unwrap_or("randomid".to_owned()),
well_known: None,
}));
}
2020-02-18 21:07:57 +00:00
#[get("/_matrix/client/r0/directory/room/<room_alias>")]
fn get_alias_route(room_alias: String) -> MatrixResult<get_alias::Response> {
let room_id = match &*room_alias {
"#room:localhost" => "!xclkjvdlfj:localhost",
_ => {
return MatrixResult(Err(Error {
kind: ErrorKind::NotFound,
message: "Room not found.".to_owned(),
status_code: http::StatusCode::NOT_FOUND,
}))
}
}
.try_into()
.unwrap();
MatrixResult(Ok(get_alias::Response {
room_id,
servers: vec!["localhost".to_owned()],
}))
}
#[post("/_matrix/client/r0/rooms/<_room_id>/join", data = "<body>")]
fn join_room_by_id_route(
_room_id: String,
body: Ruma<join_room_by_id::Request>,
) -> MatrixResult<join_room_by_id::Response> {
MatrixResult(Ok(join_room_by_id::Response {
room_id: body.room_id.clone(),
}))
}
#[put(
"/_matrix/client/r0/rooms/<_room_id>/send/<_event_type>/<_txn_id>",
data = "<body>"
)]
fn create_message_event_route(
_room_id: String,
_event_type: String,
_txn_id: String,
body: Ruma<create_message_event::IncomingRequest>,
) -> MatrixResult<create_message_event::Response> {
2020-03-28 17:50:02 +00:00
dbg!(body);
2020-02-18 21:07:57 +00:00
MatrixResult(Ok(create_message_event::Response {
event_id: "$randomeventid".try_into().unwrap(),
}))
2020-02-15 21:42:21 +00:00
}
fn main() {
2020-03-27 20:00:40 +00:00
// Log info by default
if let Err(_) = std::env::var("RUST_LOG") {
std::env::set_var("RUST_LOG", "info");
}
2020-02-15 21:42:21 +00:00
pretty_env_logger::init();
2020-03-27 20:00:40 +00:00
2020-03-28 17:50:02 +00:00
let data = Data::load_or_create();
data.set_hostname("localhost");
2020-02-20 09:12:13 +00:00
2020-02-15 21:42:21 +00:00
rocket::ignite()
2020-02-18 21:07:57 +00:00
.mount(
"/",
routes![
get_supported_versions_route,
register_route,
2020-02-20 09:12:13 +00:00
login_route,
2020-02-18 21:07:57 +00:00
get_alias_route,
join_room_by_id_route,
create_message_event_route,
],
)
2020-03-28 17:50:02 +00:00
.manage(data)
2020-02-15 21:42:21 +00:00
.launch();
}