-
Notifications
You must be signed in to change notification settings - Fork 2
/
IMAPSocket.xojo_code
840 lines (643 loc) · 18.5 KB
/
IMAPSocket.xojo_code
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
#tag Class
Protected Class IMAPSocket
Inherits SSLSocket
#tag Event
Sub DataAvailable()
dim s, response, lines() As string
s = me.ReadAll
s = trim(s)
lines = split(s, EndOfLine.Windows)
if myCommand = "CONNECT" then
myTag = left(s, 1)
end if
if instr(lines(0), ". NO") > 0 or instr(lines(0), "error") > 0 or instr(lines(0), ". BAD") > 0 then
handleError(me.ReadAll)
else
if myCommand = "UID" then
bufferString = bufferString + s
else
s = TrimResponse(s)
bufferData.Append s
end if
dim i As integer
i = UBound(lines)
if left(lines(i), 5) = ". OK " or left(lines(i), 4) = "+ OK" or myCommand = "Connect" or myCommand = "LOGIN" or myCommand = "PLAIN" or myCommand = "AUTHENTICATE CRAM-MD5" then
if myCommand = "UID" then
response = bufferString
bufferString = ""
else
response = join(bufferData, EndOfLine.Windows)
ReDim bufferData(-1)
end if
handleResponse(response)
end if
end if
End Sub
#tag EndEvent
#tag Event
Sub SendComplete(UserAborted As Boolean)
writeIMAPLog "Sending command: "+myCommand
dim s As string
s = me.Lookahead
End Sub
#tag EndEvent
#tag Method, Flags = &h0
Sub AppendMessage(Mailbox As String, Index As Integer, EmailSource As String, EmailSize As Integer)
dim s As string
me.myCommand = "APPEND"
s = ". APPEND "+Mailbox+" (\Seen) {"+str(EmailSize)+"}"+EndOfLine.Windows+EmailSource
currentEmail = index
me.SendCommand s
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub ChangeFlags(Index As Integer)
dim s As string
me.myCommand = "STORE"
s = ". UID STORE "+str(Index)+" +FLAGS ("+mySecondary+")"
currentEmail = Index
me.SendCommand s
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Function CleanUpResponse(Response As String) As String
dim i As integer
dim lines() As String
i = instr(Response, ")"+EndOfLine.Windows+". OK") - 1
if i <> -1 then
Response = mid(Response, 1, i)
Response = Trim(Response)
end if
lines = split(Response, EndOfLine.Windows)
i = UBound(lines)
if instr(lines(i), ". OK") > 0 then
lines.Remove i
Response = join(lines, EndOfLine.Windows)
end if
if myCommand = "FETCH BODY" or myCommand = "FETCH EMAIL" then
lines = split(Response, EndOfLine.Windows)
i = UBound(lines)
if instr(lines(i), "* ") > 0 and instr(lines(i), "FETCH") > 0 and instr(lines(i), "FLAGS") > 0 then
lines.Remove i
Response = join(lines, EndOfLine.Windows)
end if
end if
return Response
End Function
#tag EndMethod
#tag Method, Flags = &h0
Sub CopyMessage(Index As Integer, Destination As String)
dim s As string
if instr(destination, " ") > 0 then
destination = """"+destination+""""
end if
me.myCommand = "COPY"
s = ". UID COPY "+str(Index)+" "+Destination
currentEmail = index
me.SendCommand s
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub DeleteMessage(Index As Integer)
dim s As string
me.myCommand = "DELETE"
s = ". UID STORE "+str(Index)+" +FLAGS (\Deleted)"
currentEmail = Index
me.SendCommand s
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub ExpungeMessages()
dim s As string
me.myCommand = "EXPUNGE"
s = ". EXPUNGE"
me.SendCommand s
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub FetchBody(Index As Integer)
dim s As string
me.myCommand = "FETCH BODY"
s = ". UID FETCH "+str(Index)+" BODY[]"
currentEmail = index
me.SendCommand s
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub FetchHeaders(Index As integer)
dim s As string
me.myCommand = "FETCH HEADER"
's = ". UID FETCH "+str(Index)+" RFC822.HEADER"
s = ". UID FETCH "+str(Index)+" BODY.PEEK[HEADER]"
currentEmail = index
me.SendCommand s
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub FetchMessage(Index As integer)
dim s As string
me.myCommand = "FETCH EMAIL"
s = ". UID FETCH "+str(Index)+" BODY.PEEK[]"
currentEmail = index
me.SendCommand s
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub FetchUID(Range As String)
dim s As string
myCommand = "UID"
s = ". UID Fetch "+Range+" FLAGS"
me.SendCommand s
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub handleError(Error As string)
ProtocolError(Error)
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub handleResponse(Response As string)
dim s As string
s = myCommand
LoggedCommunication Response
Select Case myCommand
Case "Connect", "CAPABILITY"
if instr(Response, "STARTTLS") > 0 then
writeIMAPLog "This server requires STARTTLS. Starting the TLS Negotiation."
ProgressChange("Sending STARTTLS")
me.myCommand = "STARTTLS"
SendCommand "STARTTLS"
Return
end if
if instr(me.Address, "mac.com") > 0 or instr(me.Address, "icloud.com") > 0 or instr(me.Address, "me.com") > 0 then
writeIMAPLog "Proceeding with AUTH LOGIN Authorization."
s = me.UserName+" "+me.Password
me.myCommand = "LOGIN"
SendCommand ". LOGIN "+s
Return
elseif instr(me.Address, "gmail.com") > 0 and instr(Response, "xoauth") > 0 then
writeIMAPLog "Proceeding with AUTH LOGIN Authorization."
s = me.UserName+" "+me.Password
me.myCommand = "LOGIN"
SendCommand ". LOGIN "+s
Return
elseif instr(Response, "CRAM-MD5") > 0 then
writeIMAPLog "Proceeding with CRAM-MD5 Authorization."
me.myCommand = "AUTHENTICATE CRAM-MD5"
SendCommand ". AUTHENTICATE CRAM-MD5"
Return
elseif instr(Response, "LOGIN") > 0 then
writeIMAPLog "Proceeding with AUTH LOGIN Authorization."
s = me.UserName+" "+me.Password
me.myCommand = "LOGIN"
SendCommand ". LOGIN "+s
Return
elseif instr(Response, "PLAIN") > 0 then
writeIMAPLog "Proceeding with AUTH PLAIN Authorization."
s = chr(0)+me.Username+chr(0)+me.Password
me.myCommand = "PLAIN"
SendCommand ". AUTHENTICATE PLAIN "+s
Return
else
writeIMAPLog "No AUTH information. Proceed with CAPABILITY."
s = "CAPABILITY"
me.myCommand = "CAPABILITY"
SendCommand ". CAPABILITY"
Return
end if
Case "PLAIN"
s = chr(0)+me.Username+chr(0)+me.Password
s = EncodeBase64(s)
me.myCommand = "LOGIN"
SendCommand s
return
Case "AUTHENTICATE CRAM-MD5"
dim i As integer
dim temp As string
i = instr(Response, " ")+1
s = mid(Response, i)
s = DecodeBase64(s)
temp = Lowercase(generateMD5HASH(me.Password, s))
s = EncodeBase64(me.Username+" "+temp)
me.myCommand = "LOGIN"
SendCommand s
Return
Case "LOGIN"
writeIMAPLog "Retrieving Mailbox List."
me.myCommand = "LIST"
s = ". LIST "+""""" "+"""*"""
SendCommand s
Return
Case "LIST"
Response = CleanUpResponse(Response)
processMailboxLIST(Response)
Case "SELECT"
MailboxSelected(Response)
Case "UID"
Response = CleanUpResponse(Response)
UIDSReceived(Response)
Case "FETCH HEADER"
Response = CleanUpResponse(Response)
HeadersReceived(currentEmail, Response)
Case "FETCH BODY"
Response = CleanUpResponse(Response)
BodyReceived(currentEmail, Response)
Case "FETCH EMAIL"
Response = CleanUpResponse(Response)
MessageReceived(currentEmail, Response)
CASE "STORE"
FlagChanged(currentEmail, Response)
CASE "COPY"
MessageCopied(currentEmail, Response)
Case "APPEND"
MessageAppended(currentEmail, Response)
Case "DELETE"
MessageDeleted(currentEmail, Response)
Case "EXPUNGE"
MessagesExpunged(Response)
Case "LOGOUT"
me.Disconnect
End Select
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub Initialize()
myCRLF = EndOfLine.Windows
if myCommand <> "" then
mySecondary = myCommand
end if
myCommand = "CONNECT"
me.Connect
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub LogOut()
dim s As string
me.myCommand = "LOGOUT"
s = ". LOGOUT"
me.SendCommand s
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub processMailboxLIST(List As String)
dim i, x As integer
dim rg As new RegEx
dim myMatch As RegExMatch
dim s, strMatch, data(), mailboxName, mailboxParent As string
dim mb As IMAPMailbox
dim mbRank() As string
data = Split(List, EndOfLine.Windows)
//CLEANUP THE OK RESPONSE
for i = UBound(data) DownTo 0
if left(data(i), 4) = ". OK" then
data.Remove i
end if
next
rg.Options.Greedy = false
rg.Options.CaseSensitive = false
rg.Options.DotMatchAll = true
for i = 0 to UBound(data)
mb = new IMAPMailbox
s = data(i)
//DETERMINE IF THIS MAILBOX HAS CHILDREN OR NOT
if instr(s, "haschildren") > 0 then
mb.hasChildren = true
else
mb.hasChildren = false
end if
//DETERMINE IF THIS IS A FOLDER OF MAILBOX
rg.SearchPattern = "\(.*\)."
myMatch = rg.Search(s)
if myMatch <> nil then
strMatch = myMatch.SubExpressionString(0)
if instr(strMatch, "noselect") > 0 then
mb.isMailbox =false
else
mb.isMailbox = true
end if
s = Replace(s, strMatch, "")
end if
//FIND THE DELIMITER
rg.SearchPattern = """."""
myMatch = rg.Search(s)
if myMatch <> nil then
strMatch = myMatch.SubExpressionString(0)
's = Replace(s, strMatch, "")
strMatch = ReplaceAll(strMatch, """", "")
myDelimiter = strMatch
mb.myDelimiter = myDelimiter
end if
//STRIP EVERYTHING BUT WHAT SHOULD BE THE NAME
x = instr(s, myDelimiter+"""") + 2
s = mid(s, x)
s = trim(s)
strMatch = s
//FIND THE MAILBOX NAME
'rg.SearchPattern = """.*"""
'myMatch = rg.Search(s)
'if myMatch <> nil then
'strMatch = myMatch.SubExpressionString(0)
x = StringUtils.InStrReverse(-1, strMatch, myDelimiter) + 1
mailboxName = mid(strMatch, x)
mailboxParent = strMatch
//REMOVE QUOTATION MARKS FROM MAILBOXNAME
if Left(mailboxName, 1) = """" then
mailboxName = mid(mailboxName, 2)
end if
if Right(mailboxName, 1) = """" then
mailboxName = left(mailboxName, mailboxName.len-1)
end if
//REMOVE QUOTATION MARKS FROM MAILBOXPARENT
if Left(mailboxParent, 1) = """" then
mailboxParent = mid(mailboxParent, 2)
end if
if Right(mailboxParent, 1) = """" then
mailboxParent = left(mailboxParent, mailboxParent.len-1)
end if
mb.myName = mailboxName
mb.myParent = mailboxParent
mb.myHierarchy = split(mb.myParent, mb.myDelimiter)
'end if
if mb.myName <> "" then
myMailboxes.Append mb
mbRank.Append mb.myParent
end if
next
mbRank.SortWith(myMailboxes)
if UBound(myMailboxes) > -1 then
MailboxListReceived(myMailboxes)
end if
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub processUIDList(UIDList As string)
dim temp(0), strMatch As string
dim rg As new RegEx
dim myMatch As RegExMatch
dim i, x, uid(0) As integer
rg.Options.Greedy = false
rg.Options.CaseSensitive = false
rg.Options.DotMatchAll = true
temp = Split(UIDList, EndOfLine.Windows)
x = UBound(temp)
if instr(temp(x), ". OK") > 0 then
temp.Remove x
end if
for i = 1 to UBound(temp)
rg.SearchPattern = "UID (.*) "
myMatch = rg.Search( temp(i) )
if myMatch <> nil then
strMatch = myMatch.SubExpressionString(0)
strMatch = Replace(strMatch, "UID ", "")
strMatch = trim(strMatch)
uid.Append val(strMatch)
end if
next
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub SelectMailbox(Mailbox As string)
dim s As string
me.myCommand = "SELECT"
s = ". SELECT """+Mailbox+""""
currentMailbox = Mailbox
me.SendCommand s
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub SendCommand(myCommand As String)
'if myCommand = "LOGIN" then
'LoggedCommunication "This is where the socket sends the password."
'else
'LoggedCommunication "Sending Command: "+myCommand
'end if
Write myCommand + myCRLF
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Function TrimResponse(Response As string) As string
Response = trim(Response)
return Response
End Function
#tag EndMethod
#tag Hook, Flags = &h0
Event BodyReceived(Index As Integer, EmailSource As String)
#tag EndHook
#tag Hook, Flags = &h0
Event FlagChanged(Index As Integer, Response As String)
#tag EndHook
#tag Hook, Flags = &h0
Event HeadersReceived(Index As Integer, EmailSource As String)
#tag EndHook
#tag Hook, Flags = &h0
Event LoggedCommunication(Response As String)
#tag EndHook
#tag Hook, Flags = &h0
Event MailboxListReceived(Mailboxes() As IMAPMailbox)
#tag EndHook
#tag Hook, Flags = &h0
Event MailboxSelected(Flags As String)
#tag EndHook
#tag Hook, Flags = &h0
Event MessageAppended(Index As Integer, Response As String)
#tag EndHook
#tag Hook, Flags = &h0
Event MessageCopied(Index As Integer, Response As String)
#tag EndHook
#tag Hook, Flags = &h0
Event MessageDeleted(Index As Integer, Response As String)
#tag EndHook
#tag Hook, Flags = &h0
Event MessageReceived(Index As Integer, EmailSource As String)
#tag EndHook
#tag Hook, Flags = &h0
Event MessagesExpunged(Response As String)
#tag EndHook
#tag Hook, Flags = &h0
Event ProgressChange(Status As string)
#tag EndHook
#tag Hook, Flags = &h0
Event ProtocolError(Error As String)
#tag EndHook
#tag Hook, Flags = &h0
Event UIDSReceived(UIDList As String)
#tag EndHook
#tag Note, Name = RegEx
rg.SearchPattern = "name=(.*)(:|$)"
#tag EndNote
#tag Property, Flags = &h0
bufferData() As String
#tag EndProperty
#tag Property, Flags = &h0
bufferString As String
#tag EndProperty
#tag Property, Flags = &h0
currentEmail As Integer
#tag EndProperty
#tag Property, Flags = &h0
currentMailbox As String
#tag EndProperty
#tag Property, Flags = &h0
firstConnect As Boolean
#tag EndProperty
#tag Property, Flags = &h0
imapPrefix As String
#tag EndProperty
#tag Property, Flags = &h0
initialMailbox As string
#tag EndProperty
#tag Property, Flags = &h0
myCommand As string
#tag EndProperty
#tag Property, Flags = &h21
Private myCRLF As string
#tag EndProperty
#tag Property, Flags = &h21
Private myDelimiter As String
#tag EndProperty
#tag Property, Flags = &h0
myMailboxes() As IMAPMailbox
#tag EndProperty
#tag Property, Flags = &h0
mySecondary As String
#tag EndProperty
#tag Property, Flags = &h0
myState As String
#tag EndProperty
#tag Property, Flags = &h0
myTag As string
#tag EndProperty
#tag Property, Flags = &h0
Password As string
#tag EndProperty
#tag Property, Flags = &h0
UserName As string
#tag EndProperty
#tag ViewBehavior
#tag ViewProperty
Name="bufferString"
Group="Behavior"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="CertificateFile"
Visible=true
Group="Behavior"
Type="FolderItem"
EditorType="File"
#tag EndViewProperty
#tag ViewProperty
Name="CertificatePassword"
Visible=true
Group="Behavior"
Type="String"
#tag EndViewProperty
#tag ViewProperty
Name="CertificateRejectionFile"
Visible=true
Group="Behavior"
Type="FolderItem"
EditorType="File"
#tag EndViewProperty
#tag ViewProperty
Name="ConnectionType"
Visible=true
Group="Behavior"
InitialValue="2"
Type="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="currentEmail"
Group="Behavior"
Type="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="currentMailbox"
Group="Behavior"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="firstConnect"
Group="Behavior"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="imapPrefix"
Group="Behavior"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="Index"
Visible=true
Group="ID"
Type="Integer"
EditorType="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="initialMailbox"
Group="Behavior"
Type="string"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="myCommand"
Group="Behavior"
Type="string"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="mySecondary"
Group="Behavior"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="myState"
Group="Behavior"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="myTag"
Group="Behavior"
Type="string"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="Name"
Visible=true
Group="ID"
Type="String"
EditorType="String"
#tag EndViewProperty
#tag ViewProperty
Name="Password"
Group="Behavior"
Type="string"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="Secure"
Visible=true
Group="Behavior"
Type="Boolean"
#tag EndViewProperty
#tag ViewProperty
Name="Super"
Visible=true
Group="ID"
Type="String"
EditorType="String"
#tag EndViewProperty
#tag ViewProperty
Name="UserName"
Group="Behavior"
Type="string"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag EndViewBehavior
End Class
#tag EndClass