forensic_rs/notifications/
mod.rs

1use std::borrow::Cow;
2use std::cell::RefCell;
3use crate::channel::{self, Receiver, Sender};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
6#[repr(usize)]
7pub enum NotificationType {
8    Informational,
9    SuspiciousArtifact,
10    AntiForensicsDetected,
11    DeletedArtifact
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
15#[repr(usize)]
16pub enum Priority {
17    Informational,
18    Low,
19    Medium,
20    High,
21    Critical
22}
23
24#[derive(Clone)]
25pub struct Notifier {
26    pub channel : Sender<Notification>
27}
28
29impl Default for Notifier {
30    fn default() -> Self {
31        let (sender,_reveiver) = channel::channel();
32        Self { channel: sender }
33    }
34}
35impl Notifier {
36    pub fn new(sender : Sender<Notification>) -> Self {
37        Self {
38            channel : sender
39        }
40    }
41    pub fn notify(&self, priority: Priority, r#type : NotificationType, module : &'static str, file : &'static str, line : u32, data : Cow<'static, str>) {
42        let _ = self.channel.send(Notification { r#type,priority, module, file, line, data });
43    }
44}
45
46#[derive(Debug, Clone)]
47pub struct Notification {
48    pub r#type : NotificationType,
49    pub priority : Priority,
50    pub module : &'static str,
51    pub line : u32,
52    pub file : &'static str,
53    pub data : Cow<'static, str>,
54}
55
56#[macro_use]
57pub mod macros;
58
59thread_local! {
60    pub static NOTIFIER : RefCell<Notifier> = RefCell::new(Notifier::default());
61}
62
63/// Initializes the Notifier for the current thread/component.
64pub fn initialize_notifier(msngr: Notifier) {
65    let _ = NOTIFIER.with(|v| {
66        let mut brw = v.borrow_mut();
67        *brw = msngr;
68        Ok::<(), ()>(())
69    });
70    // Wait for local_key_cell_methods
71}
72
73/// Use for fast initialization during testing
74pub fn testing_notifier_dummy() -> Receiver<Notification> {
75    let (sender, receiver) = channel::channel();
76    let msngr = Notifier::new(sender);
77    initialize_notifier(msngr);
78    receiver
79}