Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions emhttp/plugins/dynamix.plugin.manager/scripts/PluginAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,19 @@ function download_url($url, $path = "") {
$existing = (array)@file("/tmp/reboot_notifications",FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$existing[] = $message;
file_put_contents("/tmp/reboot_notifications",implode("\n",array_unique($existing)));
// Surface "reboot required" as a persistent notification (sticky in the bell,
// keyed so repeated notices update rather than stack). It clears on reboot
// (RAM-backed /tmp is wiped) or via removeRebootNotice below.
exec("/usr/local/emhttp/webGui/scripts/notify add -p -k reboot-required -i alert -e ".escapeshellarg("Reboot required")." -s ".escapeshellarg("Reboot required")." -d ".escapeshellarg($message)." &>/dev/null");
break;

case 'removeRebootNotice':
$message = htmlspecialchars(trim($_POST['message']));
$existing = file_get_contents("/tmp/reboot_notifications");
$newReboots = str_replace($message,"",$existing);
file_put_contents("/tmp/reboot_notifications",$newReboots);
// Once no reboot reasons remain, resolve the persistent notification.
if (trim($newReboots)==="") exec("/usr/local/emhttp/webGui/scripts/notify clear -k reboot-required &>/dev/null");
break;
}
?>
43 changes: 38 additions & 5 deletions emhttp/plugins/dynamix/scripts/notify
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,25 @@ require_once "$docroot/webGui/include/Encryption.php";

function usage() {
echo <<<EOT
notify [-e "event"] [-s "subject"] [-d "description"] [-i "normal|warning|alert"] [-m "message"] [-x] [-t] [-b] [add]
notify [-e "event"] [-s "subject"] [-d "description"] [-i "normal|warning|alert"] [-m "message"] [-k "key"] [-p] [-x] [-t] [-b] [add]
create a notification
use -e to specify the event
use -s to specify a subject
use -d to specify a short description
use -i to specify the severity
use -m to specify a message (long description)
use -l to specify a link (clicking the notification will take you to that location)
use -k to specify a stable key; raising the same key again replaces the notification (idempotent)
use -p to make the notification persistent (sticky in the bell, not user-dismissible; clear it with 'notify clear -k key')
use -x to create a single notification ticket
use -r to specify recipients and not use default
use -t to force send email only (for testing)
use -b to NOT send a browser notification
all options are optional

notify clear -k "key"
Resolve a keyed (typically persistent) notification, removing it from the bell.

notify init
Initialize the notification subsystem.

Expand Down Expand Up @@ -209,8 +214,10 @@ case 'add':
$mailtest = false;
$overrule = false;
$noBrowser = false;
$key = '';
$persistent = false;

$options = getopt("l:e:s:d:i:m:r:xtb");
$options = getopt("l:e:s:d:i:m:r:k:xtbp");
foreach ($options as $option => $value) {
switch ($option) {
case 'e':
Expand Down Expand Up @@ -241,6 +248,12 @@ case 'add':
case 'b':
$noBrowser = true;
break;
case 'k':
$key = $value;
break;
case 'p':
$persistent = true;
break;
case 'l':
$nginx = (array)@parse_ini_file('/var/local/emhttp/nginx.ini');
$link = $value;
Expand All @@ -249,8 +262,11 @@ case 'add':
}
}

$unread = "{$unread}/".safe_filename("{$event}-{$ticket}.notify");
$archive = "{$archive}/".safe_filename("{$event}-{$ticket}.notify");
// Condition-style notifications use a stable key for the filename so raising the
// same condition again overwrites it (idempotent) instead of stacking duplicates.
$idBase = $key !== '' ? $key : "{$event}-{$ticket}";
$unread = "{$unread}/".safe_filename("{$idBase}.notify");
$archive = "{$archive}/".safe_filename("{$idBase}.notify");
if (file_exists($archive)) break;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
$entity = $overrule===false ? $notify[$importance] : $overrule;
$cleanSubject = clean_subject($subject);
Expand All @@ -262,7 +278,9 @@ case 'add':
'importance' => $importance,
];
if ($message) $archiveData['message'] = str_replace('\n','<br>',$message);
if (!$mailtest) file_put_contents($archive, build_ini_string($archiveData));
// Persistent (condition-style) notifications are never archived - they clear when
// their condition resolves - so skip writing the archive copy for them.
if (!$mailtest && !$persistent) file_put_contents($archive, build_ini_string($archiveData));
if (($entity & 1)==1 && !$mailtest && !$noBrowser) {
$unreadData = [
'timestamp' => $timestamp,
Expand All @@ -272,6 +290,8 @@ case 'add':
'importance' => $importance,
'link' => $link,
];
if ($key !== '') $unreadData['key'] = $key;
if ($persistent) $unreadData['persistent'] = 'true';
file_put_contents($unread, build_ini_string($unreadData));
}
if (($entity & 2)==2 || $mailtest) generate_email($event, $cleanSubject, str_replace('<br>','. ',$description), $importance, $message, $recipients, $fqdnlink);
Expand Down Expand Up @@ -308,6 +328,19 @@ case 'archive':
$file = $argv[2];
if (strpos(realpath("$unread/$file"),$unread.'/')===0) @unlink("$unread/$file");
break;

case 'clear':
// Resolve a condition-style notification by its stable key:
// notify clear -k <key> (or) notify clear <key>
$opt = getopt("k:");
$key = $opt['k'] ?? ($argv[2] ?? '');
if ($key === '' || $key === false) exit(usage());
$name = safe_filename("{$key}.notify");
foreach ([$unread, $archive] as $dir) {
$target = "$dir/$name";
if (strpos(realpath($target) ?: '', $dir.'/')===0) @unlink($target);
}
break;
}

exit(0);
Expand Down
Loading