Skip to content

Instantly share code, notes, and snippets.

@sunnyone
Created May 30, 2011 15:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunnyone/999021 to your computer and use it in GitHub Desktop.
Save sunnyone/999021 to your computer and use it in GitHub Desktop.
Tiarra DCC auto getter (child)
#!/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