This repository has been archived by the owner on Mar 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEML.lss
444 lines (353 loc) · 12 KB
/
EML.lss
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
%REM
Library EML
Created May 5, 2015 by Bernd Berger
Credits:
EML Stuff: http://ypastov.blogspot.de/2013/01/export-of-lotus-notes-email-to-eml-file.html
DoTrim: http://lomadi.blogspot.de/2010/01/lotusscript-fulltrim-function-does-not.html
StripAccent: http://www.xceed.be/Blog.nsf/dx/converting-accented-characters
NotesGroupManager: http://www.openntf.org/main.nsf/project.xsp?r=project/LotusScript%20Gold%20Collection
%END REM
Option Public
Option Declare
Use "NotesGroupManager"
Private Sub PrintMime(Mime As NotesMIMEEntity, Stream As NotesStream)
On Error GoTo ErrHandler
Dim child As NotesMIMEEntity
If Mime Is Nothing Then
Exit Sub
End If
If Mime.Encoding = ENC_IDENTITY_BINARY Or _
Mime.Encoding = ENC_IDENTITY_7BIT Or _
Mime.Encoding = ENC_IDENTITY_8BIT Then
Call Mime.DecodeContent
Call Mime.EncodeContent(ENC_BASE64)
End If
Call Mime.GetEntityAsText(Stream)
If Mime.ContentType = "multipart" Then
If Mime.Preamble <> "" Then
Call Stream.WriteText(Mime.Preamble, EOL_NONE)
End If
Set child = Mime.GetFirstChildEntity
While Not child Is Nothing
Call WriteAsciiBytes(Stream, child.BoundaryStart)
Call PrintMime(child, Stream)
Call WriteAsciiBytes(Stream, child.BoundaryEnd)
Set child = child.GetNextSibling
Wend
End If
ErrResume:
Exit Sub
ErrHandler:
Print GetThreadInfo(10) & "/" & GetThreadInfo(1) & ": " & Err & " - " & Error & " in line " & Erl
Resume ErrResume
End Sub
Private Function StripAccent(thestring As String) As String
On Error GoTo Err
10000
Handler
Dim A As String * 1
Dim B As String * 1
Dim i As Integer
Const AccChars= "Č芎šžŸÀÁÂÃÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÙÚÛÝàáâãåçèéêëìíîïðñòóôõùúûýÿ"
Const RegChars= "CcSZszYAAAAACEEEEIIIIDNOOOOUUUYaaaaaceeeeiiiidnoooouuuyy"
For i = 1 To Len(AccChars)
A = Mid(AccChars, i, 1)
B = Mid(RegChars, i, 1)
thestring = Replace(thestring, A, B)
Next
StripAccent = thestring
ErrResume:
Exit Function
ErrHandler:
Print GetThreadInfo(10) & "/" & GetThreadInfo(1) & ": " & Err & " - " & Error & " in line " & Erl
Resume ErrResume
End Function
%REM
Function FixOutlookHeadersMonth
Description: Format(DATE, "mmm") would return "Dez" instead of "Dec" in German Notes Client -> Error on opening the EML
%END REM
Private Function FixOutlookHeadersMonth(mm As String) As String
Select Case mm
Case "01": FixOutlookHeadersMonth = "Jan"
Case "02": FixOutlookHeadersMonth = "Feb"
Case "03": FixOutlookHeadersMonth = "Mar"
Case "04": FixOutlookHeadersMonth = "Apr"
Case "05": FixOutlookHeadersMonth = "May"
Case "06": FixOutlookHeadersMonth = "Jun"
Case "07": FixOutlookHeadersMonth = "Jul"
Case "08": FixOutlookHeadersMonth = "Aug"
Case "09": FixOutlookHeadersMonth = "Sep"
Case "10": FixOutlookHeadersMonth = "Oct"
Case "11": FixOutlookHeadersMonth = "Nov"
Case "12": FixOutlookHeadersMonth = "Dec"
End Select
End Function
%REM
Sub OutlookHeadersFixing
Description: Comments for Sub
%END REM
Private Sub OutlookHeadersFixing(mail As NotesDocument)
On Error GoTo ErrHandler
Dim body As NotesMIMEEntity
Dim header As NotesMIMEHeader
Dim headerName As String
Dim headerValue As String
Dim DateTime As NotesDateTime
Set body = mail.GetMimeentity
'Date
headerName = "Date"
Set header = body.Getnthheader(headerName, 1)
If header Is Nothing Then Set header = body.Createheader(headerName)
'SendOn/ReceivedTime
If CStr(mail.getitemvalue("DeliveredDate")(0)) = "" Then
Set DateTime = New NotesDateTime(mail.Getitemvalue("PostedDate")(0))
Call header.SetHeaderVal(Format(mail.getitemvalue("PostedDate")(0), "dd") & " " & FixOutlookHeadersMonth(Format(mail.getitemvalue("PostedDate")(0), "mm")) & " " & Format(mail.getitemvalue("PostedDate")(0), "yyyy hh:mm:ss") & " " & StrRightBack(DateTime.Zonetime, " "))
headerName = "SendOn"
Set header = body.Getnthheader(headerName, 1)
If header Is Nothing Then Set header = body.Createheader(headerName)
If mail.getitemvalue("PostedDate")(0) <> "" Then Call header.SetHeaderVal(Format(mail.getitemvalue("PostedDate")(0), "dd") & " " & FixOutlookHeadersMonth(Format(mail.getitemvalue("PostedDate")(0), "mm")) & " " & Format(mail.getitemvalue("PostedDate")(0), "yyyy hh:mm:ss") & " " & StrRightBack(DateTime.Zonetime, " "))
Else
Set DateTime = New NotesDateTime(mail.Getitemvalue("DeliveredDate")(0))
Call header.SetHeaderVal(Format(mail.getitemvalue("DeliveredDate")(0), "dd") & " " & FixOutlookHeadersMonth(Format(mail.getitemvalue("DeliveredDate")(0), "mm")) & " " & Format(mail.getitemvalue("DeliveredDate")(0), "yyyy hh:mm:ss") & " " & StrRightBack(DateTime.Zonetime, " "))
headerName = "ReceivedTime"
Set header = body.Getnthheader(headerName, 1)
If header Is Nothing Then Set header = body.Createheader(headerName)
If mail.getitemvalue("DeliveredDate")(0) <> "" Then Call header.SetHeaderVal(Format(mail.getitemvalue("DeliveredDate")(0), "dd") & " " & FixOutlookHeadersMonth(Format(mail.getitemvalue("DeliveredDate")(0), "mm")) & " " & Format(mail.getitemvalue("DeliveredDate")(0), "yyyy hh:mm:ss") & " " & StrRightBack(DateTime.Zonetime, " "))
End If
ErrResume:
Exit Sub
ErrHandler:
Print GetThreadInfo(10) & "/" & GetThreadInfo(1) & ": " & Err & " - " & Error & " in line " & Erl
Resume ErrResume
End Sub
Function createEML(mail As NotesDocument, emlpath As String, emlfilename As String) As Integer
'Return Values:
'0 Success!
'Errors:
'1 Folder not found
'2 EML-File already exists
'3 EML-File was not created (permissions?)
'4 No valid document
'5 No body
'6 Truncated
'7 Errorhandler
'8 Empty values in the function call
'9 Only reader access
'10 Quota
On Error GoTo ErrHandler
On Error 76 Resume Next 'Folder not found -> Dir(emlpath, 16)
If mail Is Nothing Or emlpath = "" Or emlfilename = "" Then
createEML = 8
Exit Function
End If
Dim s As New NotesSession
Dim db As NotesDatabase
Dim Names As New NotesDatabase(s.GetEnvironmentString("MailServer", True), "Names.nsf")
Dim MailCpy As NotesDocument
Dim Stream As NotesStream
Dim Mime As NotesMIMEEntity
Dim ACL As NotesACL
Dim entry As NotesACLEntry
Dim n As New NotesName(s.Username)
Dim gman As New NotesGroupManager(True)
Dim Group As NotesGroup
Dim FilePath As String
Dim tmpForm As String
Dim DirResult As String
Dim bIsDocValid As Boolean
Dim bHasItemBody As Boolean
Dim bHasItemAttBytesTruncated As Boolean
Dim bHasItemDocBytesTruncated As Boolean
Dim bACL As Boolean
Dim bConvertToMime As Boolean
Dim bConvertMail As Boolean
Set db = s.currentdatabase
Set ACL = db.ACL
Set entry = ACL.GetEntry(s.Username)
If Not entry Is Nothing Then
If Not (entry.Cancreatedocuments And entry.Candeletedocuments) Then
createEML = 9
Exit Function
End If
Else
Set entry = ACL.Getfirstentry()
Call gman.LoadAddressBook(Names)
bACL = False
While Not entry Is Nothing
If entry.IsGroup Then
Set Group = gman.GetGroup(entry.Name)
If Not Group Is Nothing Then
If Group.isMember(n.Canonical) Then
If entry.Cancreatedocuments And entry.Candeletedocuments Then
bACL = True
End If
End If
End If
End If
Set entry = ACL.Getnextentry(entry)
Wend
If Not bACL Then
createEML = 9
Exit Function
End If
End If
If db.Sizequota <> 0 Then
If ((db.Size/1024) + (mail.Size/1024)) >= db.Sizequota Then
createEML = 10
Exit Function
End If
End If
bIsDocValid = mail.IsValid
bHasItemBody = mail.Hasitem("Body")
bHasItemAttBytesTruncated = mail.Hasitem("$AttBytesTruncated")
bHasItemDocBytesTruncated = mail.Hasitem("$DocBytesTruncated")
If bIsDocValid = False Then
createEML = 4
Exit Function
End If
If bHasItemBody = False Then
createEML = 5
Exit Function
End If
If bHasItemAttBytesTruncated = True Or bHasItemDocBytesTruncated = True Then
createEML = 6
Exit Function
End If
DirResult = Dir(emlpath, 16)
If DirResult <> "" Then
FilePath = Replace(emlpath & "\" & ValidateFilename(emlfilename) & ".eml", "\\", "\", 2)
If Dir(FilePath) = "" Then
bConvertToMime = s.ConvertMime
s.ConvertMime = False
'Prepare Stream
Set Stream = s.CreateStream()
Call Stream.Open(FilePath, "utf-8")
Call Stream.Truncate()
'To keep original email without change I want to work with copy of email
Set MailCpy = db.Createdocument()
Call mail.Copyallitems(MailCpy, True)
'First thing: let's check if current email's Body is stored as MIME
'If not - then we need to convert Email's Body to MIME
bConvertMail = False
Set Mime = MailCpy.GetMIMEEntity
If Mime Is Nothing Then
bConvertMail = True
Else
If LCase(Mime.ContentType) = "multipart" Then bConvertMail = True
End If
If bConvertMail = True Then
tmpForm = MailCpy.GetItemValue("Form")(0)
Call MailCpy.ReplaceItemValue("Form", "MimeConvert")
Call MailCpy.ConvertToMIME(2)
Call MailCpy.ReplaceItemValue("Form", tmpForm)
End If
'Trim multiline Subjects to a single line
Call MailCpy.ReplaceItemValue("Subject", DoTrim(MailCpy.GetItemValue("Subject")(0)))
'the export does not create all items in eml-files which are required for Outlook
'So let's make some changes to MIME mail header to display it properly in Outlook
Call OutlookHeadersFixing(MailCpy)
'Export email to EML
Call GetMime(MailCpy, Stream)
'close Stream (free file)
Call Stream.Close()
s.ConvertMime = bConvertToMime
If Dir(FilePath) <> "" Then
createEML = 0
Else
createEML = 3
End If
Else
createEML = 2
End If
Else
createEML = 1
End If
ErrResume:
Exit Function
ErrHandler:
Print GetThreadInfo(10) & "/" & GetThreadInfo(1) & ": " & Err & " - " & Error & " in line " & Erl
If FilePath <> "" Then
If Dir(FilePath) <> "" Then
Kill FilePath
End If
End If
createEML = 7
Resume ErrResume
End Function
Private Sub WriteAsciiBytes (Stream As NotesStream, txt As String)
On Error GoTo ErrHandler
'** hackish workaround to write the boundary string to a UTF-8
'** NotesStream (convert it to an array of ASCII Bytes). It might be
'** something about the character set of the boundary string, but I
'** really don't know. All I DO know is that using WriteText to
'** write the string directly to a UTF-8 Stream doesn't work properly
'** for me, at least some of the time.
If Len(txt) = 0 Then
Exit Sub
End If
ReDim arr(Len(txt)-1) As Byte
Dim i As Integer
For i = 1 To Len(txt)
arr(i - 1) = CByte(Asc(Mid(txt, i, 1)))
Next
Call Stream.Write(arr)
ErrResume:
Exit Sub
ErrHandler:
Print GetThreadInfo(10) & "/" & GetThreadInfo(1) & ": " & Err & " - " & Error & " in line " & Erl
Resume ErrResume
End Sub
Private Sub GetMime(doc As NotesDocument, Stream As NotesStream)
On Error GoTo ErrHandler
Dim Mime As NotesMIMEEntity
Set Mime = doc.GetMIMEEntity
If Not (Mime Is Nothing) Then
While Not Mime Is Nothing
Call PrintMime(Mime, Stream)
Set Mime = Mime.Getnextsibling()
Wend
Else
Call Stream.WriteText("No MIME Found in " & doc.Subject(0), EOL_PLATFORM)
End If
ErrResume:
Exit Sub
ErrHandler:
Print GetThreadInfo(10) & "/" & GetThreadInfo(1) & ": " & Err & " - " & Error & " in line " & Erl
Resume ErrResume
End Sub
Function DoTrim(txt As String) As String
On Error GoTo ErrHandler
Dim strReturn As String
Dim cntr As Integer
Dim t As String
txt = FullTrim(txt)
strReturn = ""
For cntr = 1 To Len(txt)
t = Mid$(txt, cntr, 1)
If (Asc(t) <> 13) And (Asc(t) <> 10) Then '13=>CR, 10=>LF
strReturn = strReturn + t
End If
Next
Dotrim = strReturn
ErrResume:
Exit Function
ErrHandler:
Print GetThreadInfo(10) & "/" & GetThreadInfo(1) & ": " & Err & " - " & Error & " in line " & Erl
Resume ErrResume
End Function
Function ValidateFilename(filename As String) As String
On Error GoTo ErrHandler
Dim x As Integer
Dim i As Integer
Dim NewName As String
filename = StripAccent(filename)
For x = 1 To Len(filename)
If Mid$(filename,x,1) Like "[-A-Za-z0-9²³().&%$§ +_#€µ@'~]" Then
NewName = NewName + Mid$(filename,x,1)
End If
Next x
ValidateFilename = DoTrim(NewName)
ErrResume:
Exit Function
ErrHandler:
Print GetThreadInfo(10) & "/" & GetThreadInfo(1) & ": " & Err & " - " & Error & " in line " & Erl
Resume ErrResume
End Function