-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.go
245 lines (207 loc) · 6.37 KB
/
dispatcher.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
package chadango
import (
"context"
"os"
"os/signal"
"strings"
"sync"
"syscall"
)
// Application represents the main application.
type Application struct {
Config *Config // Config holds the configuration for the application.
Persistence *Persistence // Persistence manages data persistence for the application.
API *API // API provides access to various Chatango APIs used by the application.
Private Private // Private represents the private chat functionality of the application.
Groups SyncMap[string, *Group] // Groups stores the groups the application is connected to.
handlers []Handler // handlers contains the registered event handlers for the application.
interruptChan chan os.Signal // interruptChan receives interrupt signals to gracefully stop the application.
context context.Context // Context for running the application.
cancelCtx context.CancelFunc // Function for stopping the application.
initialized bool // initialized indicates whether the application has been initialized.
mu sync.Mutex // mu is a mutex used for locking access to critical sections.
}
// AddHandler adds a new handler to the application.
// It returns the `*Application` to allow for nesting.
func (app *Application) AddHandler(handler Handler) *Application {
app.mu.Lock()
defer app.mu.Unlock()
if ch, ok := handler.(*CommandHandler); ok {
ch.app = app
}
app.handlers = append(app.handlers, handler)
return app
}
// RemoveHandler removes a handler from the application.
func (app *Application) RemoveHandler(handler Handler) {
app.mu.Lock()
defer app.mu.Unlock()
// Find and remove the handler from the collection
for i, h := range app.handlers {
if h == handler {
app.handlers = append(app.handlers[:i], app.handlers[i+1:]...)
break
}
}
}
// Dispatch dispatches an event to the appropriate handler.
func (app *Application) Dispatch(event *Event) {
var context *Context
for _, handler := range app.handlers {
if handler.Check(event) {
if context == nil {
context = &Context{
App: app,
BotData: app.Persistence.GetBotData(),
}
if event.IsPrivate && event.User != nil && !event.User.IsAnon {
context.ChatData = app.Persistence.GetChatData(strings.ToLower(event.User.Name))
} else {
context.ChatData = app.Persistence.GetChatData(event.Group.Name)
}
}
handler.Invoke(event, context)
break
}
}
}
// Initialize initializes the application.
func (app *Application) Initialize() {
if app.Persistence == nil {
app.Persistence = new(Persistence)
}
app.Persistence.Initialize()
app.API = &API{
Username: app.Config.Username,
Password: app.Config.Password,
}
app.API.Initialize()
app.Groups = NewSyncMap[string, *Group]()
app.checkConfig()
app.initialized = true
}
// checkConfig checks certain configurations and assigns default values if they are left unset.
func (app *Application) checkConfig() {
if app.Config.AnonName == "" {
app.Config.AnonName = "anon0001"
}
if app.Config.NameColor == "" {
app.Config.NameColor = DEFAULT_COLOR
}
if app.Config.TextColor == "" {
app.Config.TextColor = DEFAULT_COLOR
}
if app.Config.TextFont == "" {
app.Config.TextFont = DEFAULT_TEXT_FONT
}
if app.Config.TextSize == 0 {
app.Config.TextSize = DEFAULT_TEXT_SIZE
}
}
// Start starts the application.
func (app *Application) Start(ctx context.Context) {
if !app.initialized {
panic("the application is not initialized")
}
if ctx == nil {
ctx = context.Background()
}
app.context, app.cancelCtx = context.WithCancel(ctx)
for _, groupName := range app.Config.Groups {
go app.JoinGroup(groupName)
}
if app.Config.EnablePM {
go app.ConnectPM()
}
app.Persistence.StartAutoSave(app.context)
app.interruptChan = make(chan os.Signal, 1)
signal.Notify(app.interruptChan, os.Interrupt, syscall.SIGTERM)
app.Dispatch(&Event{Type: OnStart})
}
// Park waits for the application to stop or receive an interrupt signal.
func (app *Application) Park() {
select {
case <-app.context.Done():
case <-app.interruptChan:
app.Stop()
}
}
// Stop stops the application.
func (app *Application) Stop() {
app.Dispatch(&Event{Type: OnStart})
var wg sync.WaitGroup
wg.Add(1)
go func() {
app.DisconnectPM()
wg.Done()
}()
cb := func(_ string, group *Group) bool {
wg.Add(1)
go func() {
group.Disconnect()
wg.Done()
}()
return false
}
app.Groups.Range(cb)
wg.Wait()
app.Persistence.Close()
app.cancelCtx()
}
// JoinGroup joins a group in the application.
func (app *Application) JoinGroup(groupName string) error {
groupName = strings.ToLower(groupName)
if _, ok := app.Groups.Get(groupName); ok {
return ErrAlreadyConnected
}
if isGroup, err := app.API.IsGroup(groupName); err != nil || !isGroup {
return ErrNotAGroup
}
group := Group{
App: app,
Name: groupName,
WsUrl: GetServer(groupName),
AnonName: app.Config.AnonName,
NameColor: app.Config.NameColor,
TextColor: app.Config.TextColor,
TextFont: app.Config.TextFont,
TextSize: app.Config.TextSize,
SessionID: app.Config.SessionID,
LoggedIn: app.Config.Password != "",
}
if err := group.Connect(app.context); err != nil {
return err
}
app.Groups.Set(groupName, &group)
return nil
}
// LeaveGroup leaves a group in the application.
func (app *Application) LeaveGroup(groupName string) error {
groupName = strings.ToLower(groupName)
if group, ok := app.Groups.Get(groupName); ok {
// app.Groups.Del(groupName) // Group deletion is handled by the `Group.wsOnError`.
group.Disconnect()
return nil
}
return ErrNotConnected
}
// ConnectPM connects to private messages.
func (app *Application) ConnectPM() error {
app.Private.App = app
app.Private.Name = "Private"
app.Private.WsUrl = PM_SERVER
app.Private.NameColor = app.Config.NameColor
app.Private.TextColor = app.Config.TextColor
app.Private.TextFont = app.Config.TextFont
app.Private.TextSize = app.Config.TextSize
app.Private.SessionID = app.Config.SessionID
return app.Private.Connect(app.context)
}
// DisconnectPM disconnects from private messages.
func (app *Application) DisconnectPM() {
app.Private.Disconnect()
}
// New creates a new instance of the `*Application` with the provided configuration.
func New(config *Config) *Application {
return &Application{Config: config}
}