-
Notifications
You must be signed in to change notification settings - Fork 1
/
whois.go
165 lines (137 loc) · 3.49 KB
/
whois.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
// This file is subject to a 1-clause BSD license.
// Its contents can be found in the enclosed LICENSE file.
package reputation
import (
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/chimeracoder/gopherbot/plugin"
"github.com/chimeracoder/gopherbot/proto"
"log"
"os"
"regexp"
"strings"
)
type RepChange struct {
entity string
delta int
}
var reputation map[string]int
var reputationChanges chan RepChange
var red redis.Conn
const WHOIS_DB = "2"
const WHOIS_SUFFIX = "-whois" // TODO remove this hack
type Plugin struct {
*plugin.Base
// Each entry holds a regex pattern which should be excluded
// from the url-title-lookup.
exclude []*regexp.Regexp
// Pattern which recognizes s-expressions.
// TODO use proper parser
sexpr *regexp.Regexp
}
func New(profile string) plugin.Plugin {
p := new(Plugin)
p.Base = plugin.New(profile, "url")
p.sexpr = regexp.MustCompile(`(\((whois) (.*?)\)|\((is) (.*?) (.*?)\))`)
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.parseSexpr(c, m)
})
ini := p.LoadConfig()
if ini == nil {
return
}
s := ini.Section("exclude")
list := s.List("url")
p.exclude = make([]*regexp.Regexp, len(list))
for i := range list {
p.exclude[i], err = regexp.Compile(list[i])
if err != nil {
return
}
}
return
}
// parseSexpr looks for s-expressions embedded in incoming messages.
// If they are valid s-expressions and not in the exclude list,
// we use them to fetch page titles from the internet.
func (p *Plugin) parseSexpr(c *proto.Client, m *proto.Message) {
list := p.sexpr.FindAllStringSubmatch(m.Data, -1)
if len(list) == 0 {
return
}
for _, sexpr := range list {
if !p.excluded(sexpr[0]) {
go whoIs(c, m, sexpr)
}
}
}
// excluded returns true if the given url is part of the exclusion list.
func (p *Plugin) excluded(url string) bool {
for _, excl := range p.exclude {
if excl.MatchString(url) {
return true
}
}
return false
}
// fetchTitle attempts to retrieve the title element for a given url.
func whoIs(c *proto.Client, m *proto.Message, match []string) {
log.Printf("Whois: %+v", match)
entity := strings.ToLower(match[3])
action := match[2]
response := "no idea who or what that is"
switch action {
case "whois":
descriptor_b, err := red.Do("GET", entity+WHOIS_SUFFIX)
if err != nil {
log.Print(err)
break
}
// Will be nil if not yet in the database
if descriptor_b == nil {
break
}
descriptor, ok := descriptor_b.([]byte)
if !ok {
fmt.Printf("ERROR: not a byte slice type: %+v", descriptor_b)
break
}
response = fmt.Sprintf("%s is %s", entity, string(descriptor)+"...")
default:
if match[4] != "is" {
log.Printf("action %s not supported", match[3])
return
}
entity = match[5]
descriptor := match[6]
_, err := red.Do("APPEND", entity+WHOIS_SUFFIX, descriptor+", ")
if err != nil {
log.Print(err)
return
}
response = fmt.Sprintf("%s is %s", entity, descriptor)
}
c.PrivMsg(m.Receiver, response)
}
func init() {
var err error
red, err = redis.Dial(os.Getenv("REDIS_NETWORK"), os.Getenv("REDIS_ADDRESS"))
if err != nil {
log.Printf("ERROR: Failed to connect to redis database - whois plugin will NOT load")
return
}
_, err = red.Do("AUTH", os.Getenv("REDIS_PASSWORD"))
if err != nil {
return
}
plugin.Register(New)
}