|
Sub RevertSafeLinks() |
|
Dim objMail As MailItem |
|
Dim objInspector As Inspector |
|
Dim objInlineResponse As Object |
|
|
|
' Check for inline response first |
|
Set objInlineResponse = Application.ActiveExplorer.ActiveInlineResponse |
|
If Not objInlineResponse Is Nothing Then |
|
Set objMail = objInlineResponse |
|
ProcessMailItem objMail |
|
Else |
|
' Check if there is an active inspector |
|
Set objInspector = Application.ActiveInspector |
|
If Not objInspector Is Nothing Then |
|
' Check if the active item is a mail item |
|
Set objMail = objInspector.CurrentItem |
|
If objMail.Class = olMail Then |
|
ProcessMailItem objMail |
|
End If |
|
Else |
|
MsgBox "No active inspector or inline response window found.", vbExclamation |
|
End If |
|
End If |
|
End Sub |
|
|
|
Sub ProcessMailItem(objMail As MailItem) |
|
If objMail.BodyFormat = olFormatHTML Then |
|
ProcessHTMLBody objMail |
|
ElseIf objMail.BodyFormat = olFormatPlain Then |
|
ProcessPlainTextBody objMail |
|
End If |
|
End Sub |
|
|
|
Sub ProcessHTMLBody(objMail As MailItem) |
|
Dim objDoc As Object |
|
Dim objLink As Object |
|
Dim originalURL As String |
|
Dim safeLinkPrefix As String |
|
|
|
' Define the Safe Links prefix |
|
safeLinkPrefix = "safelinks.protection.outlook.com" |
|
|
|
Set objDoc = objMail.GetInspector.WordEditor |
|
|
|
' Loop through all hyperlinks in the mail item |
|
For Each objLink In objDoc.Hyperlinks |
|
If InStr(objLink.Address, safeLinkPrefix) > 0 Then |
|
' Extract and decode the "url" parameter |
|
originalURL = ExtractURLParameter(objLink.Address, "url") |
|
If originalURL <> "" Then |
|
objLink.Address = originalURL |
|
objLink.TextToDisplay = originalURL |
|
End If |
|
End If |
|
Next objLink |
|
End Sub |
|
|
|
Sub ProcessPlainTextBody(objMail As MailItem) |
|
On Error GoTo ErrorHandler |
|
Dim safeLinkDomain As String |
|
Dim bodyText As String |
|
Dim linkStart As Long |
|
Dim linkURL As String |
|
Dim originalURL As String |
|
|
|
' Define the base Safe Links domain |
|
safeLinkDomain = "safelinks.protection.outlook.com" |
|
|
|
bodyText = objMail.Body |
|
Do While InStr(bodyText, safeLinkDomain) > 0 |
|
' Backtrack to find the start of the Safe Link (typically starts with "http" or "https://") |
|
linkStart = FindURLStart(bodyText, InStr(bodyText, safeLinkDomain)) |
|
|
|
' Extract the full Safe Link URL |
|
linkURL = ExtractFullURL(bodyText, linkStart) |
|
|
|
' Extract and decode the "url" parameter |
|
originalURL = ExtractURLParameter(linkURL, "url") |
|
If originalURL <> "" Then |
|
bodyText = Replace(bodyText, linkURL, originalURL) |
|
Else |
|
Exit Do ' Exit loop if no valid URL is found |
|
End If |
|
Loop |
|
objMail.Body = bodyText |
|
Exit Sub |
|
|
|
ErrorHandler: |
|
MsgBox "An error occurred while processing plain text body: " & Err.Description, vbCritical |
|
End Sub |
|
|
|
Function FindURLStart(text As String, domainPosition As Long) As Long |
|
Dim i As Long |
|
' Assume URLs start with "http" or "https://" |
|
For i = domainPosition To 1 Step -1 |
|
If Mid(text, i, 4) = "http" Then |
|
FindURLStart = i |
|
Exit Function |
|
End If |
|
Next i |
|
' Default to the domain position if no "http" is found |
|
FindURLStart = domainPosition |
|
End Function |
|
|
|
Function ExtractFullURL(text As String, startPos As Long) As String |
|
Dim i As Long |
|
Dim validChars As String |
|
Dim url As String |
|
|
|
' Define valid URL characters |
|
validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~/:?&=%#" |
|
|
|
' Iterate through the text starting from the given position |
|
i = startPos |
|
Do While i <= Len(text) |
|
If InStr(1, validChars, Mid(text, i, 1)) > 0 Then |
|
url = url & Mid(text, i, 1) |
|
Else |
|
Exit Do |
|
End If |
|
i = i + 1 |
|
Loop |
|
|
|
ExtractFullURL = url |
|
End Function |
|
|
|
Function ExtractURLParameter(fullURL As String, parameterName As String) As String |
|
Dim paramStart As Long |
|
Dim paramEnd As Long |
|
Dim encodedParam As String |
|
Dim decodedParam As String |
|
|
|
' Locate the parameter in the URL |
|
paramStart = InStr(fullURL, parameterName & "=") |
|
If paramStart > 0 Then |
|
paramStart = paramStart + Len(parameterName) + 1 |
|
paramEnd = InStr(paramStart, fullURL, "&") |
|
If paramEnd = 0 Then paramEnd = Len(fullURL) + 1 |
|
|
|
' Extract and decode the parameter value |
|
encodedParam = Mid(fullURL, paramStart, paramEnd - paramStart) |
|
decodedParam = DecodeURL(encodedParam) |
|
ExtractURLParameter = decodedParam |
|
Else |
|
ExtractURLParameter = "" |
|
End If |
|
End Function |
|
|
|
Function DecodeURL(safeLink As String) As String |
|
Dim decodedURL As String |
|
Dim i As Long |
|
Dim hexValue As String |
|
|
|
decodedURL = "" |
|
i = 1 |
|
|
|
Do While i <= Len(safeLink) |
|
If Mid(safeLink, i, 1) = "%" Then |
|
hexValue = Mid(safeLink, i + 1, 2) |
|
decodedURL = decodedURL & ChrW("&H" & hexValue) |
|
i = i + 3 |
|
Else |
|
decodedURL = decodedURL & Mid(safeLink, i, 1) |
|
i = i + 1 |
|
End If |
|
Loop |
|
|
|
DecodeURL = decodedURL |
|
End Function |
|
|