Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.
Rust desktop-appdiscorddiscord-clienteguielectron-alternativelightweightlinuxmacosnative-apprustwindows
//! "Leaving RustCord": before a link to a site that isn't Discord's opens in
//! the browser, a dialog shows where it really goes (a link's text can say
//! anything), as Discord's own "Leaving Discord" does. Sites you trust skip
//! it from then on.
use crate::app::RustCord;
use eframe::egui::{self, vec2, Align2, Color32, FontFamily, FontId, Key, Order, RichText, Sense, Stroke};
/// Discord's own sites: their links open straight away.
const DISCORD_HOSTS: [&str; 10] = ["discord.com", "discord.gg", "discord.gift", "discord.new", "discord.media", "discord.co", "discordapp.com", "discordapp.net", "discordstatus.com", "dis.gd"];
/// The host of a web link, lowercase.
pub fn host(url: &str) -> Option<String> {
url::Url::parse(url).ok()?.host_str().map(|h| h.trim_end_matches('.').to_ascii_lowercase())
}
/// `host` is `domain` or one of its subdomains.
fn under(host: &str, domain: &str) -> bool {
host == domain || host.ends_with(&format!(".{domain}"))
}
/// The open link waiting for the person's answer, and the "trust" tick.
pub struct Leaving {
url: String,
host: String,
trust: bool,
}
impl RustCord {
/// May `url` open without asking? Discord's sites and trusted ones may.
pub fn link_trusted(&self, url: &str) -> bool {
let Some(h) = host(url) else { return false };
DISCORD_HOSTS.iter().any(|d| under(&h, d)) || self.prefs.trusted_domains.iter().any(|d| under(&h, d))
}
/// A link that would leave RustCord: hold it and ask first.
pub fn ask_before_leaving(&mut self, url: String) {
let host = host(&url).unwrap_or_default();
self.leaving = Some(Leaving { url, host, trust: false });
}
pub fn leaving_ui(&mut self, ctx: &egui::Context) {
let Some(l) = self.leaving.as_mut() else { return };
let t = self.theme.clone();
let screen = ctx.screen_rect();
let (mut go, mut back) = (false, ctx.input(|i| i.key_pressed(Key::Escape)));
egui::Area::new(egui::Id::new("leaving_dim")).order(Order::Foreground).fixed_pos(screen.min).show(ctx, |ui| {
let (_, r) = ui.allocate_exact_size(screen.size(), Sense::click());
ui.painter().rect_filled(screen, 0.0, Color32::from_black_alpha(170));
back |= r.clicked();
});
let bold = |s: f32| FontId::new(s, FontFamily::Name(crate::style::BOLD.into()));
egui::Area::new(egui::Id::new("leaving")).order(Order::Tooltip).anchor(Align2::CENTER_CENTER, vec2(0.0, 0.0)).show(ctx, |ui| {
egui::Frame::none().fill(t.surface_high).rounding(12.0).stroke(Stroke::new(1.0_f32, super::modal_border(&t))).inner_margin(24.0).show(ui, |ui| {
ui.set_width(392.0);
ui.label(RichText::new("Leaving RustCord").font(bold(20.0)).color(t.header_primary));
ui.add_space(8.0);
ui.label(RichText::new("This link is taking you to the following website").size(14.0).color(t.text_normal));
ui.add_space(8.0);
egui::Frame::none().fill(Color32::from_black_alpha(46)).rounding(6.0).inner_margin(10.0).show(ui, |ui| {
ui.set_width(372.0);
ui.add(egui::Label::new(RichText::new(&l.url).font(crate::style::medium(14.0)).color(t.header_primary)).wrap());
});
ui.add_space(12.0);
if !l.host.is_empty() {
// A box with its label, the whole row clickable.
let label = ui.painter().layout_no_wrap(format!("Trust {} links from now on", l.host), FontId::proportional(14.0), t.text_normal);
let (row, resp) = ui.allocate_exact_size(vec2(24.0 + 8.0 + label.size().x, 24.0), Sense::click());
let resp = resp.on_hover_cursor(egui::CursorIcon::PointingHand);
let b = egui::Rect::from_min_size(row.min + vec2(2.0, 2.0), vec2(20.0, 20.0));
if l.trust {
ui.painter().rect_filled(b, 4.0, Color32::from_rgb(0x58, 0x65, 0xf2));
let p = |x: f32, y: f32| b.min + vec2(x, y);
ui.painter().line_segment([p(5.0, 10.5), p(8.5, 14.0)], Stroke::new(2.0_f32, Color32::WHITE));
ui.painter().line_segment([p(8.5, 14.0), p(15.0, 6.5)], Stroke::new(2.0_f32, Color32::WHITE));
} else {
ui.painter().rect_stroke(b, 4.0, Stroke::new(1.5_f32, t.interactive_normal));
}
ui.painter().galley(egui::pos2(b.right() + 10.0, row.center().y - label.size().y / 2.0), label, t.text_normal);
if resp.on_hover_cursor(egui::CursorIcon::PointingHand).clicked() {
l.trust = !l.trust;
}
ui.add_space(16.0);
}
ui.horizontal(|ui| {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
let visit = egui::Button::new(RichText::new("Visit Site").font(crate::style::medium(14.0)).color(Color32::WHITE)).fill(Color32::from_rgb(0x58, 0x65, 0xf2)).rounding(8.0).min_size(vec2(96.0, 36.0));
go = ui.add(visit).on_hover_cursor(egui::CursorIcon::PointingHand).clicked();
let cancel = egui::Button::new(RichText::new("Go Back").font(crate::style::medium(14.0)).color(t.header_primary)).fill(crate::style::overlay(0.12)).rounding(8.0).min_size(vec2(84.0, 36.0));
back |= ui.add(cancel).on_hover_cursor(egui::CursorIcon::PointingHand).clicked();
});
});
});
});
if go {
let Some(l) = self.leaving.take() else { return };
if l.trust && !l.host.is_empty() && !self.prefs.trusted_domains.contains(&l.host) {
self.prefs.trusted_domains.push(l.host.clone());
self.save_prefs();
}
self.allowed_link = Some(l.url.clone());
ctx.open_url(egui::OpenUrl::new_tab(l.url));
} else if back {
self.leaving = None;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hosts_and_subdomains() {
assert_eq!(host("https://Support.Discord.com/hc").as_deref(), Some("support.discord.com"));
assert!(under("support.discord.com", "discord.com"));
assert!(!under("discord.com.evil.example", "discord.com"));
assert!(!under("notdiscord.com", "discord.com"));
assert_eq!(host("not a url"), None);
}
}