-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitles.go
180 lines (149 loc) · 4.22 KB
/
titles.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
package plugins
import (
"fmt"
"net/url"
"strings"
"sync"
"regexp"
"github.com/PuerkitoBio/goquery"
badger "github.com/dgraph-io/badger/v2"
base "github.com/jriddick/geoffrey/bot"
"github.com/jriddick/geoffrey/irc"
"github.com/jriddick/geoffrey/msg"
"github.com/mvdan/xurls"
log "github.com/sirupsen/logrus"
)
func init() {
base.RegisterHandler(TitleHandler)
}
func fetchTitle(bot *base.Bot, uri *url.URL, channel string, text string) {
// Add missing scheme if possible
if uri.Scheme == "" {
uri.Scheme = "http"
}
// Get the database from the bot
db := bot.Db()
// Fetch the document
doc, err := goquery.NewDocument(uri.String())
// Notify on error
if err != nil {
log.Errorf("[title] Could not fetch website '%s': %v", uri.String(), err)
} else {
// Clean the title
title := strings.TrimSpace(replacer.ReplaceAllString(
doc.Find("title").First().Text(),
" "),
)
// Find the title
bot.Send(channel, fmt.Sprintf("[%s] %s",
irc.Foreground("LINK", irc.Green),
irc.Bold(
title,
),
))
// Save the title for future use
if err := db.Update(func(txn *badger.Txn) error {
return txn.Set([]byte(text), []byte(title))
}); err != nil {
log.Errorf("[title] Could not save title to database: %v", err)
}
}
// Mark as done
wg.Done()
}
// Waiter so we can wait for it to finish before returning
var wg sync.WaitGroup
// Blacklist for links that should not be handled
var blacklist []*regexp.Regexp
// Regex replacer for cleaning titles
var replacer = regexp.MustCompile("[\r\n]+")
// TitleHandler extracts title from posted links and sends
// them to the channel.
var TitleHandler = base.Handler{
Name: "Title",
Description: "Extracts title and website information upon detecting URLs",
Event: irc.Message,
Init: func(bot *base.Bot) (bool, error) {
// Get the configuration
config := bot.Config()
// Get the blacklist list from the configs
if settings, ok := config.Settings["title"]; ok {
if list, ok := settings.(map[interface{}]interface{})["blacklist"]; ok {
for _, matcher := range list.([]interface{}) {
if regex, err := regexp.Compile(matcher.(string)); err != nil {
log.Errorf("[title] Could not compile regex '%s'", matcher)
} else {
blacklist = append(blacklist, regex)
}
}
}
}
return true, nil
},
Run: func(bot *base.Bot, msg *msg.Message) (bool, error) {
// Get configuration
config := bot.Config()
// Check if channel message
if msg.Params[0] == config.Identification.Nick || msg.Prefix.Name == "nibbler" || msg.Prefix.Name == "geoffrey-bot" {
return false, nil
}
// Extract the urls
urls := xurls.Relaxed.FindAllString(msg.Trailing, -1)
// Add the amount of urls needed
wg.Add(len(urls))
// Check if we have nothing to do
if len(urls) < 1 {
return false, nil
}
// Get the database
db := bot.Db()
// Open a read transacation to the database
db.View(func(txn *badger.Txn) error {
// Download the information from the webpage
for _, text := range urls {
// Set to true to to skip the urls from being handled
skip := false
// Go through all blacklists
for _, matcher := range blacklist {
if matcher.MatchString(text) {
log.Infof("[title] Skipped link '%s' due to blacklist.", text)
skip = true
break
}
}
if !skip {
if uri, err := url.Parse(text); err != nil {
log.Errorf("[title] Could not parse url '%s': %v", text, err)
} else {
// Look for the URL in the database
value, err := txn.Get([]byte(text))
// Check if it was found or not
if err != nil {
if err != badger.ErrKeyNotFound {
log.Errorf("[title] Could not query the database: %v", err)
} else {
log.Infof("[title] Fetching title for url '%s'", text)
// Fetch the title from the website
go fetchTitle(bot, uri, msg.Params[0], text)
}
} else {
value.Value(func(val []byte) error {
bot.Send(msg.Params[0], fmt.Sprintf("[%s] %s",
irc.Foreground("LINK", irc.Green),
irc.Bold(
string(val),
),
))
return nil
})
}
}
}
}
return nil
})
// Wait for it to complete
wg.Wait()
return true, nil
},
}