xmpp-webhook

webhook to xmpp gateway (clone of https://github.com/opthomas-prime/xmpp-webhook/ with my own mods)
git clone https://git.e1e0.net/xmpp-webhook.git
Log | Files | Refs | README | LICENSE

commit 8a214306643b6767fc4b79b910bc58c115759e1c
parent 14c4f07c29f76d0e63387e950e37b4100c2dae35
Author: Paco Esteban <paco@e1e0.net>
Date:   Sun, 26 Jul 2020 23:20:32 +0200

interpret actions comming from xmpp messages.

For now it just does the same thing as the custom webhook.  I may
refactor it all a bit to be more tidy.

Diffstat:
MMakefile | 2+-
Aactions.go | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmain.go | 62++++++++++++++++++++++----------------------------------------
3 files changed, 86 insertions(+), 41 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,6 +1,6 @@ BINARY=xmpp-webhook -VERSION=0.2 +VERSION=0.3 BUILD_DATE=`date '+%Y%m%d%H%M'` BUILD=`git rev-parse HEAD` OS=`uname | tr "[:upper:]" "[:lower:]"` diff --git a/actions.go b/actions.go @@ -0,0 +1,63 @@ +package main + +import ( + "errors" + "fmt" + "io" + "log" + "os" + "regexp" + + "github.com/emgee/go-xmpp/src/xmpp" +) + +func parseMessage(body []xmpp.MessageBody, flagsFolder string) error { + for _, m := range body { + log.Printf("Parsing command '%s'\n", m.Value) + re := regexp.MustCompile(`AC:\d{4}`) + alertCode := re.FindString(m.Value) + if len(alertCode) == 0 { + log.Printf("ERROR: Command unknown") + return errors.New("ERROR: Command unknown") + } + log.Printf("Found alert code in command '%s'", alertCode) + if err := createFolder(flagsFolder); err != nil { + log.Printf("ERROR: %s", err.Error()) + return err + } + err := writeFileFlag(flagsFolder+"/"+alertCode, alertCode) + if err != nil { + log.Printf("ERROR (on file creation): %s", err.Error()) + return err + } + } + return nil +} + +func writeFileFlag(filename string, data string) error { + file, err := os.Create(filename) + if err != nil { + return err + } + defer file.Close() + + _, err = io.WriteString(file, data) + if err != nil { + return err + } + return file.Sync() +} + +func createFolder(path string) error { + if _, err := os.Stat(path); err != nil { + if os.IsNotExist(err) { + err = os.Mkdir(path, 0770) + if err != nil { + return fmt.Errorf("Error while creating folder!") + } + } else { + return fmt.Errorf("Folder exists, but not writable!") + } + } + return nil +} diff --git a/main.go b/main.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "io" "log" "net/http" "os" @@ -19,34 +18,6 @@ var ( BuildDate string ) -func WriteFileFlag(filename string, data string) error { - file, err := os.Create(filename) - if err != nil { - return err - } - defer file.Close() - - _, err = io.WriteString(file, data) - if err != nil { - return err - } - return file.Sync() -} - -func createFolder(path string) error { - if _, err := os.Stat(path); err != nil { - if os.IsNotExist(err) { - err = os.Mkdir(path, 0770) - if err != nil { - return fmt.Errorf("Error while creating folder!") - } - } else { - return fmt.Errorf("Folder exists, but not writable!") - } - } - return nil -} - // starts xmpp session and returns the xmpp client func xmppLogin(id string, pass string) (*xmpp.XMPP, error) { // parse jid structure @@ -77,14 +48,14 @@ func xmppLogin(id string, pass string) (*xmpp.XMPP, error) { } func main() { - verF := flag.Bool("version", false, "Prints version and exit") - flag.Parse() + verF := flag.Bool("version", false, "Prints version and exit") + flag.Parse() - if *verF { - fmt.Printf("xmpp-webhook v%s, built on: %s\n", Version, BuildDate) - fmt.Printf("Git rev: %s\n", Build) - os.Exit(0) - } + if *verF { + fmt.Printf("xmpp-webhook v%s, built on: %s\n", Version, BuildDate) + fmt.Printf("Git rev: %s\n", Build) + os.Exit(0) + } // get xmpp credentials and message receivers from env xi := os.Getenv("XMPP_ID") @@ -125,10 +96,21 @@ func main() { get first the id from the full From and then check if it's on the list of XMPP_RECEIVERS */ fromId := strings.Split(m.From, "/") - if strings.Contains(xr, fromId[0]) { + if strings.Contains(xr, fromId[0]) { + mybody := "Parsing command ..." + xc.Out <- xmpp.Message{ + To: m.From, + Body: []xmpp.MessageBody{{Value: mybody}}, + Type: "chat", + } + if err := parseMessage(m.Body, flagsFolder); err != nil { + mybody = err.Error() + } else { + mybody = "Command executed" + } xc.Out <- xmpp.Message{ To: m.From, - Body: m.Body, + Body: []xmpp.MessageBody{{Value: mybody}}, Type: "chat", } } else { @@ -155,7 +137,7 @@ func main() { m = m + "\n" + err.Error() log.Printf("ERROR: %s", err.Error()) } - err = WriteFileFlag(flagsFolder + "/" + alertCode, alertCode); + err = writeFileFlag(flagsFolder+"/"+alertCode, alertCode) if err != nil { m = m + "\n" + err.Error() log.Printf("ERROR (on file creation): %s", err.Error()) @@ -184,5 +166,5 @@ func main() { http.Handle("/custom", newMessageHandler(messages, customParserFunc)) // listen for requests - http.ListenAndServe(":" + port, nil) + http.ListenAndServe(":"+port, nil) }