# encoding: ascii
# api: powershell
# title: Check-NewGmail
# 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: 4523
# x-archived: 2013-10-21T07:32:22
# x-published: 2013-10-16T09:23: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`).
}
