summaryrefslogtreecommitdiffstats
path: root/matterhook/matterhook.go
blob: 6b036430dd13a9ca3725bfd48d10d85c961c167c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//Package matterhook provides interaction with mattermost incoming/outgoing webhooks
package matterhook

import (
	"bytes"
	"crypto/tls"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"time"

	"github.com/gorilla/schema"
	"github.com/slack-go/slack"
)

// OMessage for mattermost incoming webhook. (send to mattermost)
type OMessage struct {
	Channel     string                 `json:"channel,omitempty"`
	IconURL     string                 `json:"icon_url,omitempty"`
	IconEmoji   string                 `json:"icon_emoji,omitempty"`
	UserName    string                 `json:"username,omitempty"`
	Text        string                 `json:"text"`
	Attachments []slack.Attachment     `json:"attachments,omitempty"`
	Type        string                 `json:"type,omitempty"`
	Props       map[string]interface{} `json:"props"`
}

// IMessage for mattermost outgoing webhook. (received from mattermost)
type IMessage struct {
	BotID       string `schema:"bot_id"`
	BotName     string `schema:"bot_name"`
	Token       string `schema:"token"`
	TeamID      string `schema:"team_id"`
	TeamDomain  string `schema:"team_domain"`
	ChannelID   string `schema:"channel_id"`
	ChannelName string `schema:"channel_name"`
	Timestamp   string `schema:"timestamp"`
	UserID      string `schema:"user_id"`
	UserName    string `schema:"user_name"`
	PostId      string `schema:"post_id"` //nolint:golint
	RawText     string `schema:"raw_text"`
	ServiceId   string `schema:"service_id"` //nolint:golint
	Text        string `schema:"text"`
	TriggerWord string `schema:"trigger_word"`
	FileIDs     string `schema:"file_ids"`
}

// Client for Mattermost.
type Client struct {
	// URL for incoming webhooks on mattermost.
	Url        string // nolint:golint
	In         chan IMessage
	Out        chan OMessage
	httpclient *http.Client
	Config
}

// Config for client.
type Config struct {
	BindAddress        string // Address to listen on
	Token              string // Only allow this token from Mattermost. (Allow everything when empty)
	InsecureSkipVerify bool   // disable certificate checking
	DisableServer      bool   // Do not start server for outgoing webhooks from Mattermost.
}

// New Mattermost client.
func New(url string, config Config) *Client {
	c := &Client{Url: url, In: make(chan IMessage), Out: make(chan OMessage), Config: config}
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: config.InsecureSkipVerify}, //nolint:gosec
	}
	c.httpclient = &http.Client{Transport: tr}
	if !c.DisableServer {
		_, _, err := net.SplitHostPort(c.BindAddress)
		if err != nil {
			log.Fatalf("incorrect bindaddress %s", c.BindAddress)
		}
		go c.StartServer()
	}
	return c
}

// StartServer starts a webserver listening for incoming mattermost POSTS.
func (c *Client) StartServer() {
	mux := http.NewServeMux()
	mux.Handle("/", c)
	srv := &http.Server{
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 10 * time.Second,
		Handler:      mux,
		Addr:         c.BindAddress,
	}
	log.Printf("Listening on http://%v...\n", c.BindAddress)
	if err := srv.ListenAndServe(); err != nil {
		log.Fatal(err)
	}
}

// ServeHTTP implementation.
func (c *Client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		log.Println("invalid " + r.Method + " connection from " + r.RemoteAddr)
		http.NotFound(w, r)
		return
	}
	msg := IMessage{}
	err := r.ParseForm()
	if err != nil {
		log.Println(err)
		http.NotFound(w, r)
		return
	}
	defer r.Body.Close()
	decoder := schema.NewDecoder()
	err = decoder.Decode(&msg, r.PostForm)
	if err != nil {
		log.Println(err)
		http.NotFound(w, r)
		return
	}
	if msg.Token == "" {
		log.Println("no token from " + r.RemoteAddr)
		http.NotFound(w, r)
		return
	}
	if c.Token != "" {
		if msg.Token != c.Token {
			log.Println("invalid token " + msg.Token + " from " + r.RemoteAddr)
			http.NotFound(w, r)
			return
		}
	}
	c.In <- msg
}

// Receive returns an incoming message from mattermost outgoing webhooks URL.
func (c *Client) Receive() IMessage {
	var msg IMessage
	for msg := range c.In {
		return msg
	}
	return msg
}

// Send sends a msg to mattermost incoming webhooks URL.
func (c *Client) Send(msg OMessage) error {
	buf, err := json.Marshal(msg)
	if err != nil {
		return err
	}
	resp, err := c.httpclient.Post(c.Url, "application/json", bytes.NewReader(buf))
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	// Read entire body to completion to re-use keep-alive connections.
	io.Copy(ioutil.Discard, resp.Body)

	if resp.StatusCode != 200 {
		return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
	}
	return nil
}