rustpbx 0.4.4

A SIP PBX implementation in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Default AppRuntime implementation
//!
//! This module provides `DefaultAppRuntime` which wraps the existing
//! `CallApp` / `AppEventLoop` framework.

use async_trait::async_trait;
use std::sync::Arc;
use tokio::sync::{RwLock, mpsc};
use tokio_util::sync::CancellationToken;

use crate::call::app::{ApplicationContext, CallApp, CallController, ControllerEvent};
use crate::call::domain::{CallCommand, MediaCapability};
use crate::proxy::proxy_call::sip_session::SipSessionHandle;

use super::{AppDescriptor, AppResult, AppRuntime, AppRuntimeError, AppStatus};

/// State for a running application
#[derive(Clone)]
struct RunningApp {
    name: String,
    cancel_token: CancellationToken,
}

/// Configuration needed to create an AppRuntime
pub struct AppRuntimeConfig {
    pub session_id: String,
    pub handle: SipSessionHandle,
    pub context: Arc<ApplicationContext>,
}

/// Default implementation of AppRuntime using the existing CallApp framework
pub struct DefaultAppRuntime {
    session_id: String,
    handle: SipSessionHandle,
    /// Shared per-call context (public for post-call hook access).
    pub context: Arc<ApplicationContext>,
    /// Currently running app (if any)
    running: RwLock<Option<RunningApp>>,
    /// App factory function
    app_factory: Option<Arc<dyn AppFactory>>,
}

/// Factory trait for creating CallApp instances
#[async_trait::async_trait]
pub trait AppFactory: Send + Sync {
    fn create_app(
        &self,
        app_name: &str,
        params: Option<serde_json::Value>,
        context: &ApplicationContext,
    ) -> Option<Box<dyn CallApp>>;
}

impl DefaultAppRuntime {
    pub fn new(config: AppRuntimeConfig) -> Self {
        Self {
            session_id: config.session_id,
            handle: config.handle,
            context: config.context,
            running: RwLock::new(None),
            app_factory: None,
        }
    }

    pub fn with_factory(mut self, factory: Arc<dyn AppFactory>) -> Self {
        self.app_factory = Some(factory);
        self
    }

    /// Get app descriptor for known app types
    fn get_descriptor(&self, app_name: &str) -> AppDescriptor {
        match app_name {
            "ivr" => AppDescriptor::ivr(),
            "voicemail" => AppDescriptor::voicemail(),
            "queue" => AppDescriptor::queue(),
            _ => AppDescriptor::new(app_name).with_capabilities(vec![MediaCapability::Full]),
        }
    }
}

#[async_trait]
impl AppRuntime for DefaultAppRuntime {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    async fn start_app(
        &self,
        app_name: &str,
        params: Option<serde_json::Value>,
        auto_answer: bool,
    ) -> AppResult<()> {
        // Check if already running
        {
            let running = self.running.read().await;
            if running.is_some() {
                return Err(AppRuntimeError::AlreadyRunning(app_name.to_string()));
            }
        }

        // Create event channel for app events (DTMF, hangup, etc.)
        let (event_tx, event_rx) = mpsc::unbounded_channel::<ControllerEvent>();

        // Create controller — it owns the timer sender, we get the receiver back.
        let (controller, timer_rx) = CallController::new(self.handle.clone(), event_rx);

        // Register the event sender with the session so SipSession can forward
        // DTMF / hangup / audio-complete events to the running app.
        self.handle.set_app_event_sender(Some(event_tx.clone()));

        // Create cancel token
        let cancel_token = CancellationToken::new();

        // Get the app from factory (try per-session factory first, then global registry)
        let app = if let Some(factory) = &self.app_factory {
            factory
                .create_app(app_name, params.clone(), &self.context)
                .or_else(|| lookup_custom_app(app_name, params.clone(), &self.context))
        } else {
            lookup_custom_app(app_name, params.clone(), &self.context)
        };

        let app = match app {
            Some(app) => app,
            None => {
                self.handle.set_app_event_sender(None);
                return Err(AppRuntimeError::UnknownApp(app_name.to_string()));
            }
        };

        // Store running state
        {
            let mut running = self.running.write().await;
            *running = Some(RunningApp {
                name: app_name.to_string(),
                cancel_token: cancel_token.clone(),
            });
        }

        // Auto-answer if requested
        if auto_answer {
            self.handle
                .send_command(CallCommand::Answer {
                    leg_id: crate::call::domain::LegId::from("caller"),
                })
                .map_err(|e| AppRuntimeError::StartFailed(e.to_string()))?;
        }

        // Spawn the event loop
        let session_id_for_log = self.session_id.clone();
        let app_name_owned = app_name.to_string();
        let context = self.context.clone();
        let handle = self.handle.clone();

        tokio::spawn(async move {
            let event_loop = crate::call::app::AppEventLoop::new(
                app,
                controller,
                (*context).clone(),
                cancel_token,
                timer_rx,
            );

            if let Err(e) = event_loop.run().await {
                tracing::error!(
                    "App {} failed for session {}: {}",
                    app_name_owned,
                    session_id_for_log,
                    e
                );
            }

            // Clear the app event sender so the session knows the app has exited.
            handle.set_app_event_sender(None);
            tracing::info!(
                "App {} exited for session {}",
                app_name_owned,
                session_id_for_log
            );
        });

        tracing::info!("App {} started for session {}", app_name, self.session_id);
        Ok(())
    }

