6 use PVE
::Cluster
qw(cfs_register_file cfs_read_file cfs_lock_file cfs_write_file);
7 use Proxmox
::RS
::Notify
;
11 \
&parse_notification_config
,
12 \
&write_notification_config
,
16 'priv/notifications.cfg',
17 \
&parse_notification_config
,
18 \
&write_notification_config
,
21 my $mail_to_root_target = 'mail-to-root';
23 sub parse_notification_config
{
24 my ($filename, $raw) = @_;
26 $raw = '' if !defined($raw);
30 sub write_notification_config
{
31 my ($filename, $config) = @_;
36 my ($code, $timeout) = @_;
38 cfs_lock_file
('notifications.cfg', $timeout, sub {
39 cfs_lock_file
('priv/notifications.cfg', $timeout, $code);
46 my $config = cfs_read_file
('notifications.cfg');
47 my $priv_config = cfs_read_file
('priv/notifications.cfg');
49 my $notification_config = Proxmox
::RS
::Notify-
>parse_config($config, $priv_config);
52 # This target should always be available...
53 $notification_config->add_sendmail_endpoint(
59 'Send mail to root@pam\'s email address
'
63 return $notification_config;
67 my ($notification_config) = @_;
70 # ... but don't persist it to the config
.
71 # Rationale: If it is in the config, the user might think
72 # that it can be changed by editing the configuration there.
73 # However, since we always add it in `read_config`, any changes
74 # will be implicitly overridden by the default.
76 # If users want's to change the configuration, they are supposed to
77 # create a new sendmail endpoint.
78 $notification_config->delete_sendmail_endpoint($mail_to_root_target);
81 my ($config, $priv_config) = $notification_config->write_config();
82 cfs_write_file
('notifications.cfg', $config);
83 cfs_write_file
('priv/notifications.cfg', $priv_config);
87 return $mail_to_root_target;
90 my $send_notification = sub {
91 my ($target, $severity, $title, $message, $properties, $config) = @_;
92 $config = read_config
() if !defined($config);
93 my ($module, $file, $line) = caller(1);
95 # Augment properties with the source code location of the notify call
96 my $props_with_source = {
105 $config->send($target, $severity, $title, $message, $props_with_source);
109 my ($target, $severity, $title, $message, $properties, $config) = @_;
110 $send_notification->($target, $severity, $title, $message, $properties, $config);
114 my ($target, $title, $message, $properties, $config) = @_;
115 $send_notification->($target, 'info', $title, $message, $properties, $config);
119 my ($target, $title, $message, $properties, $config) = @_;
120 $send_notification->($target, 'notice', $title, $message, $properties, $config);
124 my ($target, $title, $message, $properties, $config) = @_;
125 $send_notification->($target, 'warning', $title, $message, $properties, $config);
129 my ($target, $title, $message, $properties, $config) = @_;
130 $send_notification->($target, 'error', $title, $message, $properties, $config);
133 sub check_may_use_target
{
134 my ($target, $rpcenv) = @_;
135 my $user = $rpcenv->get_user();
137 my $config = read_config
();
138 my $entities = $config->get_referenced_entities($target);
140 for my $entity (@$entities) {
141 $rpcenv->check($user, "/mapping/notification/$entity", [ 'Mapping.Use' ]);