2020-02-16 00:42:21 +03:00
|
|
|
#![feature(proc_macro_hygiene, decl_macro)]
|
2020-03-28 20:50:02 +03:00
|
|
|
mod data;
|
2020-03-30 14:46:18 +03:00
|
|
|
mod database;
|
2020-02-16 00:42:21 +03:00
|
|
|
mod ruma_wrapper;
|
2020-03-29 14:48:44 +03:00
|
|
|
mod utils;
|
2020-02-16 00:42:21 +03:00
|
|
|
|
2020-03-29 01:08:59 +03:00
|
|
|
pub use data::Data;
|
2020-03-30 14:46:18 +03:00
|
|
|
pub use database::Database;
|
|
|
|
|
2020-03-28 20:50:02 +03:00
|
|
|
use log::debug;
|
2020-04-03 18:27:08 +03:00
|
|
|
use rocket::{get, options, post, put, routes, State};
|
2020-03-28 20:50:02 +03:00
|
|
|
use ruma_client_api::{
|
|
|
|
error::{Error, ErrorKind},
|
|
|
|
r0::{
|
|
|
|
account::register, alias::get_alias, membership::join_room_by_id,
|
2020-04-03 18:27:08 +03:00
|
|
|
message::create_message_event, session::login, sync::sync_events,
|
2020-02-19 00:07:57 +03:00
|
|
|
},
|
2020-03-28 20:50:02 +03:00
|
|
|
unversioned::get_supported_versions,
|
2020-02-16 00:42:21 +03:00
|
|
|
};
|
2020-03-30 14:46:18 +03:00
|
|
|
use ruma_events::{collections::all::Event, room::message::MessageEvent};
|
2020-03-29 14:48:44 +03:00
|
|
|
use ruma_identifiers::{EventId, UserId};
|
2020-03-28 20:50:02 +03:00
|
|
|
use ruma_wrapper::{MatrixResult, Ruma};
|
2020-03-29 22:05:20 +03:00
|
|
|
use serde_json::map::Map;
|
2020-03-30 14:46:18 +03:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
convert::{TryFrom, TryInto},
|
2020-04-03 18:27:08 +03:00
|
|
|
path::PathBuf,
|
2020-03-30 14:46:18 +03:00
|
|
|
};
|
2020-02-16 00:42:21 +03:00
|
|
|
|
2020-02-19 00:07:57 +03:00
|
|
|
#[get("/_matrix/client/versions")]
|
|
|
|
fn get_supported_versions_route() -> MatrixResult<get_supported_versions::Response> {
|
|
|
|
MatrixResult(Ok(get_supported_versions::Response {
|
2020-04-03 18:27:08 +03:00
|
|
|
versions: vec!["r0.6.0".to_owned()],
|
2020-03-27 23:00:40 +03:00
|
|
|
unstable_features: HashMap::new(),
|
2020-02-19 00:07:57 +03:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:42:21 +03:00
|
|
|
#[post("/_matrix/client/r0/register", data = "<body>")]
|
2020-02-20 12:12:13 +03:00
|
|
|
fn register_route(
|
2020-03-28 20:50:02 +03:00
|
|
|
data: State<Data>,
|
2020-02-20 12:12:13 +03:00
|
|
|
body: Ruma<register::Request>,
|
|
|
|
) -> MatrixResult<register::Response> {
|
2020-03-29 22:05:20 +03:00
|
|
|
// Validate user id
|
2020-02-20 12:12:13 +03:00
|
|
|
let user_id: UserId = match (*format!(
|
2020-03-28 20:50:02 +03:00
|
|
|
"@{}:{}",
|
|
|
|
body.username.clone().unwrap_or("randomname".to_owned()),
|
|
|
|
data.hostname()
|
2020-02-19 00:07:57 +03:00
|
|
|
))
|
|
|
|
.try_into()
|
|
|
|
{
|
|
|
|
Err(_) => {
|
2020-03-28 17:16:18 +03:00
|
|
|
debug!("Username invalid");
|
2020-02-19 00:07:57 +03:00
|
|
|
return MatrixResult(Err(Error {
|
|
|
|
kind: ErrorKind::InvalidUsername,
|
2020-02-20 12:12:13 +03:00
|
|
|
message: "Username was invalid.".to_owned(),
|
2020-02-19 00:07:57 +03:00
|
|
|
status_code: http::StatusCode::BAD_REQUEST,
|
2020-03-28 17:16:18 +03:00
|
|
|
}));
|
2020-02-19 00:07:57 +03:00
|
|
|
}
|
|
|
|
Ok(user_id) => user_id,
|
|
|
|
};
|
|
|
|
|
2020-03-29 22:05:20 +03:00
|
|
|
// Check if username is creative enough
|
2020-03-28 20:50:02 +03:00
|
|
|
if data.user_exists(&user_id) {
|
2020-03-28 17:16:18 +03:00
|
|
|
debug!("ID already taken");
|
2020-02-20 12:12:13 +03: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-29 22:05:20 +03:00
|
|
|
// 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());
|
2020-02-20 12:12:13 +03:00
|
|
|
|
2020-02-19 00:07:57 +03:00
|
|
|
MatrixResult(Ok(register::Response {
|
2020-03-29 22:05:20 +03:00
|
|
|
access_token: token,
|
2020-03-30 14:46:18 +03:00
|
|
|
home_server: data.hostname().to_owned(),
|
2020-02-19 00:07:57 +03:00
|
|
|
user_id,
|
2020-03-29 22:05:20 +03:00
|
|
|
device_id,
|
2020-02-19 00:07:57 +03:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2020-02-20 12:12:13 +03:00
|
|
|
#[post("/_matrix/client/r0/login", data = "<body>")]
|
2020-03-28 20:50:02 +03:00
|
|
|
fn login_route(data: State<Data>, body: Ruma<login::Request>) -> MatrixResult<login::Response> {
|
2020-03-29 22:05:20 +03:00
|
|
|
// Validate login method
|
|
|
|
let user_id =
|
|
|
|
if let (login::UserInfo::MatrixId(mut username), login::LoginInfo::Password { password }) =
|
|
|
|
(body.user.clone(), body.login_info.clone())
|
|
|
|
{
|
|
|
|
if !username.contains(':') {
|
|
|
|
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.");
|
2020-03-28 20:50:02 +03:00
|
|
|
return MatrixResult(Err(Error {
|
2020-03-29 22:05:20 +03:00
|
|
|
kind: ErrorKind::Unknown,
|
|
|
|
message: "Bad login type.".to_owned(),
|
2020-03-28 20:50:02 +03:00
|
|
|
status_code: http::StatusCode::BAD_REQUEST,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-29 22:05:20 +03:00
|
|
|
debug!("Bad login type");
|
2020-02-20 12:12:13 +03:00
|
|
|
return MatrixResult(Err(Error {
|
2020-03-28 20:50:02 +03:00
|
|
|
kind: ErrorKind::Unknown,
|
|
|
|
message: "Bad login type.".to_owned(),
|
2020-02-20 12:12:13 +03:00
|
|
|
status_code: http::StatusCode::BAD_REQUEST,
|
|
|
|
}));
|
2020-03-29 22:05:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Generate new device id if the user didn't specify one
|
|
|
|
let device_id = body
|
|
|
|
.device_id
|
|
|
|
.clone()
|
|
|
|
.unwrap_or("TODO:randomdeviceid".to_owned());
|
|
|
|
|
2020-03-30 14:46:18 +03:00
|
|
|
// Add device
|
2020-03-29 22:05:20 +03:00
|
|
|
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());
|
2020-02-20 12:12:13 +03:00
|
|
|
|
|
|
|
return MatrixResult(Ok(login::Response {
|
2020-03-29 22:05:20 +03:00
|
|
|
user_id,
|
|
|
|
access_token: token,
|
2020-03-30 14:46:18 +03:00
|
|
|
home_server: Some(data.hostname().to_owned()),
|
2020-03-29 22:05:20 +03:00
|
|
|
device_id,
|
2020-02-20 12:12:13 +03:00
|
|
|
well_known: None,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-02-19 00:07:57 +03:00
|
|
|
#[get("/_matrix/client/r0/directory/room/<room_alias>")]
|
|
|
|
fn get_alias_route(room_alias: String) -> MatrixResult<get_alias::Response> {
|
2020-03-29 22:05:20 +03:00
|
|
|
// TODO
|
2020-02-19 00:07:57 +03:00
|
|
|
let room_id = match &*room_alias {
|
|
|
|
"#room:localhost" => "!xclkjvdlfj:localhost",
|
|
|
|
_ => {
|
2020-03-29 22:05:20 +03:00
|
|
|
debug!("Room not found.");
|
2020-02-19 00:07:57 +03:00
|
|
|
return MatrixResult(Err(Error {
|
|
|
|
kind: ErrorKind::NotFound,
|
|
|
|
message: "Room not found.".to_owned(),
|
|
|
|
status_code: http::StatusCode::NOT_FOUND,
|
2020-03-29 22:05:20 +03:00
|
|
|
}));
|
2020-02-19 00:07:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.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> {
|
2020-03-29 22:05:20 +03:00
|
|
|
// TODO
|
2020-02-19 00:07:57 +03:00
|
|
|
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(
|
2020-03-29 14:48:44 +03:00
|
|
|
data: State<Data>,
|
2020-02-19 00:07:57 +03:00
|
|
|
_room_id: String,
|
|
|
|
_event_type: String,
|
|
|
|
_txn_id: String,
|
2020-03-29 03:29:47 +03:00
|
|
|
body: Ruma<create_message_event::Request>,
|
2020-02-19 00:07:57 +03:00
|
|
|
) -> MatrixResult<create_message_event::Response> {
|
2020-03-30 16:38:25 +03:00
|
|
|
// Construct event
|
2020-04-03 18:27:08 +03:00
|
|
|
let mut event = Event::RoomMessage(MessageEvent {
|
2020-03-30 16:38:25 +03:00
|
|
|
content: body.data.clone().into_result().unwrap(),
|
2020-04-03 18:27:08 +03:00
|
|
|
event_id: EventId::try_from("$thiswillbefilledinlater").unwrap(),
|
2020-03-30 16:38:25 +03:00
|
|
|
origin_server_ts: utils::millis_since_unix_epoch(),
|
|
|
|
room_id: Some(body.room_id.clone()),
|
|
|
|
sender: body.user_id.clone().expect("user is authenticated"),
|
|
|
|
unsigned: Map::default(),
|
|
|
|
});
|
|
|
|
|
2020-03-30 01:10:15 +03:00
|
|
|
// Generate event id
|
2020-04-03 18:27:08 +03:00
|
|
|
let event_id = EventId::try_from(&*format!(
|
|
|
|
"${}",
|
|
|
|
ruma_signatures::reference_hash(&serde_json::to_value(&event).unwrap())
|
|
|
|
.expect("ruma can calculate reference hashes")
|
|
|
|
))
|
|
|
|
.expect("ruma's reference hashes are correct");
|
|
|
|
|
|
|
|
// Insert event id
|
|
|
|
if let Event::RoomMessage(message) = &mut event {
|
|
|
|
message.event_id = event_id.clone();
|
|
|
|
}
|
2020-03-30 16:38:25 +03:00
|
|
|
|
2020-04-03 18:27:08 +03:00
|
|
|
// Add PDU to the graph
|
|
|
|
data.pdu_append(&event_id, &body.room_id, event);
|
2020-03-29 22:05:20 +03:00
|
|
|
|
|
|
|
MatrixResult(Ok(create_message_event::Response { event_id }))
|
2020-02-16 00:42:21 +03:00
|
|
|
}
|
|
|
|
|
2020-04-03 22:17:27 +03:00
|
|
|
#[get("/_matrix/client/r0/sync", data = "<body>")]
|
|
|
|
fn sync_route(
|
|
|
|
data: State<Data>,
|
|
|
|
body: Ruma<sync_events::Request>,
|
|
|
|
) -> MatrixResult<sync_events::Response> {
|
2020-04-03 18:27:08 +03:00
|
|
|
let pdus = data.pdus_all();
|
|
|
|
let mut joined_rooms = HashMap::new();
|
|
|
|
joined_rooms.insert(
|
|
|
|
"!roomid:localhost".try_into().unwrap(),
|
|
|
|
sync_events::JoinedRoom {
|
|
|
|
account_data: sync_events::AccountData { events: Vec::new() },
|
|
|
|
summary: sync_events::RoomSummary {
|
|
|
|
heroes: Vec::new(),
|
|
|
|
joined_member_count: None,
|
|
|
|
invited_member_count: None,
|
|
|
|
},
|
|
|
|
unread_notifications: sync_events::UnreadNotificationsCount {
|
|
|
|
highlight_count: None,
|
|
|
|
notification_count: None,
|
|
|
|
},
|
|
|
|
timeline: sync_events::Timeline {
|
|
|
|
limited: None,
|
|
|
|
prev_batch: None,
|
|
|
|
events: todo!(),
|
|
|
|
},
|
|
|
|
state: sync_events::State { events: Vec::new() },
|
|
|
|
ephemeral: sync_events::Ephemeral { events: Vec::new() },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
MatrixResult(Ok(sync_events::Response {
|
|
|
|
next_batch: String::new(),
|
|
|
|
rooms: sync_events::Rooms {
|
|
|
|
leave: Default::default(),
|
|
|
|
join: joined_rooms,
|
|
|
|
invite: Default::default(),
|
|
|
|
},
|
|
|
|
presence: sync_events::Presence { events: Vec::new() },
|
|
|
|
device_lists: Default::default(),
|
|
|
|
device_one_time_keys_count: Default::default(),
|
|
|
|
to_device: sync_events::ToDevice { events: Vec::new() },
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[options("/<_segments..>")]
|
|
|
|
fn options_route(_segments: PathBuf) -> MatrixResult<create_message_event::Response> {
|
|
|
|
MatrixResult(Err(Error {
|
|
|
|
kind: ErrorKind::NotFound,
|
|
|
|
message: "Room not found.".to_owned(),
|
|
|
|
status_code: http::StatusCode::NOT_FOUND,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:42:21 +03:00
|
|
|
fn main() {
|
2020-03-27 23:00:40 +03:00
|
|
|
// Log info by default
|
|
|
|
if let Err(_) = std::env::var("RUST_LOG") {
|
2020-04-03 18:27:08 +03:00
|
|
|
std::env::set_var("RUST_LOG", "matrixserver=debug,info");
|
2020-03-27 23:00:40 +03:00
|
|
|
}
|
2020-02-16 00:42:21 +03:00
|
|
|
pretty_env_logger::init();
|
2020-03-27 23:00:40 +03:00
|
|
|
|
2020-03-30 14:46:18 +03:00
|
|
|
let data = Data::load_or_create("localhost");
|
|
|
|
data.debug();
|
2020-02-20 12:12:13 +03:00
|
|
|
|
2020-02-16 00:42:21 +03:00
|
|
|
rocket::ignite()
|
2020-02-19 00:07:57 +03:00
|
|
|
.mount(
|
|
|
|
"/",
|
|
|
|
routes![
|
|
|
|
get_supported_versions_route,
|
|
|
|
register_route,
|
2020-02-20 12:12:13 +03:00
|
|
|
login_route,
|
2020-02-19 00:07:57 +03:00
|
|
|
get_alias_route,
|
|
|
|
join_room_by_id_route,
|
|
|
|
create_message_event_route,
|
2020-04-03 18:27:08 +03:00
|
|
|
sync_route,
|
|
|
|
options_route,
|
2020-02-19 00:07:57 +03:00
|
|
|
],
|
|
|
|
)
|
2020-03-28 20:50:02 +03:00
|
|
|
.manage(data)
|
2020-02-16 00:42:21 +03:00
|
|
|
.launch();
|
|
|
|
}
|