At a join, at the watcher's start and at a session's start while PORTAL, once per ask, the watcher opens the check address with systemd-run --user and xdg-open in the person's session and then shows the notification its outcome calls for, critical when the open failed; it says when the network is open, and handles what it hears during an open after it. Builds: design 08 sign-in-page §15, §18, §19, §22
244 lines
7.7 KiB
Go
244 lines
7.7 KiB
Go
package main
|
|
|
|
// Opening the page and telling the person (design 08 sign-in-page §15, §18, §19): at the three moments, once per ask,
|
|
// in the person's session. The open runs beside the watch; rating changes heard meanwhile are handled after it, in the
|
|
// order they came.
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"os/exec"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// openBound is how long the watcher waits for the opener: ended within it, its status decides; still running at it,
|
|
// the page is opening and the run is left to go on (§18).
|
|
const openBound = 10 * time.Second
|
|
|
|
// desk is the person's desktop as the watcher reaches it: an address opened in their session, a notification shown.
|
|
type desk interface {
|
|
open(s Session, address string) openOutcome
|
|
notify(s Session, n note) error
|
|
}
|
|
|
|
// openOutcome is how an open ended: opened, opening (still running at its bound) or failed, with the program and the
|
|
// reason of a failure.
|
|
type openOutcome struct {
|
|
Address string `json:"address"`
|
|
Program string `json:"program,omitempty"`
|
|
Outcome string `json:"outcome"`
|
|
Reason string `json:"reason,omitempty"`
|
|
At time.Time `json:"at"`
|
|
}
|
|
|
|
// The outcomes of an open.
|
|
const (
|
|
outcomeOpened = "opened"
|
|
outcomeOpening = "opening"
|
|
outcomeFailed = "failed"
|
|
)
|
|
|
|
// note is one notification: its summary and body, critical or not, and, once shown, when and whether it failed.
|
|
type note struct {
|
|
Summary string `json:"summary"`
|
|
Body string `json:"body"`
|
|
Critical bool `json:"critical,omitempty"`
|
|
At time.Time `json:"at"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
const askedSummary = "This network asks you to sign in"
|
|
|
|
// signInNote is what the person is told after an open, by its outcome (§19).
|
|
func signInNote(network, address string, o openOutcome) note {
|
|
switch o.Outcome {
|
|
case outcomeOpened:
|
|
return note{Summary: askedSummary, Body: fmt.Sprintf("%s: its sign-in page is open in %s.", network, o.Program)}
|
|
case outcomeOpening:
|
|
return note{Summary: askedSummary, Body: fmt.Sprintf("%s: its sign-in page is opening in %s.", network, o.Program)}
|
|
}
|
|
return note{Summary: askedSummary, Critical: true,
|
|
Body: fmt.Sprintf("%s: the page did not open (%s); open %s in a browser.", network, o.Reason, address)}
|
|
}
|
|
|
|
// signedInNote is what the person is told when the rating leaves PORTAL for FULL (§19).
|
|
func signedInNote(network string) note {
|
|
return note{Summary: "The network is open", Body: network + ": signed in."}
|
|
}
|
|
|
|
// present opens the address in the person's session, s or the one found now, and then tells them (§15, §18, §19),
|
|
// beside the watch; with no session found it opens and shows nothing, and the events are kept all the same (§15,
|
|
// §16).
|
|
func (w *Watcher) present(s *Session, network, address string) {
|
|
if w.opening {
|
|
w.later = append(w.later, func() { w.present(s, network, address) })
|
|
return
|
|
}
|
|
if s = w.sessionNow(s, "the sign-in page of "+network+" is not opened"); s == nil {
|
|
return
|
|
}
|
|
session := *s
|
|
w.opening = true
|
|
w.Beside(func() func() {
|
|
o := w.Desk.open(session, address)
|
|
n := signInNote(network, address, o)
|
|
err := w.Desk.notify(session, n)
|
|
return func() {
|
|
o.At = w.Now()
|
|
w.state.LastOpen = &o
|
|
if o.Outcome == outcomeFailed {
|
|
journal("opening %s in session %s failed: %s", address, session.ID, o.Reason)
|
|
} else {
|
|
journal("the sign-in page %s is %s in %s", address, o.Outcome, o.Program)
|
|
}
|
|
w.shown(n, err)
|
|
w.opening = false
|
|
w.drain()
|
|
}
|
|
})
|
|
}
|
|
|
|
// tellSignedIn tells the person the network is open, in their session; with none, nothing (§19).
|
|
func (w *Watcher) tellSignedIn(network string) {
|
|
s := w.sessionNow(nil, "the network being open is not shown")
|
|
if s == nil {
|
|
return
|
|
}
|
|
session, n := *s, signedInNote(network)
|
|
w.Beside(func() func() {
|
|
err := w.Desk.notify(session, n)
|
|
return func() { w.shown(n, err) }
|
|
})
|
|
}
|
|
|
|
// sessionNow is s, or the person's session found now; nil with a journal line saying what is not done.
|
|
func (w *Watcher) sessionNow(s *Session, notDone string) *Session {
|
|
if s != nil {
|
|
return s
|
|
}
|
|
found, err := w.findSession()
|
|
if err != nil {
|
|
journal("finding the person's session failed: %v; %s", err, notDone)
|
|
return nil
|
|
}
|
|
if found == nil {
|
|
journal("no graphical session: %s", notDone)
|
|
}
|
|
return found
|
|
}
|
|
|
|
func (w *Watcher) shown(n note, err error) {
|
|
n.At = w.Now()
|
|
if err != nil {
|
|
n.Error = err.Error()
|
|
journal("showing the notification %q failed: %v", n.Summary, err)
|
|
} else {
|
|
journal("showed the notification %q: %s", n.Summary, n.Body)
|
|
}
|
|
w.state.LastNotification = &n
|
|
}
|
|
|
|
// drain handles, in the order they came, what was heard during an open, until one starts another open.
|
|
func (w *Watcher) drain() {
|
|
for len(w.later) > 0 && !w.opening {
|
|
next := w.later[0]
|
|
w.later = w.later[1:]
|
|
next()
|
|
}
|
|
}
|
|
|
|
// besideLoop runs work beside the loop and hands its end back to the loop, which runs it.
|
|
func (w *Watcher) besideLoop(work func() func()) {
|
|
go func() { w.ends <- work() }()
|
|
}
|
|
|
|
// sessionDesk opens and tells in the person's session: the programs run with the session's bus and runtime directory
|
|
// in their environment.
|
|
type sessionDesk struct {
|
|
// query runs a program to its end, within openBound, and answers its output.
|
|
query func(env []string, name string, args ...string) (string, error)
|
|
// start starts a program; its end, with its status, comes on the channel.
|
|
start func(env []string, name string, args ...string) (<-chan error, error)
|
|
after func(time.Duration) <-chan time.Time
|
|
show func(bus string, n note) error
|
|
}
|
|
|
|
func newSessionDesk() sessionDesk {
|
|
return sessionDesk{query: queryProgram, start: startProgram, after: time.After, show: notifyOn}
|
|
}
|
|
|
|
// open asks xdg-mime for the scheme's program, then runs `systemd-run --user --collect --quiet --wait -- xdg-open
|
|
// <address>`, the address one argument and no shell, so the browser runs under the person's user manager and outlives
|
|
// the watcher (§18).
|
|
func (d sessionDesk) open(s Session, address string) openOutcome {
|
|
o := openOutcome{Address: address, Outcome: outcomeFailed}
|
|
u, err := url.Parse(address)
|
|
if err != nil {
|
|
o.Reason = err.Error()
|
|
return o
|
|
}
|
|
program, err := d.query(s.Env(), "xdg-mime", "query", "default", "x-scheme-handler/"+u.Scheme)
|
|
if err != nil {
|
|
o.Reason = "xdg-mime: " + err.Error()
|
|
return o
|
|
}
|
|
if program = strings.TrimSpace(program); program == "" {
|
|
o.Reason = "no default program for " + u.Scheme
|
|
return o
|
|
}
|
|
done, err := d.start(s.Env(), "systemd-run", "--user", "--collect", "--quiet", "--wait", "--", "xdg-open", address)
|
|
if err != nil {
|
|
o.Reason = "systemd-run: " + err.Error()
|
|
return o
|
|
}
|
|
o.Program = program
|
|
select {
|
|
case err := <-done:
|
|
if err != nil {
|
|
o.Program, o.Reason = "", "xdg-open "+status(err)
|
|
return o
|
|
}
|
|
o.Outcome = outcomeOpened
|
|
case <-d.after(openBound):
|
|
o.Outcome = outcomeOpening
|
|
}
|
|
return o
|
|
}
|
|
|
|
func (d sessionDesk) notify(s Session, n note) error { return d.show(s.Bus(), n) }
|
|
|
|
// status names how a program ended: its exit status, or why it did not end with one.
|
|
func status(err error) string {
|
|
var exit *exec.ExitError
|
|
if errors.As(err, &exit) {
|
|
return fmt.Sprintf("ended with status %d", exit.ExitCode())
|
|
}
|
|
return "failed: " + err.Error()
|
|
}
|
|
|
|
func queryProgram(env []string, name string, args ...string) (string, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), openBound)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, name, args...)
|
|
cmd.Env = env
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
return "", errors.New(status(err))
|
|
}
|
|
return string(out), nil
|
|
}
|
|
|
|
func startProgram(env []string, name string, args ...string) (<-chan error, error) {
|
|
cmd := exec.Command(name, args...)
|
|
cmd.Env = env
|
|
if err := cmd.Start(); err != nil {
|
|
return nil, err
|
|
}
|
|
done := make(chan error, 1)
|
|
go func() { done <- cmd.Wait() }()
|
|
return done, nil
|
|
}
|