Initial commit with Matrix support. Messages are successfully delivered.
This commit is contained in:
23
slack/apiserverinterface/slackapiserverinterface.go
Normal file
23
slack/apiserverinterface/slackapiserverinterface.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// OpenSAPS - Open Slack API server for everyone.
|
||||
//
|
||||
// Copyright (c) 2017, Stanislav N. aka pztrn.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package slackapiserverinterface
|
||||
|
||||
type SlackAPIServerInterface interface {
|
||||
Initialize()
|
||||
Shutdown()
|
||||
}
|
39
slack/exported.go
Normal file
39
slack/exported.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// OpenSAPS - Open Slack API server for everyone.
|
||||
//
|
||||
// Copyright (c) 2017, Stanislav N. aka pztrn.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package slack
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"net/http"
|
||||
|
||||
// local
|
||||
"lab.pztrn.name/pztrn/opensaps/context"
|
||||
"lab.pztrn.name/pztrn/opensaps/slack/apiserverinterface"
|
||||
)
|
||||
|
||||
var (
|
||||
c *context.Context
|
||||
// HTTP server.
|
||||
httpsrv *http.Server
|
||||
)
|
||||
|
||||
func New(cc *context.Context) {
|
||||
c = cc
|
||||
sh := SlackAPIServer{}
|
||||
c.RegisterSlackAPIServerInterface(slackapiserverinterface.SlackAPIServerInterface(sh))
|
||||
}
|
35
slack/message/slackmessage.go
Normal file
35
slack/message/slackmessage.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// OpenSAPS - Open Slack API server for everyone.
|
||||
//
|
||||
// Copyright (c) 2017, Stanislav N. aka pztrn.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package slackmessage
|
||||
|
||||
type SlackMessage struct {
|
||||
Channel string `json:"channel"`
|
||||
Text string `json:"text"`
|
||||
Username string `json:"username"`
|
||||
IconURL string `json:"icon_url"`
|
||||
UnfurlLinks int `json:"unfurl_links"`
|
||||
LinkNames int `json:"link_names"`
|
||||
Attachments []SlackAttachments `json:"attachments"`
|
||||
}
|
||||
|
||||
type SlackAttachments struct {
|
||||
Fallback string `json:"fallback"`
|
||||
Color string `json:"color"`
|
||||
Title string `json:"title"`
|
||||
Text string `json:"text"`
|
||||
}
|
73
slack/slackapiserver.go
Normal file
73
slack/slackapiserver.go
Normal file
@@ -0,0 +1,73 @@
|
||||
// OpenSAPS - Open Slack API server for everyone.
|
||||
//
|
||||
// Copyright (c) 2017, Stanislav N. aka pztrn.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package slack
|
||||
|
||||
// This is a handler for Slack Webhooks API requests.
|
||||
// Slack Webhooks API generates next URL:
|
||||
// https://hooks.slack.com/services/TRANDOM1/BRANDOM2/randomlongstring
|
||||
//
|
||||
// Where:
|
||||
// * RANDOM1 and RANDOM2 - random 8-char thing.
|
||||
// * randomlongstring - random 24-char thing.
|
||||
//
|
||||
// These strings should be defined in configuration, so we can create proper
|
||||
// handler for it.
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SlackAPIServer struct {}
|
||||
|
||||
func (sh SlackAPIServer) Initialize() {
|
||||
c.Log.Infoln("Initializing Slack API handler...")
|
||||
|
||||
// Start HTTP server.
|
||||
// As OpenSAPS designed to be behind some proxy (nginx, Caddy, etc.)
|
||||
// we will listen only to plain HTTP.
|
||||
// Note to those who wants HTTPS - proxify with nginx, Caddy, etc!
|
||||
// Don't send pull requests, patches, don't create issues! :)
|
||||
cfg := c.Config.GetConfig()
|
||||
|
||||
httpsrv = &http.Server{
|
||||
Addr: cfg.SlackHandler.Listener.Address,
|
||||
// This handler will figure out from where request has come and will
|
||||
// send it to approriate pusher. Pusher should also determine to which
|
||||
// connection data should be sent.
|
||||
Handler: SlackHandler{},
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
MaxHeaderBytes: 1 << 20,
|
||||
}
|
||||
|
||||
go httpsrv.ListenAndServe()
|
||||
c.Log.Infof("Slack Webhooks API server starting to listen on %s", cfg.SlackHandler.Listener.Address)
|
||||
}
|
||||
|
||||
func (sh SlackAPIServer) handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "Hello, %q", r.URL.Path)
|
||||
}
|
||||
|
||||
func (sh SlackAPIServer) Shutdown() {
|
||||
c.Log.Infoln("Shutting down Slack API handler...")
|
||||
httpsrv.Shutdown(nil)
|
||||
c.Log.Infoln("Slack API HTTP server shutted down")
|
||||
}
|
76
slack/slackhandler.go
Normal file
76
slack/slackhandler.go
Normal file
@@ -0,0 +1,76 @@
|
||||
// OpenSAPS - Open Slack API server for everyone.
|
||||
//
|
||||
// Copyright (c) 2017, Stanislav N. aka pztrn.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package slack
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
// local
|
||||
"lab.pztrn.name/pztrn/opensaps/slack/message"
|
||||
)
|
||||
|
||||
type SlackHandler struct {}
|
||||
|
||||
func (sh SlackHandler) ServeHTTP(respwriter http.ResponseWriter, req *http.Request) {
|
||||
c.Log.Debugf("Received '%s' (empty = GET) request from %s, URL: '%s'", req.Method, req.Host, req.URL.Path)
|
||||
|
||||
// We should catch only POST requests. Otherwise return HTTP 404.
|
||||
if req.Method != "POST" {
|
||||
c.Log.Debugln("Not a POST request, returning HTTP 404")
|
||||
respwriter.WriteHeader(404)
|
||||
fmt.Fprintf(respwriter, "NOT FOUND")
|
||||
return
|
||||
}
|
||||
|
||||
body, _ := ioutil.ReadAll(req.Body)
|
||||
req.Body.Close()
|
||||
|
||||
c.Log.Debugf("Received body: %s", string(body))
|
||||
|
||||
// Try to figure out where we should push received data.
|
||||
url_splitted := strings.Split(req.URL.Path, "/")
|
||||
fmt.Println(url_splitted)
|
||||
cfg := c.Config.GetConfig()
|
||||
|
||||
var sent_to_pusher bool = false
|
||||
for name, config := range cfg.Webhooks {
|
||||
if strings.Contains(url_splitted[2], config.Slack.Random1) && strings.Contains(url_splitted[3], config.Slack.Random2) && strings.Contains(url_splitted[4], config.Slack.LongRandom) {
|
||||
c.Log.Debugf("Passed data belongs to '%s' and should go to '%s' pusher, protocol '%s'", name, config.Remote.PushTo, config.Remote.Pusher)
|
||||
// Parse message into SlackMessage structure.
|
||||
slackmsg := slackmessage.SlackMessage{}
|
||||
err := json.Unmarshal(body, &slackmsg)
|
||||
if err != nil {
|
||||
c.Log.Error("Failed to decode JSON into SlackMessage struct: '%s'", err.Error())
|
||||
return
|
||||
}
|
||||
c.SendToPusher(config.Remote.Pusher, config.Remote.PushTo, slackmsg)
|
||||
sent_to_pusher = true
|
||||
}
|
||||
}
|
||||
|
||||
if !sent_to_pusher {
|
||||
c.Log.Debug("Don't know where to push data. Ignoring with HTTP 404")
|
||||
respwriter.WriteHeader(404)
|
||||
fmt.Fprintf(respwriter, "NOT FOUND")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user