;--------------------------------------------------------------------------------------------------------------------------------
;msnfilesend.mrc
;Andy Carvell 2004-2005
;Version 3.45
;--------------------------------------------------------------------------------------------------------------------------------


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

alias msnsendfile {
  set %msn.fileemail %msn.clickemail
  set %msn.filename.path $sfile(c:\,Browse for the file you wish to send,Send File)
  if ( %msn.filename.path == $null ) { halt }
  if ( $file(%msn.filename.path).size == $null ) {
    echo -a Error: %msn.filename.path does not exist.
    halt
  }
  set %msn.filename.file $msngetfile( %msn.filename.path )
  if ( %msn.filename.file == $null ) { halt }
  set %msn.invitation.cookie $rand(11111,99999)
  sockwrite -n chat. $+ %msn.clickemail MSG %msn. [ $+ [ %msn.fileemail ] ] N $calc(262 + $len($file(%msn.filename.path).size) + $len(%msn.filename.file))
  sockwrite -n chat. $+ %msn.clickemail MIME-Version: 1.0
  sockwrite -n chat. $+ %msn.clickemail Content-Type: text/x-msmsgsinvite; charset=UTF-8
  sockwrite -n chat. $+ %msn.clickemail 
  sockwrite -n chat. $+ %msn.clickemail Application-Name: File Transfer
  sockwrite -n chat. $+ %msn.clickemail Application-GUID: {5D3E02AB-6190-11d3-BBBB-00C04F795683}
  sockwrite -n chat. $+ %msn.clickemail Invitation-Command: INVITE
  sockwrite -n chat. $+ %msn.clickemail Invitation-Cookie: %msn.invitation.cookie
  sockwrite -n chat. $+ %msn.clickemail Application-File: %msn.filename.file
  sockwrite -n chat. $+ %msn.clickemail Application-FileSize: $file(%msn.filename.path).size
  sockwrite -n chat. $+ %msn.clickemail 
  aline @ $+ %msn.fileemail *** Waiting for user to accept %msn.filename.file .....
}

alias declinefile {
  .timersendfile off
  aline @ $+ %msn.fileemail *** File was declined.
  dline -l @ $+ %msn.fileemail 7-20
  halt
}

alias acceptfile {
  if ( $ip == $null ) {
    echo -a 4 $+ mIRC does not know your IP.
    echo -a 4 $+ To fix this connect to any IRC server then try to send the file again
    halt
  }
  sockclose filesendlisten
  sockclose filesend
  set %msn.file.port.listen 6891

  set %msn.file.sendip $ip

  socklisten filesendlisten %msn.file.port.listen
  sockwrite -n chat. $+ %msn.fileemail MSG %msn. [ $+ [ %msn.fileemail ] ] U $calc(228 + $len(%msn.file.sendip))
  sockwrite -n chat. $+ %msn.fileemail MIME-Version: 1.0
  sockwrite -n chat. $+ %msn.fileemail Content-Type: text/x-msmsgsinvite; charset=UTF-8
  sockwrite -n chat. $+ %msn.fileemail 
  sockwrite -n chat. $+ %msn.fileemail Invitation-Command: ACCEPT
  sockwrite -n chat. $+ %msn.fileemail Invitation-Cookie: %msn.invitation.cookie
  sockwrite -n chat. $+ %msn.fileemail IP-Address: %msn.file.sendip
  sockwrite -n chat. $+ %msn.fileemail Port: %msn.file.port.listen
  sockwrite -n chat. $+ %msn.fileemail AuthCookie: 1743276
  sockwrite -n chat. $+ %msn.fileemail Launch-Application: FALSE
  sockwrite -n chat. $+ %msn.fileemail Request-Data: IP-Address:
  sockwrite -n chat. $+ %msn.fileemail 
  aline @ $+ %msn.fileemail *** File accepted, commencing file transfer...

  halt
}

on *:socklisten:filesendlisten:{
  sockaccept filesend
}

on *:sockread:filesend:{
  if ($sockerr > 0) return
  :nextread
  if ( $sock(filesend) == $null ) { halt }
  sockread %temp
  if ($sockbr == 0) return
  if ( %temp = VER MSNFTP ) { sockwrite -n filesend VER MSNFTP }
  if ( $left(%temp,3) == USR ) { sockwrite -n filesend FIL $file(%msn.filename.path).size }
  if ( $left(%temp,3) == TFR ) { msnstartsending }
  if ( $left(%temp,3) == CCL ) { 
    sockclose filesend 
    dline -l @ $+ %msn.fileemail 7-20
    aline @ $+ %msn.fileemail *** File transfer aborted
    .timersendfile off
    halt
  }
  if ( $left(%temp,3) == BYE ) { 
    dline -l @ $+ %msn.fileemail 7-20
    aline @ $+ %msn.fileemail *** File transfer completed successfully.

    sockclose file*
    .timersendfile off
  }
  goto nextread
}

alias msnstartsending {
  set %msn.location 0
  aline -l @ $+ %msn.fileemail -
  aline -l @ $+ %msn.fileemail 0% Complete
  .timersendfile -m 0 10 msnsendnextbit
}

alias -l msnsendnextbit {
  if ($sock(filesend).sq >= 14336) return
  if ( %msn.location >= $file(%msn.filename.path).size ) {
    .timersendfile off
    goto exitit
  } 
  bread $shortfn(%msn.filename.path) %msn.location 2045 &binvar
  set %msn.second $int($calc( $bvar(&binvar,0) /256))
  set %msn.first $calc( $bvar(&binvar,0) - ( %msn.second * 256))
  bset &temp 1 0 %msn.first %msn.second
  sockwrite filesend &temp
  sockwrite filesend &binvar
  rline -l @ $+ %msn.fileemail 8 $int($calc(%msn.location / $file(%msn.filename.path).size * 100 )) $+ % Complete
  set %msn.location $calc( %msn.location + 2045 )

  :exitit
}

alias msngetfile {
  set %count $len($1-)
  :start
  if ( $mid($1-,%count,1) == \ ) { goto end }
  dec %count 1
  goto start
  :end
  return $mid($1-, $calc( %count + 1) )
}
