Created
December 25, 2013 11:58
-
-
Save ssig33/8122613 to your computer and use it in GitHub Desktop.
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
require 'celluloid/io' | |
require 'ircp' | |
require 'thread' | |
require 'sinatra/base' | |
require 'json' | |
$q = Queue.new | |
class Client | |
include Celluloid::IO | |
def initialize(options) | |
@host = options[:host] | |
@port = options[:port] | |
@nick = options[:nick] | |
@user = options[:user] || @nick | |
@real = options[:real] || @nick | |
@pass = options[:pass] | |
@socket = TCPSocket.new @host, @port | |
end | |
def send(*args) | |
msg = Ircp::Message.new(*args) | |
puts "SEND: #{msg}" | |
@socket.write msg.to_irc | |
end | |
def run | |
attach | |
t = [] | |
t << Thread.start{ | |
loop do | |
sleep 1 | |
begin | |
@buffer ||= '' | |
@buffer << @socket.readpartial(4096) | |
while data = @buffer.slice!(/(.+)\r\n/, 1) | |
msg = Ircp.parse data | |
method = "on_#{msg.command.to_s.downcase}" | |
__send__ method, msg if respond_to? method | |
end | |
rescue => e | |
puts e.to_s | |
end | |
end | |
} | |
t << Thread.start{ | |
while q = $q.pop | |
p q | |
send(*q) | |
end | |
} | |
t.each{|x| x.join} | |
rescue => e | |
puts e.to_s | |
end | |
def attach | |
send 'PASS', @pass if @pass | |
send 'NICK', @nick | |
send 'USER', @user, '*', '*', @real | |
end | |
def on_ping(msg) | |
send 'PONG' | |
end | |
end | |
options = { | |
:host => '', | |
:port => 6667, | |
:nick => '', | |
pass: '' | |
} | |
client = Client.new options | |
t = [] | |
t << Thread.start{client.run} | |
t << Thread.start{ | |
class WebAPI < Sinatra::Base | |
post '/' do | |
$q << JSON.parse(params[:q]) | |
'true' | |
end | |
end | |
WebAPI.run! port: ARGV[0].to_i | |
} | |
t.each{|x| x.join} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment