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) }