use std::io::{self, BufRead, IsTerminal, Write};
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde_json::Value;
use crate::protocol::*;
use crate::schema;
pub trait Plugin: Sized {
type Config: DeserializeOwned + JsonSchema;
type SystemConfig: DeserializeOwned + JsonSchema;
fn manifest() -> Manifest;
fn start(host: Host, setup: Setup<Self::Config, Self::SystemConfig>) -> Result<Self, String>;
fn call_start(&mut self, _call: CallInfo) {}
fn call_end(&mut self, _call: CallInfo) {}
fn call_concluded(&mut self, _call: ConcludedCall) {}
fn unit(&mut self, _event: UnitEvent) {}
fn audio(&mut self, _chunk: AudioChunk) {}
fn status(&mut self, _status: Status) {}
fn tone_pattern(&mut self, _t: TonePatternEvent) {}
fn action(&mut self, _a: Action, reply: Reply) {
reply.fail("This plugin has no such action");
}
fn settings(&mut self, _setup: Setup<Self::Config, Self::SystemConfig>) -> Result<(), String> {
Err("it can't take new settings while it runs".into())
}
fn shutdown(&mut self, _grace: std::time::Duration) {}
}
pub use crate::schema::NoConfig;
#[derive(Clone, Debug)]
pub struct Setup<C, S> {
pub config: C,
pub systems: Vec<System<C, S>>,
pub host: HostInfo,
pub capture_dir: PathBuf,
pub data_dir: PathBuf,
pub audio_formats: Vec<String>,
}
impl<C, S> Setup<C, S> {
pub fn system(&self, index: u16) -> Option<&System<C, S>> {
self.systems.iter().find(|s| s.index == index)
}
pub fn system_named(&self, short_name: &str) -> Option<&System<C, S>> {
self.systems.iter().find(|s| s.short_name == short_name)
}
pub fn has_format(&self, format: &str) -> bool {
self.audio_formats.iter().any(|f| f == format)
}
}
#[derive(Clone, Debug)]
pub struct System<C, S> {
pub index: u16,
pub short_name: String,
pub kind: String,
pub name: String,
pub fcc: std::collections::BTreeMap<String, FccLicense>,
pub fcc_call_sign: String,
pub fcc_name: String,
pub sites: Vec<SiteInfo>,
pub config: Option<S>,
pub settings: C,
}
#[derive(Clone)]
pub struct Host {
out: Arc<Mutex<Box<dyn Write + Send>>>,
}
impl Host {
pub fn to_writer(w: impl Write + Send + 'static) -> Host {
Host { out: Arc::new(Mutex::new(Box::new(w))) }
}
pub fn send(&self, m: &PluginMessage) {
let mut line = serde_json::to_string(m).unwrap_or_default();
line.push('\n');
let mut o = self.out.lock().unwrap_or_else(|e| e.into_inner());
let _ = o.write_all(line.as_bytes()).and_then(|_| o.flush());
}
pub fn log(&self, level: Level, message: impl Into<String>) {
self.send(&PluginMessage::Log { level, message: message.into() });
}
pub fn error(&self, message: impl Into<String>) {
self.log(Level::Error, message)
}
pub fn warn(&self, message: impl Into<String>) {
self.log(Level::Warn, message)
}
pub fn info(&self, message: impl Into<String>) {
self.log(Level::Info, message)
}
pub fn debug(&self, message: impl Into<String>) {
self.log(Level::Debug, message)
}
pub fn status(&self, state: State, message: impl Into<String>) {
self.send(&PluginMessage::Status { state, message: message.into() });
}
pub fn metrics(&self, m: &Metrics) {
self.send(&PluginMessage::Metrics(m.clone()));
}
pub fn call_result(&self, path: &str, outcome: Outcome, message: impl Into<String>, url: impl Into<String>) {
self.send(&PluginMessage::CallResult { path: path.to_string(), outcome, message: message.into(), url: url.into(), took_ms: None });
}
pub fn call_result_took(&self, path: &str, outcome: Outcome, message: impl Into<String>, url: impl Into<String>, took: std::time::Duration) {
let took_ms = Some(took.as_millis() as u64);
self.send(&PluginMessage::CallResult { path: path.to_string(), outcome, message: message.into(), url: url.into(), took_ms });
}
}
pub struct Reply {
host: Host,
request: u64,
sent: bool,
}
impl Reply {
pub fn ok(mut self, message: impl Into<String>) {
self.send(true, message.into());
}
pub fn fail(mut self, message: impl Into<String>) {
self.send(false, message.into());
}
fn send(&mut self, ok: bool, message: String) {
self.sent = true;
self.host.send(&PluginMessage::ActionResult { request: self.request, ok, message });
}
}
impl Drop for Reply {
fn drop(&mut self) {
if !self.sent {
self.send(false, "The plugin didn't answer".into());
}
}
}
pub fn describe<P: Plugin>() -> Manifest {
let mut m = P::manifest();
m.api = API_VERSION;
let c = schema::schema_for::<P::Config>();
m.config = (!schema::is_empty(&c)).then_some(c);
let s = schema::schema_for::<P::SystemConfig>();
m.system_config = (!schema::is_empty(&s)).then_some(s);
m
}
pub fn run<P: Plugin>() {
let arg = std::env::args().nth(1);
match arg.as_deref() {
Some("--describe") => {
println!("{}", serde_json::to_string_pretty(&describe::<P>()).unwrap_or_default());
return;
}
Some("--version" | "-V") => {
println!("{}", P::manifest().version);
return;
}
Some(a) => {
eprintln!("unknown argument {a} (a plugin takes --describe or --version; the recorder runs it with none)");
std::process::exit(2);
}
None => {}
}
let stdin = io::stdin();
if stdin.is_terminal() {
let m = P::manifest();
eprintln!(
"{} {} is a Trunk Recorder Pro plugin: the recorder runs it and talks to it over stdin/stdout.\n\
`--describe` prints its manifest. Try it with `trunk-pro plugin run <this binary>`,\n\
or paste protocol lines here (a hello first).",
m.name, m.version
);
}
let code = serve::<P>(stdin.lock(), Host { out: Arc::new(Mutex::new(Box::new(io::stdout()))) });
std::process::exit(code);
}
pub fn serve<P: Plugin>(input: impl BufRead, host: Host) -> i32 {
let mut plugin: Option<P> = None;
let mut grace = std::time::Duration::from_secs(10);
for line in input.lines() {
let Ok(line) = line else { break };
if line.trim().is_empty() {
continue;
}
let msg: HostMessage = match serde_json::from_str(&line) {
Ok(m) => m,
Err(e) => {
host.warn(format!("unreadable message from the recorder ({e}): {line}"));
continue;
}
};
let Some(p) = plugin.as_mut() else {
let HostMessage::Hello(hello) = msg else {
host.warn("a message before hello, ignored");
continue;
};
match start::<P>(host.clone(), hello) {
Ok(p) => {
plugin = Some(p);
host.send(&PluginMessage::Ready);
}
Err(e) => {
host.status(State::Error, e.clone());
host.error(e);
return EXIT_CONFIG;
}
}
continue;
};
match msg {
HostMessage::Hello(_) => host.warn("a second hello, ignored"),
HostMessage::CallStart(c) => p.call_start(c),
HostMessage::CallEnd(c) => p.call_end(c),
HostMessage::CallConcluded(c) => p.call_concluded(c),
HostMessage::Unit(u) => p.unit(u),
HostMessage::Audio(a) => p.audio(a),
HostMessage::Status(s) => p.status(s),
HostMessage::TonePattern(t) => p.tone_pattern(t),
HostMessage::Action(a) => {
let reply = Reply { host: host.clone(), request: a.request, sent: false };
p.action(a, reply)
}
HostMessage::Settings(h) => match setup::<P>(h).and_then(|s| p.settings(s)) {
Ok(()) => host.status(State::Ok, ""),
Err(e) => host.status(State::Error, format!("new settings not taken: {e}")),
},
HostMessage::Shutdown(s) => {
grace = std::time::Duration::from_secs_f64(s.grace_s.max(0.0));
break;
}
HostMessage::Unknown => {}
}
}
if let Some(p) = plugin.as_mut() {
p.shutdown(grace);
}
0
}
fn start<P: Plugin>(host: Host, hello: Hello) -> Result<P, String> {
P::start(host, setup::<P>(hello)?)
}
fn setup<P: Plugin>(hello: Hello) -> Result<Setup<P::Config, P::SystemConfig>, String> {
if hello.api != 0 && hello.api < API_VERSION {
return Err(format!("this plugin needs a newer recorder (plugin API {API_VERSION}, the recorder has {})", hello.api));
}
let config: P::Config = parse(hello.config.clone(), "settings")?;
let mut systems = Vec::new();
for s in hello.systems {
let settings = match s.settings {
Value::Null => parse::<P::Config>(hello.config.clone(), "settings")?,
v => parse::<P::Config>(v, &format!("settings for {}", s.short_name))?,
};
let config = match s.config {
Value::Null => None,
v => Some(parse::<P::SystemConfig>(v, &format!("settings for {}", s.short_name))?),
};
systems.push(System { index: s.index, short_name: s.short_name, kind: s.kind, name: s.name, fcc: s.fcc, fcc_call_sign: s.fcc_call_sign, fcc_name: s.fcc_name, sites: s.sites, config, settings });
}
Ok(Setup { config, systems, host: hello.host, capture_dir: hello.capture_dir, data_dir: hello.data_dir, audio_formats: hello.audio_formats })
}
fn parse<T: DeserializeOwned>(v: Value, what: &str) -> Result<T, String> {
let v = if v.is_null() { Value::Object(Default::default()) } else { v };
serde_json::from_value(v).map_err(|e| format!("bad {what}: {e}"))
}
#[macro_export]
macro_rules! manifest {
() => {{
let authors: Vec<String> = env!("CARGO_PKG_AUTHORS").split(':').filter(|a| !a.is_empty()).map(String::from).collect();
$crate::Manifest {
id: env!("CARGO_PKG_NAME").to_string(),
name: env!("CARGO_PKG_NAME").to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
description: env!("CARGO_PKG_DESCRIPTION").to_string(),
api: $crate::API_VERSION,
homepage: env!("CARGO_PKG_HOMEPAGE").to_string(),
repository: env!("CARGO_PKG_REPOSITORY").to_string(),
authors,
license: env!("CARGO_PKG_LICENSE").to_string(),
..Default::default()
}
}};
}
#[cfg(test)]
mod tests {
use super::*;
use crate::testing;
use serde_json::json;
#[derive(serde::Deserialize, schemars::JsonSchema, Default)]
#[serde(default)]
struct Config {
server: String,
retries: u32,
}
struct Servers;
impl Plugin for Servers {
type Config = Config;
type SystemConfig = NoConfig;
fn manifest() -> Manifest {
Manifest { id: "servers".into(), ..Default::default() }
}
fn start(host: Host, setup: Setup<Config, NoConfig>) -> Result<Self, String> {
for s in &setup.systems {
host.info(format!("{} {} {}", s.short_name, s.settings.server, s.settings.retries));
}
Ok(Servers)
}
}
#[test]
fn a_system_can_override_the_plugins_settings() {
let dir = testing::temp_dir("sdk");
let mut h = testing::hello(&dir, json!({ "server": "https://a", "retries": 3 }));
h.systems.push(SystemInfo { index: 1, short_name: "sys2".into(), kind: "p25".into(), settings: json!({ "server": "https://b", "retries": 3 }), ..Default::default() });
let out = testing::run::<Servers>([HostMessage::Hello(h)]);
assert!(out.ready());
let said: Vec<String> = out.logs().into_iter().map(|(_, t)| t).collect();
assert_eq!(said, ["sys1 https://a 3", "sys2 https://b 3"]);
}
}