forked from nblagoev/Gmail.ps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGmail.ps.psm1
582 lines (487 loc) · 16.4 KB
/
Gmail.ps.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
function New-GmailSession {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $false)]
[System.Management.Automation.PSCredential]$Credential = $($cr = (Get-StoredCredential Gmail.ps:default); if ($cr -eq $null) {Get-Credential} else {$cr})
)
$ghost = "imap.gmail.com"
$gport = 993
$guser = $Credential.UserName
$gpass = $Credential.GetNetworkCredential().Password
New-Object -TypeName AE.Net.Mail.ImapClient -ArgumentList $ghost,$guser,$gpass,Login,$gport,$true,$false
}
function Remove-GmailSession {
[CmdletBinding()]
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session
)
$Session.Disconnect()
}
function Invoke-GmailSession {
[CmdletBinding()]
param (
[Parameter(Position = 1, Mandatory = $true)]
[ScriptBlock]$ScriptBlock,
[Parameter(Position = 0, Mandatory = $false)]
[System.Management.Automation.PSCredential]$Credential = $($cr = (Get-StoredCredential Gmail.ps:default); if ($cr -eq $null) {Get-Credential} else {$cr})
)
$gmail = New-GmailSession -Credential $Credential
& $ScriptBlock $gmail
$gmail | Remove-GmailSession
}
function Get-Mailbox {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Position = 0, ValueFromPipelineByPropertyName = $true)]
[ValidateSet("All Mail", "Starred", "Drafts", "Important", "Sent Mail", "Spam", "Inbox")]
[string]$Name = "Inbox",
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$Label
)
if ($Label) {
$mailbox = $Session.SelectMailbox($Label)
} elseif ($Name -and ($Name -ne "Inbox")) {
$mailbox = $Session.SelectMailbox("[Gmail]/" + $Name)
} elseif ($Name -and ($Name -eq "Inbox")) {
$mailbox = $Session.SelectMailbox("Inbox")
}
AddSessionTo $mailbox $Session
}
function Get-Message {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[switch]$Prefetch,
[switch]$Unread,
[switch]$Read,
[switch]$Answered,
[switch]$Draft,
[switch]$Undraft,
[switch]$Starred,
[switch]$Unstarred,
[DateTime]$On,
[DateTime]$After,
[DateTime]$Before,
[string]$From,
[string]$To,
[string]$Cc,
[string]$Bcc,
[string]$Text,
[string]$Body,
[string]$Subject,
[string]$Label,
[string]$Query
)
$ar = @()
if ($Unread) {
$ar += "UNSEEN"
} elseif ($Read) {
$ar += "SEEN"
}
if ($Answered) {
$ar += "ANSWERED"
}
if ($Draft) {
$ar += "DRAFT"
} elseif ($Undraft) {
$ar += "UNDRAFT"
}
if ($Starred) {
$ar += "FLAGGED"
} elseif ($Unstarred) {
$ar += "UNFLAGGED"
}
if ($On) {
$ar += 'ON "' + $(GetRFC2060Date $After) + '"'
}
if ($From) {
$ar += 'FROM "' + $From + '"'
}
if ($To) {
$ar += 'TO "' + $To + '"'
}
if ($After) {
$ar += 'AFTER "' + $(GetRFC2060Date $After) + '"'
}
if ($Before) {
$ar += 'BEFORE "' + $(GetRFC2060Date $Before) + '"'
}
if ($Cc) {
$ar += 'CC "' + $Cc + '"'
}
if ($Bcc) {
$ar += 'BCC "' + $Bcc + '"'
}
if ($Text) {
$ar += 'TEXT "' + $Text + '"'
}
if ($Body) {
$ar += 'BODY "' + $Body + '"'
}
if ($Label) {
$ar += 'LABEL "' + $Label + '"'
}
if ($Query) {
$ar += 'QUERY "' + $Query + '"'
}
if ($Subject) {
$ar += 'SUBJECT "' + $Subject + '"'
}
$criteria = '(' + ($ar -join ') (') + ')'
$result = $Session.Search($criteria);
$i = 1
foreach ($item in $result)
{
$msg = $Session.GetMessage($item, !$Prefetch, $false)
AddSessionTo $msg $Session
Write-Progress -Activity "Gathering messages" -Status "Progress: $($i)/$($result.Count)" -PercentComplete ($i / $result.Count * 100) -Id 90017
$i += 1
}
}
function GetRFC2060Date([DateTime]$date) {
$date.ToString("dd-MMM-yyyy hh:mm:ss zz", [CultureInfo]::GetCultureInfo("en-US"))
}
function AddSessionTo($item, [AE.Net.Mail.ImapClient]$session) {
$item | Add-Member -MemberType NoteProperty -Name Session -Value $session -PassThru
}
function Remove-Message {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[AE.Net.Mail.MailMessage]$Message
)
process {
$Session.DeleteMessage($Message)
}
}
function Update-Message {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[AE.Net.Mail.MailMessage]$Message,
[Parameter(ParameterSetName = "Seen")]
[switch]$Read,
[Parameter(ParameterSetName = "Unseen")]
[switch]$Unread,
[Parameter(ParameterSetName = "Unseen")]
[Parameter(ParameterSetName = "Seen")]
[Parameter(ParameterSetName = "Flagged")]
[Parameter(ParameterSetName = "Unflagged")]
[switch]$Archive,
[Parameter(ParameterSetName = "Flagged")]
[switch]$Star,
[Parameter(ParameterSetName = "Unflagged")]
[switch]$Unstar,
[Parameter(ParameterSetName = "Unseen")]
[Parameter(ParameterSetName = "Seen")]
[Parameter(ParameterSetName = "Flagged")]
[Parameter(ParameterSetName = "Unflagged")]
[switch]$Spam
)
process {
if ($Archive) {
$Session.MoveMessage($Message.Uid, "[Gmail]/All Mail")
}
if ($Spam) {
$Session.MoveMessage($Message.Uid, "[Gmail]/Spam")
}
$replace = $false
$changed = $false
if ($Read) {
$flags = $flags -bor [AE.Net.Mail.Flags]::Seen
$changed = $true
} elseif ($Unread) {
$flags = $Message.Flags
$flags = $flags -bxor [AE.Net.Mail.Flags]::Seen
$changed = $true
$replace = $true
}
if ($Star) {
$flags = $flags -bor [AE.Net.Mail.Flags]::Flagged
$changed = $true
} elseif ($Unstar) {
$flags = $Message.Flags
$flags = $flags -bxor [AE.Net.Mail.Flags]::Flagged
$changed = $true
$replace = $true
}
if ($changed) {
if (-not $replace) {
$Session.AddFlags([AE.Net.Mail.Flags]$flags, @($Message))
} else {
$Session.SetFlags([AE.Net.Mail.Flags]$flags, @($Message))
}
}
}
}
function Move-Message {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[AE.Net.Mail.MailMessage]$Message,
[Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true, ParameterSetName = "A")]
[ValidateSet("Inbox", "All Mail", "Starred", "Drafts", "Important", "Sent Mail", "Spam")]
[string]$Mailbox,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = "B")]
[string]$Label
)
if ($Label) {
$Session.MoveMessage($Message.Uid, $Label)
} elseif ($Mailbox ) {
$Session.MoveMessage($Message.Uid, $Mailbox)
}
}
function Measure-Message {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session
)
$Session.GetMessageCount()
}
function Get-Label {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[AE.Net.Mail.MailMessage]$Message,
[Parameter(Position = 0, Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
[Alias("Name")]
[string]$Like = "",
[Parameter()]
[switch]$All
)
process {
if ($Message) {
$Message.Labels
} else {
if ($All) {
$Session.ListMailboxes($Like, "*")
} else {
$Session.ListMailboxes($Like, "*") | Where-Object { $_.Name -notmatch "\[Gmail\]" -and $_.Name -ne "INBOX" }
}
}
}
}
function New-Label {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $true)]
[string[]]$Name,
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session
)
foreach ($item in $Name)
{
$Session.CreateMailbox($item)
}
}
function Remove-Label {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $true)]
[string[]]$Name,
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[AE.Net.Mail.MailMessage]$Message
)
foreach ($item in $Name)
{
if ($Message) {
$Session.RemoveLabels($Name, @($Message))
} else {
$Session.DeleteMailbox($item)
}
}
}
function Set-Label {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[AE.Net.Mail.MailMessage]$Message,
[Parameter(Position = 0, Mandatory = $true)]
[string[]]$Name,
[Parameter()]
[switch]$Force
)
process {
$labels = $Session | Get-Label
foreach ($label in $Name)
{
if (!$labels.Contains($label)) {
if ($Force) {
$Session | New-Label $label | Out-Null
} else {
Write-Error "The label '$label' doesn't exist! Use the -Force parameter to create and apply it"
$er = $true
}
}
}
if (!$er) {
$Session.AddLabels($Name, @($Message))
}
}
}
function Get-StoredCredential
{
[CmdletBinding()]
[OutputType([PSCredential])]
Param
(
[Parameter(Mandatory, Position=0)]
[ValidateNotNullOrEmpty()]
[Alias("Address", "Location", "TargetName")]
[string]$Name
)
End
{
$nCredPtr= New-Object IntPtr
$success = [ADVAPI32.Util]::CredRead($Name,1,0,[ref] $nCredPtr)
if ($success) {
$critCred = New-Object ADVAPI32.Util+CriticalCredentialHandle $nCredPtr
$cred = $critCred.GetCredential()
$username = $cred.UserName
$securePassword = $cred.CredentialBlob | ConvertTo-SecureString -AsPlainText -Force
$cred = $null
Write-Output (New-Object System.Management.Automation.PSCredential $username, $securePassword)
} else {
Write-Verbose "No credentials where found in Windows Credential Manager for TargetName: $Name"
}
}
Begin
{
$sig = @"
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct NativeCredential
{
public UInt32 Flags;
public CRED_TYPE Type;
public IntPtr TargetName;
public IntPtr Comment;
public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
public UInt32 CredentialBlobSize;
public IntPtr CredentialBlob;
public UInt32 Persist;
public UInt32 AttributeCount;
public IntPtr Attributes;
public IntPtr TargetAlias;
public IntPtr UserName;
internal static NativeCredential GetNativeCredential(Credential cred)
{
NativeCredential ncred = new NativeCredential();
ncred.AttributeCount = 0;
ncred.Attributes = IntPtr.Zero;
ncred.Comment = IntPtr.Zero;
ncred.TargetAlias = IntPtr.Zero;
ncred.Type = CRED_TYPE.GENERIC;
ncred.Persist = (UInt32)1;
ncred.CredentialBlobSize = (UInt32)cred.CredentialBlobSize;
ncred.TargetName = Marshal.StringToCoTaskMemUni(cred.TargetName);
ncred.CredentialBlob = Marshal.StringToCoTaskMemUni(cred.CredentialBlob);
ncred.UserName = Marshal.StringToCoTaskMemUni(System.Environment.UserName);
return ncred;
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct Credential
{
public UInt32 Flags;
public CRED_TYPE Type;
public string TargetName;
public string Comment;
public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
public UInt32 CredentialBlobSize;
public string CredentialBlob;
public UInt32 Persist;
public UInt32 AttributeCount;
public IntPtr Attributes;
public string TargetAlias;
public string UserName;
}
public enum CRED_TYPE : uint
{
GENERIC = 1,
DOMAIN_PASSWORD = 2,
DOMAIN_CERTIFICATE = 3,
DOMAIN_VISIBLE_PASSWORD = 4,
GENERIC_CERTIFICATE = 5,
DOMAIN_EXTENDED = 6,
MAXIMUM = 7, // Maximum supported cred type
MAXIMUM_EX = (MAXIMUM + 1000), // Allow new applications to run on old OSes
}
public class CriticalCredentialHandle : Microsoft.Win32.SafeHandles.CriticalHandleZeroOrMinusOneIsInvalid
{
public CriticalCredentialHandle(IntPtr preexistingHandle)
{
SetHandle(preexistingHandle);
}
public Credential GetCredential()
{
if (!IsInvalid)
{
NativeCredential ncred = (NativeCredential)Marshal.PtrToStructure(handle,
typeof(NativeCredential));
Credential cred = new Credential();
cred.CredentialBlobSize = ncred.CredentialBlobSize;
cred.CredentialBlob = Marshal.PtrToStringUni(ncred.CredentialBlob,
(int)ncred.CredentialBlobSize / 2);
cred.UserName = Marshal.PtrToStringUni(ncred.UserName);
cred.TargetName = Marshal.PtrToStringUni(ncred.TargetName);
cred.TargetAlias = Marshal.PtrToStringUni(ncred.TargetAlias);
cred.Type = ncred.Type;
cred.Flags = ncred.Flags;
cred.Persist = ncred.Persist;
return cred;
}
else
{
throw new InvalidOperationException("Invalid CriticalHandle!");
}
}
override protected bool ReleaseHandle()
{
if (!IsInvalid)
{
CredFree(handle);
SetHandleAsInvalid();
return true;
}
return false;
}
}
[DllImport("Advapi32.dll", EntryPoint = "CredReadW", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool CredRead(string target, CRED_TYPE type, int reservedFlag, out IntPtr CredentialPtr);
[DllImport("Advapi32.dll", EntryPoint = "CredFree", SetLastError = true)]
public static extern bool CredFree([In] IntPtr cred);
"@
try
{
Add-Type -MemberDefinition $sig -Namespace "ADVAPI32" -Name 'Util' -ErrorAction Stop
}
catch
{
Write-Error -Message "Could not load custom type. $($_.Exception.Message)"
}
}
}
New-Alias -Name Select-Mailbox -Value Get-Mailbox
New-Alias -Name Filter-Message -Value Get-Message
New-Alias -Name Count-Message -Value Measure-Message
New-Alias -Name Add-Label -Value Set-Label
Export-ModuleMember -Alias * -Function New-GmailSession, Remove-GmailSession, Invoke-GmailSession, Get-Mailbox,
Get-Message, Measure-Message, Remove-Message, Update-Message,
Get-Label, New-Label, Remove-Label, Set-Label, Move-Message