init mtxchat::MtxChat from mtxcli::Mtxcli

This commit is contained in:
nworbnhoj 2023-07-29 16:16:29 +10:00
parent 081c669b43
commit 2a1be321e9
5 changed files with 833 additions and 0 deletions

View file

@ -30,6 +30,7 @@ net = { path = "../../services/net" }
pddb = { path = "../../services/pddb" }
status = { path = "../../services/status" }
# new dependencies for mtxcli
chat = { path = "../../libs/chat" }
percent-encoding = "2.2"
serde = { version = "1.0", features = [ "derive" ] }
ureq = { version = "2.5", features = ["json"] }

344
apps/mtxchat/src/lib.rs Normal file
View file

@ -0,0 +1,344 @@
pub mod api;
mod url;
mod web;
pub use api::*;
use locales::t;
use modals::Modals;
use std::fs::File;
use std::io::{Error, ErrorKind, Read, Write as StdWrite};
use std::path::PathBuf;
/// PDDB Dict for mtxchat keys
const MTXCHAT_DICT: &str = "mtxchat";
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 = "";
pub const CLOCK_NOT_SET_ID: usize = 1;
pub const PDDB_NOT_MOUNTED_ID: usize = 2;
pub const WIFI_NOT_CONNECTED_ID: usize = 3;
pub const MTXCLI_INITIALIZED_ID: usize = 4;
pub const WIFI_CONNECTED_ID: usize = 5;
pub const SET_USER_ID: usize = 6;
pub const SET_PASSWORD_ID: usize = 7;
pub const LOGGED_IN_ID: usize = 8;
pub const LOGIN_FAILED_ID: usize = 9;
pub const SET_ROOM_ID: usize = 10;
pub const ROOMID_FAILED_ID: usize = 11;
pub const FILTER_FAILED_ID: usize = 12;
pub const SET_SERVER_ID: usize = 13;
pub const LOGGING_IN_ID: usize = 14;
pub const LOGGED_OUT_ID: usize = 15;
pub const NOT_CONNECTED_ID: usize = 16;
pub const FAILED_TO_SEND_ID: usize = 17;
pub const PLEASE_LOGIN_ID: usize = 18;
#[cfg(not(target_os = "xous"))]
pub const HOSTED_MODE: bool = true;
#[cfg(target_os = "xous")]
pub const HOSTED_MODE: bool = false;
//#[derive(Debug)]
pub struct MtxChat {
user: String,
username: String,
server: String,
token: String,
logged_in: bool,
room_id: String,
filter: String,
since: String,
}
impl MtxChat {
pub fn new() -> MtxChat {
let xns = xous_names::XousNames::new().unwrap();
let common = MtxChat {
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(),
};
common
}
pub fn set(&mut self, key: &str, value: &str) -> Result<(), Error> {
if key.starts_with("__") {
Err(Error::new(
ErrorKind::PermissionDenied,
"may not set a variable beginning with __ ",
))
} else {
log::info!("set '{}' = '{}'", key, value);
let mut keypath = PathBuf::new();
keypath.push(MTXCHAT_DICT);
if std::fs::metadata(&keypath).is_ok() { // keypath exists
// log::info!("dict '{}' exists", MTXCHAT_DICT);
} else {
log::info!("dict '{}' does NOT exist.. creating it", MTXCHAT_DICT);
std::fs::create_dir_all(&keypath)?;
}
keypath.push(key);
File::create(keypath)?.write_all(value.as_bytes())?;
match key {
// special case side effects
FILTER_KEY => {
self.filter = value.to_string();
}
PASSWORD_KEY => {
self.set_password();
}
ROOM_ID_KEY => {
self.room_id = value.to_string();
}
ROOM_KEY => {
self.set_room();
}
SERVER_KEY => {
self.server = value.to_string();
}
SINCE_KEY => {
self.since = value.to_string();
}
USERNAME_KEY => {
self.username = value.to_string();
}
USER_KEY => {
self.set_user(value);
}
_ => {}
}
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::info!("error setting key {}: {:?}", key, e);
false
}
}
}
pub fn set_user(&mut self, value: &str) {
log::info!("# 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::info!(
"# 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);
}
pub fn set_password(&mut self) {
log::info!(
"# PASSWORD_KEY set '{}' => clearing TOKEN_KEY",
PASSWORD_KEY
);
self.unset_debug(TOKEN_KEY);
}
pub fn set_room(&mut self) {
log::info!(
"# 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> {
if key.starts_with("__") {
Err(Error::new(
ErrorKind::PermissionDenied,
"may not unset a variable beginning with __ ",
))
} else {
log::info!("unset '{}'", key);
let mut keypath = PathBuf::new();
keypath.push(MTXCHAT_DICT);
if std::fs::metadata(&keypath).is_ok() { // keypath exists
// log::info!("dict '{}' exists", MTXCHAT_DICT);
} else {
log::info!("dict '{}' does NOT exist.. creating it", MTXCHAT_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", MTXCHAT_DICT, key);
std::fs::remove_file(keypath)?;
}
match key {
// special case side effects -- update cached values
FILTER_KEY => {
self.filter = EMPTY.to_string();
}
ROOM_ID_KEY => {
self.room_id = EMPTY.to_string();
}
SINCE_KEY => {
self.since = EMPTY.to_string();
}
SERVER_KEY => {
self.server = EMPTY.to_string();
}
USER_KEY => {
self.user = EMPTY.to_string();
}
USERNAME_KEY => {
self.username = EMPTY.to_string();
}
_ => {}
}
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::info!("error unsetting key {}: {:?}", key, e);
false
}
}
}
pub fn get(&mut self, key: &str) -> Result<Option<String>, Error> {
// if key.eq(CURRENT_VERSION_KEY) {
// Ok(Some(self.version.clone()))
// } else {
let mut keypath = PathBuf::new();
keypath.push(MTXCHAT_DICT);
if std::fs::metadata(&keypath).is_ok() { // keypath exists
// log::info!("dict '{}' exists", MTXCHAT_DICT);
} else {
log::info!("dict '{}' does NOT exist.. creating it", MTXCHAT_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);
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::info!("error getting key {}: {:?}", key, e);
default.to_string()
}
}
}
pub fn login(&mut self) -> bool {
// self.scalar_async_msg(LOGGING_IN_ID);
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);
if user.len() == 0 {
// self.scalar_async_msg(SET_USER_ID);
self.modals
.show_notification(t!("mtxcli.please.set.user", locales::LANG), None)
.expect("notification failed");
}
let password = self.get_default(PASSWORD_KEY, EMPTY);
if password.len() == 0 {
// self.scalar_async_msg(SET_PASSWORD_ID);
} else {
if let Some(new_token) = web::authenticate_user(&self.server, &user, &password)
{
self.set_debug(TOKEN_KEY, &new_token);
self.logged_in = true;
} else {
log::info!(
"Error: cannnot login with type: {}",
web::MTX_LOGIN_PASSWORD
);
}
}
}
}
if self.logged_in {
// self.scalar_async_msg(LOGGED_IN_ID);
log::info!("logged_in");
} else {
// self.scalar_async_msg(LOGIN_FAILED_ID);
log::info!("login failed");
}
self.logged_in
}
}
pub(crate) fn heap_usage() -> usize {
match xous::rsyscall(xous::SysCall::IncreaseHeap(0, xous::MemoryFlags::R))
.expect("couldn't get heap size")
{
xous::Result::MemoryRange(m) => {
let usage = m.len();
usage
}
_ => {
log::info!("Couldn't measure heap usage");
0
}
}
}

