diff --git a/.gitignore b/.gitignore index 4ce1907..275d4c6 100644 --- a/.gitignore +++ b/.gitignore @@ -14,21 +14,6 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb -# ---> ArchLinuxPackages -*.tar -*.tar.* -*.jar -*.exe -*.msi -*.zip -*.tgz -*.log -*.log.* -*.sig - -pkg/ -src/ - # ---> VisualStudioCode .vscode/* !.vscode/settings.json @@ -70,9 +55,3 @@ Icon Network Trash Folder Temporary Items .apdisk - - - -# Added by cargo - -/target diff --git a/Cargo.toml b/Cargo.toml index 3e378ac..9497182 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,14 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = "4.1.1" matrix-sdk = "0.6.2" matrix-sdk-base = "0.6.1" matrix-sdk-crypto = "0.6.0" +anyhow = "1" +dirs = "4.0.0" +tracing-subscriber = "0.3.15" + +[dependencies.tokio] +version = "1.24.2" +features = ["full"] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1e0cad6 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,73 @@ +use clap::{arg, Command}; + +use matrix_sdk::{ + config::SyncSettings, + room::Room, + ruma::events::room::{ + member::StrippedRoomMemberEvent, + message::{MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent}, + }, + Client, +}; +use tokio::time::{sleep, Duration}; + +fn cli() -> Command { + Command::new("gaychat") + .about("a matrix client for the gays") + .allow_external_subcommands(false) + .arg(arg!( "The homeserver your account uses.")) + .arg(arg!( "Your account's username.")).arg_required_else_help(true) + .arg(arg!( "Your account's password.")).arg_required_else_help(true) +} + +#[tokio::main] +async fn main() -> Result<(), ()> { + tracing_subscriber::fmt::init(); + + let args = cli().get_matches(); + + let homeserver: &String = args.get_one("HOMESERVER").unwrap(); + let username: &String = args.get_one("USERNAME").unwrap(); + let password: &String = args.get_one("PASSWORD").unwrap(); + + login_and_sync(homeserver, username, password).await.unwrap(); + + Ok(()) +} + +async fn login_and_sync( + homeserver: &String, + username: &String, + password: &String +) -> anyhow::Result<()> { + + let home = dirs::data_dir().expect("no home dir found").join("gaychat"); + + let client = Client::builder() + .homeserver_url(homeserver) + .sled_store(home, None)? + .build() + .await?; + + client.login_username(username, password) + .initial_device_display_name("testing"); + + println!("Logged in as {username} at {homeserver}"); + + + let sync_token = client.sync_once(SyncSettings::default()).await.unwrap().next_batch; + + client.add_event_handler(on_room_message); + + let settings = SyncSettings::default().token(sync_token); + client.sync(settings).await?; + + Ok(()) +} + +async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) { + let Room::Joined(room) = room else {return}; + let MessageType::Text(text_content) = event.content.msgtype else {return}; + + println!("{:?}", text_content); +}