    async fn stop_app(&self, reason: Option<String>) -> AppResult<()> {
        let running = {
            let running = self.running.read().await;
            running.clone()
        };

        match running {
            Some(running) => {
                // Cancel the event loop
                running.cancel_token.cancel();

                // Clear running state
                {
                    let mut running_guard = self.running.write().await;
                    *running_guard = None;
                }

                tracing::info!(
                    "App {} stopped for session {}: {}",
                    running.name,
                    self.session_id,
                    reason.unwrap_or_else(|| "no reason".to_string())
                );

                Ok(())
            }
            None => Err(AppRuntimeError::NotRunning),
        }
    }

    fn inject_event(&self, event: serde_json::Value) -> AppResult<()> {
        // Parse the event type and convert to ControllerEvent
        let controller_event = parse_json_event(&event)?;

        // Try to send via the handle's send_app_event
        if self.handle.send_app_event(controller_event) {
            Ok(())
        } else {
            Err(AppRuntimeError::InjectFailed(
                "no app running or channel closed".to_string(),
            ))
        }
    }

    fn is_running(&self) -> bool {
        // Check if there's an app event sender set
        // This is a quick synchronous check
        if let Ok(guard) = self.running.try_read() {
            guard.is_some()
        } else {
            false
        }
    }

    fn status(&self) -> AppStatus {
        if self.is_running() {
            AppStatus::Running
        } else {
            AppStatus::Idle
        }
    }

    fn current_app(&self) -> Option<String> {
        if let Ok(guard) = self.running.try_read() {
            guard.as_ref().map(|r| r.name.clone())
        } else {
            None
        }
    }

    fn required_capabilities(&self) -> Vec<MediaCapability> {
        if let Ok(guard) = self.running.try_read()
            && let Some(running) = guard.as_ref()
        {
            let descriptor = self.get_descriptor(&running.name);
            return descriptor.required_capabilities;
        }
        vec![]
    }

    fn app_descriptor(&self, app_name: &str) -> Option<AppDescriptor> {
        Some(self.get_descriptor(app_name))
    }

    fn get_queue_name(&self) -> Option<String> {
        self.context.queue_name.try_read().ok()?.clone()
    }
}

/// Parse a JSON event into a ControllerEvent
fn parse_json_event(value: &serde_json::Value) -> AppResult<ControllerEvent> {
    let obj = value
        .as_object()
        .ok_or_else(|| AppRuntimeError::InjectFailed("event must be a JSON object".to_string()))?;

    let event_type = obj.get("type").and_then(|v| v.as_str()).ok_or_else(|| {
        AppRuntimeError::InjectFailed("event must have a 'type' field".to_string())
    })?;

    match event_type {
        "dtmf" => {
            let digit = obj
                .get("digit")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();
            Ok(ControllerEvent::DtmfReceived(digit))
        }
        "audio_complete" => {
            let track_id = obj
                .get("track_id")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();
            let interrupted = obj
                .get("interrupted")
                .and_then(|v| v.as_bool())
                .unwrap_or(false);
            Ok(ControllerEvent::AudioComplete {
                track_id,
                interrupted,
            })
        }
        "recording_complete" => {
            let path = obj
                .get("path")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();
            Ok(ControllerEvent::RecordingComplete(
                crate::call::app::RecordingInfo {
                    path,
                    duration: std::time::Duration::from_secs(0),
                    size_bytes: 0,
                },
            ))
        }
        "hangup" => {
            let _reason = obj.get("reason").and_then(|v| v.as_str());
            // Note: CallRecordHangupReason doesn't have FromStr, so we just use None
            Ok(ControllerEvent::Hangup(None))
        }
        "timeout" => {
            let timer_id = obj
                .get("timer_id")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();
            Ok(ControllerEvent::Timeout(timer_id))
        }
        "custom" => {
            let name = obj
                .get("name")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();
            let data = obj.get("data").cloned().unwrap_or(serde_json::Value::Null);
            Ok(ControllerEvent::Custom(name, data))
        }
        _ => Err(AppRuntimeError::InjectFailed(format!(
            "unknown event type: {}",
            event_type
        ))),
    }
}

