# encoding: ascii
# api: powershell
# title: mari07031986
# description: Not for regular use, this is just a demo with using COM object to check new letters on gmail.
# version: 3.0
# type: function
# author: greg zakharov
# license: CC0
# function: Check-NewGmail
# x-poshcode-id: 6724
# x-archived: 2017-02-16T14:58:45
# x-published: 2017-02-07T15:27:00
#
#
function Check-NewGmail {
  param(
    [String]$Email = (Read-Host "Enter your email"),    
    [Security.SecureString]$Password = (Read-Host "Enter email password" -as)
  )
  
  function str([Security.SecureString]$s) {
    return [Runtime.InteropServices.Marshal]::PtrToStringAuto(
      [Runtime.InteropServices.Marshal]::SecureStringToBSTR($s)
    )
  }
  $com = New-Object -com MSXML2.XMLHTTP.3.0
  $com.open('GET', $('https://' + $Email + ':' + `
             (str $Password) + '@mail.google.com/mail/feed/atom'), $false)
  $com.setRequestHeader('Content-Type', 'application/x-www-from-urlcoded')
  $com.send()
  
  $com.responseText -match 'fullcount>\d+' | Out-Null; $res = ($matches[0] -split '>')[1]
  Write-Host You have $res new letter`(s`).
}
