initial import

This commit is contained in:
lif 2023-11-12 02:15:57 -08:00
commit 473b98b0f3
5 changed files with 2919 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
/.idea

2744
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

15
Cargo.toml Normal file
View file

@ -0,0 +1,15 @@
[package]
name = "tubest"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
futures = { version = "0.3" }
libmpv = { git = "https://github.com/ParadoxSpiral/libmpv-rs", rev = "3e6c389b716f52a595cc5e8e3fa1f96cb76b3de7" }
matrix-sdk = { version = "0.6" }
tokio = { version = "1", features = ["full"] }
tokio-stream = { version = "0.1", features = ["full"] }
serde = { version = "1" }
serde_json = { version = "1" }

32
src/bin/main.rs Normal file
View file

@ -0,0 +1,32 @@
use std::process::Stdio;
use futures::StreamExt;
use tokio::io::{AsyncRead, AsyncBufReadExt, BufReader};
use tokio::process::Command;
use libmpv::{FileState, Mpv};
use tokio_stream::wrappers::LinesStream;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let args: Vec<String> = std::env::args().skip(1).collect();
let mut subprocs: Vec<_> = args
.iter()
.map(|arg| Command::new(arg)
.stdout(Stdio::piped())
.spawn()
.unwrap_or_else(|e| panic!("couldn't spawn {arg}: {e:?}")))
.collect();
let stdin_box: Box<dyn AsyncRead + Unpin> = Box::new(tokio::io::stdin());
let mut combined = futures::stream::select_all(Some(LinesStream::new(BufReader::new(stdin_box).lines())));
for subproc in &mut subprocs {
let stdout_box: Box<dyn AsyncRead + Unpin> = Box::new(subproc.stdout.take().unwrap());
combined.push(LinesStream::new(BufReader::new(stdout_box).lines()));
}
let mpv = Mpv::new().expect("couldn't create mpv");
while let Some(Ok(line)) = combined.next().await {
mpv.playlist_load_files(&[(&line, FileState::AppendPlay, None)]).expect("couldn't load path");
}
Ok(())
}

126
src/bin/matrix.rs Normal file
View file

@ -0,0 +1,126 @@
use std::io::Write;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use matrix_sdk::{Client, config::SyncSettings, ruma::{
OwnedRoomId, OwnedUserId,
events::room::message::SyncRoomMessageEvent
}, Session};
use matrix_sdk::ruma::api::client::filter::{FilterDefinition, RoomFilter};
use matrix_sdk::ruma::api::client::sync::sync_events::v3::Filter;
use matrix_sdk::ruma::api::client::uiaa;
#[derive(Serialize, Deserialize)]
struct Config {
user: OwnedUserId,
room: OwnedRoomId,
password: Option<String>,
session: Option<Session>
}
impl Config {
fn path() -> PathBuf {
#[allow(deprecated)]
let home = std::env::home_dir().expect("no coats and no home");
home.join(".config").join("tubest").join("matrix.json")
}
fn read() -> Self {
let path = Self::path();
let f = std::fs::File::open(&path)
.unwrap_or_else(|e| panic!("couldn't read {path:?}: {e:?}"));
serde_json::from_reader(f)
.unwrap_or_else(|e| panic!("couldn't load config: {e:?}"))
}
fn write(&self) {
let json = serde_json::to_string_pretty(self).unwrap();
let path = Self::path();
std::fs::File::create(&path)
.unwrap_or_else(|e| panic!("couldn't create {path:?}: {e:?}"))
.write_all(json.as_bytes())
.unwrap_or_else(|e| panic!("couldn't write config: {e:?}"));
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let mut cfg = Config::read();
let client = Client::builder()
.server_name(cfg.user.server_name())
.handle_refresh_tokens()
.build()
.await?;
let mut logged_in = false;
/*
if let Some(session) = cfg.session.clone() {
//logged_in = client.restore_login(session).await.is_ok();
client
.login_username(&cfg.user, cfg.password.as_ref().unwrap())
.device_id(session.device_id.as_str())
.initial_device_display_name("tubest")
.request_refresh_token()
.send()
.await?;
logged_in = true;
}
*/
if !logged_in {
if let Some(ref password) = cfg.password {
client
.login_username(&cfg.user, password)
.initial_device_display_name("tubest")
.request_refresh_token()
.send()
.await?;
logged_in = true;
let devices_to_delete: Vec<_> = client.devices()
.await
.unwrap()
.devices
.into_iter()
.filter(|d| d.display_name.clone().unwrap_or_default().as_str() == "tubest" && d.device_id != client.device_id().unwrap())
.map(|d| d.device_id)
.collect();
if let Err(e) = client.delete_devices(&devices_to_delete, None).await {
if let Some(info) = e.uiaa_response() {
let mut password = uiaa::Password::new(uiaa::UserIdentifier::UserIdOrLocalpart(cfg.user.localpart()), password);
password.session = info.session.as_deref();
client.delete_devices(&devices_to_delete, Some(uiaa::AuthData::Password(password)))
.await?;
}
}
}
}
assert!(logged_in);
if let Some(session) = client.session() {
cfg.session = Some(session);
cfg.write();
}
client.add_room_event_handler(cfg.room.as_ref(), |ev: SyncRoomMessageEvent| async {
match ev {
SyncRoomMessageEvent::Original(orig) => {
let body = orig.content.body();
// TODO: my own IPC wrapper
println!("{}", body);
eprintln!("{}", body);
}
SyncRoomMessageEvent::Redacted(_) => {}
}
});
// Syncing is important to synchronize the client state with the server.
// This method will never return.
let mut filter_def = FilterDefinition::empty();
let mut room_filter = RoomFilter::empty();
let room_list = [cfg.room];
room_filter.rooms = Some(&room_list);
filter_def.room = room_filter;
client
.sync(SyncSettings::default()
.filter(Filter::FilterDefinition(filter_def)))
.await?;
Ok(())
}