;--------------------------------------------------------------------------------------------------------------------------------
;msnfilerecv.mrc
;Andy Carvell 2003
;Version 3.00
;
;--------------------------------------------------------------------------------------------------------------------------------


;***** DO NOT EDIT ANYTHING BELOW *****

alias alldone {
  sockwrite -n msnfile BYE 16777989
}

alias msnfilercvd {
  set %msn.file.filename.spaces $gettok(%msn.file.filename,2-,32)
  set %msn.file.filename $replace(%msn.file.filename.spaces,$chr(32),$null)
  set %msn.file.cookie $gettok(%msn.file.cookie,2,32)
  set %msn.file.filesize $gettok(%msn.file.filesize,2,32)
  mkdir $shortfn($mircdir) $+ msn\downloads
  set %msn.file.sizetoreceive $calc( %msn.file.filesize + $calc($calc($int($calc( %msn.file.filesize  / 2045 )) + 1) * 3) )
  write -c $shortfn($mircdir) $+ msn\downloads\ $+ %msn.file.filename
  echo -a 4 $+ %msn.msgnick.log is trying to send you %msn.file.filename on MSN. 
  echo -a 4 $+  Would you like to accept it?
  echo -a 4 $+ Type /msnaccept to accept the file or /msndecline to decline it
  set %msn.fileuser $1
}

alias msnaccept {
  msnfileaccept %msn.fileuser
  echo -a 4 $+ File accepted
}

alias msndecline { 
  msnfiledecline %msn.fileuser
  echo -a 4 $+ File declined
}

alias msnfiledecline {
  sockwrite -n chat. $+ $1 MSG %msn. [ $+ [ $1 ] ] N $calc(143 + $len(%msn.file.cookie) )
  sockwrite -n chat. $+ $1 MIME-Version: 1.0
  sockwrite -n chat. $+ $1 Content-Type: text/x-msmsgsinvite; charset=UTF-8
  sockwrite -n chat. $+ $1
  sockwrite -n chat. $+ $1 Invitation-Command: CANCEL
  sockwrite -n chat. $+ $1 Invitation-Cookie: %msn.file.cookie
  sockwrite -n chat. $+ $1 Cancel-Code: REJECT
  sockwrite -n chat. $+ $1
  halt
}

alias msnfileaccept {
  sockwrite -n chat. $+ $1 MSG %msn. [ $+ [ $1 ] ] N $calc(176 + $len(%msn.file.cookie) )
  sockwrite -n chat. $+ $1 MIME-Version: 1.0
  sockwrite -n chat. $+ $1 Content-Type: text/x-msmsgsinvite; charset=UTF-8
  sockwrite -n chat. $+ $1
  sockwrite -n chat. $+ $1 Invitation-Command: ACCEPT
  sockwrite -n chat. $+ $1 Invitation-Cookie: %msn.file.cookie
  sockwrite -n chat. $+ $1 Launch-Application: FALSE
  sockwrite -n chat. $+ $1 Request-Data: IP-Address:
  sockwrite -n chat. $+ $1
}

alias msnfileconnect {
  sockopen msnfile %msn.file.ip %msn.file.port
  set %msn.file.willy notreceive
}

on *:sockopen:msnfile:{


  if ( $sockerr > 0 ) { echo -a File Sending Failed - Could not connect to your contact | halt }


  sockwrite -n msnfile VER MSNFTP


}

on *:sockread:msnfile:{
  ;echo -a 13 msn file read
  if ($sockerr > 0) return
  :nextread
  sockread -f &binvar

  write -c temp2.txt
  if ( %msn.file.willy == receive ) { goto willg }
  bwrite temp2.txt -1 -1 &binvar
  set %msn.file.data $read(temp2.txt)
  ;echo -a 13 %msn.file.data
  :willg
  if ( %msn.file.data == $null ) { set %msn.file.data (null) }
  ;echo 7 %msn.file.data
  if ($sockbr == 0) return
  if ( %msn.file.data == VER MSNFTP ) { sockwrite -n msnfile USR %msn.user %msn.file.authcookie }
  elseif ( $left(%msn.file.data,3) == FIL ) { 
    ;echo -a 13 it's fil
    set %msn.file.totalbytes $gettok( %msn.file.data,2,32)
    sockwrite -n msnfile TFR 
    dialog -m filerecv filerecv
  }
  else { 
    set %msn.file.willy receive
    bwrite $mircdir $+ msn\downloads\ $+ %msn.file.filename $+ .temp -1 -1 &binvar
    if ( $dialog(filerecv) != $null ) { 
      did -o filerecv 2 1 $int($calc( ( $file( $mircdir $+ msn\downloads\ $+ %msn.file.filename $+ .temp ).size / %msn.file.sizetoreceive) * 100 ) ) $+ % completed
    }
    if ( $file( $mircdir $+ msn\downloads\ $+ %msn.file.filename $+ .temp ).size = %msn.file.sizetoreceive ) {
      alldone
      ;run $mircdir $+ msn\filerecv.exe $mircdir $+ msn\downloads\ $+ %msn.file.filename
      set %msn.temp $dll($scriptdirblah.dll,get,$shortfn($mircdir $+ msn\downloads\ $+ %msn.file.filename $+ .temp) $mircdir $+ msn\downloads\ $+ %msn.file.filename) 
      did -v filerecv 3
      did -v filerecv 4
      did -b filerecv 5
      set %msn.file.willy notreceive
      .remove $mircdir $+ msn\downloads\ $+ %msn.file.filename $+ .temp
    }

    goto nextread
  }
  goto nextread
}
