Skip to content

Instantly share code, notes, and snippets.

@dtenenba
Last active February 24, 2017 17:23
Show Gist options
  • Save dtenenba/1790fc06e171e2339f229608d48931f3 to your computer and use it in GitHub Desktop.
Save dtenenba/1790fc06e171e2339f229608d48931f3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
Use this as a pipe to change proofpoint-mangled URLs like this:
https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_bioc-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=TF6f93hjWmgMzjqP9F3thRifibmFvfjc5Ae-bzNwDGo&m=oY-7oZ61w7BXE0OdDWKGHJBQVDfKclm7qIZa9Ax4MKo&s=2yq6rudFXxjoM851HucPcWoNvhH2RgPkAMcWOl7tRZc&e=
to 'unmangled' URLs like this:
https://stat.ethz.ch/mailman/listinfo/bioc-devel
Usage Example:
Say you have copied the contents of an email to the clipboard. On a Mac, you have the
`pbcopy` and `pbpaste` commands to work with clipboard contents on the command line.
(Ubuntu equivalent here: http://garywoodfine.com/use-pbcopy-on-ubuntu/ )
After making this script executable (with `chmod +x unmangle.py`) you can then run
it like this:
pbpaste | ./unmangle.py | pbcopy
Your clipboard now contains the unmangled version of the email text you copied.
"""
import re
import fileinput
import sys
if sys.version_info[0] == 2:
from urllib import unquote
elif sys.version_info[0] == 3:
from urllib.parse import unquote
regex = re.compile(r"https://urldefense.proofpoint.com/v2/url[^\s]*")
def replacefunc(input):
matches = re.findall(regex, input)
for match in matches:
instr = match.split("?u=")[1]
instr = instr.split("&")[0]
instr = instr.replace("_", "/").replace("-", "%")
instr = unquote(instr)
input = input.replace(match, instr)
return input
for line in fileinput.input():
line = replacefunc(line)
print(line.strip())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment