-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
188 lines (174 loc) · 4.59 KB
/
commands.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
package commands
import (
"fmt"
"log"
"strings"
"time"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
"github.com/whyrusleeping/hellabot"
)
type Commands struct {
Identifier string
commands map[string]interface{}
db *gorm.DB
self string
}
var (
c Commands
)
func init() {
c = Commands{}
c.commands = map[string]interface{}{
"help": help,
"seen": lastSeen,
"tell": tell,
"ack": ack,
}
}
type LastSeen struct {
Id int64
Nick string
Seen time.Time
}
type TellMessage struct {
Id int64
Sender string
Destination string
Message string
Channel string
}
func ack(irc *hbot.IrcCon, msg *hbot.Message) bool {
c.db.Where("destination like ?", msg.Name).Delete(TellMessage{})
irc.Channels[msg.To].Say(fmt.Sprintf("Ok %s all your pending messages deleted", msg.Name))
return true
}
func tell(irc *hbot.IrcCon, msg *hbot.Message) bool {
splitted := strings.SplitN(msg.Content, " ", 3)
if len(splitted) == 3 {
tm := TellMessage{
Destination: splitted[1],
Message: splitted[2],
Sender: msg.Name,
Channel: msg.To,
}
c.db.Save(&tm)
irc.Channels[msg.To].Say("Message saved")
} else {
irc.Channels[msg.To].Say(fmt.Sprintf("Command TELL: %stell <nickname> <message>", c.Identifier))
}
return true
}
func lastSeen(irc *hbot.IrcCon, msg *hbot.Message) bool {
log.Printf("Running lastSeen command")
const layout = "Jan 2, 2006 at 3:04pm (MST)"
fields := strings.Fields(msg.Content)
if len(fields) < 2 {
irc.Channels[msg.To].Say(fmt.Sprintf("Command TELL: %sseen <nickname>", c.Identifier))
return true
}
nick := fields[1]
seen := LastSeen{}
c.db.Where("nick = ?", nick).First(&seen)
if seen.Id == 0 {
irc.Channels[msg.To].Say(fmt.Sprintf("Never seen %s, sorry", nick))
} else {
irc.Channels[msg.To].Say(fmt.Sprintf("Last seen %s at %s", nick, seen.Seen.Format(layout)))
}
return true
}
func help(irc *hbot.IrcCon, msg *hbot.Message) bool {
log.Printf("Running help command")
keys := []string{}
for k := range c.commands {
keys = append(keys, k)
}
irc.Channels[msg.To].Say(fmt.Sprintf("Available commands: %s", strings.Join(keys, ",")))
return true
}
func Configure(identifier string, dblocation string, self string, bot *hbot.IrcCon) (err error) {
c.Identifier = identifier
c.self = self
log.Printf("Command module running with command identifier %s", identifier)
db, err := gorm.Open("sqlite3", dblocation)
if err != nil {
return err
}
c.db = &db
c.db.AutoMigrate(&LastSeen{})
c.db.AutoMigrate(&TellMessage{})
lastSeenTrigger := &hbot.Trigger{
func(mes *hbot.Message) bool {
if mes.Name != self && mes.Command == "PRIVMSG" {
return true
}
return false
},
func(irc *hbot.IrcCon, mes *hbot.Message) bool {
lastseen := LastSeen{}
err := c.db.Where("nick = ?", mes.Name).Find(&lastseen).Error
if err != nil {
log.Printf("Cannot find last seen for user %s: %s", mes.Name, err)
}
if lastseen.Nick == "" {
lastseen.Nick = mes.Name
}
lastseen.Seen = time.Now()
c.db.Save(&lastseen)
return false
},
}
tellTrigger := &hbot.Trigger{
func(mes *hbot.Message) bool {
if mes.Command == "JOIN" {
if mes.Name != c.self {
return true
}
}
return false
},
func(irc *hbot.IrcCon, mes *hbot.Message) bool {
rows, err := c.db.Model(TellMessage{}).Where("destination = ?", mes.Name).Select("sender, message, channel").Rows()
if err != nil {
log.Println("ERROR in tell message: %s", err)
}
defer rows.Close()
var message, channel, from string
for rows.Next() {
rows.Scan(&from, &message, &channel)
irc.Channels[channel].Say(fmt.Sprintf("Hey %s! %s told me to tell you: \"%s\"", mes.Name, from, message))
}
irc.Channels[channel].Say("Use !ack to acnowledge all messages or i will repeat them each time you log in")
return true
},
}
bot.AddTrigger(tellTrigger)
bot.AddTrigger(lastSeenTrigger)
return nil
}
func GetTrigger() *hbot.Trigger {
trigger := &hbot.Trigger{
func(mes *hbot.Message) bool {
if mes.Command == "PRIVMSG" {
if mes.Content != "" && string(mes.Content[0]) == c.Identifier {
return true
}
}
return false
},
func(irc *hbot.IrcCon, mes *hbot.Message) bool {
return handleCommand(irc, mes)
},
}
return trigger
}
func handleCommand(irc *hbot.IrcCon, mes *hbot.Message) bool {
log.Printf("Handle command invoked on: %s", mes.Content)
fields := strings.Fields(mes.Content)
handler, ok := c.commands[string(fields[0][1:])]
if !ok {
irc.Channels[mes.To].Say(fmt.Sprintf("Command %s unknown", fields[0]))
return false
}
return handler.(func(*hbot.IrcCon, *hbot.Message) bool)(irc, mes)
}