publish.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package cmd
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/urfave/cli/v2"
  6. "heckel.io/ntfy/v2/client"
  7. "heckel.io/ntfy/v2/log"
  8. "heckel.io/ntfy/v2/util"
  9. "io"
  10. "os"
  11. "os/exec"
  12. "path/filepath"
  13. "strings"
  14. "time"
  15. )
  16. func init() {
  17. commands = append(commands, cmdPublish)
  18. }
  19. var flagsPublish = append(
  20. append([]cli.Flag{}, flagsDefault...),
  21. &cli.StringFlag{Name: "config", Aliases: []string{"c"}, EnvVars: []string{"NTFY_CONFIG"}, Usage: "client config file"},
  22. &cli.StringFlag{Name: "title", Aliases: []string{"t"}, EnvVars: []string{"NTFY_TITLE"}, Usage: "message title"},
  23. &cli.StringFlag{Name: "message", Aliases: []string{"m"}, EnvVars: []string{"NTFY_MESSAGE"}, Usage: "message body"},
  24. &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)"},
  25. &cli.StringFlag{Name: "tags", Aliases: []string{"tag", "T"}, EnvVars: []string{"NTFY_TAGS"}, Usage: "comma separated list of tags and emojis"},
  26. &cli.StringFlag{Name: "delay", Aliases: []string{"at", "in", "D"}, EnvVars: []string{"NTFY_DELAY"}, Usage: "delay/schedule message"},
  27. &cli.StringFlag{Name: "click", Aliases: []string{"U"}, EnvVars: []string{"NTFY_CLICK"}, Usage: "URL to open when notification is clicked"},
  28. &cli.StringFlag{Name: "icon", Aliases: []string{"i"}, EnvVars: []string{"NTFY_ICON"}, Usage: "URL to use as notification icon"},
  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.BoolFlag{Name: "markdown", Aliases: []string{"md"}, EnvVars: []string{"NTFY_MARKDOWN"}, Usage: "Message is formatted as Markdown"},
  32. &cli.StringFlag{Name: "template", Aliases: []string{"tpl"}, EnvVars: []string{"NTFY_TEMPLATE"}, Usage: "use templates to transform JSON message body"},
  33. &cli.StringFlag{Name: "filename", Aliases: []string{"name", "n"}, EnvVars: []string{"NTFY_FILENAME"}, Usage: "filename for the attachment"},
  34. &cli.StringFlag{Name: "sequence-id", Aliases: []string{"sequence_id", "sid", "S"}, EnvVars: []string{"NTFY_SEQUENCE_ID"}, Usage: "sequence ID for updating notifications"},
  35. &cli.StringFlag{Name: "file", Aliases: []string{"f"}, EnvVars: []string{"NTFY_FILE"}, Usage: "file to upload as an attachment"},
  36. &cli.StringFlag{Name: "email", Aliases: []string{"mail", "e"}, EnvVars: []string{"NTFY_EMAIL"}, Usage: "also send to e-mail address"},
  37. &cli.StringFlag{Name: "user", Aliases: []string{"u"}, EnvVars: []string{"NTFY_USER"}, Usage: "username[:password] used to auth against the server"},
  38. &cli.StringFlag{Name: "token", Aliases: []string{"k"}, EnvVars: []string{"NTFY_TOKEN"}, Usage: "access token used to auth against the server"},
  39. &cli.IntFlag{Name: "wait-pid", Aliases: []string{"wait_pid", "pid"}, EnvVars: []string{"NTFY_WAIT_PID"}, Usage: "wait until PID exits before publishing"},
  40. &cli.BoolFlag{Name: "wait-cmd", Aliases: []string{"wait_cmd", "cmd", "done"}, EnvVars: []string{"NTFY_WAIT_CMD"}, Usage: "run command and wait until it finishes before publishing"},
  41. &cli.BoolFlag{Name: "no-cache", Aliases: []string{"no_cache", "C"}, EnvVars: []string{"NTFY_NO_CACHE"}, Usage: "do not cache message server-side"},
  42. &cli.BoolFlag{Name: "no-firebase", Aliases: []string{"no_firebase", "F"}, EnvVars: []string{"NTFY_NO_FIREBASE"}, Usage: "do not forward message to Firebase"},
  43. &cli.BoolFlag{Name: "quiet", Aliases: []string{"q"}, EnvVars: []string{"NTFY_QUIET"}, Usage: "do not print message"},
  44. )
  45. var cmdPublish = &cli.Command{
  46. Name: "publish",
  47. Aliases: []string{"pub", "send", "trigger"},
  48. Usage: "Send message via a ntfy server",
  49. UsageText: `ntfy publish [OPTIONS..] TOPIC [MESSAGE...]
  50. ntfy publish [OPTIONS..] --wait-cmd COMMAND...
  51. NTFY_TOPIC=.. ntfy publish [OPTIONS..] [MESSAGE...]`,
  52. Action: execPublish,
  53. Category: categoryClient,
  54. Flags: flagsPublish,
  55. Before: initLogFunc,
  56. Description: `Publish a message to a ntfy server.
  57. Examples:
  58. ntfy publish mytopic This is my message # Send simple message
  59. ntfy send myserver.com/mytopic "This is my message" # Send message to different default host
  60. ntfy pub -p high backups "Backups failed" # Send high priority message
  61. ntfy pub --tags=warning,skull backups "Backups failed" # Add tags/emojis to message
  62. ntfy pub --delay=10s delayed_topic Laterzz # Delay message by 10s
  63. ntfy pub --at=8:30am delayed_topic Laterzz # Send message at 8:30am
  64. ntfy pub -e phil@example.com alerts 'App is down!' # Also send email to phil@example.com
  65. ntfy pub --click="https://reddit.com" redd 'New msg' # Opens Reddit when notification is clicked
  66. ntfy pub --icon="http://some.tld/icon.png" 'Icon!' # Send notification with custom icon
  67. ntfy pub --attach="http://some.tld/file.zip" files # Send ZIP archive from URL as attachment
  68. ntfy pub --file=flower.jpg flowers 'Nice!' # Send image.jpg as attachment
  69. ntfy pub -S my-id mytopic 'Update me' # Send with sequence ID for updates
  70. echo 'message' | ntfy publish mytopic # Send message from stdin
  71. ntfy pub -u phil:mypass secret Psst # Publish with username/password
  72. ntfy pub --wait-pid 1234 mytopic # Wait for process 1234 to exit before publishing
  73. ntfy pub --wait-cmd mytopic rsync -av ./ /tmp/a # Run command and publish after it completes
  74. NTFY_USER=phil:mypass ntfy pub secret Psst # Use env variables to set username/password
  75. NTFY_TOPIC=mytopic ntfy pub "some message" # Use NTFY_TOPIC variable as topic
  76. cat flower.jpg | ntfy pub --file=- flowers 'Nice!' # Same as above, send image.jpg as attachment
  77. ntfy trigger mywebhook # Sending without message, useful for webhooks
  78. Please also check out the docs on publishing messages. Especially for the --tags and --delay options,
  79. it has incredibly useful information: https://ntfy.sh/docs/publish/.
  80. ` + clientCommandDescriptionSuffix,
  81. }
  82. func execPublish(c *cli.Context) error {
  83. conf, err := loadConfig(c)
  84. if err != nil {
  85. return err
  86. }
  87. title := c.String("title")
  88. priority := c.String("priority")
  89. tags := c.String("tags")
  90. delay := c.String("delay")
  91. click := c.String("click")
  92. icon := c.String("icon")
  93. actions := c.String("actions")
  94. attach := c.String("attach")
  95. markdown := c.Bool("markdown")
  96. template := c.String("template")
  97. filename := c.String("filename")
  98. sequenceID := c.String("sequence-id")
  99. file := c.String("file")
  100. email := c.String("email")
  101. user := c.String("user")
  102. token := c.String("token")
  103. noCache := c.Bool("no-cache")
  104. noFirebase := c.Bool("no-firebase")
  105. quiet := c.Bool("quiet")
  106. pid := c.Int("wait-pid")
  107. // Checks
  108. if user != "" && token != "" {
  109. return errors.New("cannot set both --user and --token")
  110. }
  111. // Do the things
  112. topic, message, command, err := parseTopicMessageCommand(c)
  113. if err != nil {
  114. return err
  115. }
  116. var options []client.PublishOption
  117. if title != "" {
  118. options = append(options, client.WithTitle(title))
  119. }
  120. if priority != "" {
  121. options = append(options, client.WithPriority(priority))
  122. }
  123. if tags != "" {
  124. options = append(options, client.WithTagsList(tags))
  125. }
  126. if delay != "" {
  127. options = append(options, client.WithDelay(delay))
  128. }
  129. if click != "" {
  130. options = append(options, client.WithClick(click))
  131. }
  132. if icon != "" {
  133. options = append(options, client.WithIcon(icon))
  134. }
  135. if actions != "" {
  136. options = append(options, client.WithActions(strings.ReplaceAll(actions, "\n", " ")))
  137. }
  138. if attach != "" {
  139. options = append(options, client.WithAttach(attach))
  140. }
  141. if markdown {
  142. options = append(options, client.WithMarkdown())
  143. }
  144. if template != "" {
  145. options = append(options, client.WithTemplate(template))
  146. }
  147. if filename != "" {
  148. options = append(options, client.WithFilename(filename))
  149. }
  150. if sequenceID != "" {
  151. options = append(options, client.WithSequenceID(sequenceID))
  152. }
  153. if email != "" {
  154. options = append(options, client.WithEmail(email))
  155. }
  156. if noCache {
  157. options = append(options, client.WithNoCache())
  158. }
  159. if noFirebase {
  160. options = append(options, client.WithNoFirebase())
  161. }
  162. if token != "" {
  163. options = append(options, client.WithBearerAuth(token))
  164. } else if user != "" {
  165. var pass string
  166. parts := strings.SplitN(user, ":", 2)
  167. if len(parts) == 2 {
  168. user = parts[0]
  169. pass = parts[1]
  170. } else {
  171. fmt.Fprint(c.App.ErrWriter, "Enter Password: ")
  172. p, err := util.ReadPassword(c.App.Reader)
  173. if err != nil {
  174. return err
  175. }
  176. pass = string(p)
  177. fmt.Fprintf(c.App.ErrWriter, "\r%s\r", strings.Repeat(" ", 20))
  178. }
  179. options = append(options, client.WithBasicAuth(user, pass))
  180. } else if conf.DefaultToken != "" {
  181. options = append(options, client.WithBearerAuth(conf.DefaultToken))
  182. } else if conf.DefaultUser != "" && conf.DefaultPassword != nil {
  183. options = append(options, client.WithBasicAuth(conf.DefaultUser, *conf.DefaultPassword))
  184. }
  185. if pid > 0 {
  186. newMessage, err := waitForProcess(pid)
  187. if err != nil {
  188. return err
  189. } else if message == "" {
  190. message = newMessage
  191. }
  192. } else if len(command) > 0 {
  193. newMessage, err := runAndWaitForCommand(command)
  194. if err != nil {
  195. return err
  196. } else if message == "" {
  197. message = newMessage
  198. }
  199. }
  200. var body io.Reader
  201. if file == "" {
  202. body = strings.NewReader(message)
  203. } else {
  204. if message != "" {
  205. options = append(options, client.WithMessage(message))
  206. }
  207. if file == "-" {
  208. if filename == "" {
  209. options = append(options, client.WithFilename("stdin"))
  210. }
  211. body = c.App.Reader
  212. } else {
  213. if filename == "" {
  214. options = append(options, client.WithFilename(filepath.Base(file)))
  215. }
  216. body, err = os.Open(file)
  217. if err != nil {
  218. return err
  219. }
  220. }
  221. }
  222. cl := client.New(conf)
  223. m, err := cl.PublishReader(topic, body, options...)
  224. if err != nil {
  225. return err
  226. }
  227. if !quiet {
  228. fmt.Fprintln(c.App.Writer, strings.TrimSpace(m.Raw))
  229. }
  230. return nil
  231. }
  232. // parseTopicMessageCommand reads the topic and the remaining arguments from the context.
  233. // There are a few cases to consider:
  234. //
  235. // ntfy publish <topic> [<message>]
  236. // ntfy publish --wait-cmd <topic> <command>
  237. // NTFY_TOPIC=.. ntfy publish [<message>]
  238. // NTFY_TOPIC=.. ntfy publish --wait-cmd <command>
  239. func parseTopicMessageCommand(c *cli.Context) (topic string, message string, command []string, err error) {
  240. var args []string
  241. topic, args, err = parseTopicAndArgs(c)
  242. if err != nil {
  243. return
  244. }
  245. if c.Bool("wait-cmd") {
  246. if len(args) == 0 {
  247. err = errors.New("must specify command when --wait-cmd is passed, type 'ntfy publish --help' for help")
  248. return
  249. }
  250. command = args
  251. } else {
  252. message = strings.Join(args, " ")
  253. }
  254. if c.String("message") != "" {
  255. message = c.String("message")
  256. }
  257. if message == "" && isStdinRedirected() {
  258. var data []byte
  259. data, err = io.ReadAll(io.LimitReader(c.App.Reader, 1024*1024))
  260. if err != nil {
  261. log.Debug("Failed to read from stdin: %s", err.Error())
  262. return
  263. }
  264. message = strings.TrimSpace(string(data))
  265. }
  266. return
  267. }
  268. func parseTopicAndArgs(c *cli.Context) (topic string, args []string, err error) {
  269. envTopic := os.Getenv("NTFY_TOPIC")
  270. if envTopic != "" {
  271. topic = envTopic
  272. return topic, remainingArgs(c, 0), nil
  273. }
  274. if c.NArg() < 1 {
  275. return "", nil, errors.New("must specify topic, type 'ntfy publish --help' for help")
  276. }
  277. return c.Args().Get(0), remainingArgs(c, 1), nil
  278. }
  279. func remainingArgs(c *cli.Context, fromIndex int) []string {
  280. if c.NArg() > fromIndex {
  281. return c.Args().Slice()[fromIndex:]
  282. }
  283. return []string{}
  284. }
  285. func waitForProcess(pid int) (message string, err error) {
  286. if !processExists(pid) {
  287. return "", fmt.Errorf("process with PID %d not running", pid)
  288. }
  289. start := time.Now()
  290. log.Debug("Waiting for process with PID %d to exit", pid)
  291. for processExists(pid) {
  292. time.Sleep(500 * time.Millisecond)
  293. }
  294. runtime := time.Since(start).Round(time.Millisecond)
  295. log.Debug("Process with PID %d exited after %s", pid, runtime)
  296. return fmt.Sprintf("Process with PID %d exited after %s", pid, runtime), nil
  297. }
  298. func runAndWaitForCommand(command []string) (message string, err error) {
  299. prettyCmd := util.QuoteCommand(command)
  300. log.Debug("Running command: %s", prettyCmd)
  301. start := time.Now()
  302. cmd := exec.Command(command[0], command[1:]...)
  303. if log.IsTrace() {
  304. cmd.Stdout = os.Stdout
  305. cmd.Stderr = os.Stderr
  306. }
  307. err = cmd.Run()
  308. runtime := time.Since(start).Round(time.Millisecond)
  309. if err != nil {
  310. if exitError, ok := err.(*exec.ExitError); ok {
  311. log.Debug("Command failed after %s (exit code %d): %s", runtime, exitError.ExitCode(), prettyCmd)
  312. return fmt.Sprintf("Command failed after %s (exit code %d): %s", runtime, exitError.ExitCode(), prettyCmd), nil
  313. }
  314. // Hard fail when command does not exist or could not be properly launched
  315. return "", fmt.Errorf("command failed: %s, error: %s", prettyCmd, err.Error())
  316. }
  317. log.Debug("Command succeeded after %s: %s", runtime, prettyCmd)
  318. return fmt.Sprintf("Command succeeded after %s: %s", runtime, prettyCmd), nil
  319. }
  320. func isStdinRedirected() bool {
  321. stat, err := os.Stdin.Stat()
  322. if err != nil {
  323. log.Debug("Failed to stat stdin: %s", err.Error())
  324. return false
  325. }
  326. return (stat.Mode() & os.ModeCharDevice) == 0
  327. }