mirror of
https://github.com/python/cpython.git
synced 2025-09-11 19:27:07 +00:00
New, improved parseaddr() by Sjoerd.
This commit is contained in:
parent
7698d12a8b
commit
3534a8932a
1 changed files with 83 additions and 30 deletions
111
Lib/rfc822.py
111
Lib/rfc822.py
|
@ -297,35 +297,88 @@ def unquote(str):
|
||||||
# Parse an address into (name, address) tuple
|
# Parse an address into (name, address) tuple
|
||||||
|
|
||||||
def parseaddr(address):
|
def parseaddr(address):
|
||||||
# This is probably not perfect
|
import string
|
||||||
address = string.strip(address)
|
str = ''
|
||||||
# Case 1: part of the address is in <xx@xx> form.
|
email = ''
|
||||||
pos = regex.search('<.*>', address)
|
comment = ''
|
||||||
if pos >= 0:
|
backslash = 0
|
||||||
name = address[:pos]
|
dquote = 0
|
||||||
address = address[pos:]
|
space = 0
|
||||||
length = regex.match('<.*>', address)
|
paren = 0
|
||||||
name = name + address[length:]
|
bracket = 0
|
||||||
address = address[:length]
|
seen_bracket = 0
|
||||||
|
for c in address:
|
||||||
|
if backslash:
|
||||||
|
str = str + c
|
||||||
|
backslash = 0
|
||||||
|
continue
|
||||||
|
if c == '\\':
|
||||||
|
backslash = 1
|
||||||
|
continue
|
||||||
|
if dquote:
|
||||||
|
if c == '"':
|
||||||
|
dquote = 0
|
||||||
else:
|
else:
|
||||||
# Case 2: part of the address is in (comment) form
|
str = str + c
|
||||||
pos = regex.search('(.*)', address)
|
continue
|
||||||
if pos >= 0:
|
if c == '"':
|
||||||
name = address[pos:]
|
dquote = 1
|
||||||
address = address[:pos]
|
continue
|
||||||
length = regex.match('(.*)', name)
|
if c in string.whitespace:
|
||||||
address = address + name[length:]
|
space = 1
|
||||||
name = name[:length]
|
continue
|
||||||
|
if space:
|
||||||
|
str = str + ' '
|
||||||
|
space = 0
|
||||||
|
if paren:
|
||||||
|
if c == '(':
|
||||||
|
paren = paren + 1
|
||||||
|
str = str + c
|
||||||
|
continue
|
||||||
|
if c == ')':
|
||||||
|
paren = paren - 1
|
||||||
|
if paren == 0:
|
||||||
|
comment = comment + str
|
||||||
|
str = ''
|
||||||
|
continue
|
||||||
|
if c == '(':
|
||||||
|
paren = paren + 1
|
||||||
|
if bracket:
|
||||||
|
email = email + str
|
||||||
|
str = ''
|
||||||
|
elif not seen_bracket:
|
||||||
|
email = email + str
|
||||||
|
str = ''
|
||||||
|
continue
|
||||||
|
if bracket:
|
||||||
|
if c == '>':
|
||||||
|
bracket = 0
|
||||||
|
email = email + str
|
||||||
|
str = ''
|
||||||
|
continue
|
||||||
|
if c == '<':
|
||||||
|
bracket = 1
|
||||||
|
seen_bracket = 1
|
||||||
|
comment = comment + str
|
||||||
|
str = ''
|
||||||
|
email = ''
|
||||||
|
continue
|
||||||
|
if c == '#' and not bracket and not paren:
|
||||||
|
# rest is comment
|
||||||
|
break
|
||||||
|
str = str + c
|
||||||
|
if str:
|
||||||
|
if seen_bracket:
|
||||||
|
if bracket:
|
||||||
|
email = str
|
||||||
else:
|
else:
|
||||||
# Case 3: neither. Only an address
|
comment = comment + str
|
||||||
name = ''
|
else:
|
||||||
name = string.strip(name)
|
if paren:
|
||||||
address = string.strip(address)
|
comment = comment + str
|
||||||
if address and address[0] == '<' and address[-1] == '>':
|
else:
|
||||||
address = address[1:-1]
|
email = email + str
|
||||||
if name and name[0] == '(' and name[-1] == ')':
|
return string.strip(comment), string.strip(email)
|
||||||
name = name[1:-1]
|
|
||||||
return name, address
|
|
||||||
|
|
||||||
|
|
||||||
# Parse a date field
|
# Parse a date field
|
||||||
|
@ -376,8 +429,8 @@ def parsedate(data):
|
||||||
# message in RFC-822 format.
|
# message in RFC-822 format.
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import sys
|
import sys, os
|
||||||
file = '/ufs/guido/Mail/drafts/,1'
|
file = os.path.join(os.environ['HOME'], 'Mail/drafts/,1')
|
||||||
if sys.argv[1:]: file = sys.argv[1]
|
if sys.argv[1:]: file = sys.argv[1]
|
||||||
f = open(file, 'r')
|
f = open(file, 'r')
|
||||||
m = Message(f)
|
m = Message(f)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue