]> git.proxmox.com Git - pve-cluster.git/blame - src/PVE/Notify.pm
fix #4937: notify: write configuration files in utf8 encoding
[pve-cluster.git] / src / PVE / Notify.pm
CommitLineData
d1606d46
LW
1package PVE::Notify;
2
3use strict;
4use warnings;
5
6use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_lock_file cfs_write_file);
c1b3b947 7use Proxmox::RS::Notify;
d1606d46
LW
8
9cfs_register_file(
10 'notifications.cfg',
11 \&parse_notification_config,
12 \&write_notification_config,
13);
14
15cfs_register_file(
16 'priv/notifications.cfg',
17 \&parse_notification_config,
18 \&write_notification_config,
19);
20
21my $mail_to_root_target = 'mail-to-root';
22
23sub parse_notification_config {
24 my ($filename, $raw) = @_;
25
26 $raw = '' if !defined($raw);
27 return $raw;
28}
29
30sub write_notification_config {
31 my ($filename, $config) = @_;
32 return $config;
33}
34
35sub lock_config {
36 my ($code, $timeout) = @_;
37
38 cfs_lock_file('notifications.cfg', $timeout, sub {
39 cfs_lock_file('priv/notifications.cfg', $timeout, $code);
40 die $@ if $@;
41 });
42 die $@ if $@;
43}
44
45sub read_config {
46 my $config = cfs_read_file('notifications.cfg');
47 my $priv_config = cfs_read_file('priv/notifications.cfg');
48
c1b3b947 49 my $notification_config = Proxmox::RS::Notify->parse_config($config, $priv_config);
d1606d46
LW
50
51 eval {
52 # This target should always be available...
53 $notification_config->add_sendmail_endpoint(
54 $mail_to_root_target,
55 undef,
56 ['root@pam'],
57 undef,
58 undef,
59 'Send mail to root@pam\'s email address'
60 );
61 };
62
63 return $notification_config;
64}
65
66sub write_config {
67 my ($notification_config) = @_;
68
69 eval {
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.
75
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);
79 };
80
81 my ($config, $priv_config) = $notification_config->write_config();
4a78dac0
LW
82 cfs_write_file('notifications.cfg', $config, 1);
83 cfs_write_file('priv/notifications.cfg', $priv_config, 1);
d1606d46
LW
84}
85
86sub default_target {
87 return $mail_to_root_target;
88}
89
90my $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);
94
95 # Augment properties with the source code location of the notify call
96 my $props_with_source = {
97 %$properties,
98 source => {
99 module => $module,
100 file => $file,
101 line => $line,
102 }
103 };
104
105 $config->send($target, $severity, $title, $message, $props_with_source);
106};
107
108sub notify {
109 my ($target, $severity, $title, $message, $properties, $config) = @_;
110 $send_notification->($target, $severity, $title, $message, $properties, $config);
111}
112
113sub info {
114 my ($target, $title, $message, $properties, $config) = @_;
115 $send_notification->($target, 'info', $title, $message, $properties, $config);
116}
117
118sub notice {
119 my ($target, $title, $message, $properties, $config) = @_;
120 $send_notification->($target, 'notice', $title, $message, $properties, $config);
121}
122
123sub warning {
124 my ($target, $title, $message, $properties, $config) = @_;
125 $send_notification->($target, 'warning', $title, $message, $properties, $config);
126}
127
128sub error {
129 my ($target, $title, $message, $properties, $config) = @_;
130 $send_notification->($target, 'error', $title, $message, $properties, $config);
131}
132
133sub check_may_use_target {
134 my ($target, $rpcenv) = @_;
135 my $user = $rpcenv->get_user();
136
137 my $config = read_config();
138 my $entities = $config->get_referenced_entities($target);
139
140 for my $entity (@$entities) {
141 $rpcenv->check($user, "/mapping/notification/$entity", [ 'Mapping.Use' ]);
142 }
143}
144
1451;