This repository has been archived by the owner on Jan 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
irc.rb
252 lines (201 loc) · 6.81 KB
/
irc.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
class Service::IRC < Service
string :server, :port, :room, :nick, :branch_regexes
password :password
boolean :ssl, :message_without_join, :no_colors, :long_url, :notice
white_list :server, :port, :room, :nick
default_events :push, :pull_request
def receive_push
return unless branch_name_matches?
messages = []
messages << "#{irc_push_summary_message}: #{fmt_url url}"
messages += distinct_commits.first(3).map {
|commit| self.irc_format_commit_message(commit)
}
send_messages messages
end
def receive_pull_request
return unless opened?
send_messages "#{irc_pull_request_summary_message} #{fmt_url url}"
end
def receive_issues
return unless opened?
send_messages "#{irc_issue_summary_message} #{fmt_url url}"
end
def send_messages(messages)
messages = Array(messages)
if data['no_colors'].to_i == 1
messages.each{|message|
message.gsub!(/\002|\017|\026|\037|\003\d{0,2}(?:,\d{1,2})?/, '')}
end
rooms = data['room'].to_s
if rooms.empty?
raise_config_error "No rooms: #{rooms.inspect}"
return
end
rooms = rooms.gsub(",", " ").split(" ").map{|room| room[0].chr == '#' ? room : "##{room}"}
botname = data['nick'].to_s.empty? ? "GitHub#{rand(200)}" : data['nick']
command = data['notice'].to_i == 1 ? 'NOTICE' : 'PRIVMSG'
irc_puts "PASS #{data['password']}" if !data['password'].to_s.empty?
irc_puts "NICK #{botname}"
irc_puts "MSG NICKSERV IDENTIFY #{data['nickservidentify']}" if !data['nickservidentify'].to_s.empty?
irc_puts "USER #{botname} 8 * :GitHub IRCBot"
loop do
case irc_gets
when / 00[1-4] #{Regexp.escape(botname)} /
break
when /^PING\s*:\s*(.*)$/
irc_puts "PONG #{$1}"
end
end
without_join = data['message_without_join'] == '1'
rooms.each do |room|
room, pass = room.split("::")
irc_puts "JOIN #{room} #{pass}" unless without_join
messages.each do |message|
irc_puts "#{command} #{room} :#{message}"
end
irc_puts "PART #{room}" unless without_join
end
irc_puts "QUIT"
irc_response = []
irc_response << irc_gets unless irc_eof?
rescue SocketError => boom
if boom.to_s =~ /getaddrinfo: Name or service not known/
raise_config_error 'Invalid host'
elsif boom.to_s =~ /getaddrinfo: Servname not supported for ai_socktype/
raise_config_error 'Invalid port'
else
raise
end
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
raise_config_error 'Invalid host'
rescue OpenSSL::SSL::SSLError
raise_config_error 'Host does not support SSL'
end
def irc_gets
irc.gets
end
def irc_eof?
irc.eof?
end
def irc_puts(*args)
irc.puts *args
end
def irc
@irc ||= begin
socket = TCPSocket.open(data['server'], port)
socket = new_ssl_wrapper(socket) if use_ssl?
socket
end
end
def new_ssl_wrapper(socket)
ssl_context = OpenSSL::SSL::SSLContext.new
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
ssl_socket.sync_close = true
ssl_socket.connect
ssl_socket
end
def use_ssl?
data['ssl'].to_i == 1
end
def default_port
use_ssl? ? 9999 : 6667
end
def port
data['port'] ? data['port'].to_i : default_port
end
def url
data['long_url'].to_i == 1 ? summary_url : shorten_url(summary_url)
end
### IRC message formatting. For reference:
### \002 bold \003 color \017 reset \026 italic/reverse \037 underline
### 0 white 1 black 2 dark blue 3 dark green
### 4 dark red 5 brownish 6 dark purple 7 orange
### 8 yellow 9 light green 10 dark teal 11 light teal
### 12 light blue 13 light purple 14 dark gray 15 light gray
def fmt_url(s)
"\00302\037#{s}\017"
end
def fmt_repo(s)
"\00313#{s}\017"
end
def fmt_name(s)
"\00315#{s}\017"
end
def fmt_branch(s)
"\00306#{s}\017"
end
def fmt_tag(s)
"\00306#{s}\017"
end
def fmt_hash(s)
"\00314#{s}\017"
end
def irc_push_summary_message
message = []
message << "\00301[#{fmt_repo repo_name}\00301] #{fmt_name pusher_name}"
if created?
if tag?
message << "tagged #{fmt_tag tag_name} at"
message << (base_ref ? fmt_branch(base_ref_name) : fmt_hash(after_sha))
else
message << "created #{fmt_branch branch_name}"
if base_ref
message << "from #{fmt_branch base_ref_name}"
elsif distinct_commits.empty?
message << "at #{fmt_hash after_sha}"
end
num = distinct_commits.size
message << "(+\002#{num}\017 new commit#{num != 1 ? 's' : ''})"
end
elsif deleted?
message << "\00304deleted\017 #{fmt_branch branch_name} at #{fmt_hash before_sha}"
elsif forced?
message << "\00304force-pushed\017 #{fmt_branch branch_name} from #{fmt_hash before_sha} to #{fmt_hash after_sha}"
elsif commits.any? and distinct_commits.empty?
if base_ref
message << "merged #{fmt_branch base_ref_name} into #{fmt_branch branch_name}"
else
message << "fast-forwarded #{fmt_branch branch_name} from #{fmt_hash before_sha} to #{fmt_hash after_sha}"
end
else
num = distinct_commits.size
message << "pushed \002#{num}\017 new commit#{num != 1 ? 's' : ''} to #{fmt_branch branch_name}"
end
message.join(' ')
end
def irc_format_commit_message(commit)
short = commit['message'].split("\n", 2).first.to_s
short += '...' if short != commit['message']
author = commit['author']['name']
sha1 = commit['id']
files = Array(commit['modified'])
dirs = files.map { |file| File.dirname(file) }.uniq
"#{fmt_repo repo_name}/#{fmt_branch branch_name} #{fmt_hash sha1[0..6]} " +
"#{fmt_name commit['author']['name']}: #{short}"
end
def irc_issue_summary_message
"[#{fmt_repo repo.name}] #{fmt_name sender.login} #{action} issue \##{issue.number}: #{issue.title}"
rescue
raise_config_error "Unable to build message: #{$!.to_s}"
end
def irc_pull_request_summary_message
base_ref = pull.base.label.split(':').last
head_ref = pull.head.label.split(':').last
head_label = head_ref != base_ref ? head_ref : pull.head.label
"[#{fmt_repo repo.name}] #{fmt_name sender.login} #{action} pull request " +
"\##{pull.number}: #{pull.title} (#{fmt_branch base_ref}...#{fmt_branch head_ref})"
rescue
raise_config_error "Unable to build message: #{$!.to_s}"
end
def branch_name_matches?
return true if data['branch_regexes'].nil?
return true if data['branch_regexes'].strip == ""
branch_regexes = data['branch_regexes'].split(',')
branch_regexes.each do |regex|
return true if Regexp.new(regex) =~ branch_name
end
false
end
end