forked from hbollon/IGopher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_messages.go
315 lines (274 loc) · 10.9 KB
/
gui_messages.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package igopher
import (
"context"
"encoding/json"
"fmt"
"github.com/asticode/go-astilectron"
"github.com/go-playground/validator/v10"
"github.com/hbollon/igopher/internal/proxy"
"github.com/sirupsen/logrus"
)
type MsgState string
const (
SUCCESS MsgState = "Success"
ERROR MsgState = "Error"
INFO MsgState = "Info"
)
var (
window *astilectron.Window
config BotConfigYaml
validate = validator.New()
reloadCh, hotReloadCh, exitedCh chan bool
ctx context.Context
cancel context.CancelFunc
)
// CallbackMap is a map of callback functions for each message
var CallbackMap = map[string]func(*MessageIn) MessageOut{
"resetGlobalDefaultSettings": (*MessageIn).resetGlobalSettingsCallback,
"clearAllData": (*MessageIn).clearDataCallback,
"igCredentialsForm": (*MessageIn).credentialsFormCallback,
"quotasForm": (*MessageIn).quotasFormCallback,
"schedulerForm": (*MessageIn).schedulerCallback,
"blacklistForm": (*MessageIn).blacklistFormCallback,
"dmSettingsForm": (*MessageIn).dmBotFormCallback,
"dmUserScrappingSettingsForm": (*MessageIn).dmScrapperFormCallback,
"proxyForm": (*MessageIn).proxyFormCallback,
"launchDmBot": (*MessageIn).launchDmBotCallback,
"stopDmBot": (*MessageIn).stopDmBotCallback,
"hotReloadBot": (*MessageIn).hotReloadCallback,
"getLogs": (*MessageIn).getLogsCallback,
"getConfig": (*MessageIn).getConfigCallback,
}
// MessageOut represents a message for electron (going out)
type MessageOut struct {
Status MsgState `json:"status"`
Msg string `json:"msg"`
Payload interface{} `json:"payload,omitempty"`
}
// MessageIn represents a message from electron (going in)
type MessageIn struct {
Msg string `json:"msg"`
Payload json.RawMessage `json:"payload,omitempty"`
}
// IsElectronRunning checks if electron is running
func IsElectronRunning() bool {
return window != nil
}
// SendMessageToElectron will send a message to Electron Gui and execute a callback
// Callback function is optional
func SendMessageToElectron(msg MessageOut, callbacks ...astilectron.CallbackMessage) {
if IsElectronRunning() {
window.SendMessage(msg, callbacks...)
}
}
// HandleMessages is handling function for incoming messages
func HandleMessages(w *astilectron.Window) {
w.OnMessage(func(m *astilectron.EventMessage) interface{} {
// Unmarshal
var i MessageIn
var err error
if err = m.Unmarshal(&i); err != nil {
logrus.Errorf("Unmarshaling message %+v failed: %v", *m, err)
return MessageOut{Status: "Error during message reception"}
}
// Process message
config = ImportConfig()
if callback, ok := CallbackMap[i.Msg]; ok {
return callback(&i)
}
logrus.Errorf("Unexpected message received: \"%s\"", i.Msg)
return MessageOut{Status: ERROR, Msg: "Unknown error: Invalid message received"}
})
window = w
}
/* Callback functiosn to handle electron messages */
func (m *MessageIn) resetGlobalSettingsCallback() MessageOut {
config = ResetBotConfig()
ExportConfig(config)
return MessageOut{Status: SUCCESS, Msg: "Global configuration was successfully reset!"}
}
func (m *MessageIn) clearDataCallback() MessageOut {
if err := ClearData(); err != nil {
return MessageOut{Status: ERROR, Msg: fmt.Sprintf("IGopher data clearing failed! Error: %v", err)}
}
return MessageOut{Status: SUCCESS, Msg: "IGopher data successfully cleared!"}
}
func (m *MessageIn) credentialsFormCallback() MessageOut {
var err error
var credentialsConfig AccountYaml
// Unmarshal payload
if err = json.Unmarshal([]byte(m.Payload), &credentialsConfig); err != nil {
logrus.Errorf("Failed to unmarshal message payload: %v", err)
return MessageOut{Status: ERROR, Msg: "Failed to unmarshal message payload."}
}
err = validate.Struct(credentialsConfig)
if err != nil {
logrus.Warning("Validation issue on credentials form, abort.")
return MessageOut{Status: ERROR, Msg: "Validation issue on credentials form, please check given informations."}
}
config.Account = credentialsConfig
ExportConfig(config)
return MessageOut{Status: SUCCESS, Msg: "Credentials settings successfully updated!"}
}
func (m *MessageIn) quotasFormCallback() MessageOut {
var err error
var quotasConfig QuotasYaml
// Unmarshal payload
if err = json.Unmarshal([]byte(m.Payload), "asConfig); err != nil {
logrus.Errorf("Failed to unmarshal message payload: %v", err)
return MessageOut{Status: ERROR, Msg: "Failed to unmarshal message payload."}
}
err = validate.Struct(quotasConfig)
if err != nil {
logrus.Warning("Validation issue on quotas form, abort.")
return MessageOut{Status: ERROR, Msg: "Validation issue on quotas form, please check given informations."}
}
config.Quotas = quotasConfig
ExportConfig(config)
return MessageOut{Status: SUCCESS, Msg: "Quotas settings successfully updated!"}
}
func (m *MessageIn) schedulerCallback() MessageOut {
var err error
var schedulerConfig ScheduleYaml
// Unmarshal payload
if err = json.Unmarshal([]byte(m.Payload), &schedulerConfig); err != nil {
logrus.Errorf("Failed to unmarshal message payload: %v", err)
return MessageOut{Status: ERROR, Msg: "Failed to unmarshal message payload."}
}
err = validate.Struct(schedulerConfig)
if err != nil {
logrus.Warning("Validation issue on scheduler form, abort.")
return MessageOut{Status: ERROR, Msg: "Validation issue on scheduler form, please check given informations."}
}
config.Schedule = schedulerConfig
ExportConfig(config)
return MessageOut{Status: SUCCESS, Msg: "Scheduler settings successfully updated!"}
}
func (m *MessageIn) blacklistFormCallback() MessageOut {
var err error
var blacklistConfig BlacklistYaml
// Unmarshal payload
if err = json.Unmarshal([]byte(m.Payload), &blacklistConfig); err != nil {
logrus.Errorf("Failed to unmarshal message payload: %v", err)
return MessageOut{Status: ERROR, Msg: "Failed to unmarshal message payload."}
}
err = validate.Struct(blacklistConfig)
if err != nil {
logrus.Warning("Validation issue on blacklist form, abort.")
return MessageOut{Status: ERROR, Msg: "Validation issue on blacklist form, please check given informations."}
}
config.Blacklist = blacklistConfig
ExportConfig(config)
return MessageOut{Status: SUCCESS, Msg: "Blacklist settings successfully updated!"}
}
func (m *MessageIn) dmBotFormCallback() MessageOut {
var err error
var dmConfig struct {
DmTemplates SplitStringSlice `json:"dmTemplates" validate:"required"`
GreetingTemplate string `json:"greetingTemplate"`
GreetingActivated bool `json:"greetingActivation,string"`
Activated bool `json:"dmActivation,string"`
}
// Unmarshal payload
if err = json.Unmarshal([]byte(m.Payload), &dmConfig); err != nil {
logrus.Errorf("Failed to unmarshal message payload: %v", err)
return MessageOut{Status: ERROR, Msg: "Failed to unmarshal message payload."}
}
err = validate.Struct(dmConfig)
if err != nil {
logrus.Warning("Validation issue on dm tool form, abort.")
return MessageOut{Status: ERROR, Msg: "Validation issue on dm tool form, please check given informations."}
}
config.AutoDm.DmTemplates = dmConfig.DmTemplates
config.AutoDm.Greeting.Template = dmConfig.GreetingTemplate
config.AutoDm.Greeting.Activated = dmConfig.GreetingActivated
config.AutoDm.Activated = dmConfig.Activated
ExportConfig(config)
return MessageOut{Status: SUCCESS, Msg: "Dm bot settings successfully updated!"}
}
func (m *MessageIn) dmScrapperFormCallback() MessageOut {
var err error
var scrapperConfig ScrapperYaml
// Unmarshal payload
if err = json.Unmarshal([]byte(m.Payload), &scrapperConfig); err != nil {
logrus.Errorf("Failed to unmarshal message payload: %v", err)
return MessageOut{Status: ERROR, Msg: "Failed to unmarshal message payload."}
}
err = validate.Struct(scrapperConfig)
if err != nil {
logrus.Warning("Validation issue on scrapper form, abort.")
return MessageOut{Status: ERROR, Msg: "Validation issue on scrapper form, please check given informations."}
}
config.SrcUsers = scrapperConfig
ExportConfig(config)
return MessageOut{Status: SUCCESS, Msg: "Scrapper settings successfully updated!"}
}
func (m *MessageIn) proxyFormCallback() MessageOut {
var err error
var proxyConfig proxy.Proxy
// Unmarshal payload
if err = json.Unmarshal([]byte(m.Payload), &proxyConfig); err != nil {
logrus.Errorf("Failed to unmarshal message payload: %v", err)
return MessageOut{Status: ERROR, Msg: "Failed to unmarshal message payload."}
}
err = validate.Struct(proxyConfig)
if err != nil {
logrus.Warning("Validation issue on proxy form, abort.")
return MessageOut{Status: ERROR, Msg: "Validation issue on proxy form, please check given informations."}
}
config.Selenium.Proxy = proxyConfig
ExportConfig(config)
return MessageOut{Status: SUCCESS, Msg: "Proxy settings successfully updated!"}
}
func (m *MessageIn) launchDmBotCallback() MessageOut {
var err error
if err = CheckConfigValidity(); err == nil {
ctx, cancel = context.WithCancel(context.Background())
go launchBot(ctx)
return MessageOut{Status: SUCCESS, Msg: "Dm bot successfully launched!"}
}
return MessageOut{Status: ERROR, Msg: err.Error()}
}
func (m *MessageIn) stopDmBotCallback() MessageOut {
if exitedCh != nil {
cancel()
res := <-exitedCh
if res {
return MessageOut{Status: SUCCESS, Msg: "Dm bot successfully stopped!"}
}
return MessageOut{Status: ERROR, Msg: "Error during bot stopping! Please restart IGopher"}
}
return MessageOut{Status: ERROR, Msg: "Bot is in the initialization phase, please wait before trying to stop it."}
}
func (m *MessageIn) hotReloadCallback() MessageOut {
if BotStruct.running {
if hotReloadCh != nil {
hotReloadCh <- true
res := <-hotReloadCh
if res {
return MessageOut{Status: SUCCESS, Msg: "Bot hot reload successfully!"}
}
return MessageOut{Status: ERROR, Msg: "Error during bot hot reload! Please restart the bot"}
}
return MessageOut{Status: ERROR, Msg: "Bot is in the initialization phase, please wait before trying to hot reload it."}
}
return MessageOut{Status: ERROR, Msg: "Bot isn't running yet."}
}
func (m *MessageIn) getLogsCallback() MessageOut {
logs, err := parseLogsToString()
if err != nil {
logrus.Errorf("Can't parse logs: %v", err)
return MessageOut{Status: ERROR, Msg: fmt.Sprintf("Can't parse logs: %v", err)}
}
logrus.Debug("Logs fetched successfully!")
return MessageOut{Status: SUCCESS, Msg: logs}
}
func (m *MessageIn) getConfigCallback() MessageOut {
config, err := json.Marshal(config)
if err != nil {
logrus.Errorf("Can't parse config structure to Json: %v", err)
return MessageOut{Status: ERROR, Msg: fmt.Sprintf("Can't parse config structure to Json: %v", err)}
}
logrus.Debug("Configuration structure successfully parsed!")
return MessageOut{Status: SUCCESS, Msg: string(config)}
}