Last active
December 12, 2015 02:08
-
-
Save erukiti/4696525 to your computer and use it in GitHub Desktop.
fluentd forward を受け付けて、IRC に流す bot のサンプル(fluent-catにのみ対応ver)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! ruby | |
# coding: utf-8 | |
CONF = { | |
server: "<IRC SERVER HOST>", | |
port: 6667, | |
nick: "<NICK>", | |
user: "<USERNAME>", | |
real: "<REALNAME>", | |
channel: "<IRC CHANNEL>", | |
} | |
require 'net/irc' | |
class OutIRC < Net::IRC::Client | |
def initialize(*args) | |
super | |
@queue = args[2][:queue] | |
@channel = CONF[:channel] | |
@mutex = Mutex.new | |
@timer_thread = Thread.new do | |
while true | |
sleep(1) | |
on_timer | |
end | |
end | |
end | |
def on_rpl_welcome(m) | |
post JOIN, @channel | |
@counter = Time.now() - 60 * 60 * 4 | |
end | |
def on_timer | |
while data = @queue.pop | |
post NOTICE, @channel, data.inspect | |
end | |
end | |
def post(command, *params) | |
@mutex.synchronize do | |
super | |
end | |
end | |
end | |
class Queue | |
def initialize | |
@mutex = Mutex.new | |
@buffer = [] | |
end | |
def <<(data) | |
@mutex.synchronize do | |
@buffer << data | |
end | |
end | |
def pop | |
data = nil | |
@mutex.synchronize do | |
data = @buffer.shift | |
end | |
data | |
end | |
end | |
require 'msgpack' | |
require 'socket' | |
class InForward | |
def initialize(queue) | |
@port = 24224 | |
@server = TCPServer.open(@port) | |
@queue = queue | |
end | |
def run | |
Thread.start(@server.accept) do |s| | |
while s.gets | |
parsed = MessagePack.unpack($_) | |
tag = parsed[0] | |
parsed[1].each do |data| | |
@queue << {:tag => tag, :time => data[0], :data => data[1]} | |
end | |
end | |
end | |
end | |
end | |
queue = Queue.new | |
in_forward = InForward.new(queue) | |
Thread.start do | |
while true | |
in_forward.run | |
end | |
end | |
Bot.new(CONF[:server], CONF[:port], { | |
:nick => CONF[:nick], | |
:user => CONF[:user], | |
:real => CONF[:real], | |
:queue => queue | |
}).start | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment