1
1
use std:: {
2
2
os:: unix:: process:: CommandExt ,
3
3
process:: { Command , Stdio } ,
4
- sync:: Arc ,
5
4
} ;
6
5
7
6
use anyhow:: { Context , Result } ;
7
+ use async_trait:: async_trait;
8
8
use pulsar_core:: {
9
9
event:: Threat ,
10
10
pdk:: {
11
- CleanExit , ConfigError , Event , ModuleConfig , ModuleContext , ModuleError , PulsarModule ,
12
- ShutdownSignal , Version ,
11
+ ConfigError , Event , Module , ModuleConfig , ModuleContext , ModuleError , PulsarModule , Version ,
13
12
} ,
14
13
} ;
15
14
16
15
const MODULE_NAME : & str = "desktop-notifier" ;
17
16
18
- pub fn module ( ) -> PulsarModule {
19
- PulsarModule :: new (
20
- MODULE_NAME ,
21
- Version :: parse ( env ! ( "CARGO_PKG_VERSION" ) ) . unwrap ( ) ,
22
- desktop_nitifier_task ,
23
- )
17
+ # [ derive ( Clone ) ]
18
+ pub struct DesktopNotifier {
19
+ user_id : u32 ,
20
+ display : String ,
21
+ notify_send_executable : String ,
22
+ bus_address : String ,
24
23
}
25
24
26
- async fn desktop_nitifier_task (
27
- ctx : ModuleContext ,
28
- mut shutdown : ShutdownSignal ,
29
- ) -> Result < CleanExit , ModuleError > {
30
- let mut receiver = ctx. get_receiver ( ) ;
31
- let mut rx_config = ctx. get_config ( ) ;
32
- let mut config = rx_config. read ( ) ?;
25
+ impl TryFrom < & ModuleConfig > for DesktopNotifier {
26
+ type Error = ConfigError ;
33
27
34
- loop {
35
- tokio:: select! {
36
- r = shutdown. recv( ) => return r,
37
- _ = rx_config. changed( ) => {
38
- config = rx_config. read( ) ?;
39
- continue ;
40
- }
41
- msg = receiver. recv( ) => {
42
- handle_event( & config, msg?) . await ;
43
- }
44
- }
28
+ fn try_from ( config : & ModuleConfig ) -> Result < Self , Self :: Error > {
29
+ let user_id = config. with_default ( "user_id" , 1000 ) ?;
30
+ Ok ( Self {
31
+ user_id,
32
+ display : config. with_default ( "display" , ":0" . to_string ( ) ) ?,
33
+ notify_send_executable : config
34
+ . with_default ( "notify_send_executable" , "notify-send" . to_string ( ) ) ?,
35
+ bus_address : config
36
+ . with_default ( "bus_address" , format ! ( "unix:path=/run/user/{user_id}/bus" ) ) ?,
37
+ } )
38
+ }
39
+ }
40
+
41
+ #[ async_trait]
42
+ impl Module for DesktopNotifier {
43
+ fn start ( ) -> PulsarModule {
44
+ PulsarModule :: new (
45
+ MODULE_NAME ,
46
+ Version :: parse ( env ! ( "CARGO_PKG_VERSION" ) ) . unwrap ( ) ,
47
+ |ctx : & ModuleContext | {
48
+ let desktop_notifier: DesktopNotifier = ctx. get_config ( ) . read ( ) ?;
49
+ Ok ( desktop_notifier)
50
+ } ,
51
+ )
52
+ }
53
+
54
+ async fn on_event ( & mut self , event : & Event , _ctx : & ModuleContext ) -> Result < ( ) , ModuleError > {
55
+ handle_event ( self , event) . await ;
56
+ Ok ( ( ) )
57
+ }
58
+
59
+ fn on_change ( & mut self , ctx : & ModuleContext ) -> Result < ( ) , ModuleError > {
60
+ let desktop_notifier: DesktopNotifier = ctx. get_config ( ) . read ( ) ?;
61
+ * self = desktop_notifier;
62
+ Ok ( ( ) )
45
63
}
46
64
}
47
65
48
66
/// Check if the given event is a threat which should be notified to the user
49
- async fn handle_event ( config : & Config , event : Arc < Event > ) {
67
+ async fn handle_event ( config : & DesktopNotifier , event : & Event ) {
50
68
if let Some ( Threat {
51
69
source,
52
70
description,
@@ -61,7 +79,7 @@ async fn handle_event(config: &Config, event: Arc<Event>) {
61
79
}
62
80
63
81
/// Send a desktop notification spawning `notify-send` with the provided arguments
64
- async fn notify_send ( config : & Config , args : Vec < String > ) {
82
+ async fn notify_send ( config : & DesktopNotifier , args : Vec < String > ) {
65
83
let mut command = Command :: new ( & config. notify_send_executable ) ;
66
84
command
67
85
. args ( args)
@@ -96,27 +114,3 @@ async fn notify_send(config: &Config, args: Vec<String>) {
96
114
}
97
115
} ) ;
98
116
}
99
-
100
- #[ derive( Clone ) ]
101
- struct Config {
102
- user_id : u32 ,
103
- display : String ,
104
- notify_send_executable : String ,
105
- bus_address : String ,
106
- }
107
-
108
- impl TryFrom < & ModuleConfig > for Config {
109
- type Error = ConfigError ;
110
-
111
- fn try_from ( config : & ModuleConfig ) -> Result < Self , Self :: Error > {
112
- let user_id = config. with_default ( "user_id" , 1000 ) ?;
113
- Ok ( Self {
114
- user_id,
115
- display : config. with_default ( "display" , ":0" . to_string ( ) ) ?,
116
- notify_send_executable : config
117
- . with_default ( "notify_send_executable" , "notify-send" . to_string ( ) ) ?,
118
- bus_address : config
119
- . with_default ( "bus_address" , format ! ( "unix:path=/run/user/{user_id}/bus" ) ) ?,
120
- } )
121
- }
122
- }
0 commit comments