#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Â Â Â Â Â Â Â Â Â Download & Upload Files with RGSS
#  par berka            v 2.1          rgss 1
#             [url=http://www.rpgmakervx-fr.com ]http://www.rpgmakervx-fr.com [/url]              # azirht001 bug fixes    Â
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# thanks to: [url=http://www.66rpg.com]http://www.66rpg.com[/url] for documentation on wininet
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# ! do not use ftp which contains privates data
# ! this scripts need ftp account information !
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Ftp :
#   â–¼ receive :
# Â Â Â Â Â Net::FTP.download("dir_on_ftp/file.zip","./Graphics")
#   â–¼ send :
# Â Â Â Â Â Net::FTP.upload("./directoryinfolder/file.exe","directoryonftp newfilename.exe")
#   â–¼ make directory :
# Â Â Â Â Â Net::FTP.mkdir("/dir_on_ftp")
# Http :
#   â–¼ receive :
# Â Â Â Â Â Net::HTTP.download("url","./Graphics")
#   â–¼ total octets downloaded :
# Â Â Â Â Â Net::HTTP.dloaded
#   â–¼ size of file :
# Â Â Â Â Â Net::HTTP.size("test.zip")
#   â–¼ % dl progress :
# Â Â Â Â Â Net::HTTP.progress("test.zip")
#   â–¼ transfer time:
# Â Â Â Â Â Net::HTTP.temps("test.zip")
#   â–¼ list files :
# Â Â Â Â Â Net::HTTP.transfers
#   â–¼ file loaded? :
# Â Â Â Â Â Net::HTTP.loaded?("test.zip")
#   â–¼ octets transfered :
# Â Â Â Â Â Net::HTTP.transfered
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
module Berka
 module NetError
  ErrConIn="Unable to connect to Internet"
  ErrConFtp="Unable to connect to Ftp"
  ErrConHttp="Unable to connect to the Server"
  ErrNoFFtpIn="The file to be download doesn't exist"
  ErrNoFFtpEx="The file to be upload doesn't exist"
  ErrTranHttp="Http Download is failed"
  ErrDownFtp="Ftp Download is failed"
  ErrUpFtp="Ftp Upload is failed"
  ErrNoFile="No file to be download"
  ErrMkdir="Unable to create a new directory"
 end
end
module Net
 W='wininet'
 SPC=Win32API.new('kernel32','SetPriorityClass','pi','i').call(-1,128)
 IOA=Win32API.new(W,'InternetOpenA','plppl','l').call('',0,'','',0)
 IC=Win32API.new(W,'InternetConnectA','lplpplll','l')
 print(Berka::NetError::ConIn)if IOA==0
 module FTP
  FSCD=Win32API.new(W,'FtpSetCurrentDirectoryA','lp','l')
  FGF=Win32API.new(W,'FtpGetFileA','lppllll','l')
  FPF=Win32API.new(W,'FtpPutFile','lppll','l')
  FCD=Win32API.new(W,'FtpCreateDirectoryA','lp','l')
  module_function  Â
  def init
   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-#
   ftp="ftp.host.com"      #
   port=21            #  A modifier ! 21 is default
   identifiant="username"       #
   motdepasse="password"     #
   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-#
   @fb=IC.call(IOA,ftp,port,identifiant,motdepasse,1,0,0)
   ftp,port,identifiant,motdepasse=[nil]*4 # efface les ids par sécurité
   (print(Berka::NetError::ErrConFtp))if @fb==0
  end
  def download(ext,int='./')
   init if @fb.nil?  Â
   c=ext.split('/').pop
   if FSCD.call(@fb,ext.gsub(c,''))!=0
    print(Berka::NetError::ErrDownFtp)if FGF.call(@fb,c,"#{int}/#{c}",0,32,2,0)==0
   else
    print(Berka::NetError::ErrNoFFtpIn)
   end
  end
  def mkdir(rep)
   init if @fb.nil?
   print(Berka::NetError::ErrMkdir)if FCD.call(@fb,rep)==0
  end
  def upload(int,ext)
   init if @fb.nil?
   if FSCD.call(@fb,ext)&&File.exist?(int)
    print(Berka::NetError::ErrUpFtp) if FPF.call(@fb,int,ext,2,0)==0
   else
    print(Berka::NetError::ErrNoFFtpEx)
   end
  end
 end
 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 module HTTP
  IOU=Win32API.new(W,'InternetOpenUrl','lppllp','l')
  IRF=Win32API.new(W,'InternetReadFile','lpip','l')
  ICH=Win32API.new(W,'InternetCloseHandle','l','l')
  HQI=Win32API.new(W,'HttpQueryInfo','llppp','i')
  module_function
  def sizeloaded(i='');@read[i];end
  def transfered;@dloaded;end
  def transfers;@dls;end
  def progress(i='');(@read[i].to_f/@size[i]*100);end
  def loaded?(i='');@read[i]>=@size[i]rescue nil;end
  def temps(i='');@tps[i]if loaded?(i);end
  def size(i='');@size[i];end
  def download(url,int='./')
   @dloaded||=0;@dls||={};@i||=-1;@size||={};@read||={};@tps={}
   a=url.split('/');serv,root,fich=a[2],a[3..a.size].join('/'),a[-1]
   print(Berka::NetError::ErrNoFile)if fich.nil?
   @dls[fich]=Thread.start(url,int){|url,int|txt='';t=Time.now
   Berka::NetError::ErrConHttp if(e=IC.call(IOA,serv,80,'','',3,1,0))==0 Â
   f=IOU.call(IOA,url,nil,0,0,0)
   HQI.call(f,5,k="\0"*1024,[k.size-1].pack('l'),nil)
   @read[fich],@size[fich]=0,k.delete!("\0").to_i
   loop do
    buf,n=' '*1024,0
    r=IRF.call(f,buf,1024,o=[n].pack('i!'))
    n=o.unpack('i!')[0]
    break if r&&n==0
    txt << buf[0,n]
    @read[fich]=txt.size
   end
   (File.open(int+fich,'wb')<<txt).close
   @dloaded+=@read[fich]
   ICH.call(f);sleep(0.01)
   @tps[fich]=Time.now-t}
  end
 end
end