-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
203 lines (183 loc) · 5.76 KB
/
server.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package bot
import (
"fmt"
"os"
"os/signal"
"github.com/mattermost/mattermost-server/model"
)
type ProtocolType int
const (
HTTPServer ProtocolType = iota
WSServer
)
type Server struct {
SeverType string `yaml:"server_type"`
Host string `yaml:"host"`
HTTPS bool `yaml:"https"`
WSS bool `yaml:"wss"`
Account *model.User
HTTPClient *model.Client4
WSClient *model.WebSocketClient
TeamName string `yaml:"team"`
Team *model.Team `yaml:"-"`
DebugChannel *model.Channel
Channels []Channel `yaml:"channels"`
CachedUsers map[string]*model.User
CachedChannels map[string]*model.Channel
}
func (self Server) GetDebugChannel() *model.Channel {
var debugChannelName string
for _, channel := range self.Channels {
if channel.Debug {
debugChannelName = channel.Name
}
}
if debugChannelName == "" {
debugChannelName = "bots"
}
debugChannel, response := self.HTTPClient.GetChannelByName(debugChannelName, self.Team.Id, "")
if response.Error != nil {
if self.CreateChannel(debugChannelName, "Bot Testing", "Bot debug channel used for testing bots") {
debugChannel, response := self.HTTPClient.GetChannelByName(debugChannelName, self.Team.Id, "")
if response.Error != nil {
FatalError("[Fatal Error] Failed to join newly created debug channel", nil)
} else {
self.DebugChannel = debugChannel
}
} else {
FatalError("[Fatal Error] Failed to create debug channel", nil)
return nil
}
} else {
self.DebugChannel = debugChannel
}
return debugChannel
}
func (self Server) JoinChannels() Server {
for _, channel := range self.Channels {
channel.API = self.GetChannel(channel.Name)
}
return self
}
func (self Server) Connect(login, password string) Server {
self.HTTPClient = model.NewAPIv4Client(self.ServerAddress(HTTPServer))
self.Ping()
fmt.Println("[SERVER] Logging into server as:", login)
self.Account = self.Login(login, password)
fmt.Println("[SERVER] Logged into to server and obtained AuthToken:", self.HTTPClient.AuthToken)
self.Team = self.GetTeam(self.TeamName)
fmt.Println("[SERVER] Team ID:", self.Team.Id)
fmt.Println("[SERVER] Connecting to wss address:", self.ServerAddress(WSServer))
self.WSClient, _ = model.NewWebSocketClient4(self.ServerAddress(WSServer), self.HTTPClient.AuthToken)
self.WSClient.Listen()
self.DebugChannel = self.GetDebugChannel()
fmt.Println("[SERVER] Using the following channel for debugging:", self.DebugChannel.Name)
self.CachedUsers = make(map[string]*model.User)
self.CachedChannels = make(map[string]*model.Channel)
var updatedChannels []Channel
for _, channel := range self.Channels {
channel.API = self.GetChannel(channel.Name)
updatedChannels = append(updatedChannels, channel)
}
self.Channels = updatedChannels
self.HandleSignals()
fmt.Println("[SERVER] Connecting to server with bot named: ", login)
return (self.JoinChannels())
}
func (self Server) ServerAddress(protocolType ProtocolType) string {
if protocolType == HTTPServer {
if self.HTTPS {
return "https://" + self.Host
} else {
return "http://" + self.Host
}
} else if protocolType == WSServer {
if self.WSS {
return "wss://" + self.Host
} else {
return "ws://" + self.Host
}
}
FatalError(UNABLE_TO_GENERATE_SERVER_ADDRESS, nil)
// This never happens because FatalError calls os.Exit(1)
return ""
}
func (self Server) GetUser(userId string) *model.User {
user, response := self.HTTPClient.GetUser(userId, "")
if response.Error != nil {
RuntimeError("Unable to get user", response.Error)
}
return user
}
func (self Server) GetTeam(teamName string) (team *model.Team) {
team, response := self.HTTPClient.GetTeamByName(teamName, "")
if response.Error != nil {
RuntimeError(fmt.Sprintf(UNABLE_TO_JOIN_TEAM, teamName, ":"), response.Error)
}
return team
}
func (self Server) GetChannel(channelName string) (channel *model.Channel) {
channel, response := self.HTTPClient.GetChannelByName(channelName, self.Team.Id, "")
if response.Error != nil {
RuntimeError(UNABLE_TO_CREATE_OR_JOIN_CHANNEL, response.Error)
}
return channel
}
func (self Server) CreateChannel(channelName, displayName, channelPurpose string) bool {
channel := &model.Channel{
Name: channelName,
DisplayName: displayName,
Purpose: channelPurpose,
Type: model.CHANNEL_OPEN,
TeamId: self.Team.Id,
}
if _, response := self.HTTPClient.CreateChannel(channel); response.Error != nil {
RuntimeError(UNABLE_TO_CREATE_CHANNEL, response.Error)
return false
} else {
return true
}
}
func (self Server) SendPost(post *model.Post) bool {
if _, response := self.HTTPClient.CreatePost(post); response.Error != nil {
RuntimeError(UNABLE_TO_SEND_MESSAGE_TO_CHANNEL, response.Error)
return false
} else {
return true
}
}
func (self Server) Ping() bool {
if pingResponse, response := self.HTTPClient.GetOldClientConfig(""); response.Error != nil {
FatalError(UNABLE_TO_PING_SERVER, response.Error)
return false
} else {
fmt.Println("[SERVER] Server responded top ping, is running Mattermost version:" + pingResponse["Version"])
return true
}
}
func (self Server) Login(email, password string) (account *model.User) {
account, response := self.HTTPClient.Login(email, password)
if response.Error != nil {
FatalError(UNABLE_TO_LOGIN, response.Error)
}
return account
}
func (self Server) UpdateAccount(account *model.User) (savedAccount *model.User) {
savedAccount, response := self.HTTPClient.UpdateUser(account)
if response.Error != nil {
FatalError(UNABLE_TO_UPDATE_PROFILE, response.Error)
}
return savedAccount
}
func (self Server) HandleSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
if self.WSClient != nil {
self.WSClient.Close()
}
os.Exit(0)
}
}()
}