This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
relay.rb
executable file
·111 lines (84 loc) · 2.68 KB
/
relay.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
#!/usr/bin/env ruby
require 'yaml'
require 'thread'
require 'xmpp4r'
require 'xmpp4r/muc'
require 'isaac/bot'
module SophSec
module XMPP
module IRC
class Relay < Isaac::Bot
include Jabber
FLOOD_LIMIT = 2.0
def initialize(options={},&block)
@xmpp_user = JID.new(options[:xmpp][:user])
@xmpp_password = options[:xmpp][:password]
@xmpp_channel = JID.new(options[:xmpp][:channel])
@xmpp_channel.resource ||= @xmpp_user.resource
@irc_channel = "##{options[:irc][:channel]}"
@flood_limit = (options[:irc][:flood_limit] || FLOOD_LIMIT)
@mesg_queue = Queue.new
super()
configure do |c|
c.nick = options[:irc][:nick]
c.server = options[:irc][:server]
c.port = options[:irc][:port] if options[:irc][:port]
end
on :connect do
puts "Joining #{@irc_channel}"
join @irc_channel
@xmpp = Client.new(@xmpp_user)
puts "Connecting to #{@xmpp_user.domain}"
@xmpp.connect
puts "Logging in as #{@xmpp_user}"
@xmpp.auth(@xmpp_password) if @xmpp_password
@muc = MUC::SimpleMUCClient.new(@xmpp)
@muc.on_message { |time,nick,text| to_irc(nick,text) }
puts "Joining #{@xmpp_channel}"
@muc.join(@xmpp_channel)
@consumer = Thread.new do
loop do
sleep(@flood_limit)
msg(@irc_channel,@mesg_queue.pop)
end
end
puts "Relaying messages between #{@xmpp_channel} and #{@irc_channel} on #{@config.server}:#{@config.port}"
end
on :channel, /\001ACTION ([^\001]*)\001/ do
to_xmpp(nick,"/me #{match[0]}")
end
on :channel do
to_xmpp(nick,message)
end
end
def self.start(options={})
self.new(options).start
end
def start
begin
super()
rescue Interrupt
ensure
@consumer.kill if (@consumer && @consumer.alive?)
@muc.exit if (@muc && @muc.active?)
@xmpp.close if @xmpp
end
end
protected
def to_xmpp(from,text)
if @muc
@muc.say("#{from}: #{text}") unless text.strip.empty?
end
end
def to_irc(from,text)
unless from == @xmpp_channel.resource
text.each_line do |line|
@mesg_queue << "#{from}: #{line.chomp}"
end
end
end
end
end
end
end
SophSec::XMPP::IRC::Relay.start(YAML.load_file(ARGV[0])) if ARGV[0]