-
Notifications
You must be signed in to change notification settings - Fork 1
/
describe.go
80 lines (62 loc) · 1.74 KB
/
describe.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
// This file is subject to a 1-clause BSD license.
// Its contents can be found in the enclosed LICENSE file.
package describe
import (
"fmt"
"github.com/chimeracoder/gopherbot/plugin"
"github.com/chimeracoder/gopherbot/proto"
"log"
"regexp"
)
const otherBotUsername = "manyabot"
//This regex will check if a URL points to a Twitter status
var descriptionRegex = regexp.MustCompile(`ACTION is (.*)`)
func init() { plugin.Register(New) }
type Plugin struct {
*plugin.Base
// Each entry holds a regex pattern which should be excluded
exclude []*regexp.Regexp
// Pattern which recognizes descriptions.
description *regexp.Regexp
}
func New(profile string) plugin.Plugin {
p := new(Plugin)
p.Base = plugin.New(profile, "description")
p.description = descriptionRegex
return p
}
// Init initializes the plugin. it loads configuration data and binds
// commands and protocol handlers.
func (p *Plugin) Load(c *proto.Client) (err error) {
err = p.Base.Load(c)
if err != nil {
return
}
c.Bind(proto.CmdPrivMsg, func(c *proto.Client, m *proto.Message) {
p.parseDescription(c, m)
})
ini := p.LoadConfig()
if ini == nil {
return
}
s := ini.Section("exclude")
list := s.List("description")
p.exclude = make([]*regexp.Regexp, len(list))
for i := range list {
p.exclude[i], err = regexp.Compile(list[i])
if err != nil {
return
}
}
return
}
// parseURL looks for descriptions in incoming messages.
func (p *Plugin) parseDescription(c *proto.Client, m *proto.Message) {
list := p.description.FindStringSubmatch(m.Data)
if len(list) == 0 {
return
}
description := list[len(list)-1]
log.Printf("Found description: %s", description)
c.PrivMsg(m.Receiver, fmt.Sprintf("%s %s is %s", otherBotUsername, m.SenderName, description))
}