forked from carlasouza/gitlab-irc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab-irc.rb
53 lines (47 loc) · 2.12 KB
/
gitlab-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
require 'rubygems'
require 'sinatra'
require 'json'
require 'socket'
require 'yaml'
config = YAML.load(File.read(File.expand_path("../config.yml", __FILE__)))
# IRC Config
IRC_HOST = config[:IRC][:Host]
IRC_PORT = config[:IRC][:Port]
IRC_CHANNEL = config[:IRC][:Channel]
# !! Channel must be with mode n disabled to allow extern messages !!
IRC_NICK = config[:IRC][:Nick]
IRC_REALNAME = config[:IRC][:RealName]
DEBUG = config[:Debug]
$socket = TCPSocket.open(IRC_HOST, IRC_PORT)
$socket.puts("NICK #{IRC_NICK}")
$socket.puts("USER #{IRC_NICK} #{IRC_NICK} #{IRC_NICK} #{IRC_REALNAME}")
Thread.new do
while line = $socket.gets
puts line if DEBUG
line = line.split
if line[0] == 'PING'
$socket.puts("PONG "+line[1] )
puts "PONG "+line[1] if DEBUG
end
end
end
post '/*' do
$channel = (request.path_info == '/') ? IRC_CHANNEL : '#' + request.path_info.split('/')[1]
puts $channel if DEBUG
json = JSON.parse(request.env["rack.input"].read)
puts json if DEBUG
$socket.puts "NOTICE #{$channel} : #{json['total_commits_count']} new Commits for \x0306#{json['repository']['name']}\x0315 by #{2.chr} #{json['user_name']} #{15.chr} : #{json['repository']['homepage']}/compare/#{json['before'].slice(0,7)}...#{json['after'].slice(0,7)}"
puts "NOTICE #{$channel} : #{json['total_commits_count']} new Commits for \x0306#{json['repository']['name']}\x0315 by #{2.chr} #{json['user_name']} #{15.chr} : #{json['repository']['homepage']}/compare/#{json['before'].slice(0,7)}...#{json['after'].slice(0,7)}" if DEBUG
i = 0
json['commits'].each do |commit|
$socket.puts "NOTICE #{$channel} :by #{2.chr + commit['author']['name'] + 15.chr } | \x0309#{commit['message'].split("\n")[0]}\x0315 | #{json['repository']['homepage'] + "/commit/" + commit['id'].slice(0,7)}"
puts "NOTICE #{$channel} :by #{2.chr + commit['author']['name'] + 15.chr } | \x0309#{commit['message'].split("\n")[0]}\x0315 | #{json['repository']['homepage'] + "/commit/" + commit['id'].slice(0,7)}" if DEBUG
i += 1
if i == 3
break
end
end
if i < json['commits'].length
$socket.puts "NOTICE #{$channel} : ... And many others."
end
end