Add the notify and ntfy modules and scoped tokens
Breaking: a job with a raw element prints only with a token issued with uberbringer token --raw. Every other token, every existing one included, gets 403 token_scope for it, with the raw elements in details, and keeps every other route. The notify module prints a notification card from a title, message, priority and tags, with topic, click, up to three actions, image, markdown and time as options; it decodes strictly and refuses a payload over its length caps. The ntfy module takes one ntfy message as ntfy sends it, such as the ntfy client's $NTFY_RAW, ignores fields it does not know, cuts text over the caps, and answers 204 for other events and binary bodies. Both print every message plain: one message cannot tell the first of a sequence from a replacement, since a publisher may choose the sequence ID. internal/pkg/ntfy holds ntfy's message types. Both print the same card. The priority picks the form: a dark URGENT rule and a double box, a double box headed HIGH, a single box with the title in its border, or a rule with no box. Tags print as markers in front of the title, the rest on a Tags row; links print as QR codes with their labels; attached PNG, JPEG, GIF and WebP images print after the card. A line-level markdown subset is laid out without a dependency, keeping marks inside code spans and URLs, and emoji are removed after a few typed ones become markers. A card that cannot be laid out prints as a plain receipt, and the server logs the fault once at ERROR and reports it to Sentry once. [ntfy.format] and [ntfy.markers] shape the cards; the markers table is a setting with the user's own keys, also settable as UBERBRINGER_NTFY_MARKERS=name=value,... uberbringer token --modules issues a token limited to those modules' print and preview routes, job status and the module list; each route in the table says which tokens it takes, and a scope claim that could read as absent makes the token invalid. image elements take optional, which also shrinks a too-wide image to the paper; a QR code that could be too wide for the paper prints with smaller modules; box takes title_bold. Submit takes a Meta whose source shows in job status and, with a copy of the request ID, in every worker log line of the job. The four ntfy cards passed on the TM-T88IV, posted as the client recipe posts them, and their bytes are pinned in TestPaperReceipts. docs/api.md describes the modules, the card and the client recipe, with tokens read from a header file; docs/swagger.json is regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fa15cb27b1
commit
1335ed0687
119 files changed
+5626
-468
No files matched your search
@@ -0,0 +1,171 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Priorities of a Notice.
|
||||
const (
|
||||
minPriority = 1
|
||||
defaultPriority = 3
|
||||
highPriority = 4
|
||||
maxPriority = 5
|
||||
)
|
||||
|
||||
// Notice limits. notify refuses a payload over them, and a card cuts what
|
||||
// is longer, so that a notification from any source prints at a bounded
|
||||
// length. Lengths are in characters, except where they say bytes.
|
||||
const (
|
||||
MaxTitle = 250
|
||||
MaxTopic = 64 // as ntfy's topic names
|
||||
MaxTags = 20
|
||||
MaxTag = 64
|
||||
MaxLabel = 64
|
||||
MaxFileName = 128
|
||||
MaxMessage = 4096 // bytes, as ntfy's default message limit
|
||||
MaxURL = 2000 // bytes
|
||||
)
|
||||
|
||||
// priorityNames are the names a priority may be given by, as ntfy takes
|
||||
// them.
|
||||
var priorityNames = map[string]int{
|
||||
"min": 1,
|
||||
"low": 2,
|
||||
"default": 3,
|
||||
"high": 4,
|
||||
"max": 5,
|
||||
"urgent": 5,
|
||||
}
|
||||
|
||||
// Notice is a notification to print: what the notify module takes, and
|
||||
// what an ntfy message carries.
|
||||
type Notice struct {
|
||||
// ID is the notification's ID at its source, if it has one.
|
||||
ID string
|
||||
|
||||
Topic string
|
||||
Title string
|
||||
Message string
|
||||
|
||||
// Markdown is set when Message is written in markdown.
|
||||
Markdown bool
|
||||
|
||||
// Priority is 1-5; any other value counts as 3.
|
||||
Priority int
|
||||
Tags []string
|
||||
|
||||
// Time is when the notification was sent. The zero time means the
|
||||
// time the card is built.
|
||||
Time time.Time
|
||||
|
||||
// Links are the URLs the notification points to, in order.
|
||||
Links []Link
|
||||
|
||||
// Image is the URL of an image to print.
|
||||
Image string
|
||||
|
||||
// File is an attached file. An image the printer can print is printed;
|
||||
// any other file is named.
|
||||
File *File
|
||||
|
||||
// Updated is set when the notification is known to replace one printed
|
||||
// before; its title is then marked "(updated)". One message on its own
|
||||
// cannot tell, so the notify and ntfy modules never set it.
|
||||
Updated bool
|
||||
}
|
||||
|
||||
// Link is a URL a notification points to, with its label if it has one.
|
||||
type Link struct {
|
||||
Label string
|
||||
URL string
|
||||
}
|
||||
|
||||
// File is a file attached to a notification.
|
||||
type File struct {
|
||||
Name string
|
||||
|
||||
// Type is the media type, e.g. image/png; it may be empty.
|
||||
Type string
|
||||
|
||||
// Size is in bytes; 0 when unknown.
|
||||
Size int64
|
||||
|
||||
URL string
|
||||
|
||||
// Expires is when the file stops being available; the zero time means
|
||||
// never.
|
||||
Expires time.Time
|
||||
}
|
||||
|
||||
// priority returns n's priority, with a value outside 1-5 counted as the
|
||||
// default.
|
||||
func (n Notice) priority() int {
|
||||
if n.Priority < minPriority || n.Priority > maxPriority {
|
||||
return defaultPriority
|
||||
}
|
||||
return n.Priority
|
||||
}
|
||||
|
||||
// printableImageTypes are the media types the renderer decodes, and
|
||||
// printableImageExts the file name extensions it takes as them.
|
||||
var (
|
||||
printableImageTypes = []string{"image/png", "image/jpeg", "image/gif", "image/webp"}
|
||||
printableImageExts = []string{".png", ".jpg", ".jpeg", ".gif", ".webp"}
|
||||
)
|
||||
|
||||
// printableImage reports whether f is an image the renderer can print:
|
||||
// its type says so, or it has no type and its name or URL ends in an image
|
||||
// extension.
|
||||
func (f File) printableImage() bool {
|
||||
if f.Type != "" {
|
||||
media, _, _ := strings.Cut(strings.ToLower(f.Type), ";")
|
||||
return containsFold(printableImageTypes, strings.TrimSpace(media))
|
||||
}
|
||||
for _, name := range []string{f.Name, urlPath(f.URL)} {
|
||||
if containsFold(printableImageExts, path.Ext(name)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// expired reports whether f is no longer available at now.
|
||||
func (f File) expired(now time.Time) bool {
|
||||
return !f.Expires.IsZero() && !now.Before(f.Expires)
|
||||
}
|
||||
|
||||
// webURL reports whether s is an absolute http or https URL.
|
||||
func webURL(s string) bool {
|
||||
u, err := url.Parse(s)
|
||||
return err == nil && (u.Scheme == "http" || u.Scheme == "https") && u.Host != ""
|
||||
}
|
||||
|
||||
// urlPath returns the path of the URL s, or "" if it is not one.
|
||||
func urlPath(s string) string {
|
||||
u, err := url.Parse(s)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return u.Path
|
||||
}
|
||||
|
||||
// shortURL is the host and path of the URL s, without a trailing slash,
|
||||
// e.g. grafana.lan/d/disk.
|
||||
func shortURL(s string) string {
|
||||
u, err := url.Parse(s)
|
||||
if err != nil || u.Host == "" {
|
||||
return s
|
||||
}
|
||||
return strings.TrimSuffix(u.Host+u.Path, "/")
|
||||
}
|
||||
|
||||
// fileName is the last path segment of the URL s, or its host.
|
||||
func fileName(s string) string {
|
||||
if name := path.Base(urlPath(s)); name != "." && name != "/" && name != "" {
|
||||
return name
|
||||
}
|
||||
return shortURL(s)
|
||||
}
|
||||
Reference in new issue
Block a user