#!/usr/bin/env ruby

begin

DEVELOPER COMMENTS

COOKIE(SHA256): 
        /home/$USER/.nova.cookie (for permanent file sharing systems)

USAGE: 
        ruby __FILE__ address port file1 file2 ... fileN

SENDING FORMAT:

        $FILENAME\n
        $LENGTH\n
        $FILE_BASE64

end

require ‘socket’ require ‘base64’ require ‘digest’

unless ARGV.length >= 3 # address, port, file(s)

abort("USAGE: nova-send ADDRESS PORT FILE1 FILE2 ... FILEn")

end

addr, port = ARGV files_rel = ARGV.drop(2)

files = []

for f in files_rel

files.concat Dir.glob(f)

end

ARGV.clear

def puts(s=nil); $stdout << (s ? “#{Time.now}

#{s}“ : ”“) << ”n“ ;end

puts “Connecting to #{addr}:#{port}…”

begin

$sock = TCPSocket.new(addr, port.to_i)

rescue Errno::ECONNREFUSED

abort("Could not connect to #{addr}:#{port}. Please check your settings")

else

puts "Connected."

end

md5 = {}

files.each_with_index do |file, index|

puts "Preparing transfer of file #{index+1}/#{files.length}"
raw_content = File.read(file)
fname,content = [ file , Base64.encode64(raw_content).gsub("\n","") ]
md5[file] = Digest::MD5.hexdigest(raw_content)
len = content.length
puts "Sending #{file}"
$sock.puts [fname,len,content].join("\n")
resp = $sock.gets.chomp
case resp
when "ok"
        puts "Sending complete"
when "err"
        puts "SENDING FAILED"
        abort
end

end

$sock.puts(“SOCKET_EOF”)

printf(“n”) printf(“Check your MD5s!n”) md5.each_pair do |k,v|

printf("%s:\n",k)
printf("  %s\n",v)
printf("\n")

end