mod quine;
#[cfg(target_os = "macos")]
mod updater;
use serde::{Deserialize, Serialize};
use std::fs;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::Arc;
use tauri::webview::NewWindowResponse;
use tauri::{Emitter, Manager, Url, WebviewUrl, WebviewWindowBuilder};
#[derive(Serialize, Deserialize, Clone)]
struct AppConfig {
#[serde(default = "default_server_url")]
server_url: String,
#[serde(default = "default_jitsi_base")]
jitsi_base: String,
#[serde(default = "default_identity_url")]
identity_url: String,
}
fn default_server_url() -> String {
"https://meet.quineglobal.com".to_string()
}
fn default_jitsi_base() -> String {
String::new()
}
fn default_identity_url() -> String {
"https://login.quineglobal.com/realms/public".to_string()
}
const RETIRED_IDENTITY_URL: &str = "https://identity.quineglobal.com";
impl Default for AppConfig {
fn default() -> Self {
Self {
server_url: default_server_url(),
jitsi_base: default_jitsi_base(),
identity_url: default_identity_url(),
}
}
}
fn config_path(app: &tauri::AppHandle) -> Result<std::path::PathBuf, String> {
let dir = app
.path()
.app_data_dir()
.map_err(|e| format!("could not resolve app data dir: {e}"))?;
fs::create_dir_all(&dir).map_err(|e| format!("could not create app data dir: {e}"))?;
Ok(dir.join("config.json"))
}
#[tauri::command]
fn load_config(app: tauri::AppHandle) -> Result<AppConfig, String> {
let path = config_path(&app)?;
match fs::read_to_string(&path) {
Ok(raw) => {
let mut config: AppConfig =
serde_json::from_str(&raw).map_err(|e| format!("corrupt config: {e}"))?;
if config.identity_url.trim_end_matches('/') == RETIRED_IDENTITY_URL {
config.identity_url = default_identity_url();
}
Ok(config)
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(AppConfig::default()),
Err(e) => Err(format!("could not read config: {e}")),
}
}
#[tauri::command]
fn save_config(app: tauri::AppHandle, config: AppConfig) -> Result<(), String> {
let path = config_path(&app)?;
let raw = serde_json::to_string_pretty(&config).map_err(|e| e.to_string())?;
fs::write(&path, raw).map_err(|e| format!("could not write config: {e}"))
}
#[cfg(target_os = "macos")]
const SAFARI_USER_AGENT: &str = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) \
AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Safari/605.1.15";
const JITSI_PUBLIC_HOST: &str = "meet.jit.si";
const JITSI_SIGNIN_URL: &str =
"https://meet.jit.si/v1/_cdn/auth-static/meet-jit-si/v1/signin.html";
const SIGNIN_DONE_ROOM: &str = "quple-signed-in";
const SIGNIN_LABEL: &str = "jitsi-signin";
const POPUP_LABEL_PREFIX: &str = "popup-";
static POPUP_COUNTER: AtomicU32 = AtomicU32::new(0);
fn percent_encode(s: &str) -> String {
s.bytes()
.map(|b| match b {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {
(b as char).to_string()
}
_ => format!("%{b:02X}"),
})
.collect()
}
fn signed_in_marker(app: &tauri::AppHandle) -> Result<std::path::PathBuf, String> {
Ok(config_path(app)?.with_file_name("jitsi-signed-in"))
}
fn is_signed_in(app: &tauri::AppHandle) -> bool {
signed_in_marker(app).map(|p| p.exists()).unwrap_or(false)
}
fn uses_public_jitsi(jitsi_base: &str) -> bool {
Url::parse(jitsi_base)
.map(|u| u.host_str() == Some(JITSI_PUBLIC_HOST))
.unwrap_or(false)
}
fn signin_url(state: &serde_json::Value) -> Result<Url, String> {
let encoded = percent_encode(&percent_encode(&state.to_string()));
Url::parse(&format!("{JITSI_SIGNIN_URL}?state={encoded}")).map_err(|e| e.to_string())
}
fn web_window<'a>(
app: &'a tauri::AppHandle,
label: impl Into<String>,
url: Url,
) -> WebviewWindowBuilder<'a, tauri::Wry, tauri::AppHandle> {
let builder = WebviewWindowBuilder::new(app, label, WebviewUrl::External(url));
#[cfg(target_os = "macos")]
let builder = builder.user_agent(SAFARI_USER_AGENT);
let app_ = app.clone();
builder.on_new_window(move |url, features| {
let n = POPUP_COUNTER.fetch_add(1, Ordering::Relaxed);
let builder = WebviewWindowBuilder::new(
&app_,
format!("{POPUP_LABEL_PREFIX}{n}"),
WebviewUrl::External("about:blank".parse().unwrap()),
)
.window_features(features)
.title(url.host_str().unwrap_or("Sign in"));
#[cfg(target_os = "macos")]
let builder = builder.user_agent(SAFARI_USER_AGENT);
match builder.build() {
Ok(window) => NewWindowResponse::Create { window },
Err(_) => NewWindowResponse::Deny,
}
})
}
fn close_popups(app: &tauri::AppHandle) {
for (label, window) in app.webview_windows() {
if label.starts_with(POPUP_LABEL_PREFIX) {
let _ = window.close();
}
}
}
#[tauri::command]
fn jitsi_signed_in(app: tauri::AppHandle) -> bool {
is_signed_in(&app)
}
#[tauri::command]
fn jitsi_sign_in(app: tauri::AppHandle) -> Result<(), String> {
if let Some(existing) = app.get_webview_window(SIGNIN_LABEL) {
return existing.set_focus().map_err(|e| e.to_string());
}
let url = signin_url(&serde_json::json!({ "room": SIGNIN_DONE_ROOM }))?;
let app_ = app.clone();
web_window(&app, SIGNIN_LABEL, url)
.title("Sign in to meet.jit.si")
.inner_size(480.0, 640.0)
.on_navigation(move |url| {
let done = url.host_str() == Some(JITSI_PUBLIC_HOST)
&& url.path() == format!("/{SIGNIN_DONE_ROOM}");
if !done {
return true;
}
let app = app_.clone();
let _ = app_.run_on_main_thread(move || {
if let Ok(marker) = signed_in_marker(&app) {
let _ = fs::write(marker, "");
}
let _ = app.emit("jitsi-auth-changed", true);
if let Some(w) = app.get_webview_window(SIGNIN_LABEL) {
let _ = w.close();
}
close_popups(&app);
});
false
})
.build()
.map_err(|e| format!("could not open sign-in window: {e}"))?;
Ok(())
}
#[tauri::command]
fn jitsi_sign_out(app: tauri::AppHandle) -> Result<(), String> {
if let Some(main) = app.get_webview_window("main") {
main.clear_all_browsing_data().map_err(|e| e.to_string())?;
}
let marker = signed_in_marker(&app)?;
if marker.exists() {
fs::remove_file(marker).map_err(|e| e.to_string())?;
}
let _ = app.emit("jitsi-auth-changed", false);
Ok(())
}
const MEETING_STATE_SCRIPT: &str = r#"
(function () {
if (window.top !== window) return;
var left = false;
setInterval(function () {
try {
var app = window.APP;
if (!app || !app.store || !app.conference) return;
var state = app.store.getState();
var st = state['features/base/conference'];
var conf = st && st.conference;
if (conf && conf.isEndConferenceSupported) {
conf.isEndConferenceSupported = function () { return false; };
}
var notes = (state['features/notifications'] || {}).notifications || [];
if (!left && notes.some(function (n) { return n.props && n.props.titleKey === 'dialog.thankYou'; })) {
left = true;
location.assign(location.origin + '/quple-hung-up');
return;
}
var joined = !!conf;
var remotes = 0;
if (joined) {
try { remotes = Math.max(0, app.conference.membersCount - 1); } catch (e) {}
}
var media = state['features/base/media'] || {};
var title = 'quple-state:' + JSON.stringify({
joined: joined,
remotes: remotes,
audioMuted: !!(media.audio && media.audio.muted),
videoMuted: !!(media.video && media.video.muted)
});
if (document.title !== title) document.title = title;
} catch (e) {}
}, 500);
})();
"#;
const MEETING_STATE_PREFIX: &str = "quple-state:";
const MEETING_IN_BROWSER: bool = cfg!(target_os = "linux");
#[derive(Serialize, Clone)]
#[serde(rename_all = "camelCase")]
struct MeetingState {
room: String,
joined: bool,
remotes: u32,
audio_muted: bool,
video_muted: bool,
}
#[derive(Serialize, Clone)]
struct MeetingClosed {
room: String,
}
fn meeting_label(room: &str) -> String {
format!("meeting-{room}")
}
#[tauri::command]
fn close_meeting(app: tauri::AppHandle, room: String) -> Result<(), String> {
if let Some(w) = app.get_webview_window(&meeting_label(&room)) {
w.close().map_err(|e| e.to_string())?;
}
Ok(())
}
#[tauri::command]
fn rejoin_meeting(app: tauri::AppHandle, room: String) -> Result<(), String> {
match app.get_webview_window(&meeting_label(&room)) {
Some(w) => w.eval("location.reload()").map_err(|e| e.to_string()),
None => Err(format!("no meeting window for {room}")),
}
}
#[tauri::command]
fn set_meeting_muted(app: tauri::AppHandle, room: String, muted: bool) -> Result<(), String> {
match app.get_webview_window(&meeting_label(&room)) {
Some(w) => w
.eval(format!(
"try {{ window.APP && APP.conference.muteAudio({muted}).catch(function () {{}}); }} catch (e) {{}}"
))
.map_err(|e| e.to_string()),
None => Err(format!("no meeting window for {room}")),
}
}
#[tauri::command]
fn open_meeting(
app: tauri::AppHandle,
jitsi_base: String,
room: String,
display_name: String,
peer: Option<String>,
audio_muted: Option<bool>,
video_muted: Option<bool>,
) -> Result<(), String> {
let room = room.trim().to_string();
if room.is_empty() {
return Err("room is required".into());
}
match Url::parse(jitsi_base.trim()) {
Ok(u) if matches!(u.scheme(), "https" | "http") && u.host_str().is_some() => {}
_ => return Err(format!("{jitsi_base} isn't a Jitsi Meet server URL")),
}
let label = meeting_label(&room);
if let Some(existing) = app.get_webview_window(&label) {
existing.set_focus().map_err(|e| e.to_string())?;
return Ok(());
}
let display_name = display_name.trim();
let audio_muted = audio_muted.unwrap_or(false);
let video_muted = video_muted.unwrap_or(false);
let via_signin = is_signed_in(&app) || MEETING_IN_BROWSER;
let url = if uses_public_jitsi(&jitsi_base) && via_signin {
signin_url(&serde_json::json!({
"room": room,
"userInfo.displayName": display_name,
"config.prejoinConfig.enabled": false,
"config.prejoinPageEnabled": false,
"config.startWithAudioMuted": audio_muted,
"config.startWithVideoMuted": video_muted,
}))?
} else {
let name_json = serde_json::to_string(display_name).map_err(|e| e.to_string())?;
let base = jitsi_base.trim_end_matches('/');
let url = format!(
"{base}/{room}#userInfo.displayName={}\
&config.prejoinConfig.enabled=false&config.prejoinPageEnabled=false\
&config.startWithAudioMuted={audio_muted}&config.startWithVideoMuted={video_muted}",
percent_encode(&name_json)
);
Url::parse(&url).map_err(|_| format!("invalid meeting URL for {base}/{room}"))?
};
if MEETING_IN_BROWSER {
use tauri_plugin_opener::OpenerExt;
return app
.opener()
.open_url(url.as_str(), None::<&str>)
.map_err(|e| format!("could not open the call in the browser: {e}"));
}
let title = match peer.as_deref().map(str::trim) {
Some(p) if !p.is_empty() => format!("Quple β {p}"),
_ => format!("Quple β {room}"),
};
let app_ = app.clone();
let room_ = room.clone();
let room_path = format!("/{room}");
let reached = Arc::new(AtomicBool::new(false));
let app_title = app.clone();
let room_title = room.clone();
let title_ = title.clone();
let window = web_window(&app, label, url)
.title(&title)
.inner_size(1000.0, 700.0)
.min_inner_size(480.0, 360.0)
.initialization_script(MEETING_STATE_SCRIPT)
.on_document_title_changed(move |window, page_title| {
let Some(json) = page_title.strip_prefix(MEETING_STATE_PREFIX) else {
return;
};
let _ = window.set_title(&title_);
let Ok(v) = serde_json::from_str::<serde_json::Value>(json) else {
return;
};
let state = MeetingState {
room: room_title.clone(),
joined: v["joined"].as_bool().unwrap_or(false),
remotes: v["remotes"].as_u64().unwrap_or(0) as u32,
audio_muted: v["audioMuted"].as_bool().unwrap_or(false),
video_muted: v["videoMuted"].as_bool().unwrap_or(false),
};
let _ = app_title.emit_to("main", "meeting-state", state);
})
.on_navigation(move |url| {
if url.path() == room_path {
reached.store(true, Ordering::Relaxed);
let app = app_.clone();
let _ = app_.run_on_main_thread(move || close_popups(&app));
return true;
}
if !reached.load(Ordering::Relaxed) {
return true;
}
let app = app_.clone();
let room = room_.clone();
let _ = app_.run_on_main_thread(move || {
if let Some(w) = app.get_webview_window(&meeting_label(&room)) {
let _ = w.close();
}
});
false
})
.build()
.map_err(|e| format!("could not open meeting window: {e}"))?;
let app_closed = app.clone();
window.on_window_event(move |event| {
if let tauri::WindowEvent::Destroyed = event {
let _ = app_closed.emit_to("main", "meeting-closed", MeetingClosed { room: room.clone() });
}
});
Ok(())
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
let builder = tauri::Builder::default();
#[cfg(target_os = "macos")]
let builder = builder
.plugin(tauri_plugin_sparkle_updater::init())
.menu(|handle| updater::menu(handle))
.on_menu_event(|app, event| {
if event.id() == updater::CHECK_FOR_UPDATES_ID {
updater::check_for_updates(app);
}
})
.setup(|app| {
updater::setup(app.handle());
Ok(())
});
builder
.plugin(tauri_plugin_opener::init())
.manage(quine::QuineAuth::default())
.invoke_handler(tauri::generate_handler![
load_config,
save_config,
open_meeting,
close_meeting,
rejoin_meeting,
set_meeting_muted,
jitsi_signed_in,
jitsi_sign_in,
jitsi_sign_out,
quine::quine_sign_in,
quine::quine_cancel_sign_in,
quine::quine_restore,
quine::quine_access_token,
quine::quine_sign_out
])
.run(tauri::generate_context!())
.expect("error while running Quple");
}