From patchwork Wed Jul 19 11:20:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Eckert X-Patchwork-Id: 1809837 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=zch13rTr; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R5YMp4YkLz20Cs for ; Wed, 19 Jul 2023 21:23:00 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=ZZn5MryZks1J9RXTtwq0q/JAb1j0hiN1OV8XILrgJvk=; b=zch13rTr6FWMzG bjEO9OABS9P10HWAxXozn1Cxgqic/NUUlwD23A6eh7Pi6ETVcS5vLWdxeutnauLgUULBES6W1jhqQ qfGxi/z76ue6EM0DTnecRNKMzLJE/UNnivVqkHoBrreA9ZdpVpXxbinUuCVmJ9SQYeRb385/BWFwk CQ63MfQJ/MtglCITd/V8zcrLnZCWBsBmJxTxvtDLCXISB5ZvJA8Ae13TvrOR5Kvu+IuKkWjntE+hl J/OYGIf8Pj7nalguUp6xekVJa8jSnrwYvgw1BzulJ2wL9ALP6k7NShikKnmXg2OLY2xVMpJQwRvOF tQYnfyJiHqxd3K4jZbMQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qM5EX-00773q-27; Wed, 19 Jul 2023 11:20:41 +0000 Received: from mxout70.expurgate.net ([91.198.224.70]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qM5ET-0076mE-1C for openwrt-devel@lists.openwrt.org; Wed, 19 Jul 2023 11:20:39 +0000 Received: from [127.0.0.1] (helo=localhost) by relay.expurgate.net with smtp (Exim 4.92) (envelope-from ) id 1qM5EC-007cqW-Qh; Wed, 19 Jul 2023 13:20:20 +0200 Received: from [195.243.126.94] (helo=securemail.tdt.de) by relay.expurgate.net with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qM5EC-0045tj-5X; Wed, 19 Jul 2023 13:20:20 +0200 Received: from securemail.tdt.de (localhost [127.0.0.1]) by securemail.tdt.de (Postfix) with ESMTP id B46EE240049; Wed, 19 Jul 2023 13:20:19 +0200 (CEST) Received: from mail.dev.tdt.de (unknown [10.2.4.42]) by securemail.tdt.de (Postfix) with ESMTP id 60CE1240040; Wed, 19 Jul 2023 13:20:19 +0200 (CEST) Received: from localhost.localdomain (unknown [10.2.3.40]) by mail.dev.tdt.de (Postfix) with ESMTPSA id 9C8892EEA8; Wed, 19 Jul 2023 13:20:18 +0200 (CEST) From: Florian Eckert To: ansuelsmth@gmail.com Cc: openwrt-devel@lists.openwrt.org, Eckert.Florian@googlemail.com Subject: [PATCH] system-notification: initial checkin Date: Wed, 19 Jul 2023 13:20:14 +0200 Message-ID: <20230719112014.28110-1-fe@dev.tdt.de> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED,URIBL_ZEN_BLOCKED_OPENDNS autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.dev.tdt.de X-purgate-type: clean X-purgate-ID: 151534::1689765620-3F48EA8C-D68C0924/0/0 X-purgate: clean X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230719_042037_562887_D21D352E X-CRM114-Status: GOOD ( 19.83 ) X-Spam-Score: -0.7 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This package provides a ubus backend that can be used to save system notification. The notifications are not persistent. They are lost after every reboot. The following ubus methods are available: add: The command adds a notification to the ubus backend. ubus call system.notification add '{"title":"","description":""}' In addition, the following json string could be o [...] Content analysis details: (-0.7 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at https://www.dnswl.org/, low trust [91.198.224.70 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org This package provides a ubus backend that can be used to save system notification. The notifications are not persistent. They are lost after every reboot. The following ubus methods are available: add: The command adds a notification to the ubus backend. ubus call system.notification add '{"title":"","description":""}' In addition, the following json string could be optional set. * url: {"url": ""} Additional information are available at this extrenal URL. * opkg: {"package": ""} Package can be updated. remove: The command removes a notification from the ubus backend. ubus call system.notification remove '{"uuid": ""}' list: The command dumps all staged notification from the ubus backend. ubus call system.notification list flush: The command removes all stage notification from the ubus backend. ubus call system.notification flush Signed-off-by: Florian Eckert --- package/utils/system-notification/Makefile | 81 +++++++ .../files/usr/bin/system-notification | 105 +++++++++ .../usr/libexec/rpcd/system.notification | 220 ++++++++++++++++++ 3 files changed, 406 insertions(+) create mode 100644 package/utils/system-notification/Makefile create mode 100644 package/utils/system-notification/files/usr/bin/system-notification create mode 100644 package/utils/system-notification/files/usr/libexec/rpcd/system.notification diff --git a/package/utils/system-notification/Makefile b/package/utils/system-notification/Makefile new file mode 100644 index 0000000000..8dcbc190e6 --- /dev/null +++ b/package/utils/system-notification/Makefile @@ -0,0 +1,81 @@ +# +# Copyright (C) 2021 TDT AG +# +# This is free software, licensed under the GNU General Public License v2. +# See https://www.gnu.org/licenses/gpl-2.0.txt for more information. +# + +include $(TOPDIR)/rules.mk + +PKG_NAME:=system-notification +PKG_VERSION:=0.1 +PKG_RELEASE:=1 + +PKG_MAINTAINER:=Florian Eckert +PKG_LICENSE:=GPL-2.0 + +include $(INCLUDE_DIR)/package.mk + +define Package/system-notification + SECTION:=base + CATEGORY:=Base system + TITLE:=System notification +endef + +define Package/system-notification/description + This package provides a ubus backend that can be used to save system + notification. The notifications are not persistent. They are lost after + every reboot. + + The following ubus methods are available: + + add: + The command adds a notification to the ubus backend. + ubus call system.notification add '{"title":"","description":""}' + In addition, the following json string could be optional set. + * url: {"url": ""} Additional information are available at this extrenal URL. + * opkg: {"package": ""} Package can be updated. + + remove: + The command removes a notification from the ubus backend. + ubus call system.notification remove '{"uuid": ""}' + + list: + The command dumps all staged notification from the ubus backend. + ubus call system.notification list + + flush: + The command removes all stage notification from the ubus backend. + ubus call system.notification flush +endef + +define Build/Compile +endef + +define Package/system-notification/postinst +#!/bin/sh +if [ -z "$${IPKG_INSTROOT}" ] && [ -x /etc/init.d/rpcd ]; then + /etc/init.d/rpcd restart +fi +exit 0 +endef + +define Package/system-notification/postrm +#!/bin/sh +if [ -z "$${IPKG_INSTROOT}" ] && [ -x /etc/init.d/rpcd ]; then + /etc/init.d/rpcd restart +fi +exit 0 +endef + +define Package/system-notification/install + $(INSTALL_DIR) $(1)/usr/bin + $(INSTALL_BIN) ./files/usr/bin/system-notification \ + $(1)/usr/bin/system-notification + + $(INSTALL_DIR) $(1)/usr/libexec/rpcd + $(INSTALL_BIN) ./files/usr/libexec/rpcd/system.notification \ + $(1)/usr/libexec/rpcd/ +endef + +$(eval $(call BuildPackage,system-notification)) diff --git a/package/utils/system-notification/files/usr/bin/system-notification b/package/utils/system-notification/files/usr/bin/system-notification new file mode 100644 index 0000000000..7a60985eaf --- /dev/null +++ b/package/utils/system-notification/files/usr/bin/system-notification @@ -0,0 +1,105 @@ +#!/bin/sh +# Copyright (C) 2021 TDT AG +# +# This is free software, licensed under the GNU General Public License v2. +# See https://www.gnu.org/licenses/gpl-2.0.txt for more information. +# + +. /usr/share/libubox/jshn.sh + +remove() { + local uuid="$1" + + json_init + json_add_string "uuid" "$uuid" + ubus call system.notification remove "$(json_dump)" + json_cleanup +} + +list() { + local title description uuid timestamp + + json_init + json_load "$(ubus call system.notification list)" + json_select "messages" 1>/dev/null 2>&1 + [ $? -eq 0 ] || { + echo "No notifications are available" + return + } + + json_get_keys notifications + for notification in $notifications; do + json_select "$notification" + json_get_vars title description uuid timestamp + + echo "$title" + echo "" + echo "$description" + + json_select "actions" 1>/dev/null 2>&1 + if [ $? -eq 0 ]; then + json_get_keys actions + for action in $actions; do + json_select "$action" + local action + json_get_var entry action + [ "$entry" = "link" ] && { + local url + json_get_var url url + echo "- Check $url for more information" + } + + [ "$entry" = "opkg" ] && { + local package + json_get_var package package + echo "- To upgrade, execute \"opkg upgrade $package\"" + } + json_select .. + done + json_select .. + fi + + local dstring + echo "" + dstring="$(date +"%Y-%m-%d %H:%M:%S" -d "@${timestamp}")" + echo "date: $dstring" + echo "uuid: $uuid" + echo "---" + json_select .. + done + json_select .. +} + +usage() { + local msg="$1" + + if [ -n "$msg" ]; then + echo "$msg" + echo "" + fi + + echo "Usage: $(basename "$0") " + echo "" + echo "commmand:" + echo "" + echo "remove : Remove notification" + echo "list : Show notification" +} + +main() { + cmd="$1" + uuid="$2" + + case "$cmd" in + remove) + remove "$uuid" + ;; + list) + list + ;; + *) + usage "Command not supported" + esac +} + +main "$@" diff --git a/package/utils/system-notification/files/usr/libexec/rpcd/system.notification b/package/utils/system-notification/files/usr/libexec/rpcd/system.notification new file mode 100644 index 0000000000..aa32b967a1 --- /dev/null +++ b/package/utils/system-notification/files/usr/libexec/rpcd/system.notification @@ -0,0 +1,220 @@ +#!/bin/sh +# +# Copyright (C) 2021 TDT AG +# +# This is free software, licensed under the GNU General Public License v2. +# See https://www.gnu.org/licenses/gpl-2.0.txt for more information. +# + +. /usr/share/libubox/jshn.sh + +MESSAGE_DIR="/tmp/notification/messages.d" +MESSAGE_MAX=20 + +get_uuid() { + local file="$1" + local uuid + + uuid="${file##*/}" + + echo "${uuid%*.json}" +} + +gen_uuid() { + cat /proc/sys/kernel/random/uuid +} + +list() { + json_init + + json_add_object "add" + json_add_string "title" + json_add_string "description" + json_add_string "url" + json_add_string "package" + json_close_object + + json_add_object "remove" + json_add_string "uuid" + json_close_object + + json_add_object "list" + json_close_object + + json_add_object "flush" + json_close_object + + json_dump +} + +notification_add() { + local title="$1" + local description="$2" + local url="$3" + local package="$4" + + local timestamp uuid + + local code=-1 + local msg="Unable to add notification" + + timestamp="$(date +%s)" + + uuid="$(gen_uuid)" + if [ -z "$uuid" ]; then + json_init + json_add_int "code" "-2" + json_add_string "message" "Unable to get uuid" + json_dump + return + fi + + json_init + json_add_string title "$title" + json_add_string description "$description" + json_add_int timestamp "$timestamp" + [ -z "$url" ] || json_add_string "url" "$url" + [ -z "$package" ] || json_add_string "package" "$package" + json_dump > "${MESSAGE_DIR}/${uuid}.json" + + if [ -f "${MESSAGE_DIR}/${uuid}.json" ]; then + code=0 + msg="Notification added" + fi + + local count=0 + for file in "${MESSAGE_DIR}"/*.json; do + count=$((count + 1)) + if [ "$count" -gt "$MESSAGE_MAX" ]; then + rm "${MESSAGE_DIR}/${file}" + fi + done + + json_init + json_add_int "code" "$code" + json_add_string "message" "$msg" + json_dump +} + +notification_remove() { + local uuid="$1" + + local code=-1 + local msg="Notification not found" + + + if [ -f "${MESSAGE_DIR}/${uuid}.json" ]; then + rm "${MESSAGE_DIR}/${uuid}.json" + code=0 + msg="Notication removed" + fi + + json_init + json_add_int "code" "$code" + json_add_string "message" "$msg" + json_dump +} + +notification_list() { + local uuid title description timestamp url package + + json_init + if [ -n "$(ls -A "$MESSAGE_DIR")" ]; then + json_add_array "messages" + for entry in "${MESSAGE_DIR}"/*.json; do + uuid="$(get_uuid "$entry")" + + json_set_namespace dump old_cb + json_load_file "$entry" + json_get_vars title description timestamp url package + json_cleanup + json_set_namespace "$old_cb" + + json_add_object "" + json_add_string uuid "$uuid" + json_add_string title "$title" + json_add_string description "$description" + json_add_int timestamp "$timestamp" + json_add_array actions + json_add_object + json_add_string action dismiss + json_close_object + [ -z "$url" ] || { + json_add_object + json_add_string action link + json_add_string url "$url" + json_close_object + } + [ -z "$package" ] || { + json_add_object + json_add_string action opkg + json_add_string package "$package" + json_close_object + } + json_close_array + json_close_object + done + json_close_array + fi + json_dump +} + +notification_flush() { + local code=0 + local msg="All notifications flushed" + + rm "$MESSAGE_DIR"/*.json + + json_init + json_add_int "code" "$code" + json_add_string "message" "$msg" + json_dump +} + +call() { + local method="$1" + + local title description url package uuid + + case "$method" in + add) + read -r input; + json_load "${input}" + json_get_var title title + json_get_var description description + json_get_var url url + json_get_var package package + notification_add "$title" "$description" "$url" "$package" + ;; + remove) + read -r input; + json_load "${input}" + json_get_var uuid uuid + notification_remove "$uuid" + ;; + list) + notification_list + ;; + flush) + notification_flush + ;; + esac +} + +main() { + local path="$1" + local method="$2" + + mkdir -p "${MESSAGE_DIR}" + + case "$path" in + list) + list + ;; + call) + call "$method" + ;; + esac +} + +main "$@"