Add Initial Support for OAuth2 / O365

This commit is contained in:
Jonathan Jenne
2023-09-06 12:55:02 +02:00
parent 5f04d6a7a6
commit c43903154c
7 changed files with 350 additions and 332 deletions

View File

@@ -17,13 +17,15 @@ Public Class clsWorkEmail
Private CurrentMail As MailContainer = Nothing
Private CURRENT_TEMP_MAIL_PATH As String
Private CurrentTempMailPath As String
Private CURRENT_MAIL_BODY_ALL As String
Private CURRENT_MAIL_BODY_ANSWER1 As String = ""
Private CURRENT_MAIL_BODY_Substr2 As String = ""
Private CURRENT_MAIL_SUBJECT As String = ""
Private CURRENT_MAIL_FROM As String = ""
''' <summary>
''' Primary Mail Identifier.
''' Is a hash of the MessageId, used to be the MessageId itself.
@@ -85,7 +87,7 @@ Public Class clsWorkEmail
CURRENT_MAIL_BODY_ALL = ""
CURRENT_MAIL_BODY_ANSWER1 = ""
CURRENT_MAIL_BODY_Substr2 = ""
CURRENT_MAIL_MESSAGE = pMailMessage
'CURRENT_MAIL_MESSAGE = pMailMessage
CURRENT_MAIL_SUBJECT = pMailMessage.Subject.ToUpper.EscapeForSQL()
CURRENT_MAIL_UID = poUID
@@ -136,7 +138,7 @@ Public Class clsWorkEmail
Dim oTempMailAccessible As Boolean = False
If oTempMailExists = True Then
Try
Dim oFS As FileStream = File.OpenRead(CURRENT_TEMP_MAIL_PATH)
Dim oFS As FileStream = File.OpenRead(CurrentTempMailPath)
oTempMailAccessible = True
oFS.Close()
Catch ex As Exception
@@ -151,7 +153,7 @@ Public Class clsWorkEmail
Else
_Logger.Debug("CommonEmail-Process-Sniffer")
If COMMON_EMAIL_IN(CurrentMail) = True Then
InsertHistoryEntry()
InsertHistoryEntry(CurrentMail)
If CURRENT_ATTMT_COUNT = 0 Then
_Logger.Info("### Mail contained no Attachments!! ###")
Dim oBody = EmailStrings.EMAIL_NO_FERDS
@@ -239,8 +241,8 @@ Public Class clsWorkEmail
Private Function PROCESS_MANAGER_IN(pCurrentMail As MailContainer) As Boolean
Try
_Logger.Info(String.Format("PM-related message found....[{0}]", CURRENT_MAIL_MESSAGE.Subject))
_Logger.Debug(String.Format("PM-related message found....[{0}]", CURRENT_MAIL_MESSAGE.Subject))
_Logger.Info(String.Format("PM-related message found....[{0}]", pCurrentMail.Mail.Subject))
_Logger.Debug(String.Format("PM-related message found....[{0}]", pCurrentMail.Mail.Subject))
Dim oExpression = "PROCESS_NAME = 'ProcessManager'"
'Filter the rows using Select() method of DataTable
Dim TEMP_PROCESS_PROFILE_DT As DataTable = DT_POLL_PROCESS
@@ -260,8 +262,8 @@ Public Class clsWorkEmail
WM_IDX_BODY_SUBSTR_LENGTH = row("WM_IDX_BODY_SUBSTR_LENGTH")
DeleteMail = row("DELETE_MAIL")
If COPY2HDD(row("COPY_2_HDD"), row("PATH_ORIGINAL"), row("PATH_EMAIL_ERRORS"), False) = True Then
EXTRACT_BODY()
If COPY2HDD(pCurrentMail, row("COPY_2_HDD"), row("PATH_ORIGINAL"), row("PATH_EMAIL_ERRORS"), False) = True Then
EXTRACT_BODY(pCurrentMail)
End If
@@ -330,8 +332,8 @@ Public Class clsWorkEmail
Function COMMON_EMAIL_IN(pCurrentMail As MailContainer) As Boolean
Try
_Logger.Info(String.Format("COMMON_EMAIL_IN...Subject [{0}]", CURRENT_MAIL_MESSAGE.Subject))
_Logger.Debug(String.Format("COMMON_EMAIL_IN...Subject [{0}]", CURRENT_MAIL_MESSAGE.Subject))
_Logger.Info(String.Format("COMMON_EMAIL_IN...Subject [{0}]", pCurrentMail.Mail.Subject))
_Logger.Debug(String.Format("COMMON_EMAIL_IN...Subject [{0}]", pCurrentMail.Mail.Subject))
Dim oExpression = "PROCESS_NAME = 'Attachment Sniffer' or PROCESS_NAME = 'ZugFeRD-Parser'"
'Filter the rows using Select() method of DataTable
@@ -391,7 +393,7 @@ Public Class clsWorkEmail
_Logger.Warn($"Unexpected Error in Checking RemainingTotalFreeSpace for Storage [{oExtractMainPath}]: {ex.Message}")
End Try
If COPY2HDD(oRow("COPY_2_HDD"), oRow("PATH_ORIGINAL"), oRow("PATH_EMAIL_ERRORS"), True) = True Then
If COPY2HDD(pCurrentMail, oRow("COPY_2_HDD"), oRow("PATH_ORIGINAL"), oRow("PATH_EMAIL_ERRORS"), True) = True Then
If EXTRACT_ATTACHMENTS(pCurrentMail, oExtractMainPath, oRow("PATH_EMAIL_ERRORS")) = True Then
Return True
@@ -439,12 +441,12 @@ Public Class clsWorkEmail
'Dim oSubjectFilename = CURRENT_MAIL_MESSAGE.Subject.Truncate(SUBJECT_MAX_LENGTH) & ".eml"
Dim oSubjectFilename = CURRENT_MAIL_MESSAGE_ID & ".eml"
_Logger.Debug($"oSubjectFilename (beforeclean) is: {oSubjectFilename}")
_Logger.Debug($"Filename (beforeclean) is: {oSubjectFilename}")
oSubjectFilename = RemoveIllegalFileNameChars(oSubjectFilename)
oTempFilename = oTempPath & "\" & oSubjectFilename
_Logger.Debug($"oTempFilename (afterclean) is: {oTempFilename}")
oTempFilename = Path.Combine(oTempPath, oSubjectFilename)
_Logger.Debug($"Filepath (afterclean) is: {oTempFilename}")
Dim oCounter As Integer = 1
'If File.Exists(oTempFilename) = True Then
' Do While File.Exists(oTempFilename)
' oCounter += 1
@@ -455,12 +457,15 @@ Public Class clsWorkEmail
' oTempFilename = oTempFilename.Replace("\", "")
' Loop
'End If
CURRENT_MAIL_MESSAGE.Save(oTempFilename)
CURRENT_TEMP_MAIL_PATH = oTempFilename
_Logger.Debug($"Email saved to Temppath {CURRENT_TEMP_MAIL_PATH}")
oCounter = 0
pCurrentMail.Mail.Save(oTempFilename)
CurrentTempMailPath = oTempFilename
_Logger.Debug($"Email saved to Temppath {CurrentTempMailPath}")
Dim oCounter As Integer = 1
Dim oCancel As Boolean
Do While File.Exists(CURRENT_TEMP_MAIL_PATH) = False
Do While File.Exists(CurrentTempMailPath) = False
_Logger.Debug("Trying to read saved mail.. ({0}/{1})", oCounter, 10)
Thread.Sleep(1000)
oCounter += 1
If oCounter > 10 Then
@@ -472,7 +477,7 @@ Public Class clsWorkEmail
If oCancel = True Then
oResult = False
Else
If File.Exists(CURRENT_TEMP_MAIL_PATH) Then
If File.Exists(CurrentTempMailPath) Then
oResult = True
End If
End If
@@ -483,13 +488,13 @@ Public Class clsWorkEmail
Catch ex As Exception
_Logger.Error(ex)
_Logger.Info($"Unexpected error in Save2Temp [{oTempFilename}]")
CURRENT_TEMP_MAIL_PATH = Nothing
CurrentTempMailPath = Nothing
'clsLogger.Add("Unexpected Error in COPY2HDD: " & ex.Message & "MESSAGE_ID: " & CURRENT_MAIL_MESSAGE.MessageID, True)
Return False
End Try
End Function
Private Function COPY2HDD(pShouldCopyToDisk As Boolean, pPathOriginal As String, pPathErrors As String, pUseMessageIdAsFilename As Boolean) As Boolean
Private Function COPY2HDD(pCurrentMail As MailContainer, pShouldCopyToDisk As Boolean, pPathOriginal As String, pPathErrors As String, pUseMessageIdAsFilename As Boolean) As Boolean
Try
If pShouldCopyToDisk = True Then
_Logger.Debug("COPY_2_HDD is ACTIVE!")
@@ -519,7 +524,7 @@ Public Class clsWorkEmail
Return False
End Try
CURRENT_MAIL_MESSAGE.Save(oTempFilename)
pCurrentMail.Mail.Save(oTempFilename)
Dim oFileInfo As New FileInfo(oTempFilename)
Dim oFileLenth As Long = oFileInfo.Length
If oFileLenth > 0 Then
@@ -536,7 +541,7 @@ Public Class clsWorkEmail
End If
Else
_Logger.Info("COPY2HDD (" & CURRENT_MAIL_MESSAGE.Subject & ") already existing in [{oTempFilename}]!", False, "RUN_THREAD.COPY_2_HDD")
_Logger.Info("COPY2HDD (" & pCurrentMail.Mail.Subject & ") already existing in [{oTempFilename}]!", False, "RUN_THREAD.COPY_2_HDD")
Return True
End If
Else
@@ -551,11 +556,11 @@ Public Class clsWorkEmail
Return False
End Try
End Function
Private Function EXTRACT_BODY()
Private Function EXTRACT_BODY(pCurrentMail As MailContainer)
Dim oDTFunctionRegex As DataTable = _DB_MSSQL.GetDatatable("SELECT * FROM TBDD_FUNCTION_REGEX WHERE UPPER(FUNCTION_NAME) IN (UPPER('EMAIL_PROFILER - RemoveHTMLText'),UPPER('EMAIL_PROFILER - RemoveHTMLText1'))")
Dim oBodyText As String = ""
If Not IsNothing(CURRENT_MAIL_MESSAGE.Text) Then
If Not IsNothing(pCurrentMail.Mail.Text) Then
CURRENT_MAIL_BODY_ALL = oBodyText
End If
@@ -656,11 +661,12 @@ Public Class clsWorkEmail
_Logger.Debug(String.Format("PATH_TEMP[{0}]", pExtractPath))
Dim oAttachmentCount As Integer
oAttachmentCount = 0
Try
If CURRENT_TEMP_MAIL_PATH <> Nothing Then
If File.Exists(CURRENT_TEMP_MAIL_PATH) Then
For Each oAttachment As MimeData In CURRENT_MAIL_MESSAGE.Attachments
Try
If CurrentTempMailPath <> Nothing Then
If File.Exists(CurrentTempMailPath) Then
For Each oAttachment As MimeData In pCurrentMail.Mail.Attachments
Dim oATTFilename = oAttachment.SafeFileName.ToString.ToLower
Dim oValidExtensions = New List(Of String) From {"pdf", "xls", "xlsx", "doc", "docx", "ppt", "pptx"}
@@ -687,28 +693,32 @@ Public Class clsWorkEmail
'28.07.23
'The original filename part will now be slugified to prevent errors
'when opening the file in windream
'05.09.23
'The filename will now only contain a hash and a counter to prevent all errors relating to filenames and subject lines being shit
Dim oFileInfo1 = New FileInfo(oAttachment.SafeFileName)
Dim oFileInfo = New FileInfo(oAttachment.SafeFileName)
Dim oFilenameWithoutExtension = Path.GetFileNameWithoutExtension(oAttachment.SafeFileName)
Dim oFilename = StringEx.ConvertTextToSlug(oFilenameWithoutExtension) & oFileInfo1.Extension
Dim oAttachmentFileName = $"{CURRENT_MAIL_MESSAGE_ID}~{pCurrentMail.SenderDomain}~{oFilename}"
Dim oFilename = StringEx.ConvertTextToSlug(oFilenameWithoutExtension) & oFileInfo.Extension
'Dim oAttachmentFileName = $"{CURRENT_MAIL_MESSAGE_ID}~{pCurrentMail.SenderDomain}~{oFilename}"
Dim oAttachmentFileName = $"{CURRENT_MAIL_MESSAGE_ID}~Attachment{oAttachmentCount}{oFileInfo.Extension}"
_Logger.Debug("Final Filename for Attachment: [{0}]", oAttachmentFileName)
oAttachmentFilePath = Path.Combine(pExtractPath, oAttachmentFileName)
_Logger.Debug("Final Path for Attachment: [{0}]", oAttachmentFilePath)
If System.IO.File.Exists(oAttachmentFilePath) = False Then
If File.Exists(oAttachmentFilePath) = False Then
_Logger.Debug(String.Format("Trying to save attachment [{0}]", oAttachmentFilePath))
Try
oAttachment.Save(oAttachmentFilePath)
'oAttachment.Save(oAttachmentFileString)
Dim oFileInfo As New FileInfo(oAttachmentFilePath)
Dim oFileLenth As Long = oFileInfo.Length
Dim oFileInfo1 As New FileInfo(oAttachmentFilePath)
Dim oFileLenth As Long = oFileInfo1.Length
If oFileLenth > 2 Then
_Logger.Info(String.Format("Attachment saved to [{0}]", oAttachmentFilePath))
'INSERT_HISTORY_FB(CURRENT_MAIL_MESSAGE_ID, oAttachment.SafeFileName)
InsertAttachmentHistoryEntry(CURRENT_MAIL_MESSAGE_ID, oAttachment.SafeFileName)
InsertAttachmentHistoryEntry(pCurrentMail, CURRENT_MAIL_MESSAGE_ID, oAttachment.SafeFileName)
oAttachmentCount += 1
Else
_Logger.Warn($"##!! oFileLenth for AttachmentObjects is <2 !!##")
@@ -737,7 +747,7 @@ Public Class clsWorkEmail
End Try
Next
Else
_Logger.Warn($"If cause 2 EXTRACT_ATTACHMENTS: {CURRENT_TEMP_MAIL_PATH} not existing")
_Logger.Warn($"If cause 2 EXTRACT_ATTACHMENTS: {CurrentTempMailPath} not existing")
End If
Else
_Logger.Warn($"EXTRACT_ATTACHMENTSIf cause 1: CURRENT_TEMP_MAIL_PATH is NOTHING")
@@ -755,13 +765,13 @@ Public Class clsWorkEmail
End Try
End Function
Private Function InsertHistoryEntry() As Boolean
Private Function InsertHistoryEntry(pCurrentMail As MailContainer) As Boolean
If MessageError = False Then
Dim ins = $"INSERT INTO TBEMLP_HISTORY (WORK_PROCESS,EMAIL_MSGID,EMAIL_SUBJECT,EMAIL_DATE,EMAIL_BODY,EMAIL_SUBSTRING1,EMAIL_SUBSTRING2,EMAIL_FROM,PROFILE_ID) VALUES " &
$"('{CurrentMailProcessName}'," &
$"'{CURRENT_MAIL_MESSAGE_ID}'," &
$"'{CURRENT_MAIL_SUBJECT}'," &
$"'{CURRENT_MAIL_MESSAGE.Date}'," &
$"'{pCurrentMail.Mail.Date}'," &
$"'{CURRENT_MAIL_BODY_ALL}'," &
$"'{CURRENT_MAIL_BODY_ANSWER1}'," &
$"'{CURRENT_MAIL_BODY_Substr2}'," &
@@ -774,7 +784,7 @@ Public Class clsWorkEmail
End If
End Function
Private Function InsertAttachmentHistoryEntry(pMessageId As String, pFileName As String) As Boolean
Private Function InsertAttachmentHistoryEntry(pCurrentMail As MailContainer, pMessageId As String, pFileName As String) As Boolean
If IsNothing(_DB_MSSQL) Then
_Logger.Info("INSERT_HISTORY_FB: _DB_MSSQL is nothing ")
Return False
@@ -786,7 +796,7 @@ Public Class clsWorkEmail
$"'{pMessageId}'," &
$"'{CURRENT_MAIL_FROM}'," &
$"'{CURRENT_MAIL_SUBJECT}'," &
$"'{CURRENT_MAIL_MESSAGE.Date}'," &
$"'{pCurrentMail.Mail.Date}'," &
$"'{CURRENT_MAIL_BODY_ALL}'," &
$"'{pFileName}')"
_DB_MSSQL.ExecuteNonQuery(ins)