package modules import ( "bytes" "context" "encoding/json" "errors" "fmt" "io" "strings" "time" "uberbringer/internal/pkg/ntfy" "uberbringer/internal/pkg/receipt" ) // Ntfy returns the ntfy module: the card of one ntfy message, as the ntfy // command line client passes it to a command in $NTFY_RAW. func Ntfy() Module { return ntfyModule{} } type ntfyModule struct{} func (ntfyModule) Name() string { return "ntfy" } func (ntfyModule) Description() string { return "One ntfy message object, e.g. the CLI's $NTFY_RAW" } // Build lays out the message as a card. The body is decoded leniently: // fields it does not know are ignored, so that messages from newer ntfy // versions still print. Events other than message, and messages with a // binary body, give ErrSkip. func (ntfyModule) Build(_ context.Context, body []byte, env Env) (receipt.Job, error) { m, err := decodeNtfy(body) if err != nil { return receipt.Job{}, err } n, err := FromNtfy(m) if err != nil { return receipt.Job{}, err } return noticeJob(n, env) } // decodeNtfy decodes body, one JSON object, as an ntfy message, ignoring // unknown fields. A problem is returned as a *PayloadError. func decodeNtfy(body []byte) (ntfy.Message, error) { var ( object map[string]json.RawMessage m ntfy.Message ) dec := json.NewDecoder(bytes.NewReader(body)) if err := dec.Decode(&object); err != nil { return m, &PayloadError{Problems: []string{bodyProblem(err)}} } if object == nil { return m, &PayloadError{Problems: []string{"body: expected a JSON object, got null"}} } if _, err := dec.Token(); !errors.Is(err, io.EOF) { return m, &PayloadError{Problems: []string{"body: trailing data after the payload"}} } if err := json.Unmarshal(body, &m); err != nil { var typeErr *json.UnmarshalTypeError if errors.As(err, &typeErr) && typeErr.Field != "" { return m, &PayloadError{Problems: []string{fmt.Sprintf("%s: expected %s", typeErr.Field, describe(typeErr.Type))}} } return m, &PayloadError{Problems: []string{"body: " + err.Error()}} } return m, nil } // FromNtfy turns an ntfy message into a Notice. It returns ErrSkip for an // event other than message, such as open or keepalive, and for a message // whose body is binary (encoding base64). A message without an event // counts as a message. // // The links are the click URL and the view actions' URLs; the other // actions are left out, since their headers, bodies and values can hold // secrets. The text ntfy gives a file sent without a message is dropped, // since the file prints on its own. The notice is never marked as an // update: a publisher may choose a message's sequence ID, so one message // cannot tell the first of its sequence from one that replaces it. func FromNtfy(m ntfy.Message) (Notice, error) { switch m.Event { case "", ntfy.EventMessage: default: return Notice{}, ErrSkip } if strings.EqualFold(m.Encoding, ntfy.EncodingBase64) { return Notice{}, ErrSkip } n := Notice{ ID: m.ID, Topic: m.Topic, Title: m.Title, Message: m.Message, Markdown: strings.EqualFold(m.ContentType, ntfy.ContentTypeMarkdown), Priority: m.Priority(), Tags: m.Tags, } if m.Time > 0 { n.Time = time.Unix(m.Time, 0) } if m.Click != "" { n.Links = append(n.Links, Link{URL: m.Click}) } for _, a := range m.Actions { if strings.EqualFold(a.Action, ntfy.ActionView) { n.Links = append(n.Links, Link{Label: a.Label, URL: a.URL}) } } if a := m.Attachment; a != nil && a.URL != "" { if n.Message == fmt.Sprintf(ntfy.AttachmentMessage, a.Name) { n.Message = "" } n.File = &File{Name: a.Name, Type: a.Type, Size: a.Size, URL: a.URL} if a.Expires > 0 { n.File.Expires = time.Unix(a.Expires, 0) } } return n, nil }