Skip to content

Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.

5.1 KB
Raw Blame
//! rustcord-uninstall.exe: removes what the installer put in place (the
//! program's files, the shortcuts and the Installed apps entry), closing
//! RustCord first if it is open. Settings and the saved login are left alone
//! unless the user ticks the box (or a script passes `--delete-data`); then
//! every saved account is also logged out at Discord, so no token outlives it.
//!
//!   rustcord-uninstall.exe /S [--delete-data]
//!
//! A program can't delete the folder it runs from, so the copy in the install
//! folder hands over to a copy of itself in %TEMP%, which does the removing.
#![cfg_attr(windows, windows_subsystem = "windows")]

#[cfg(not(windows))]
fn main() {}

#[cfg(windows)]
fn main() {
    use rustcord_setup::*;
    const REMOVE: i32 = 100;
    system_dlls_only();
    let opts = Options::from_args();
    init_com();
    let Ok(me) = std::env::current_exe() else { std::process::exit(1) };

    let Some(dir) = opts.remove.clone() else {
        // First stage, in the install folder.
        let Some(dir) = me.parent().map(|p| p.to_path_buf()) else { std::process::exit(1) };
        let mut delete_data = opts.delete_data;
        if !opts.silent {
            let running = if running_from(&dir).is_empty() { "" } else { "\n\nRustCord is open: it is closed first." };
            let page = Page::new(Icon::App, format!("Remove {APP}?"), format!("This removes {APP} and its shortcuts from this computer.{running}"))
                .button(REMOVE, "Remove")
                .button(CANCEL, "Cancel")
                .checkbox("Also delete my settings, cache and saved login", delete_data);
            let (button, ticked) = show(page);
            if button != REMOVE {
                return;
            }
            delete_data = ticked;
        }
        // Deleting the saved login also ends those sessions at Discord, while
        // RustCord is still here to do it: a token left anywhere stops working.
        if delete_data {
            log_out_everywhere(&dir);
        }
        let temp = std::env::temp_dir().join(format!("rustcord-uninstall-{}.exe", std::process::id()));
        if let Err(e) = std::fs::copy(&me, &temp) {
            if !opts.silent {
                error(&format!("{APP} wasn't removed"), &format!("Couldn't start removing it: {e}"));
            }
            std::process::exit(1);
        }
        let mut cmd = std::process::Command::new(&temp);
        // (Not started in the install folder, which it could then not remove.)
        cmd.arg("--remove").arg(&dir).current_dir(std::env::temp_dir());
        if opts.silent {
            cmd.arg("/S");
        }
        if delete_data {
            cmd.arg("--delete-data");
        }
        // This copy has to exit before its file can go, so it can't wait
        // for the result: the removal finishes a moment after it returns.
        std::process::exit(if cmd.spawn().is_ok() { 0 } else { 1 });
    };

    // Second stage, from %TEMP%: only ever touch a folder that is a RustCord
    // install (it holds this uninstaller), and in it only the files the
    // installer wrote: anything else that was put there stays.
    if !dir.join(UNINSTALLER).is_file() {
        std::process::exit(1);
    }
    close_running(&dir);
    let files: Vec<_> = [EXE, UNINSTALLER].iter().flat_map(|name| ["exe", "old", "new"].map(|ext| dir.join(name).with_extension(ext))).collect();
    let mut removed = false;
    for attempt in 0..40 {
        // (The first stage needs a moment to exit and let go of its file.)
        if files.iter().filter(|f| f.exists()).all(|f| std::fs::remove_file(f).is_ok()) {
            removed = true;
            break;
        }
        if attempt < 20 || opts.silent {
            std::thread::sleep(std::time::Duration::from_millis(250));
        } else if !retry(&format!("{APP} is still running"), "Close it (also from the tray icon), then choose Retry.") {
            break;
        }
    }
    if removed {
        // (Fails, as it should, when the folder holds something else.)
        let _ = std::fs::remove_dir(&dir);
        // The shortcuts and the Installed apps entry belong to the install
        // they point at, which may be another one than this.
        if registered_dir().is_none_or(|registered| same_path(&registered, &dir)) {
            for link in [start_menu_link(), desktop_link()].into_iter().flatten() {
                let _ = std::fs::remove_file(link);
            }
            unregister_uninstaller();
        }
        if opts.delete_data {
            delete_user_data();
        }
        if !opts.silent {
            let kept = if opts.delete_data { "Your settings, cache and saved login were deleted too." } else { "Your settings and login are kept, in case you install it again." };
            let folder = if dir.exists() { format!("\n\nThe folder {} holds other files and was left in place.", dir.display()) } else { String::new() };
            info(&format!("{APP} was removed"), &format!("{kept}{folder}"));
        }
    } else if !opts.silent {
        error(&format!("{APP} wasn't removed"), &format!("Files in {} are still in use.", dir.display()));
    }
    delete_after_exit(&me);
    std::process::exit(if removed { 0 } else { 1 });
}