This repository has been archived by the owner on Jan 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBaxter.rb
executable file
·218 lines (195 loc) · 6.89 KB
/
Baxter.rb
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
212
213
214
215
216
217
require 'rubygems'
require 'cinch'
require 'cgi'
require 'open-uri'
require 'nokogiri'
require 'parseconfig'
require 'octokit'
$config = ParseConfig.new('credentials.cfg')
$urlhelpers = Hash["c" => "Core", "p" => "Portals", "n" => "NetherPortals", "s" => "SignPortals", "a" => "Adventure",
"core" => "Core", "portals" => "Portals", "netherportals" => "NetherPortals", "signportals" => "SignPortals", "adventure" => "Adventure"]
$authrequired = true
bot = Cinch::Bot.new do
configure do |c|
c.server = "irc.esper.net"
c.nick = "MV-Baxter"
c.user = $config.get_value('username')
c.password = $config.get_value('password')
c.channels = ARGV
end
helpers do
def get_repo(abrev)
return $urlhelpers[abrev.downcase]
end
def issue(section, issue)
begin
return Octokit.issue("Multiverse/Multiverse-#{get_repo(section)}", CGI.escape(issue)).html_url
rescue Octokit::NotFound
return "Sorry, issue ##{issue} didn't exist on #{get_repo(section)}!"
end
end
def open_issues(section, milestone)
open_issues = 0
closed_issues = 0
if milestone != nil and milestone.length > 0
milestones = Octokit.milestones("Multiverse/Multiverse-#{get_repo(section)}").map{|mile| [mile.title, mile.number]}
matches = []
matchnames = []
milestones.each do |ms|
if ms[0].downcase.include?(milestone.downcase)
matches<<ms[1]
matchnames<<ms[0]
end
end
# Now go get all of those milestones
matches.each do |match|
open_issues += Octokit.issues("Multiverse/Multiverse-#{get_repo(section)}", {:milestone => match}).count
closed_issues += Octokit.issues("Multiverse/Multiverse-#{get_repo(section)}", {:milestone => match, :state => "closed"}).count
end
return "There are #{open_issues} open and #{closed_issues} in #{get_repo(section)} for Milestone(s) #{matchnames}."
else
open_issues += Octokit.issues("Multiverse/Multiverse-#{get_repo(section)}").count
closed_issues += Octokit.issues("Multiverse/Multiverse-#{get_repo(section)}", {:state => "closed"}).count
return "There are #{open_issues} open and #{closed_issues} in #{get_repo(section)}."
end
end
def commit(hash)
$urlhelpers.each_value do |section|
url = "https://github.com/Multiverse/Multiverse-#{section}/commit/#{CGI.escape(hash)}"
begin
doc = Nokogiri::HTML(open(url))
return url
rescue OpenURI::HTTPError
# Keep trying
end
end
return nil
end
def wiki(sections, search)
actualresults = []
sections.each do |section|
actualsection = $urlhelpers[section]
url = "https://github.com/Multiverse/Multiverse-#{actualsection}/wiki/_pages"
doc = Nokogiri::HTML(open(url))
if doc == nil
return "Couldn't find the #{actualsection} wiki!!!"
end
things = doc.xpath("//div[@id='template']//ul//li//a")
pagelist = things.map{|link| [link.children.text, link['href']]}
pagelist.each do |result|
if(result[0].downcase.include?(CGI.escape(search)))
actualresults<<"http://github.com" + result[1]
end
end
end
if actualresults.count > 3
return actualresults[0, 5]<<"..."
elsif actualresults.count == 0
return ["No results found :("]
end
return actualresults
end
def latest(plugins)
if plugins == nil
plugins = ["c", "p", "n", "s", "a"]
end
results = ""
plugins.each do |plugin|
actual = $urlhelpers[plugin]
url = "http://ci.onarandombox.com/job/Multiverse-#{actual}/lastSuccessfulBuild/"
doc = Nokogiri::HTML(open(url))
results<<"#{actual} " + doc.xpath("//h1").first.text.strip.gsub(/\s+\(.*\)/, ", ")
end
return results[0..-3]
end
end
# TODO: Move these to a file
on :message, /^wiki$/i do |m|
if m.channel.opped?(m.user) || m.channel.voiced?(m.user)
m.reply "https://github.com/Multiverse/Multiverse-Core/wiki/"
end
end
on :message, /^ci$/i do |m|
if m.channel.opped?(m.user) || m.channel.voiced?(m.user)
m.reply "http://ci.onarandombox.com/view/Multiverse"
end
end
on :message, /^jd$/i do |m|
if m.channel.opped?(m.user) || m.channel.voiced?(m.user)
m.reply "http://ci.onarandombox.com/job/Multiverse-Core"
end
end
on :message, /^forum$/i do |m|
if m.channel.opped?(m.user) || m.channel.voiced?(m.user)
m.reply "http://forums.bukkit.org/threads/3707/page-9999"
end
end
on :message, /^latest$/i do |m|
if m.channel.opped?(m.user) || m.channel.voiced?(m.user)
m.reply(latest(nil))
end
end
on :message, "fish" do |m|
if m.user.authname == "fernferret"
m.reply "Hello master Fern..."
bot.logger.debug m.user
end
end
on :message, /^issues\s*([^\s]*)?\s*([^\s]*)?$/i do |m, section, mile|
if m.channel.opped?(m.user) || m.channel.voiced?(m.user)
m.reply(open_issues(section, mile))
end
end
on :message, /\b([0-9a-f]{5,40})\b/ do |m, hash|
# Ensure we're not in a loop.
if m.user.authname == $config.get_value('username')
return
end
if m.channel.opped?(m.user) || m.channel.voiced?(m.user)
url = commit(hash)
if url
m.reply(url)
end
end
end
on :message, /(hello|hi|greetings)\s*baxter/i do |m|
if m.channel.opped?(m.user) || m.channel.voiced?(m.user)
greetings = ["Hello", "Bonjour", "Hi", "Aloha", "Sup"]
m.reply(greetings[rand(greetings.size)] + " #{m.user.nick}!")
end
end
on :message, "help" do |m|
m.user.send("Hi, My name is Baxter and I'm a super helpful bot.")
m.user.send("I'm here to help, but I'm currently being developed.")
m.user.send("Only OPs and VOICEs in #multiverse can use me for now!")
end
on :message, /!issue-?([cpnsa])\s?#(\d+)/i do |m, type, issue|
if m.user.authname == "fernferret"
m.reply "Hello master Fern..."
m.reply(issue(type, issue))
elsif m.channel.opped?(m.user)
m.reply "Hello cool person #{m.user.nick}!"
m.reply(issue(type, issue))
elsif m.channel.voiced?(m.user)
m.reply "Hello super person #{m.user.nick}!"
m.reply(issue(type, issue))
end
end
on :message, /!wiki-?([cpnsa])\s?:(.+)/i do |m, type, page|
if m.channel.opped?(m.user) || m.channel.voiced?(m.user)
wikiresults = wiki([type], page)
wikiresults.each do |result|
m.reply(result)
end
end
end
on :message, /!wiki\s?:(.+)/i do |m, page|
if m.channel.opped?(m.user) || m.channel.voiced?(m.user)
wikiresults = wiki(["c", "p", "n", "s"], page)
wikiresults.each do |result|
m.reply(result)
end
end
end
end
bot.start