-
Notifications
You must be signed in to change notification settings - Fork 35
/
robot.go
169 lines (139 loc) · 3.5 KB
/
robot.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
package hal
import (
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
)
// Robot receives messages from an adapter and sends them to listeners
type Robot struct {
Name string
Alias string
Adapter Adapter
Store Store
handlers []handler
Users *UserMap
Auth *Auth
signalChan chan os.Signal
}
// Handlers returns the robot's handlers
func (robot *Robot) Handlers() []handler {
return robot.handlers
}
// NewRobot returns a new Robot instance
func NewRobot() (*Robot, error) {
robot := &Robot{
Name: Config.Name,
Alias: Config.Alias,
signalChan: make(chan os.Signal, 1),
}
adapter, err := NewAdapter(robot)
if err != nil {
Logger.Error(err)
return nil, err
}
robot.SetAdapter(adapter)
store, err := NewStore(robot)
if err != nil {
Logger.Error(err)
return nil, err
}
robot.SetStore(store)
robot.Users = NewUserMap(robot)
robot.Auth = NewAuth(robot)
return robot, nil
}
// Handle registers a new handler with the robot
func (robot *Robot) Handle(handlers ...interface{}) {
for _, h := range handlers {
nh, err := NewHandler(h)
if err != nil {
Logger.Fatal(err)
panic(err)
}
robot.handlers = append(robot.handlers, nh)
}
}
// Receive dispatches messages to our handlers
func (robot *Robot) Receive(msg *Message) error {
Logger.Debugf("%s - robot received message", Config.AdapterName)
// check if we've seen this user yet, and add if we haven't.
user := msg.User
if _, err := robot.Users.Get(user.ID); err != nil {
Logger.Debug(err)
robot.Users.Set(user.ID, user)
robot.Users.Save()
}
for _, handler := range robot.handlers {
response := NewResponseFromMessage(robot, msg)
if err := handler.Handle(response); err != nil {
Logger.Error(err)
return err
}
}
return nil
}
// Run initiates the startup process
func (robot *Robot) Run() error {
Logger.Info("starting robot")
// HACK
Logger.Debugf("opening %s store connection", Config.StoreName)
go func() {
robot.Store.Open()
Logger.Debug("loading users from store")
robot.Users.Load()
}()
Logger.Debugf("starting %s adapter", Config.AdapterName)
go robot.Adapter.Run()
// Start the HTTP server after the adapter, as adapter.Run() adds additional
// handlers to the router.
Logger.Debug("starting HTTP server")
go func() {
if err := http.ListenAndServe(`:`+strconv.Itoa(Config.Port), Router); err != nil {
Logger.Debug(err)
}
}()
signal.Notify(robot.signalChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
stop := false
for !stop {
select {
case sig := <-robot.signalChan:
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
stop = true
}
}
}
// Stop listening for new signals
signal.Stop(robot.signalChan)
// Initiate the shutdown process for our robot
robot.Stop()
return nil
}
// Stop initiates the shutdown process
func (robot *Robot) Stop() error {
Logger.Info() // so we don't break up the log formatting when running interactively ;)
Logger.Debugf("stopping %s adapter", Config.AdapterName)
if err := robot.Adapter.Stop(); err != nil {
return err
}
Logger.Debugf("closing %s store connection", Config.StoreName)
if err := robot.Store.Close(); err != nil {
return err
}
Logger.Info("stopping robot")
return nil
}
// SetName sets robot's name
func (robot *Robot) SetName(name string) {
robot.Name = name
}
// SetAdapter sets robot's adapter
func (robot *Robot) SetAdapter(adapter Adapter) {
robot.Adapter = adapter
}
// SetStore sets robot's adapter
func (robot *Robot) SetStore(store Store) {
robot.Store = store
}