View file

@ -6,6 +6,9 @@ use api::*;
use num_traits::*;
use xous_ipc::Buffer;
use chat::{Chat, ChatOp, Event};
use mtxchat::MtxChat;
fn main () -> ! {
let stack_size = 1024 * 1024;
std::thread::Builder::new()
@ -47,6 +50,15 @@ fn wrapped_main() -> ! {
.expect("can't register server");
// log::trace!("registered with NS -- {:?}", sid);
let chat = Chat::new(
gam::APP_NAME_MTXCHAT,
Some(xous::connect(sid).unwrap()),
Some(MtxchatOp::Post as usize),
Some(MtxchatOp::Event as usize),
Some(MtxchatOp::Rawkeys as usize),
);
let mut mtxchat = MtxChat::new();
loop {
let msg = xous::receive_message(sid).unwrap();
log::debug!("got message {:?}", msg);

22
apps/mtxchat/src/url.rs Normal file
View file

@ -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()
}

454
apps/mtxchat/src/web.rs Normal file
View file

@ -0,0 +1,454 @@
use serde::{Deserialize, Serialize};
use ureq;
use ureq::serde_json::{Map, Value};
use crate::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<T: ?Sized + Serialize>(object: &T) -> Option<String> {
match ureq::serde_json::to_string(&object) {
Ok(value) => Some(value),
Err(e) => {
log::info!("ERROR in serialize: {:?}", e);
None
}
}
}
pub fn handle_response(maybe_response: Result<ureq::Response, ureq::Error>) -> Option<Value> {
match maybe_response {
Ok(response) => {
if let Ok(body) = response.into_json() {
Some(body)
} else {
log::info!("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::info!("ERROR code {} err_body = {}", code, err_body);
None
}
Err(e) => {
log::info!("ERROR in handle_response: {:?}", e);
None
}
}
}
pub fn get_json(url: &str) -> Result<ureq::Response, ureq::Error> {
ureq::get(&url).set(ACCEPT, ACCEPT_JSON).call()
}
pub fn get_json_auth(url: &str, token: &str) -> Result<ureq::Response, ureq::Error> {
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<ureq::Response, ureq::Error> {
ureq::post(&url)
.set(ACCEPT, ACCEPT_JSON)
.send_string(request_body)
}
pub fn post_string_auth(
url: &str,
request_body: &str,
token: &str,
) -> Result<ureq::Response, ureq::Error> {
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<ureq::Response, ureq::Error> {
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::info!("device_id = {}", device_id);
}
if let Some(Value::String(user_id)) = body.get("user_id") {
log::info!("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<String, ureq::Error> {
pub fn authenticate_user(server: &str, user: &str, password: &str) -> Option<String> {
let mut maybe_token: Option<String> = 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<String> {
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::info!("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::info!("invalid response for get_room_id");
None
}
} else {
log::info!("invalid response for get_room_id");
None
}
} else {
log::info!("Error for get_room_id");
None
}
}
#[derive(Serialize, Deserialize)]
struct EventFilter {
limit: i32,
not_types: Vec<String>,
}
impl EventFilter {
pub fn new(limit: i32) -> Self {
let mut not_types: Vec<String> = Vec::new();
not_types.push("*".to_string());
EventFilter { limit, not_types }
}
}
#[derive(Serialize, Deserialize)]
struct RoomEventFilter {
limit: i32,
types: Vec<String>,
rooms: Vec<String>,
}
impl RoomEventFilter {
pub fn new(limit: i32, room_id: &str, type_0: &str) -> Self {
let mut types = Vec::new();
types.push(type_0.to_string());
let mut rooms: Vec<String> = Vec::new();
rooms.push(room_id.to_string());
RoomEventFilter {
limit,
types,
rooms,
}
}
}
#[derive(Serialize, Deserialize)]
struct RoomFilter {
account_data: EventFilter, // Should be RoomEventFilter
ephemeral: EventFilter,
rooms: Vec<String>,
state: EventFilter, // Should be StateFilter
timeline: RoomEventFilter,
}
impl RoomFilter {
pub fn new(room_id: &str) -> Self {
let account_data = EventFilter::new(0);
let ephemeral = EventFilter::new(0);
let mut rooms: Vec<String> = Vec::new();
rooms.push(room_id.to_string());
let state = EventFilter::new(0);
let timeline = RoomEventFilter::new(10, room_id, "m.room.message");
RoomFilter {
account_data,
ephemeral,
rooms,
state,
timeline,
}
}
}
#[derive(Serialize, Deserialize)]
struct FilterRequest {
account_data: EventFilter,
event_fields: Vec<String>,
presence: EventFilter,
room: RoomFilter,
}
impl FilterRequest {
pub fn new(room_id: &str) -> Self {
let account_data = EventFilter::new(0);
let mut event_fields: Vec<String> = Vec::new();
event_fields.push("type".to_string());
event_fields.push("sender".to_string());
event_fields.push("content.body".to_string());
let presence = EventFilter::new(0);
let room = RoomFilter::new(room_id);
FilterRequest {
account_data,
event_fields,
presence,
room,
}
}
}
pub fn get_filter(user: &str, server: &str, room_id: &str, token: &str) -> Option<String> {
let user_encoded = url::encode(user);
let mut url = String::from(server);
url.push_str("/_matrix/client/v3/user/");
url.push_str(&user_encoded);
url.push_str("/filter");
log::info!("get_filter = {}", url);
let filter_request = FilterRequest::new(room_id);
if let Some(request_body) = serialize(&filter_request) {
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::info!("filter_id = {}", filter_id);
Some(filter_id.to_string())
} else {
log::info!("invalid response for get_filter");
None
}
} else {
log::info!("invalid response for get_filter");
None
}
} else {
log::info!("Error for get_filter");
None
}
} else {
log::info!("Error unable to serialize request for get_filter");
None
}
}
fn get_messages(body: Map<String, Value>, room_id: &str) -> String {
log::info!("heap usage: {}", crate::heap_usage());
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)> {
log::info!("heap usage: {}", crate::heap_usage());
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::info!("client_sync = {}", url);
if let Some(value) = handle_response(get_json_auth(&url, token)) {
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::info!("invalid response for client_sync");
None
}
} else {
log::info!("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 {
log::info!("heap usage: {}", crate::heap_usage());
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::info!("send_message = {}", url);
let message_request = MessageRequest::new(text);
if let Some(request_body) = serialize(&message_request) {
if let Some(value) = handle_response(put_string_auth(&url, &request_body, token)) {
if let Value::Object(_body) = value {
true
} else {
log::info!("invalid response for send_message");
false
}
} else {
log::info!("Error for send_message");
false
}
} else {
log::info!("Error unable to serialize request for send_message");
false
}
}