publish.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package cmd
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/urfave/cli/v2"
  6. "heckel.io/ntfy/client"
  7. "heckel.io/ntfy/log"
  8. "heckel.io/ntfy/util"
  9. "io"
  10. "os"
  11. "os/exec"
  12. "path/filepath"
  13. "regexp"
  14. "strings"
  15. "time"
  16. )
  17. func init() {
  18. commands = append(commands, cmdPublish, cmdDone)
  19. }
  20. var flagsPublish = append(
  21. flagsDefault,
  22. &cli.StringFlag{Name: "config", Aliases: []string{"c"}, EnvVars: []string{"NTFY_CONFIG"}, Usage: "client config file"},
  23. &cli.StringFlag{Name: "title", Aliases: []string{"t"}, EnvVars: []string{"NTFY_TITLE"}, Usage: "message title"},
  24. &cli.StringFlag{Name: "message", Aliases: []string{"m"}, EnvVars: []string{"NTFY_MESSAGE"}, Usage: "message body"},
  25. &cli.StringFlag{Name: "priority", Aliases: []string{"p"}, EnvVars: []string{"NTFY_PRIORITY"}, Usage: "priority of the message (1=min, 2=low, 3=default, 4=high, 5=max)"},
  26. &cli.StringFlag{Name: "tags", Aliases: []string{"tag", "T"}, EnvVars: []string{"NTFY_TAGS"}, Usage: "comma separated list of tags and emojis"},
  27. &cli.StringFlag{Name: "delay", Aliases: []string{"at", "in", "D"}, EnvVars: []string{"NTFY_DELAY"}, Usage: "delay/schedule message"},
  28. &cli.StringFlag{Name: "click", Aliases: []string{"U"}, EnvVars: []string{"NTFY_CLICK"}, Usage: "URL to open when notification is clicked"},
  29. &cli.StringFlag{Name: "actions", Aliases: []string{"A"}, EnvVars: []string{"NTFY_ACTIONS"}, Usage: "actions JSON array or simple definition"},
  30. &cli.StringFlag{Name: "attach", Aliases: []string{"a"}, EnvVars: []string{"NTFY_ATTACH"}, Usage: "URL to send as an external attachment"},
  31. &cli.StringFlag{Name: "filename", Aliases: []string{"name", "n"}, EnvVars: []string{"NTFY_FILENAME"}, Usage: "filename for the attachment"},
  32. &cli.StringFlag{Name: "file", Aliases: []string{"f"}, EnvVars: []string{"NTFY_FILE"}, Usage: "file to upload as an attachment"},
  33. &cli.StringFlag{Name: "email", Aliases: []string{"mail", "e"}, EnvVars: []string{"NTFY_EMAIL"}, Usage: "also send to e-mail address"},
  34. &cli.StringFlag{Name: "user", Aliases: []string{"u"}, EnvVars: []string{"NTFY_USER"}, Usage: "username[:password] used to auth against the server"},
  35. &cli.IntFlag{Name: "pid", Aliases: []string{"done", "w"}, EnvVars: []string{"NTFY_PID"}, Usage: "monitor process with given PID and publish when it exists"},
  36. &cli.BoolFlag{Name: "no-cache", Aliases: []string{"C"}, EnvVars: []string{"NTFY_NO_CACHE"}, Usage: "do not cache message server-side"},
  37. &cli.BoolFlag{Name: "no-firebase", Aliases: []string{"F"}, EnvVars: []string{"NTFY_NO_FIREBASE"}, Usage: "do not forward message to Firebase"},
  38. &cli.BoolFlag{Name: "env-topic", Aliases: []string{"P"}, EnvVars: []string{"NTFY_ENV_TOPIC"}, Usage: "use topic from NTFY_TOPIC env variable"},
  39. &cli.BoolFlag{Name: "quiet", Aliases: []string{"q"}, EnvVars: []string{"NTFY_QUIET"}, Usage: "do not print message"},
  40. )
  41. var cmdPublish = &cli.Command{
  42. Name: "publish",
  43. Aliases: []string{"pub", "send", "trigger"},
  44. Usage: "Send message via a ntfy server",
  45. UsageText: "ntfy publish [OPTIONS..] TOPIC [MESSAGE]\nNTFY_TOPIC=.. ntfy publish [OPTIONS..] -P [MESSAGE]",
  46. Action: execPublish,
  47. Category: categoryClient,
  48. Flags: flagsPublish,
  49. Before: initLogFunc,
  50. Description: `Publish a message to a ntfy server.
  51. Examples:
  52. ntfy publish mytopic This is my message # Send simple message
  53. ntfy send myserver.com/mytopic "This is my message" # Send message to different default host
  54. ntfy pub -p high backups "Backups failed" # Send high priority message
  55. ntfy pub --tags=warning,skull backups "Backups failed" # Add tags/emojis to message
  56. ntfy pub --delay=10s delayed_topic Laterzz # Delay message by 10s
  57. ntfy pub --at=8:30am delayed_topic Laterzz # Send message at 8:30am
  58. ntfy pub -e phil@example.com alerts 'App is down!' # Also send email to phil@example.com
  59. ntfy pub --click="https://reddit.com" redd 'New msg' # Opens Reddit when notification is clicked
  60. ntfy pub --attach="http://some.tld/file.zip" files # Send ZIP archive from URL as attachment
  61. ntfy pub --file=flower.jpg flowers 'Nice!' # Send image.jpg as attachment
  62. ntfy pub -u phil:mypass secret Psst # Publish with username/password
  63. NTFY_USER=phil:mypass ntfy pub secret Psst # Use env variables to set username/password
  64. NTFY_TOPIC=mytopic ntfy pub -P "some message"" # Use NTFY_TOPIC variable as topic
  65. cat flower.jpg | ntfy pub --file=- flowers 'Nice!' # Same as above, send image.jpg as attachment
  66. ntfy trigger mywebhook # Sending without message, useful for webhooks
  67. Please also check out the docs on publishing messages. Especially for the --tags and --delay options,
  68. it has incredibly useful information: https://ntfy.sh/docs/publish/.
  69. ` + clientCommandDescriptionSuffix,
  70. }
  71. var cmdDone = &cli.Command{
  72. Name: "done",
  73. Usage: "xxx",
  74. UsageText: "xxx",
  75. Action: execDone,
  76. Category: categoryClient,
  77. Flags: flagsPublish,
  78. Before: initLogFunc,
  79. Description: `xxx
  80. ` + clientCommandDescriptionSuffix,
  81. }
  82. func execDone(c *cli.Context) error {
  83. return execPublishInternal(c, true)
  84. }
  85. func execPublish(c *cli.Context) error {
  86. return execPublishInternal(c, false)
  87. }
  88. func parseTopicMessageCommand(c *cli.Context, isDoneCommand bool) (topic string, message string, command []string, err error) {
  89. // 1. ntfy done <topic> <command>
  90. // 2. ntfy done --pid <pid> <topic> [<message>]
  91. // 3. NTFY_TOPIC=.. ntfy done <command>
  92. // 4. NTFY_TOPIC=.. ntfy done --pid <pid> [<message>]
  93. // 5. ntfy publish <topic> [<message>]
  94. // 6. NTFY_TOPIC=.. ntfy publish [<message>]
  95. var args []string
  96. topic, args, err = parseTopicAndArgs(c)
  97. if err != nil {
  98. return
  99. }
  100. if isDoneCommand {
  101. if c.Int("pid") > 0 {
  102. message = strings.Join(args, " ")
  103. } else if len(args) > 0 {
  104. command = args
  105. } else {
  106. err = errors.New("must either specify --pid or a command")
  107. }
  108. } else {
  109. message = strings.Join(args, " ")
  110. }
  111. if c.String("message") != "" {
  112. message = c.String("message")
  113. }
  114. return
  115. }
  116. func parseTopicAndArgs(c *cli.Context) (topic string, args []string, err error) {
  117. envTopic := c.Bool("env-topic")
  118. if envTopic {
  119. topic = os.Getenv("NTFY_TOPIC")
  120. if topic == "" {
  121. return "", nil, errors.New("if --env-topic is passed, must define NTFY_TOPIC environment variable")
  122. }
  123. return topic, remainingArgs(c, 0), nil
  124. }
  125. if c.NArg() < 1 {
  126. return "", nil, errors.New("must specify topic")
  127. }
  128. return c.Args().Get(0), remainingArgs(c, 1), nil
  129. }
  130. func remainingArgs(c *cli.Context, fromIndex int) []string {
  131. if c.NArg() > fromIndex {
  132. return c.Args().Slice()[fromIndex:]
  133. }
  134. return []string{}
  135. }
  136. func execPublishInternal(c *cli.Context, doneCmd bool) error {
  137. conf, err := loadConfig(c)
  138. if err != nil {
  139. return err
  140. }
  141. title := c.String("title")
  142. priority := c.String("priority")
  143. tags := c.String("tags")
  144. delay := c.String("delay")
  145. click := c.String("click")
  146. actions := c.String("actions")
  147. attach := c.String("attach")
  148. filename := c.String("filename")
  149. file := c.String("file")
  150. email := c.String("email")
  151. user := c.String("user")
  152. noCache := c.Bool("no-cache")
  153. noFirebase := c.Bool("no-firebase")
  154. quiet := c.Bool("quiet")
  155. pid := c.Int("pid")
  156. topic, message, command, err := parseTopicMessageCommand(c, doneCmd)
  157. if err != nil {
  158. return err
  159. }
  160. var options []client.PublishOption
  161. if title != "" {
  162. options = append(options, client.WithTitle(title))
  163. }
  164. if priority != "" {
  165. options = append(options, client.WithPriority(priority))
  166. }
  167. if tags != "" {
  168. options = append(options, client.WithTagsList(tags))
  169. }
  170. if delay != "" {
  171. options = append(options, client.WithDelay(delay))
  172. }
  173. if click != "" {
  174. options = append(options, client.WithClick(click))
  175. }
  176. if actions != "" {
  177. options = append(options, client.WithActions(strings.ReplaceAll(actions, "\n", " ")))
  178. }
  179. if attach != "" {
  180. options = append(options, client.WithAttach(attach))
  181. }
  182. if filename != "" {
  183. options = append(options, client.WithFilename(filename))
  184. }
  185. if email != "" {
  186. options = append(options, client.WithEmail(email))
  187. }
  188. if noCache {
  189. options = append(options, client.WithNoCache())
  190. }
  191. if noFirebase {
  192. options = append(options, client.WithNoFirebase())
  193. }
  194. if user != "" {
  195. var pass string
  196. parts := strings.SplitN(user, ":", 2)
  197. if len(parts) == 2 {
  198. user = parts[0]
  199. pass = parts[1]
  200. } else {
  201. fmt.Fprint(c.App.ErrWriter, "Enter Password: ")
  202. p, err := util.ReadPassword(c.App.Reader)
  203. if err != nil {
  204. return err
  205. }
  206. pass = string(p)
  207. fmt.Fprintf(c.App.ErrWriter, "\r%s\r", strings.Repeat(" ", 20))
  208. }
  209. options = append(options, client.WithBasicAuth(user, pass))
  210. }
  211. if pid > 0 {
  212. if err := waitForProcess(pid); err != nil {
  213. return err
  214. }
  215. } else if len(command) > 0 {
  216. cmdResultMessage, err := runAndWaitForCommand(command)
  217. if err != nil {
  218. return err
  219. } else if message == "" {
  220. message = cmdResultMessage
  221. }
  222. }
  223. var body io.Reader
  224. if file == "" {
  225. body = strings.NewReader(message)
  226. } else {
  227. if message != "" {
  228. options = append(options, client.WithMessage(message))
  229. }
  230. if file == "-" {
  231. if filename == "" {
  232. options = append(options, client.WithFilename("stdin"))
  233. }
  234. body = c.App.Reader
  235. } else {
  236. if filename == "" {
  237. options = append(options, client.WithFilename(filepath.Base(file)))
  238. }
  239. body, err = os.Open(file)
  240. if err != nil {
  241. return err
  242. }
  243. }
  244. }
  245. cl := client.New(conf)
  246. m, err := cl.PublishReader(topic, body, options...)
  247. if err != nil {
  248. return err
  249. }
  250. if !quiet {
  251. fmt.Fprintln(c.App.Writer, strings.TrimSpace(m.Raw))
  252. }
  253. return nil
  254. }
  255. func waitForProcess(pid int) error {
  256. if !processExists(pid) {
  257. return fmt.Errorf("process with PID %d not running", pid)
  258. }
  259. log.Debug("Waiting for process with PID %d to exit", pid)
  260. for processExists(pid) {
  261. time.Sleep(500 * time.Millisecond)
  262. }
  263. log.Debug("Process with PID %d exited", pid)
  264. return nil
  265. }
  266. func runAndWaitForCommand(command []string) (message string, err error) {
  267. prettyCmd := formatCommand(command)
  268. log.Debug("Running command: %s", prettyCmd)
  269. cmd := exec.Command(command[0], command[1:]...)
  270. if log.IsTrace() {
  271. cmd.Stdout = os.Stdout
  272. cmd.Stderr = os.Stderr
  273. }
  274. if err := cmd.Run(); err != nil {
  275. if exitError, ok := err.(*exec.ExitError); ok {
  276. message = fmt.Sprintf("Command failed (exit code %d): %s", exitError.ExitCode(), prettyCmd)
  277. } else {
  278. message = fmt.Sprintf("Command failed: %s, error: %s", prettyCmd, err.Error())
  279. }
  280. } else {
  281. message = fmt.Sprintf("Command done: %s", prettyCmd)
  282. }
  283. log.Debug(message)
  284. return message, nil
  285. }
  286. func formatCommand(command []string) string {
  287. quoted := []string{command[0]}
  288. noQuotesRegex := regexp.MustCompile(`^[-_./a-z0-9]+$`)
  289. for _, c := range command[1:] {
  290. if noQuotesRegex.MatchString(c) {
  291. quoted = append(quoted, c)
  292. } else {
  293. quoted = append(quoted, fmt.Sprintf(`"%s"`, c))
  294. }
  295. }
  296. return strings.Join(quoted, " ")
  297. }