-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f14cf1c
commit 526e1a6
Showing
12 changed files
with
218 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ | |
/.envrc | ||
/geteduroam-cli | ||
/geteduroam-gui | ||
/geteduroam-notifcheck | ||
/shell.nix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"time" | ||
|
||
"golang.org/x/exp/slog" | ||
|
||
"github.com/geteduroam/linux-app/internal/config" | ||
"github.com/geteduroam/linux-app/internal/log" | ||
"github.com/geteduroam/linux-app/internal/nm" | ||
"github.com/geteduroam/linux-app/internal/notification" | ||
) | ||
|
||
func sendnotif(notif string) error { | ||
_, err := exec.Command("notify-send", "geteduroam", notif).Output() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func main() { | ||
log.Initialize("geteduroam-notifcheck", false) | ||
cfg, err := config.Load() | ||
if err != nil { | ||
slog.Error("no previous state", "error", err) | ||
return | ||
} | ||
con, err := nm.PreviousCon(cfg.UUID) | ||
if err != nil { | ||
slog.Error("no connection with uuid", "uuid", cfg.UUID, "error", err) | ||
return | ||
} | ||
if con == nil { | ||
slog.Error("connection is nil") | ||
return | ||
} | ||
|
||
if cfg.Validity == nil { | ||
slog.Info("validity is nil") | ||
return | ||
} | ||
valid := *cfg.Validity | ||
now := time.Now() | ||
diff := valid.Sub(now) | ||
days := int(diff.Hours() / 24) | ||
|
||
var text string | ||
if days > 10 { | ||
slog.Info("It is still more than 10 days", "days", days) | ||
return | ||
} | ||
if days < 0 { | ||
text = "connection is expired" | ||
} | ||
if days == 0 { | ||
text = "connection expires today" | ||
} | ||
if days > 0 { | ||
text = fmt.Sprintf("connection expires in %d days", days) | ||
} | ||
msg := fmt.Sprintf("Your eduroam %s. Re-run geteduroam to renew the connection", text) | ||
err = notification.Send(msg) | ||
if err != nil { | ||
slog.Error("failed to send notification", "error", err) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package notification | ||
|
||
import ( | ||
"os/exec" | ||
|
||
"github.com/geteduroam/linux-app/internal/notification/systemd" | ||
) | ||
|
||
// Send sends a single notification with notify-send | ||
func Send(msg string) error { | ||
_, err := exec.Command("notify-send", "geteduroam", msg).Output() | ||
return err | ||
} | ||
|
||
// HasDaemonSupport returns whether or not notifications can be enabled globally | ||
func HasDaemonSupport() bool { | ||
// currently we only support systemd | ||
return systemd.HasDaemonSupport() | ||
} | ||
|
||
// EnableDaemon enables the notification using SystemD's user daemon | ||
func EnableDameon() error { | ||
// currently we only support systemd | ||
return systemd.EnableDaemon() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package systemd | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
|
||
"golang.org/x/exp/slog" | ||
) | ||
|
||
func hasSystemD() bool { | ||
if _, err := os.Stat("/run/systemd/system"); !os.IsNotExist(err) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func hasUnit(unit string) bool { | ||
_, err := exec.Command("systemctl", "--user", "list-unit-files", unit).Output() | ||
return err == nil | ||
} | ||
|
||
func hasUnitFiles() bool { | ||
if !hasUnit("geteduroam-notifs.service") { | ||
slog.Error("geteduroam-notifs.service is not installed anywhere") | ||
return false | ||
} | ||
if !hasUnit("geteduroam-notifs.timer") { | ||
slog.Error("geteduroam-notifs.timer is not installed anywhere") | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// HasDaemonSupport returns whether or not notifications can be enabled globally | ||
// This depends on if systemd is used and if the unit is ready to be enabled | ||
func HasDaemonSupport() bool { | ||
if !hasSystemD() { | ||
return false | ||
} | ||
if !hasUnitFiles() { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// EnableDaemon enables the notification daemon using systemctl commands | ||
func EnableDaemon() error { | ||
_, err := exec.Command("systemctl", "--user", "enable", "--now", "geteduroam-notifs.timer").Output() | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[Unit] | ||
Description=This service runs a geteduroam command that checks if the eduroam \ | ||
connection is about to expire and if so it shows a notification | ||
|
||
[Service] | ||
Type=oneshot | ||
ExecStart=/usr/bin/geteduroam-notifcheck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[Unit] | ||
Description=Run geteduroam-notifs.service daily | ||
|
||
[Timer] | ||
OnCalendar=*-*-* 12:00:00 | ||
Persistent=true | ||
|
||
[Install] | ||
WantedBy=timers.target |