+def valid_link(link):
+ import re
+ mail_re = r"^[^ \t\n\r@<>()]+@[a-z0-9][a-z0-9\.\-_]*\.[a-z]+$"
+ scheme_re = r'^[a-zA-Z]+:'
+ url_re = r'^(?:[a-z0-9\-]+|[a-z0-9][a-z0-9\-\.\_]*\.[a-z]+)' \
+ r'(?::[0-9]+)?(?:/.*)?$'
+ scheme = ''
+ rest = link
+ if re.match(scheme_re, link, re.I):
+ scheme, rest = link.split(':', 1)
+ if (not scheme or scheme == 'mailto') and re.match(mail_re, rest, re.I):
+ return 'mailto:' + link
+ if not scheme and re.match(url_re, rest, re.I):
+ return 'http://' + rest
+ if scheme:
+ return link
+ return None
+