/
main.go
211 lines (189 loc) · 6.06 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/plouc/go-jira-client"
irc "github.com/thoj/go-ircevent"
goyaml "gopkg.in/yaml.v2"
"io/ioutil"
"log"
"regexp"
"strings"
"time"
)
// Config struct to store configurable data in a yaml file
type Config struct {
Host string `yaml:"host"`
APIPath string `yaml:"api_path"`
ActivityPath string `yaml:"activity_path"`
Login string `yaml:"login"`
Password string `yaml:"password"`
IRCHostname string `yaml:"irc_hostname"`
IRCPass string `yaml:"irc_pass"`
IRCUsername string `yaml:"irc_username"`
IRCNick string `yaml:"irc_nick"`
IRCNickPass string `yaml:"irc_nick_pass"`
IRCChannels []string `yaml:"irc_channels"`
}
var (
imgurExtRegex = regexp.MustCompile(`.*\.\w\w\wv?$`)
urlRegex = regexp.MustCompile(`\b(https?://?[\dA-Za-z\.-]+[/\w\.\+\?\:\;\#\&=-]*)`)
jiraRegex = regexp.MustCompile(`^https?://jira2.advance.net/browse/([a-zA-Z0-9-]+)?.*$`)
config = new(Config)
)
func jiraScrape(url string, messageCh chan string) {
jira := gojira.NewJira(
config.Host,
config.APIPath,
config.ActivityPath,
&gojira.Auth{config.Login, config.Password},
)
if jiraRegex.MatchString(url) {
match := jiraRegex.FindStringSubmatch(url)[1]
log.Printf("Match: '%s'\n", match)
issue := jira.Issue(match)
//log.Printf("Jira: %#v\n", issue.Fields)
messageCh <- fmt.Sprintf("%s - %s\n", issue.Key, issue.Fields.Summary)
// TODO: Check len on the following to avoid flooding
//fmt.Printf("%s\n", issue.Fields.Summary)
} else {
log.Printf("Can't find JIRA match for: %s\n", url)
}
}
// youtubeScrape get's the title from a youtube video page.
func youtubeScrape(url string, messageCh chan string) {
doc, err := goquery.NewDocument(url)
if err != nil {
log.Print(err)
}
doc.Find("#eow-title").Each(func(i int, s *goquery.Selection) {
log.Printf("Youtube: %s\n", strings.Trim(s.Text(), "\n "))
messageCh <- fmt.Sprintf("YouTube: %s\n", strings.Trim(s.Text(), "\n "))
})
}
// imgurScrape get's the title from a imgur album page.
// This differs from vimgurScrape in the html container it's in
func imgurScrape(url string, messageCh chan string) {
doc, err := goquery.NewDocument(url)
if err != nil {
log.Print(err)
}
doc.Find("div#content .album-description").Each(func(i int, s *goquery.Selection) {
title := strings.Trim(s.Find("h1").Text(), "\n ")
if title != "Imgur" {
log.Printf("Imgur: %s\n", title)
messageCh <- fmt.Sprintf("Imgur: %s\n", title)
}
})
}
func vimgurScrape(url string, messageCh chan string) {
doc, err := goquery.NewDocument(url)
if err != nil {
log.Print(err)
}
doc.Find("head title").Each(func(i int, s *goquery.Selection) {
title := strings.Trim(s.Text(), "\n ")
if title != "Imgur" {
log.Printf("Imgur: %s\n", title)
messageCh <- fmt.Sprintf("Imgur: %s\n", title)
}
})
}
// scrapePage figures out what kind of page we're dealing with and calls the appropriate func.
func scrapePage(url string, messageCh chan string) {
switch {
// Commented out youtube since we have another bot that does this in channel
// case strings.Contains(url, "youtu.be"):
// youtubeScrape(url, messageCh)
// case strings.Contains(url, "youtube"):
// youtubeScrape(url, messageCh)
case strings.Contains(url, "imgur.com/a"):
imgurScrape(url, messageCh)
case strings.Contains(url, "imgur.com/gallery"):
vimgurScrape(url, messageCh)
case strings.Contains(url, ".gifv"): // .gifv images are actually pages
vimgurScrape(url, messageCh)
case imgurExtRegex.MatchString(url): // has an extension (ex. .jpg) but not a .gifv
vimgurScrape(url[:len(url)-3]+"gifv", messageCh) // Remove the extension and add a .gifv which seems to work
case strings.Contains(url, "jira2.advance.net"):
jiraScrape(url, messageCh)
default:
log.Printf("Unknown url type: %s\n", url)
}
}
// UnmarshalConfig reads in the config.yaml file
func UnmarshalConfig(filename string) {
file, e := ioutil.ReadFile(filename)
if e != nil {
log.Fatalf("Problem reading config file: %v\n", e)
}
err := goyaml.Unmarshal(file, &config)
if err != nil {
log.Fatalf("Problem unmarshalling config file: %v\n", err)
}
}
// ConnectIRC is used to connect to IRC
func ConnectIRC() *irc.Connection {
con := irc.IRC(config.IRCNick, config.IRCUsername)
if config.IRCPass != "" {
con.Password = config.IRCPass
}
err := con.Connect(config.IRCHostname)
if err != nil {
log.Fatalf("Problem connecting to irc: %s", err)
}
return con
}
// WelcomeCallback is a PrivMsgCallback for the welcome event 001, join channels when we're logged in
func WelcomeCallback(con *irc.Connection) {
con.AddCallback("001", func(e *irc.Event) {
con.Privmsgf("NickServ", "identify %s", config.IRCNickPass)
time.Sleep(time.Second * 15) // Need to wait a little for the registration to take effect
for _, room := range config.IRCChannels {
con.Join(room)
log.Printf("Connected to channel %s\n", room)
}
})
}
// JoinCallback triggers on JOIN event which happens whenever *anyone* joins
func JoinCallback(con *irc.Connection) {
con.AddCallback("JOIN", func(e *irc.Event) {
if e.Nick != config.IRCNick { // Exclude bot joins
log.Printf("%s joined %s", e.Nick, e.Arguments[0])
} else {
log.Printf(e.Message())
}
})
}
// PrivMsgCallback (PRIVMSG) is really any message in IRC that the bot sees
func PrivMsgCallback(con *irc.Connection) {
con.AddCallback("PRIVMSG", func(e *irc.Event) {
message := e.Arguments[1]
channel := e.Arguments[0]
log.Printf("%s said: %s\n", e.Nick, message)
// Look for urls in messages
urls := urlRegex.FindAllString(message, -1)
if len(urls) > 0 {
for _, url := range urls {
messageCh := make(chan string, 1)
go scrapePage(url, messageCh)
con.Privmsg(channel, <-messageCh)
}
}
})
}
// ErrorCallback should log anytime an error happens
func ErrorCallback(con *irc.Connection) {
con.AddCallback("ERROR", func(e *irc.Event) {
log.Printf("Error: %#v\n", e)
})
}
func main() {
UnmarshalConfig("config.yaml")
con := ConnectIRC()
WelcomeCallback(con)
JoinCallback(con)
PrivMsgCallback(con)
ErrorCallback(con)
con.Loop()
}