alias msnnewauth {
  sockclose conn
  sockopen -e conn nexus.passport.com 443
  .timerssltimeout 1 20 msnssltimeout
}

alias msnssltimeout {
  aline @Messenger 10 $+ *** OpenSSL connection attempt timed out.  Falling back on WinHTTP method...
  msnauth
  msnauth2
}

on *:sockopen:conn:{
  sockwrite -n conn GET https://nexus.passport.com/rdr/pprdr.asp HTTP/1.0 $+ $crlf $+ $crlf
}

on 1:sockread:conn:{
  if ($sockerr > 0) return
  :nextread
  sockread %temp
  if ($sockbr == 0) return
  if (%temp == $null) %temp = -
  if (%msn.debug == 1) { aline @Messenger ~~~ %temp }
  if $left(%temp,13) == PassportURLs: {
    set %temp $gettok(%temp,2,44)
    set %msnauthurl $gettok(%temp,2,61)
    .timerssltimeout off
    msnloginserver
  }
  goto nextread
}

alias msnloginserver {
  sockclose msnlogin
  sockopen -e msnlogin $gettok(%msnauthurl,1,47) 443
  .timerssltimeout 1 20 msnssltimeout
}

on *:sockopen:msnlogin:{
  sockwrite -n msnlogin GET / $+ $gettok(%msnauthurl,2-,47) HTTP/1.1
  sockwrite -n msnlogin Authorization: $hget(msnauth,info)
  sockwrite -n msnlogin Host: $gettok(%msnauthurl,1,47) $+ $crlf $+ $crlf
}

on 1:sockread:msnlogin:{
  if ($sockerr > 0) return
  :nextread
  if ($sock(msnlogin)) { 
    sockread %temp 
  }
  else { return }
  if ($sockbr == 0) return
  if (%temp == $null) { set %temp - }
  if (%msn.debug == 1) { aline @Messenger ~~~ %temp }
  if (da-status=success isin %temp) { 
    .timerssltimeout off
    sendmsn USR %msn.main TWN S $gettok(%temp,2,39) 
  }
  if ($left(%temp,12) ==  HTTP/1.1 401) {
    aline -p @messenger 4*** ERROR: Authentication failed, is your login name and password correct?
    .timerssltimeout off
    sockclose msn
    window -l50k0Sz @Messenger %msn.messenger.font $shortfn($scriptdir) $+ msn.ico
    aline @messenger 10*** Disconnected
    titlebar @Messenger -=[ Offline ]=-
    dline -l @Messenger 1- $+ $line(@Messenger,0,1)
    halt
  }
  if ($left(%temp,9) == Location:) { 
    set %msnauthurl $gettok(%temp,2,32)
    set %msnauthurl $gettok(%msnauthurl,2-,47)
    sockclose msnlogin
    sockopen -e msnlogin $gettok(%msnauthurl,1,47) 443
    goto end
  }
  goto nextread
  :end
}
