forked from 89luca89/transactional-update-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.go
109 lines (91 loc) · 2.13 KB
/
daemon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// main package
package main
import (
"io"
"log"
"net"
"os"
"strconv"
"strings"
"time"
"github.com/godbus/dbus/v5"
)
var sockAddr = "/run/user/" +
strconv.Itoa(os.Geteuid()) +
"/transactionalupdatenotification.socket"
func notifySend(input string) {
success := strings.Split(input, ":")[1]
// Customize message based on success state
message := "Updates successfully installed"
submessage := "System has been upgraded, on " +
string(time.Now().Format(time.RFC1123)) +
" please reboot to take effect."
icon := "appointment-soon"
if strings.Compare(success, "failure") == 0 {
message = "Update process failed"
submessage = "An error was encountered while upgrading on " +
string(time.Now().Format(time.RFC1123))
icon = "appointment-missed"
}
conn, err := dbus.ConnectSessionBus()
if err != nil {
panic(err)
}
defer conn.Close()
obj := conn.Object(
"org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
)
call := obj.Call(
"org.freedesktop.Notifications.Notify",
0,
"",
uint32(0),
icon,
message,
submessage,
[]string{},
map[string]dbus.Variant{},
int32(5000),
)
if call.Err != nil {
panic(call.Err)
}
}
func handleMessage(connection net.Conn) {
log.Printf("Client connected [%s]", connection.RemoteAddr().Network())
inputBuffer := make([]byte, 1024)
data, err := connection.Read(inputBuffer)
if err != nil {
panic("Receiving error")
}
if strings.Contains(string(inputBuffer[:data]), Message) {
notifySend(string(inputBuffer[:data]))
}
_, err = io.Copy(connection, connection)
if err != nil {
panic("Receiving error")
}
connection.Close()
}
// NotifyDaemon is the user-facing running daemon that will be sending the graphical
// notifications.
func NotifyDaemon() {
if err := os.RemoveAll(sockAddr); err != nil {
log.Fatal(err)
}
listener, err := net.Listen("unix", sockAddr)
if err != nil {
log.Fatal("listen error:", err)
}
defer listener.Close()
for {
// Accept new connections, dispatching them to echoServer
// in a goroutine.
conn, err := listener.Accept()
if err != nil {
log.Println("accept error:", err)
}
go handleMessage(conn)
}
}