Created
May 30, 2011 15:02
-
-
Save sunnyone/999021 to your computer and use it in GitHub Desktop.
Tiarra DCC auto getter (child)
This file contains hidden or 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
#!/usr/bin/ruby -Ku | |
require 'socket' | |
require 'logger' | |
LOG_PATH = '/var/log/fetch_dcc.log' | |
DCC_DIR = '/mnt/dcc' | |
(nick, address, port, size, filename) = ARGV | |
port = port.to_i | |
size = size.to_i | |
logger = Logger.new(LOG_PATH) | |
logger.info("Start fetching DCC from #{address}:#{port} (size: #{size}) : #{filename}") | |
begin | |
recv_len = 8192 | |
recvd = 0 | |
dir = "#{DCC_DIR}/#{nick}" | |
path = "#{dir}/#{filename}" | |
if File.exists?(path) then | |
logger.error("File exists. exited: #{path}") | |
exit 2 | |
end | |
unless File.exists?(dir) then | |
Dir.mkdir(dir) | |
end | |
start_time = nil | |
end_time = nil | |
File.open(path, "w") do |fp| | |
TCPSocket.open(address, port) do |sock| | |
while !sock.eof? && (remain = size - recvd) > 0 | |
len = [remain, recv_len].min | |
buf = sock.read(len) | |
recvd += len | |
fp.write(buf) | |
end | |
end | |
end | |
rescue => e | |
logger.info("Failed to fetch from #{address}:#{port} : #{e}") | |
exit 1 | |
end | |
logger.info("Finished fetch DCC from #{address}:#{port}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment