123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- package server
- import (
- "encoding/json"
- "errors"
- "fmt"
- "heckel.io/ntfy/util"
- "regexp"
- "strings"
- "unicode/utf8"
- )
- const (
- actionIDLength = 10
- actionEOF = rune(0)
- actionsMax = 3
- )
- const (
- actionView = "view"
- actionBroadcast = "broadcast"
- actionHTTP = "http"
- )
- var (
- actionsAll = []string{actionView, actionBroadcast, actionHTTP}
- actionsWithURL = []string{actionView, actionHTTP}
- actionsKeyRegex = regexp.MustCompile(`^([-.\w]+)\s*=\s*`)
- )
- type actionParser struct {
- input string
- pos int
- }
- func parseActions(s string) (actions []*action, err error) {
-
- s = strings.TrimSpace(s)
- if strings.HasPrefix(s, "[") {
- actions, err = parseActionsFromJSON(s)
- } else {
- actions, err = parseActionsFromSimple(s)
- }
- if err != nil {
- return nil, err
- }
-
- for i := range actions {
- actions[i].ID = util.RandomString(actionIDLength)
- actions[i].Action = strings.ToLower(actions[i].Action)
- actions[i].Method = strings.ToUpper(actions[i].Method)
- }
-
- if len(actions) > actionsMax {
- return nil, fmt.Errorf("only %d actions allowed", actionsMax)
- }
- for _, action := range actions {
- if !util.InStringList(actionsAll, action.Action) {
- return nil, fmt.Errorf("parameter 'action' cannot be '%s', valid values are 'view', 'broadcast' and 'http'", action.Action)
- } else if action.Label == "" {
- return nil, fmt.Errorf("parameter 'label' is required")
- } else if util.InStringList(actionsWithURL, action.Action) && action.URL == "" {
- return nil, fmt.Errorf("parameter 'url' is required for action '%s'", action.Action)
- } else if action.Action == actionHTTP && util.InStringList([]string{"GET", "HEAD"}, action.Method) && action.Body != "" {
- return nil, fmt.Errorf("parameter 'body' cannot be set if method is %s", action.Method)
- }
- }
- return actions, nil
- }
- func parseActionsFromJSON(s string) ([]*action, error) {
- actions := make([]*action, 0)
- if err := json.Unmarshal([]byte(s), &actions); err != nil {
- return nil, fmt.Errorf("JSON error: %w", err)
- }
- return actions, nil
- }
- func parseActionsFromSimple(s string) ([]*action, error) {
- if !utf8.ValidString(s) {
- return nil, errors.New("invalid utf-8 string")
- }
- parser := &actionParser{
- pos: 0,
- input: s,
- }
- return parser.Parse()
- }
- func (p *actionParser) Parse() ([]*action, error) {
- actions := make([]*action, 0)
- for !p.eof() {
- a, err := p.parseAction()
- if err != nil {
- return nil, err
- }
- actions = append(actions, a)
- }
- return actions, nil
- }
- func (p *actionParser) parseAction() (*action, error) {
- a := newAction()
- section := 0
- for {
- key, value, last, err := p.parseSection()
- if err != nil {
- return nil, err
- }
- if err := populateAction(a, section, key, value); err != nil {
- return nil, err
- }
- p.slurpSpaces()
- if last {
- return a, nil
- }
- section++
- }
- }
- func populateAction(newAction *action, section int, key, value string) error {
-
- if key == "" && section == 0 {
- key = "action"
- } else if key == "" && section == 1 {
- key = "label"
- } else if key == "" && section == 2 && util.InStringList(actionsWithURL, newAction.Action) {
- key = "url"
- }
-
- if key == "" {
- return fmt.Errorf("term '%s' unknown", value)
- }
-
- if strings.HasPrefix(key, "headers.") {
- newAction.Headers[strings.TrimPrefix(key, "headers.")] = value
- } else if strings.HasPrefix(key, "extras.") {
- newAction.Extras[strings.TrimPrefix(key, "extras.")] = value
- } else {
- switch strings.ToLower(key) {
- case "action":
- newAction.Action = value
- case "label":
- newAction.Label = value
- case "clear":
- lvalue := strings.ToLower(value)
- if !util.InStringList([]string{"true", "yes", "1", "false", "no", "0"}, lvalue) {
- return fmt.Errorf("parameter 'clear' cannot be '%s', only boolean values are allowed (true/yes/1/false/no/0)", value)
- }
- newAction.Clear = lvalue == "true" || lvalue == "yes" || lvalue == "1"
- case "url":
- newAction.URL = value
- case "method":
- newAction.Method = value
- case "body":
- newAction.Body = value
- default:
- return fmt.Errorf("key '%s' unknown", key)
- }
- }
- return nil
- }
- func (p *actionParser) parseSection() (key string, value string, last bool, err error) {
- p.slurpSpaces()
- key = p.parseKey()
- r, w := p.peek()
- if isSectionEnd(r) {
- p.pos += w
- last = isLastSection(r)
- return
- } else if r == '"' || r == '\'' {
- value, last, err = p.parseQuotedValue(r)
- return
- }
- value, last = p.parseValue()
- return
- }
- func (p *actionParser) parseKey() string {
- matches := actionsKeyRegex.FindStringSubmatch(p.input[p.pos:])
- if len(matches) == 2 {
- p.pos += len(matches[0])
- return matches[1]
- }
- return ""
- }
- func (p *actionParser) parseValue() (value string, last bool) {
- start := p.pos
- for {
- r, w := p.peek()
- if isSectionEnd(r) {
- last = isLastSection(r)
- value = strings.TrimSpace(p.input[start:p.pos])
- p.pos += w
- return
- }
- p.pos += w
- }
- }
- func (p *actionParser) parseQuotedValue(quote rune) (value string, last bool, err error) {
- p.pos++
- start := p.pos
- var prev rune
- for {
- r, w := p.peek()
- if r == actionEOF {
- err = fmt.Errorf("unexpected end of input, quote started at position %d", start)
- return
- } else if r == quote && prev != '\\' {
- value = strings.ReplaceAll(p.input[start:p.pos], "\\"+string(quote), string(quote))
- p.pos += w
-
- p.slurpSpaces()
- r, w := p.peek()
- last = isLastSection(r)
- if !isSectionEnd(r) {
- err = fmt.Errorf("unexpected character '%c' at position %d", r, p.pos)
- return
- }
- p.pos += w
- return
- }
- prev = r
- p.pos += w
- }
- }
- func (p *actionParser) slurpSpaces() {
- for {
- r, w := p.peek()
- if r == actionEOF || !isSpace(r) {
- return
- }
- p.pos += w
- }
- }
- func (p *actionParser) peek() (rune, int) {
- if p.eof() {
- return actionEOF, 0
- }
- return utf8.DecodeRuneInString(p.input[p.pos:])
- }
- func (p *actionParser) eof() bool {
- return p.pos >= len(p.input)
- }
- func isSpace(r rune) bool {
- return r == ' ' || r == '\t' || r == '\r' || r == '\n'
- }
- func isSectionEnd(r rune) bool {
- return r == actionEOF || r == ';' || r == ','
- }
- func isLastSection(r rune) bool {
- return r == actionEOF || r == ';'
- }
|