// ── AppFactory Registry ───────────────────────────────────────────────────────

/// Type for custom app factory functions.
pub type AppFactoryFn = Arc<
    dyn Fn(&str, Option<serde_json::Value>, &ApplicationContext) -> Option<Box<dyn CallApp>>
        + Send
        + Sync,
>;

/// Register a custom call app factory.
/// Stores to the per-context registry (ApplicationContext.app_factories).
pub fn register_call_app(_name: &'static str, _factory: AppFactoryFn) {
    tracing::warn!(
        "register_call_app is deprecated — set app_factories on ApplicationContext instead"
    );
}

/// Look up a custom call app, checking context.app_factories first.
pub fn lookup_custom_app(
    app_name: &str,
    params: Option<serde_json::Value>,
    context: &ApplicationContext,
) -> Option<Box<dyn CallApp>> {
    for (name, factory) in context.app_factories.iter() {
        if *name == app_name {
            let app = factory(app_name, params.clone(), context);
            if app.is_some() {
                return app;
            }
        }
    }
    None
}

/// PostCallHook allows addons to react when the connected callee (agent)
/// disconnects after a queue call.  The hook can start a survey app, log
/// results, etc.
#[async_trait]
pub trait PostCallHook: Send + Sync {
    /// Called when the queue's connected callee (agent) disconnects.
    /// If the hook returns `true`, the caller's session will NOT be hung up
    /// (the hook is responsible for starting a survey app or taking other action).
    /// If it returns `false`, normal hangup proceeds.
    async fn on_agent_disconnected(
        &self,
        session_id: &str,
        caller: &str,
        agent_id: &str,
        queue_name: &str,
        app_runtime: &dyn AppRuntime,
    ) -> bool;
}

/// Register a PostCallHook (deprecated — set post_call_hook on ApplicationContext).
pub fn set_post_call_hook(_hook: Arc<dyn PostCallHook>) {
    tracing::warn!(
        "set_post_call_hook is deprecated — set post_call_hook on ApplicationContext instead"
    );
}

/// Invoke the PostCallHook from the per-context hook, if registered.
pub async fn invoke_post_call_hook(
    session_id: &str,
    caller: &str,
    agent_id: &str,
    queue_name: &str,
    app_runtime: &dyn AppRuntime,
) -> bool {
    // Downcast to DefaultAppRuntime to access the per-call context
    if let Some(runtime) = app_runtime.as_any().downcast_ref::<DefaultAppRuntime>() {
        if let Some(ref hook) = runtime.context.post_call_hook {
            return hook
                .on_agent_disconnected(session_id, caller, agent_id, queue_name, app_runtime)
                .await;
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_dtmf_event() {
        let json = serde_json::json!({
            "type": "dtmf",
            "digit": "5"
        });
        let event = parse_json_event(&json).unwrap();
        assert!(matches!(event, ControllerEvent::DtmfReceived(d) if d == "5"));
    }

    #[test]
    fn test_parse_audio_complete_event() {
        let json = serde_json::json!({
            "type": "audio_complete",
            "track_id": "track-123",
            "interrupted": true
        });
        let event = parse_json_event(&json).unwrap();
        if let ControllerEvent::AudioComplete {
            track_id,
            interrupted,
        } = event
        {
            assert_eq!(track_id, "track-123");
            assert!(interrupted);
        } else {
            panic!("Expected AudioComplete");
        }
    }

    #[test]
    fn test_parse_custom_event() {
        let json = serde_json::json!({
            "type": "custom",
            "name": "webhook",
            "data": {"action": "transfer", "target": "1001"}
        });
        let event = parse_json_event(&json).unwrap();
        if let ControllerEvent::Custom(name, data) = event {
            assert_eq!(name, "webhook");
            assert_eq!(data["action"], "transfer");
        } else {
            panic!("Expected Custom");
        }
    }

    #[test]
    fn test_parse_unknown_event() {
        let json = serde_json::json!({
            "type": "unknown"
        });
        let result = parse_json_event(&json);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_missing_type() {
        let json = serde_json::json!({
            "digit": "5"
        });
        let result = parse_json_event(&json);
        assert!(result.is_err());
    }
}