diff --git a/.gitignore b/.gitignore index a55fe4d85..b2287d6fc 100644 --- a/.gitignore +++ b/.gitignore @@ -74,4 +74,5 @@ share/python-wheels/ *.egg-info/ .installed.cfg *.egg +*.pyc MANIFEST diff --git a/Cargo.lock b/Cargo.lock index 461ac5bca..49ee3f226 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2226,7 +2226,10 @@ dependencies = [ "log", "num-derive", "num-traits", + "percent-encoding", + "serde", "trng", + "ureq", "xous 0.9.20 (registry+https://github.com/rust-lang/crates.io-index)", "xous-api-log", "xous-api-names", @@ -2563,9 +2566,9 @@ dependencies = [ [[package]] name = "percent-encoding" -version = "2.1.0" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" [[package]] name = "perflib" diff --git a/apps/mtxcli/Cargo.toml b/apps/mtxcli/Cargo.toml index e2e2a634b..b1cbfd9a9 100644 --- a/apps/mtxcli/Cargo.toml +++ b/apps/mtxcli/Cargo.toml @@ -25,6 +25,10 @@ content-plugin-api = {path = "../../services/content-plugin-api"} # all content com = {path = "../../services/com"} llio = {path = "../../services/llio"} codec = {path = "../../services/codec"} +# new dependencies for mtxcli +percent-encoding = "2.2" +serde = { version = "1.0", features = [ "derive" ] } +ureq = { version = "2.5", features = ["json"] } [features] default = [] diff --git a/apps/mtxcli/README.md b/apps/mtxcli/README.md index 67cbaf276..af1647bce 100644 --- a/apps/mtxcli/README.md +++ b/apps/mtxcli/README.md @@ -1,33 +1,36 @@ -# MTXCLI +# mtxcli -This is a new Matrix chat application which will be derived from REPL. +This is a simple Matrix chat application. ![screenshot](mtxcli_screenshot.png) ## Background -This is a simple [Matrix chat](https://matrix.org/) application based on the REPL framework. +This is a simple [Matrix chat](https://matrix.org/) application based on the REPL framework and adapted from the Desktop project [mtxcli](https://github.com/betrusted-io/mtxcli) ## Setup In order to simplify the **mtxcli** application as much as possible the following must be setup on a conventional device before using Betrusted (laptop, mobile phone). -* Create a new Matrix user: +* Create a new Matrix user (if needed): * https://matrix.org/faq/#how-do-i-get-an-account-and-get-started%3F + * https://matrix.org/docs/projects/try-matrix-now/ * Find (or create) a new room to join for chatting: * https://doc.matrix.tu-dresden.de/en/rooms/create/ * https://spec.matrix.org/latest/#room-structure * Join the room * Save the configuration in the pddb * `/set user @myname:matrix.org` - * `/set password AbigSecret` + * `/set password MySecret` * `/set room #my-chat-room` ## Commands The following commands are available in **mtxcli**: * `/help [cmd]` -- shows help message for all commands (or one cmd if given) +* `/login` -- logs in to the server (assumes user and password are set) +* `/logout` -- logs out of the server * `/get key` -- shows value of key (from the PDDB) * `/set key value` -- sets value of key (in the PDDB) * `/unset key` -- removes key (from the PDDB) @@ -38,4 +41,11 @@ The following commands are available in **mtxcli**: Any line not starting with a slash ('/') will be sent to the current room. New messages from the current room will be displayed. -_NOTE:_ chatting is not implemented yet +## Acknowledgements + +This app is supported in part by a grant from the +[NGI0 PET Fund](https://nlnet.nl/PET/), +a fund established by NLnet with financial support from the +European Commission's [Next Generation Internet](https://www.ngi.eu/) program. + +![nl.net+NGI0](https://www.crowdsupply.com/img/001b/precursor-grant-logos_png_md-xl.jpg) diff --git a/apps/mtxcli/locales/i18n.json b/apps/mtxcli/locales/i18n.json index d780acfc4..352394602 100644 --- a/apps/mtxcli/locales/i18n.json +++ b/apps/mtxcli/locales/i18n.json @@ -1,50 +1,134 @@ { + "mtxcli.filter.failed": { + "en": "error: could not create filter", + "ja": "error: could not create filter *EN*", + "zh": "error: could not create filter *EN*", + "en-tts": "error: could not create filter" + }, "mtxcli.greeting": { - "en": "Welcome to the MTXCLI demo.", + "en": "Welcome to the MTXCLI demo. Type /help for available commands", "ja": "シンプルなMTXCLIデモへようこそ", "zh": "简单的 mtxcli 演示活动", - "en-tts": "Welcome to the simple MTXCLI demo." + "en-tts": "Welcome to the simple MTXCLI demo. Type /help for available commands" }, "mtxcli.get.help": { "en": "/get key", - "ja": "TBD /get key", - "zh": "TBD /get key", + "ja": "/get key *EN*", + "zh": "/get key *EN*", "en-tts": "get key" }, "mtxcli.help.help": { "en": "/help [cmd]", - "ja": "TBD /help [cmd]", - "zh": "TBD /help [cmd]", + "ja": "/help [cmd] *EN*", + "zh": "/help [cmd] *EN*", "en-tts": "help [cmd]" }, "mtxcli.help.overview": { "en": "available mtxcli commands:", - "ja": "TBD available mtxcli commands:", - "zh": "TBD available mtxcli commands:", + "ja": "available mtxcli commands: *EN*", + "zh": "available mtxcli commands: *EN*", "en-tts": "available mtxcli commands:" }, + "mtxcli.logging.in": { + "en": "logging in...", + "ja": "logging in... *EN*", + "zh": "logging in... *EN*", + "en-tts": "logging in" + }, + "mtxcli.logged.in": { + "en": "logged in", + "ja": "logged in *EN*", + "zh": "logged in *EN*", + "en-tts": "logged in" + }, + "mtxcli.logged.out": { + "en": "logged out", + "ja": "logged out *EN*", + "zh": "logged out *EN*", + "en-tts": "logged out" + }, + "mtxcli.login.failed": { + "en": "authentication failed", + "ja": "authentication failed *EN*", + "zh": "authentication failed *EN*", + "en-tts": "authentication failed" + }, + "mtxcli.login.help": { + "en": "/login", + "ja": "/login *EN*", + "zh": "/login *EN*", + "en-tts": "login" + }, + "mtxcli.logout.help": { + "en": "/logout", + "ja": "/logout *EN*", + "zh": "/logout *EN*", + "en-tts": "logout" + }, + "mtxcli.not.connected": { + "en": "not connected", + "ja": "not connected *EN*", + "zh": "not connected *EN*", + "en-tts": "not conntected" + }, + "mtxcli.please.set.user": { + "en": "please /set user @USER:matrix.org", + "ja": "please /set user @USER:matrix.org *EN*", + "zh": "please /set user @USER:matrix.org *EN*", + "en-tts": "please /set user @USER:matrix.org" + }, + "mtxcli.please.set.password": { + "en": "please /set password my-password", + "ja": "please /set password my-password *EN*", + "zh": "please /set password my-password *EN*", + "en-tts": "please /set password my-password" + }, + "mtxcli.please.set.room": { + "en": "please /set room #my-room-to-join", + "ja": "please /set room #my-room-to-join *EN*", + "zh": "please /set room #my-room-to-join *EN*", + "en-tts": "please /set room #my-room-to-join" + }, + "mtxcli.please.set.server": { + "en": "please /set server https://matrix.org", + "ja": "please /set server https://matrix.org *EN*", + "zh": "please /set server https://matrix.org *EN*", + "en-tts": "please /set server https://matrix.org" + }, + "mtxcli.roomid.failed": { + "en": "error: could not find room_id", + "ja": "error: could not find room_id *EN*", + "zh": "error: could not find room_id *EN*", + "en-tts": "error: could not find room_id" + }, "mtxcli.set.help": { "en": "/set key value", - "ja": "TBD /set key value", - "zh": "TBD /set key value", + "ja": "/set key value *EN*", + "zh": "/set key value *EN*", "en-tts": "set key value" }, + "mtxcli.send.failed": { + "en": "FAILED TO SEND!", + "ja": "FAILED TO SEND! *EN*", + "zh": "FAILED TO SEND! *EN*", + "en-tts": "FAILED TO SEND!" + }, "mtxcli.status.help": { "en": "/status", - "ja": "TBD /status", - "zh": "TBD /status", + "ja": "/status *EN*", + "zh": "/status *EN*", "en-tts": "status" }, "mtxcli.unset.help": { "en": "/unset key", - "ja": "TBD /unset key", - "zh": "TBD /unset key", + "ja": "/unset key *EN*", + "zh": "/unset key *EN*", "en-tts": "unset key" }, "mtxcli.unknown.help": { "en": "unknown command", - "ja": "TBD unknown command", - "zh": "TBD unknown command", + "ja": "unknown command *EN*", + "zh": "unknown command *EN*", "en-tts": "unknown command" } } diff --git a/apps/mtxcli/src/cmds.rs b/apps/mtxcli/src/cmds.rs index 076ccef95..08dc52c55 100644 --- a/apps/mtxcli/src/cmds.rs +++ b/apps/mtxcli/src/cmds.rs @@ -1,15 +1,38 @@ use xous::{MessageEnvelope}; use xous_ipc::String as XousString; use core::fmt::Write; +use locales::t; use std::collections::HashMap; use std::fs::File; use std::io::{Read, Write as StdWrite, Error}; use std::path::PathBuf; +use std::time::{SystemTime, UNIX_EPOCH}; // to help gen_txn_id + +mod url; +mod web; + +const APP: &str = "mtxcli"; /// PDDB Dict for mtxcli keys const MTXCLI_DICT: &str = "mtxcli"; +const FILTER_KEY: &str = "_filter"; +const PASSWORD_KEY: &str = "password"; +const ROOM_ID_KEY: &str = "_room_id"; +const ROOM_KEY: &str = "room"; +const SINCE_KEY: &str = "_since"; +const SERVER_KEY: &str = "server"; +const TOKEN_KEY: &str = "_token"; +const USER_KEY: &str = "user"; +const USERNAME_KEY: &str = "username"; + +const HTTPS: &str = "https://"; +const SERVER_MATRIX: &str = "https://matrix.org"; + +const EMPTY: &str = ""; +const MTX_TIMEOUT: i32 = 300; // ms + /////////////////////////// Common items to all commands pub trait ShellCmdApi<'a> { // user implemented: @@ -56,46 +79,333 @@ pub struct CommonEnv { cb_registrations: HashMap::>, trng: Trng, xns: xous_names::XousNames, + user: String, + username: String, + server: String, + token: String, + logged_in: bool, + room_id: String, + filter: String, + since: String, + first_line: bool, } impl CommonEnv { + pub fn gen_txn_id(&mut self) -> String { + let mut txn_id = self.trng.get_u32() + .expect("unable to generate random u32"); + log::info!("trng.get_u32() = {}", txn_id); + txn_id += SystemTime::now().duration_since(UNIX_EPOCH) + .expect("Time went backwards").subsec_nanos(); + txn_id.to_string() + } + pub fn set(&mut self, key: &str, value: &str) -> Result<(), Error> { log::info!("set '{}' = '{}'", key, value); let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); - std::fs::create_dir_all(&keypath)?; + if std::fs::metadata(&keypath).is_ok() { // keypath exists + log::info!("dict '{}' exists", MTXCLI_DICT); + } else { + log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); + std::fs::create_dir_all(&keypath)?; + } keypath.push(key); File::create(keypath)?.write_all(value.as_bytes())?; + match key { // special case side effects + USER_KEY => { self.set_user(value); } + PASSWORD_KEY => { self.set_password(); } + ROOM_KEY => { self.set_room(); } + _ => { } + } Ok(()) } + // will log on error (vs. panic) + pub fn set_debug(&mut self, key: &str, value: &str) -> bool { + match self.set(key, value) { + Ok(()) => { + true + }, + Err(e) => { + log::error!("error setting key {}: {:?}", key, e); + false + } + } + } + + pub fn set_user(&mut self, value: &str) { + log::debug!("# USER_KEY set '{}' = '{}'", USER_KEY, value); + let i = match value.find('@') { + Some(index) => { index + 1 }, + None => { 0 }, + }; + let j = match value.find(':') { + Some(index) => { index }, + None => { value.len() }, + }; + self.username = (&value[i..j]).to_string(); + if j < value.len() { + self.server = String::from(HTTPS); + self.server.push_str(&value[j + 1..]); + } else { + self.server = SERVER_MATRIX.to_string(); + } + self.user = value.to_string(); + log::debug!("# user = '{}' username = '{}' server = '{}'", self.user, self.username, self.server); + self.set_debug(USERNAME_KEY, &self.username.clone()); + self.set_debug(SERVER_KEY, &self.server.clone()); + self.unset_debug(TOKEN_KEY); + self.token = EMPTY.to_string(); + } + + pub fn set_password(&mut self) { + log::debug!("# PASSWORD_KEY set '{}' => clearing TOKEN_KEY", PASSWORD_KEY); + self.unset_debug(TOKEN_KEY); + } + + pub fn set_room(&mut self) { + log::debug!("# ROOM_KEY set '{}' => clearing ROOM_ID_KEY, SINCE_KEY, FILTER_KEY", ROOM_KEY); + self.unset_debug(ROOM_ID_KEY); + self.unset_debug(SINCE_KEY); + self.unset_debug(FILTER_KEY); + } + pub fn unset(&mut self, key: &str) -> Result<(), Error> { - log::info!("unset '{}'", key); + // log::info!("unset '{}'", key); let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); - std::fs::create_dir_all(&keypath)?; + if std::fs::metadata(&keypath).is_ok() { // keypath exists + log::info!("dict '{}' exists", MTXCLI_DICT); + } else { + log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); + std::fs::create_dir_all(&keypath)?; + } keypath.push(key); if std::fs::metadata(&keypath).is_ok() { // keypath exists + log::info!("dict:key = '{}:{}' exists.. deleting it", MTXCLI_DICT, key); + std::fs::remove_file(keypath)?; } Ok(()) } + // will log on error (vs. panic) + pub fn unset_debug(&mut self, key: &str) -> bool { + match self.unset(key) { + Ok(()) => { + true + }, + Err(e) => { + log::error!("error unsetting key {}: {:?}", key, e); + false + } + } + } + pub fn get(&mut self, key: &str) -> Result, Error> { let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); - std::fs::create_dir_all(&keypath)?; + if std::fs::metadata(&keypath).is_ok() { // keypath exists + log::info!("dict '{}' exists", MTXCLI_DICT); + } else { + log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); + std::fs::create_dir_all(&keypath)?; + } keypath.push(key); if let Ok(mut file)= File::open(keypath) { let mut value = String::new(); file.read_to_string(&mut value)?; - log::info!("get '{}' = '{}'", key, value); + // log::info!("get '{}' = '{}'", key, value); Ok(Some(value)) } else { Ok(None) } } + pub fn get_default(&mut self, key: &str, default: &str) -> String { + match self.get(key) { + Ok(None) => { + default.to_string() + }, + Ok(Some(value)) => { + value.to_string() + } + Err(e) => { + log::error!("error getting key {}: {:?}", key, e); + default.to_string() + } + } + } + + pub fn prompt(&mut self, ret: &mut XousString::<1024>, text: &str) { + if self.first_line { + write!(ret, "{}> {}", APP, text).unwrap(); + self.first_line = false; + } else { + write!(ret, "\n{}> {}", APP, text).unwrap(); + } + } + + pub fn user_says(&mut self, ret: &mut XousString::<1024>, text: &str) { + if ! self.logged_in { + if ! self.login(ret) { + return; + } + } + if self.room_id.len() == 0 { + if ! self.get_room_id(ret) { + return; + } + } + if self.filter.len() == 0 { + if ! self.get_filter(ret) { + return; + } + } + self.read_messages(ret); + // The following is not required, because we will get what + // the user said when we read_messages the second time + // write!(ret, "{}> {}", self.username, text).unwrap(); + + let txn_id = self.gen_txn_id(); + log::info!("txn_id = {}", txn_id); + if web::send_message(&self.server, &self.room_id, &text, &txn_id, &self.token) { + log::info!("SENT: {}", text); + self.read_messages(ret); // update since to include what user said + } else { + log::info!("FAILED TO SEND"); + write!(ret, "{}", t!("mtxcli.send.failed", xous::LANG)).unwrap(); + } + } + + pub fn login(&mut self, ret: &mut XousString::<1024>) -> bool { + self.prompt(ret, t!("mtxcli.logging.in", xous::LANG)); + self.token = self.get_default(TOKEN_KEY, EMPTY); + self.logged_in = false; + if self.token.len() > 0 { + if web::whoami(&self.server, &self.token) { + self.logged_in = true; + } + } + if ! self.logged_in { + if web::get_login_type(&self.server) { + let user = self.get_default(USER_KEY, USER_KEY); + let password = self.get_default(PASSWORD_KEY, EMPTY); + if password.len() == 0 { + self.prompt(ret, t!("mtxcli.please.set.user", xous::LANG)); + self.prompt(ret, t!("mtxcli.please.set.password", xous::LANG)); + } else { + if let Some(new_token) = web::authenticate_user(&self.server, &user, &password) { + self.set_debug(TOKEN_KEY, &new_token); + self.token = new_token; + self.logged_in = true; + } else { + log::error!("Error: cannnot login with type: {}", web::MTX_LOGIN_PASSWORD); + } + } + } + } + if self.logged_in { + self.prompt(ret, t!("mtxcli.logged.in", xous::LANG)); + } else { + self.prompt(ret, t!("mtxcli.login.failed", xous::LANG)); + } + self.logged_in + } + + pub fn logout(&mut self, ret: &mut XousString::<1024>) { + self.unset_debug(TOKEN_KEY); + self.prompt(ret, t!("mtxcli.logged.out", xous::LANG)); + self.logged_in = false; + } + + // assume logged in, token is valid + pub fn get_room_id(&mut self, ret: &mut XousString::<1024>) -> bool { + if self.room_id.len() > 0 { + true + } else { + let room = self.get_default(ROOM_KEY, EMPTY); + let server = self.get_default(SERVER_KEY, EMPTY); + if room.len() == 0 { + self.prompt(ret, t!("mtxcli.please.set.room", xous::LANG)); + false + } else if server.len() == 0 { + self.prompt(ret, t!("mtxcli.please.set.server", xous::LANG)); + false + } else { + let mut room_server = String::new(); + if ! room.starts_with("#") { + room_server.push_str("#"); + } + room_server.push_str(&room); + room_server.push_str(":"); + let i = match server.find(HTTPS) { + Some(index) => { + index + HTTPS.len() + }, + None => { + server.len() + }, + }; + if i >= server.len() { + self.prompt(ret, t!("mtxcli.please.set.server", xous::LANG)); + false + } else { + room_server.push_str(&server[i..]); + if let Some(new_room_id) = web::get_room_id(&self.server, &room_server, &self.token) { + self.set_debug(ROOM_ID_KEY, &new_room_id); + self.room_id = new_room_id; + true + } else { + self.prompt(ret, t!("mtxcli.roomid.failed", xous::LANG)); + false + } + } + } + } + } + + // assume logged in, token is valid, room_id is valid, user is valid + pub fn get_filter(&mut self, ret: &mut XousString::<1024>) -> bool { + if self.filter.len() > 0 { + true + } else { + if let Some(new_filter) = web::get_filter(&self.user, &self.server, &self.token) { + self.set_debug(FILTER_KEY, &new_filter); + self.filter = new_filter; + true + } else { + self.prompt(ret, t!("mtxcli.filter.failed", xous::LANG)); + false + } + } + } + + // assume logged in, token is valid, room_id is valid, user is valid, + // and filter is valid + pub fn read_messages(&mut self, ret: &mut XousString::<1024>) -> bool { + if let Some((since, messages)) = web::client_sync(&self.server, &self.filter, + &self.since, MTX_TIMEOUT, + &self.room_id, &self.token) { + self.set_debug(SINCE_KEY, &since); + self.since = since; + if messages.len() > 0 { + if self.first_line { + write!(ret, "{}", messages).unwrap(); + self.first_line = false; + } else { + write!(ret, "\n{}", messages).unwrap(); + } + true + } else { + false + } + } else { + false + } + } + } /* @@ -113,6 +423,8 @@ impl CommonEnv { ///// 1. add your module here, and pull its namespace into the local crate mod get; use get::*; mod help; use help::*; +mod login; use login::*; +mod logout; use logout::*; mod set; use set::*; mod status; use status::*; mod unset; use unset::*; @@ -123,6 +435,8 @@ pub struct CmdEnv { ///// 2. declare storage for your command here. get_cmd: Get, help_cmd: Help, + login_cmd: Login, + logout_cmd: Logout, set_cmd: Set, status_cmd: Status, unset_cmd: Unset, @@ -140,6 +454,16 @@ impl CmdEnv { cb_registrations: HashMap::new(), trng: Trng::new(&xns).unwrap(), xns: xous_names::XousNames::new().unwrap(), + user: EMPTY.to_string(), + username: EMPTY.to_string(), + + server: SERVER_MATRIX.to_string(), + token: EMPTY.to_string(), + logged_in: false, + room_id: EMPTY.to_string(), + filter: EMPTY.to_string(), + since: EMPTY.to_string(), + first_line: true }; log::info!("done creating CommonEnv"); CmdEnv { @@ -148,6 +472,8 @@ impl CmdEnv { ///// 3. initialize your storage, by calling new() get_cmd: Get::new(&xns), help_cmd: Help::new(&xns), + login_cmd: Login::new(&xns), + logout_cmd: Logout::new(&xns), set_cmd: Set::new(&xns), status_cmd: Status::new(&xns), unset_cmd: Unset::new(&xns), @@ -156,16 +482,28 @@ impl CmdEnv { pub fn dispatch(&mut self, maybe_cmdline: Option<&mut XousString::<1024>>, maybe_callback: Option<&MessageEnvelope>) -> Result>, xous::Error> { let mut ret = XousString::<1024>::new(); + self.common_env.first_line = true; let commands: &mut [& mut dyn ShellCmdApi] = &mut [ ///// 4. add your command to this array, so that it can be looked up and dispatched &mut self.get_cmd, &mut self.help_cmd, + &mut self.login_cmd, + &mut self.logout_cmd, &mut self.set_cmd, &mut self.status_cmd, &mut self.unset_cmd, ]; + if self.common_env.username.len() == 0 { // first time initialization + self.common_env.user = self.common_env.get_default(USER_KEY, EMPTY); + self.common_env.username = self.common_env.get_default(USERNAME_KEY, USERNAME_KEY); + self.common_env.server = self.common_env.get_default(SERVER_KEY, SERVER_MATRIX); + self.common_env.room_id = self.common_env.get_default(ROOM_ID_KEY, EMPTY); + self.common_env.filter = self.common_env.get_default(FILTER_KEY, EMPTY); + self.common_env.since = self.common_env.get_default(SINCE_KEY, EMPTY); + } + if let Some(cmdline) = maybe_cmdline { let maybe_verb = tokenize(cmdline); @@ -205,11 +543,19 @@ impl CmdEnv { cmd_ret } } else { // chat - write!(ret, "SAY: {} {}", verb, cmdline.to_str()).unwrap(); + let mut text = String::from(verb); + text.push_str(" "); + text.push_str(cmdline.to_str()); + self.common_env.user_says(&mut ret, &text); Ok(Some(ret)) } } else { - Ok(None) + log::info!("NO INPUT"); + if self.common_env.read_messages(&mut ret) { + Ok(Some(ret)) + } else { + Ok(None) + } } } else if let Some(callback) = maybe_callback { let mut cmd_ret: Result>, xous::Error> = Ok(None); diff --git a/apps/mtxcli/src/cmds/get.rs b/apps/mtxcli/src/cmds/get.rs index 9094a718f..b2904f23e 100644 --- a/apps/mtxcli/src/cmds/get.rs +++ b/apps/mtxcli/src/cmds/get.rs @@ -34,7 +34,6 @@ impl<'a> ShellCmdApi<'a> for Get { write!(ret, "{}", value).unwrap(); } Err(e) => { - write!(ret, "error getting key {}: {:?}", key, e).unwrap(); log::error!("error getting key {}: {:?}", key, e); } } diff --git a/apps/mtxcli/src/cmds/help.rs b/apps/mtxcli/src/cmds/help.rs index 0e5666918..aa7602646 100644 --- a/apps/mtxcli/src/cmds/help.rs +++ b/apps/mtxcli/src/cmds/help.rs @@ -33,6 +33,12 @@ impl<'a> ShellCmdApi<'a> for Help { "help" => { write!(ret, "{}", t!("mtxcli.help.help", xous::LANG)).unwrap(); } + "login" => { + write!(ret, "{}", t!("mtxcli.login.help", xous::LANG)).unwrap(); + } + "logout" => { + write!(ret, "{}", t!("mtxcli.logout.help", xous::LANG)).unwrap(); + } "set" => { write!(ret, "{}", t!("mtxcli.set.help", xous::LANG)).unwrap(); } @@ -46,6 +52,8 @@ impl<'a> ShellCmdApi<'a> for Help { write!(ret, "{}\n", t!("mtxcli.help.overview", xous::LANG)).unwrap(); write!(ret, "{}\n", t!("mtxcli.get.help", xous::LANG)).unwrap(); write!(ret, "{}\n", t!("mtxcli.help.help", xous::LANG)).unwrap(); + write!(ret, "{}\n", t!("mtxcli.login.help", xous::LANG)).unwrap(); + write!(ret, "{}\n", t!("mtxcli.logout.help", xous::LANG)).unwrap(); write!(ret, "{}\n", t!("mtxcli.set.help", xous::LANG)).unwrap(); write!(ret, "{}\n", t!("mtxcli.status.help", xous::LANG)).unwrap(); write!(ret, "{}", t!("mtxcli.unset.help", xous::LANG)).unwrap(); diff --git a/apps/mtxcli/src/cmds/login.rs b/apps/mtxcli/src/cmds/login.rs new file mode 100644 index 000000000..c0f463cfd --- /dev/null +++ b/apps/mtxcli/src/cmds/login.rs @@ -0,0 +1,22 @@ +use crate::{ShellCmdApi,CommonEnv}; +use xous_ipc::String as XousString; + +#[derive(Debug)] +pub struct Login { +} +impl Login { + pub fn new(_xns: &xous_names::XousNames) -> Self { + Login { + } + } +} + +impl<'a> ShellCmdApi<'a> for Login { + cmd_api!(login); + + fn process(&mut self, _args: XousString::<1024>, env: &mut CommonEnv) -> Result>, xous::Error> { + let mut ret = XousString::<1024>::new(); + env.login(&mut ret); + Ok(Some(ret)) + } +} diff --git a/apps/mtxcli/src/cmds/logout.rs b/apps/mtxcli/src/cmds/logout.rs new file mode 100644 index 000000000..5e490d8b4 --- /dev/null +++ b/apps/mtxcli/src/cmds/logout.rs @@ -0,0 +1,22 @@ +use crate::{ShellCmdApi,CommonEnv}; +use xous_ipc::String as XousString; + +#[derive(Debug)] +pub struct Logout { +} +impl Logout { + pub fn new(_xns: &xous_names::XousNames) -> Self { + Logout { + } + } +} + +impl<'a> ShellCmdApi<'a> for Logout { + cmd_api!(logout); + + fn process(&mut self, _args: XousString::<1024>, env: &mut CommonEnv) -> Result>, xous::Error> { + let mut ret = XousString::<1024>::new(); + env.logout(&mut ret); + Ok(Some(ret)) + } +} diff --git a/apps/mtxcli/src/cmds/set.rs b/apps/mtxcli/src/cmds/set.rs index fefa6cc37..b0c23dda2 100644 --- a/apps/mtxcli/src/cmds/set.rs +++ b/apps/mtxcli/src/cmds/set.rs @@ -34,13 +34,11 @@ impl<'a> ShellCmdApi<'a> for Set { _ => { match env.set(key, value) { Ok(()) => { - write!(ret, "set {} = {}", key, value).unwrap(); + write!(ret, "set {}", key).unwrap(); }, Err(e) => { - write!(ret, "error setting key {}: {:?}", - key, e).unwrap(); - log::error!("error setting key {}: {:?}", - key, e); + // write!(ret, "error setting key {}: {:?}", key, e).unwrap(); + log::error!("error setting key {}: {:?}", key, e); } } } diff --git a/apps/mtxcli/src/cmds/status.rs b/apps/mtxcli/src/cmds/status.rs index 31e4197e5..3ef2f106d 100644 --- a/apps/mtxcli/src/cmds/status.rs +++ b/apps/mtxcli/src/cmds/status.rs @@ -1,7 +1,6 @@ use crate::{ShellCmdApi,CommonEnv}; use xous_ipc::String as XousString; -use core::fmt::Write; -// use locales::t; +use locales::t; #[derive(Debug)] pub struct Status { @@ -16,10 +15,16 @@ impl Status { impl<'a> ShellCmdApi<'a> for Status { cmd_api!(status); - fn process(&mut self, _args: XousString::<1024>, _env: &mut CommonEnv) -> Result>, xous::Error> { + fn process(&mut self, _args: XousString::<1024>, env: &mut CommonEnv) -> Result>, xous::Error> { let mut ret = XousString::<1024>::new(); - write!(ret, "status: not connected").unwrap(); - + let mut text = String::new(); + text.push_str("status: "); + if env.logged_in { + text.push_str(t!("mtxcli.logged.in", xous::LANG)); + } else { + text.push_str(t!("mtxcli.not.connected", xous::LANG)); + } + env.prompt(&mut ret, &text); Ok(Some(ret)) } } diff --git a/apps/mtxcli/src/cmds/unset.rs b/apps/mtxcli/src/cmds/unset.rs index ea9951907..60543e8ba 100644 --- a/apps/mtxcli/src/cmds/unset.rs +++ b/apps/mtxcli/src/cmds/unset.rs @@ -31,8 +31,9 @@ impl<'a> ShellCmdApi<'a> for Unset { write!(ret, "unset {}", key).unwrap(); }, Err(e) => { - write!(ret, "error unsetting key {}: {:?}", - key, e).unwrap(); + // NOTE: we current expect this error + // when unsetting a non existant key + write!(ret, "unset {} (did not exist)", key).unwrap(); log::error!("error unsetting key {}: {:?}", key, e); } } diff --git a/apps/mtxcli/src/cmds/url.rs b/apps/mtxcli/src/cmds/url.rs new file mode 100644 index 000000000..7dc085866 --- /dev/null +++ b/apps/mtxcli/src/cmds/url.rs @@ -0,0 +1,22 @@ +//! URL encoding and decoding + +/// Encoding to use for conversions +pub const RFC3986: &percent_encoding::AsciiSet = &percent_encoding::NON_ALPHANUMERIC + .remove(b'.') + .remove(b'-') + .remove(b'_') + .remove(b'~'); + +/// URL encode string +pub fn encode(v: &str) -> String { + percent_encoding::utf8_percent_encode(v, RFC3986).to_string() +} + +/// URL decode string +#[allow(dead_code)] +pub fn decode(v: &str) -> String { + let decoded = percent_encoding::percent_decode_str(v) + .decode_utf8() + .expect("unable to decode"); + decoded.to_string() +} diff --git a/apps/mtxcli/src/cmds/web.rs b/apps/mtxcli/src/cmds/web.rs new file mode 100644 index 000000000..4527cec8d --- /dev/null +++ b/apps/mtxcli/src/cmds/web.rs @@ -0,0 +1,446 @@ +use serde::{Serialize,Deserialize}; +use ureq::serde_json::{Value, Map}; +use ureq; + +use crate::cmds::url; + +const ACCEPT: &str = "Accept"; +const ACCEPT_JSON: &str = "application/json"; +const AUTHORIZATION: &str = "Authorization"; +const BEARER: &str = "Bearer "; + +pub const MTX_LOGIN_PASSWORD: &str = "m.login.password"; +const MTX_ID_USER: &str = "m.id.user"; + +pub fn get_username(user: &str) -> String { + let i = match user.find('@') { + Some(index) => { index + 1 }, + None => { 0 }, + }; + let j = match user.find(':') { + Some(index) => { index }, + None => { user.len() }, + }; + (&user[i..j]).to_string() +} + +fn serialize(object: &T) -> Option { + match ureq::serde_json::to_string(&object) { + Ok(value) => { + Some(value) + }, + Err(_) => { + // println!("FAILED TO SERIALIZE"); + None + } + } +} + +pub fn handle_response(maybe_response: Result) -> Option { + match maybe_response { + Ok(response) => { + // DEBUG + // if let Ok(json) = response.into_string() { + // println!("JSON = \n{}\n", json); + // None + if let Ok(body) = response.into_json() { + Some(body) + } else { + log::error!("Error: could not convert response into JSON"); + None + } + }, + Err(ureq::Error::Status(code, response)) => { + /* the server returned an unexpected status + code (such as 400, 500 etc) */ + let err_body = response.into_string().unwrap(); + log::error!("ERROR code {} err_body = {}", code, err_body); + // let status_text = "the status text"; + // Err(ureq::Error::Status(code, ureq::Response::new(code, status_text, &body).unwrap())) + None + } + Err(_) => { + log::error!("Unknown Error"); + // Err(e) + None + } + } +} + +pub fn get_json(url: &str) -> Result { + // println!("getting json from {}", &url); + ureq::get(&url) + .set(ACCEPT, ACCEPT_JSON) + .call() +} + +pub fn get_json_auth(url: &str, token:&str) -> Result { + // println!("getting json auth from {}", &url); + let mut authorization = String::from(BEARER); + authorization.push_str(token); + ureq::get(&url) + .set(ACCEPT, ACCEPT_JSON) + .set(AUTHORIZATION, &authorization) + .call() +} + +pub fn post_string(url: &str, request_body: &str) -> Result { + // println!("post json to {}", &url); + ureq::post(&url) + .set(ACCEPT, ACCEPT_JSON) + .send_string(request_body) +} + +pub fn post_string_auth(url: &str, request_body: &str, token: &str) -> Result { + // println!("post json auth from {}", &url); + let mut authorization = String::from(BEARER); + authorization.push_str(token); + ureq::post(&url) + .set(ACCEPT, ACCEPT_JSON) + .set(AUTHORIZATION, &authorization) + .send_string(request_body) +} + +pub fn put_string_auth(url: &str, request_body: &str, token: &str) -> Result { + // println!("put json auth from {}", &url); + let mut authorization = String::from(BEARER); + authorization.push_str(token); + ureq::put(&url) + .set(ACCEPT, ACCEPT_JSON) + .set(AUTHORIZATION, &authorization) + .send_string(request_body) +} + +// -------------------------------- + +pub fn whoami(server: &str, token: &str) -> bool { + let mut url = String::from(server); + url.push_str("/_matrix/client/r0/account/whoami"); + if let Some(value) = handle_response(get_json_auth(&url, token)) { + if let Value::Object(body) = value { + if let Some(Value::String(device_id)) = body.get("device_id") { + log::debug!("device_id = {}", device_id); + } + if let Some(Value::String(user_id)) = body.get("user_id") { + log::debug!("user_id = {}", user_id); + } + } + true + } else { + false + } +} + +pub fn get_login_type(server: &str) -> bool { + let mut url = String::from(server); + url.push_str("/_matrix/client/r0/login"); + let mut found = false; + if let Some(value) = handle_response(get_json(&url)) { + if let Value::Object(body) = value { + if let Some(Value::Array(flows)) = body.get("flows") { + for flow in flows.iter() { + if let Some(Value::String(login_type)) = flow.get("type") { + if login_type.eq(MTX_LOGIN_PASSWORD) { + found = true; + break; + } + } + } + } + } + } + found +} + +#[derive(Serialize, Deserialize)] +struct AuthIdentifier { + #[serde(rename = "type")] + type_: String, + user: String, +} + +#[derive(Serialize, Deserialize)] +struct AuthRequest { + #[serde(rename = "type")] + type_: String, + identifier: AuthIdentifier, + password: String, +} + +impl AuthRequest { + pub fn new(user: &str, password: &str) -> Self { + let identifier = AuthIdentifier { + type_: MTX_ID_USER.to_string(), + user: user.to_string() + }; + AuthRequest { + type_: MTX_LOGIN_PASSWORD.to_string(), + identifier: identifier, + password: password.to_string() + } + } +} + +// fn authenticate_user() -> Result { +pub fn authenticate_user(server: &str, user: &str, password: &str) -> Option { + let mut maybe_token: Option = None; + let mut url = String::from(server); + url.push_str("/_matrix/client/r0/login"); + let auth_request = AuthRequest::new(user, password); + if let Some(request_body) = serialize(&auth_request) { + if let Some(value) = handle_response(post_string(&url, &request_body)) { + if let Value::Object(body) = value { + if let Some(Value::String(access_token)) = body.get("access_token") { + maybe_token = Some(access_token.to_string()) + } + } + } + } + maybe_token +} + +pub fn get_room_id(server: &str, room_server: &str, token: &str) -> Option { + let room_encoded = url::encode(room_server); + let mut url = String::from(server); + url.push_str("/_matrix/client/v3/directory/room/"); + url.push_str(&room_encoded); + log::debug!("get_room_id = {}", url); + if let Some(value) = handle_response(get_json_auth(&url, token)) { + if let Value::Object(body) = value { + if let Some(Value::String(room_id)) = body.get("room_id") { + Some(room_id.to_string()) + } else { + log::error!("invalid response for get_room_id"); + None + } + } else { + log::error!("invalid response for get_room_id"); + None + } + } else { + log::error!("Error for get_room_id"); + None + } +} + +#[derive(Serialize, Deserialize)] +struct EventFilter { + limit: i32 +} + +impl EventFilter { + pub fn new(limit: i32) -> Self { + EventFilter { + limit + } + } +} + +#[derive(Serialize, Deserialize)] +struct RoomEventFilter { + limit: i32, + types: Vec, +} + +impl RoomEventFilter { + pub fn new(limit: i32, type_0: &str) -> Self { + let mut types = Vec::new(); + types.push(type_0.to_string()); + RoomEventFilter { + limit, + types, + } + } +} + +#[derive(Serialize, Deserialize)] +struct RoomFilter { + account_data: EventFilter, // Should be RoomEventFilter + state: EventFilter, // Should be StateFilter + ephemeral: EventFilter, + timeline: RoomEventFilter, +} + +impl RoomFilter { + pub fn new() -> Self { + let account_data = EventFilter::new(0); + let state = EventFilter::new(0); + let ephemeral = EventFilter::new(0); + let timeline = RoomEventFilter::new(10, "m.room.message"); + RoomFilter { + account_data, + state, + ephemeral, + timeline, + } + } +} + +#[derive(Serialize, Deserialize)] +struct FilterRequest { + presence: EventFilter, + account_data: EventFilter, + room: RoomFilter, +} + +impl FilterRequest { + pub fn new() -> Self { + let presence = EventFilter::new(0); + let account_data = EventFilter::new(0); + let room = RoomFilter::new(); + FilterRequest { + presence, + account_data, + room + } + } +} + +pub fn get_filter(user: &str, server: &str, token: &str) -> Option { + let user_encoded = url::encode(user); + let mut url = String::from(server); + url.push_str("/_matrix/client/r0/user/"); + url.push_str(&user_encoded); + url.push_str("/filter"); + log::debug!("get_filter = {}", url); + let filter_request = FilterRequest::new(); + if let Some(request_body) = serialize(&filter_request) { + // println!("filter_request = {}", request_body); + if let Some(value) = handle_response(post_string_auth(&url, &request_body, token)) { + if let Value::Object(body) = value { + if let Some(Value::String(filter_id)) = body.get("filter_id") { + // log::debug!("filter_id = {}", filter_id); + Some(filter_id.to_string()) + } else { + log::error!("invalid response for get_filter"); + None + } + } else { + log::error!("invalid response for get_filter"); + None + } + } else { + log::error!("Error for get_filter"); + None + } + } else { + log::error!("Error unable to serialize request for get_filter"); + None + } +} + +fn get_messages(body: Map, room_id: &str) -> String { + let mut messages = String::new(); + if let Some(Value::Object(rooms)) = body.get("rooms") { + if let Some(Value::Object(join)) = rooms.get("join") { + if let Some(Value::Object(room)) = join.get(room_id) { + if let Some(Value::Object(timeline)) = room.get("timeline") { + if let Some(Value::Array(events)) = timeline.get("events") { + for event in events.iter() { + if let Some(Value::String(type_)) = event.get("type") { + if type_.eq("m.room.message") { + if messages.len() > 0 { + messages.push_str("\n"); + } + if let Some(Value::String(sender)) = event.get("sender") { + messages.push_str(&get_username(sender)); + } else { + messages.push_str("unknown"); + } + messages.push_str("> "); + if let Some(Value::Object(content)) = event.get("content") { + if let Some(Value::String(body)) = content.get("body") { + messages.push_str(body); + + } else { + messages.push_str("...."); + } + } else { + messages.push_str("..."); + } + } // m.room.message + } + } // event + } + } + } + } + } + messages +} + +pub fn client_sync(server: &str, filter: &str, since: &str, timeout: i32, + room_id: &str, token: &str) -> Option<(String, String)> { + let mut url = String::from(server); + url.push_str("/_matrix/client/r0/sync?filter="); + url.push_str(filter); + url.push_str("&timeout="); + url.push_str(&timeout.to_string()); + if since.len() > 0 { + url.push_str("&since="); + url.push_str(since); + } + log::debug!("client_sync = {}", url); + if let Some(value) = handle_response(get_json_auth(&url, token)) { + // println!("SYNC = {:?}", value); + if let Value::Object(body) = value { + if let Some(Value::String(next_batch)) = body.get("next_batch") { + Some((next_batch.to_string(), get_messages(body, room_id))) + } else { + log::error!("invalid response for client_sync"); + None + } + } else { + log::error!("Error for client_sync: deserialization"); + None + } + } else { + None + } +} + +#[derive(Serialize, Deserialize)] +struct MessageRequest { + msgtype: String, + body: String, +} + +impl MessageRequest { + pub fn new(text: &str) -> Self { + let msgtype = "m.text".to_string(); + let body = text.to_string(); + MessageRequest { + msgtype, + body, + } + } +} + +pub fn send_message(server: &str, room_id: &str, text: &str, txn_id: &str, token: &str) -> bool { + let room_id_encoded = url::encode(room_id); + let mut url = String::from(server); + url.push_str("/_matrix/client/r0/rooms/"); + url.push_str(&room_id_encoded); + url.push_str("/send/m.room.message/"); + url.push_str(txn_id); + log::debug!("send_message = {}", url); + let message_request = MessageRequest::new(text); + if let Some(request_body) = serialize(&message_request) { + // println!("request_body = {}", request_body); + if let Some(value) = handle_response(put_string_auth(&url, &request_body, token)) { + if let Value::Object(_body) = value { + // println!("SENT = {:?}", body); + true + } else { + log::error!("invalid response for send_message"); + false + } + } else { + log::error!("Error for send_message"); + false + } + } else { + log::error!("Error unable to serialize request for send_message"); + false + } +}