po/main.go

136 lines
3.0 KiB
Go
Raw Permalink Normal View History

2020-06-10 16:23:01 -06:00
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strings"
"text/tabwriter"
"time"
2023-01-29 05:38:34 -07:00
"suah.dev/protect"
2020-06-10 16:23:01 -06:00
)
2020-07-31 16:13:16 -06:00
var verbose bool
2020-06-10 16:23:01 -06:00
// Push represents a message sent to the Pushover api
type Push struct {
Token string `json:"token"`
User string `json:"user"`
Message string `json:"message"`
Device string `json:"device"`
Title string `json:"title"`
URL string `json:"url"`
URLTitle string `json:"url_title"`
Priority int `json:"priority"`
Sound string `json:"sound"`
Timestamp time.Time `json:"timestamp"`
}
// PushResponse is a response from the Pushover api
type PushResponse struct {
Status int `json:"status,omitempty"`
Request string `json:"request,omitempty"`
User string `json:"user,omitempty"`
Errors []string `json:"errors,omitempty"`
}
2020-07-31 16:13:16 -06:00
func msg(msg interface{}) {
if verbose {
2020-06-10 16:23:01 -06:00
fmt.Println(msg)
}
}
func main() {
2023-12-09 15:47:30 -07:00
protect.Unveil("/etc/ssl", "r")
2023-01-29 05:38:34 -07:00
protect.Pledge("stdio inet dns rpath")
protect.UnveilBlock()
2020-06-10 16:23:01 -06:00
2020-07-31 16:13:30 -06:00
var token, userToken string
2020-06-10 16:23:01 -06:00
var err error
var req *http.Request
var client = *http.DefaultClient
var pushURL = "https://api.pushover.net/1/messages.json"
var title = flag.String("title", "", "title of message to send")
var body = flag.String("body", "", "body of message to send")
var url = flag.String("url", "", "url to send")
var priority = flag.Int("pri", 0, "priority of message")
var sound = flag.String("sound", "pushover", "sound")
2020-07-31 16:13:16 -06:00
flag.BoolVar(&verbose, "v", false, "verbose")
2020-06-10 16:23:01 -06:00
buf := new(bytes.Buffer)
flag.Parse()
if *title == "" || *body == "" {
flag.PrintDefaults()
os.Exit(1)
}
2020-07-31 16:13:30 -06:00
token = os.Getenv("PUSHOVER_TOKEN")
userToken = os.Getenv("PUSHOVER_USER")
if token == "" || userToken == "" {
fmt.Println("please set PUSHOVER_TOKEN and PUSHOVER_USER")
os.Exit(1)
}
2020-06-10 16:23:01 -06:00
var push = &Push{
Token: os.Getenv("PUSHOVER_TOKEN"),
User: os.Getenv("PUSHOVER_USER"),
Timestamp: time.Now(),
Title: *title,
Message: *body,
URL: *url,
Priority: *priority,
Sound: *sound,
}
if err := json.NewEncoder(buf).Encode(push); err != nil {
2020-07-31 16:13:16 -06:00
msg(err)
2020-06-10 16:23:01 -06:00
os.Exit(1)
}
req, err = http.NewRequest("POST", pushURL, buf)
if err != nil {
2020-07-31 16:13:16 -06:00
msg(fmt.Sprintf("can't POST: %s\n", err))
2020-06-10 16:23:01 -06:00
os.Exit(1)
}
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
2020-07-31 16:13:16 -06:00
msg(fmt.Sprintf("can't make request: %s\n", err))
2020-06-10 16:23:01 -06:00
os.Exit(1)
}
defer res.Body.Close()
var resBody PushResponse
if err = json.NewDecoder(res.Body).Decode(&resBody); err != nil {
if err != nil {
2020-07-31 16:13:16 -06:00
msg(err)
2020-06-10 16:23:01 -06:00
os.Exit(1)
}
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
defer w.Flush()
if len(resBody.Errors) > 0 {
2020-07-31 16:13:16 -06:00
if verbose {
2020-06-10 16:23:01 -06:00
fmt.Fprintf(w, "Errors:\t%s\n", strings.Join(resBody.Errors, ", "))
if resBody.User != "" {
fmt.Fprintf(w, "User:\t%s\n", resBody.User)
}
}
os.Exit(1)
} else {
2020-07-31 16:13:16 -06:00
if verbose {
2020-06-10 16:23:01 -06:00
fmt.Fprintf(w, "Request:\t%s\n", resBody.Request)
fmt.Fprintf(w, "Status:\t%d\n", resBody.Status)
}
}
}