This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
152 lines (128 loc) · 3.51 KB
/
main.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
// Copyright 2014-2015 Chadev. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"os"
"strings"
"github.com/danryan/hal"
_ "github.com/danryan/hal/adapter/irc"
_ "github.com/danryan/hal/adapter/shell"
_ "github.com/danryan/hal/store/memory"
_ "github.com/danryan/hal/store/redis"
)
// VERSION contians the current verison number and revison if need be
var VERSION string
// handler is an interface for objects to implement in order to respond to messages.
type handler interface {
Handle(res *hal.Response) error
}
var helpMessages = make(map[string]string)
var pingHandler = hear(`ping`, "ping", "Causes Ash to reply with PONG", func(res *hal.Response) error {
return res.Send("PONG")
})
var fooHandler = hear(`foo`, "foo", "Causes Ash to reply with a BAR", func(res *hal.Response) error {
return res.Send("BAR")
})
var synHandler = hear(`SYN`, "SYN", "Causes Ash to reply with ACK", func(res *hal.Response) error {
return res.Send("ACK")
})
var selfHandler = hear(`who are you`, "self", "", func(res *hal.Response) error {
return res.Send("I'm Ash, the friendly #chadev bot. I can preform a variety of tasks, and I am learning new tricks all the time. I am open source, and pull requests are welcome!")
})
var quitHandler = hear(`(.*)+/quit(.*)+`, "quit", "", func(res *hal.Response) error {
return res.Send(fmt.Sprintf("No! Bad %s!", res.UserName()))
})
var helpHandler = hear(`help`, "help", "Displays this message", func(res *hal.Response) error {
helpMsg := []string{
"HAL Chadev IRC Edition build: " + VERSION + "\n",
"Supported commands:\n",
}
for command, message := range helpMessages {
if command != "" && message != "" {
helpMsg = append(helpMsg, command+" - "+message+"\n")
}
}
var text string
for _, msg := range helpMsg {
text = text + msg
}
text = uploadHelpMsg(text)
res.Send(fmt.Sprintf("My usage information can be found at %s", text))
return nil
})
var modHandler = hear(`mods`, "mods", "Returns a list of channel moderators", func(res *hal.Response) error {
admin := res.Robot.Auth.Admins()
hal.Logger.Debugf("admins: %#v", admin)
var ops []string
for _, a := range admin {
ops = append(ops, a.Name)
}
msg := strings.Join(ops, ", ")
err := res.Reply("Current mods are: " + msg)
return err
})
func hear(pattern string, command string, message string, fn func(res *hal.Response) error) handler {
addHelpMessage(command, message)
return hal.Hear("^(?i)Ash "+pattern, fn)
}
func addHelpMessage(command string, message string) {
helpMessages[command] = message
}
func main() {
os.Exit(run())
}
func run() int {
robot, err := hal.NewRobot()
if err != nil {
hal.Logger.Error(err)
return 1
}
robot.Handle(
fooHandler,
tableFlipHandler,
eventHandler,
synHandler,
helpHandler,
pingHandler,
sourceHandler,
issueHandler,
cageMeHandler,
whoisHandler,
whoamHandler,
isHandler,
selfHandler,
quitHandler,
fizzBuzzHandler,
noteStoreHandler,
noteGetHandler,
noteRemoveHandler,
chadevCountHandler,
chadevListAllHandler,
chadevInfoHandler,
fatherHandler,
partyHandler,
whoBackHandler,
whatAreHandler,
musicHandler,
lunchHandler,
talkHandler,
addTalkHandler,
devTalkLinkHandler,
isAliveHandler,
groupListHandler,
groupDetailsHandler,
groupRSVPHandler,
devlunchRSVPHandler,
karmaHandler,
karmaStatsHandler,
karmaRankingHandler,
modHandler,
)
if err := robot.Run(); err != nil {
hal.Logger.Error(err)
return 1
}
return 0
}