This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatty.go
160 lines (139 loc) · 4.25 KB
/
chatty.go
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
package chatty
import (
"net/http"
"sync"
"github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/nlopes/slack"
"golang.org/x/net/websocket"
)
const exitMessage = "*My fan just left.*"
// Config contains the configuration of the app.
type Config struct {
Mu *sync.Mutex
Logger *logrus.Logger
Router *mux.Router
Teams map[string]*Team
TeamService TeamService
ClientID string
ClientSecret string
VerificationToken string
}
// Team contains the fields related to the teams connected
// to this application.
type Team struct {
ID string `json:"id"`
Name string `json:"name"`
Scope string `json:"scope"`
Channel string `json:"channel"`
Websites []string `json:"websites"`
WebsocketURI string `json:"websocket_uri"`
Token string `json:"token"`
Connections map[string]*websocket.Conn `json:"-"`
Slack *slack.Client `json:"-"`
Logger *logrus.Logger `json:"-"`
}
// TeamService allows the management of the team object.
type TeamService interface {
Loads() ([]*Team, error)
Load(id string) (*Team, error)
Save(t *Team) error
Delete(t *Team) error
}
// Start starts the program for this team.
func (t *Team) Start(c *Config) {
t.Slack = slack.New(t.Token)
t.Logger = c.Logger
t.Connections = make(map[string]*websocket.Conn)
c.Router.Handle(t.WebsocketURI, websocket.Handler(t.websocket))
c.Logger.Infof("Successfully started team %s on %s", t.ID, t.WebsocketURI)
}
func (t *Team) allowed(url string) bool {
for _, website := range t.Websites {
if website == url {
return true
}
}
// If it's not allowed, let's notify our dear team that
// someone from the outside tried to access the websocket
params := slack.PostMessageParameters{
Attachments: []slack.Attachment{
{
Title: "Website Approval",
Text: "Do you wish to add <" + url + "> to the verified websites' list?",
Fallback: "You are unable to either reject or accept.",
CallbackID: "accept_channel",
Color: "#8BC34A",
Actions: []slack.AttachmentAction{
{
Name: "approve",
Text: "Approve",
Type: "button",
Style: "primary",
Value: url,
Confirm: &slack.ConfirmationField{
Title: "Are you sure?",
Text: "This will let *" + url + "* access your chat from now on.",
OkText: "Yes",
DismissText: "No",
},
},
{
Name: "approve",
Text: "Reject",
Type: "button",
Value: "",
Confirm: &slack.ConfirmationField{
Title: "Are you sure?",
Text: "This will not let *" + url + "* access your chat.",
OkText: "Yes",
DismissText: "No",
},
},
},
},
},
}
text := "Someone tried to access your API websocket from a new URL. If it was you, click the button bellow to accept it and add this to the list of verified websites."
_, _, err := t.Slack.PostMessage(t.Channel, text, params)
if err != nil {
t.Logger.Errorf("can't post '%s' message for '%s' team", text, t.ID)
}
return false
}
// websocket is the handler for the socket that each team has.
func (t *Team) websocket(ws *websocket.Conn) {
if !t.allowed(ws.RemoteAddr().String()) {
err := ws.WriteClose(http.StatusForbidden)
if err != nil {
t.Logger.Errorf("error closing the websocket %v", err)
}
}
var err error
var thread string
for {
var reply string
if err = websocket.Message.Receive(ws, &reply); err != nil {
// This means the connection was closed.
break
}
params := slack.PostMessageParameters{ThreadTimestamp: thread}
_, ts, err := t.Slack.PostMessage(t.Channel, reply, params)
if err != nil {
t.Logger.Errorf("can't post '%s' message for '%s' team", reply, t.ID)
break
}
if thread == "" {
thread = ts
t.Connections[thread] = ws
}
}
if thread == "" {
return
}
params := slack.PostMessageParameters{ThreadTimestamp: thread}
_, _, err = t.Slack.PostMessage(t.Channel, exitMessage, params)
if err != nil {
t.Logger.Errorf("can't post '%s' message for '%s' team", exitMessage, t.ID)
}
}