pymilter 1.0.5
Milter.utils Namespace Reference

Miscellaneous functions. More...

Functions

 addr2bin (s)
 bin2long6 (s)
 inet_ntop (s)
 inet_pton (s)
 cidr (i, n, mask=MASK)
 iniplist (ipaddr, iplist)
 parseaddr (t)
 Split email into Fullname and address.
 decode (s, convert_eols=None)
 Fix email.base64mime.decode to add any missing padding.
 parse_addr (t)
 parse_header (val)
 Decode headers gratuitously encoded to hide the content.

Variables

 dnsre = re.compile(r'^[a-z][-a-z\d.]+$', re.IGNORECASE)
str PAT_IP4 = r'\.'.join([r'(?:\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])']*4)
 ip4re = re.compile(PAT_IP4+'$')
 ip6re
int MASK = 0xFFFFFFFF
int MASK6 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
 decode

Detailed Description

Miscellaneous functions.

Function Documentation

◆ addr2bin()

Milter.utils.addr2bin ( s)
Convert a string IPv4 address into an unsigned integer.

References bin2long6().

Referenced by iniplist().

◆ bin2long6()

Milter.utils.bin2long6 ( s)
Convert binary IP6 address into an unsigned Python long integer.

Referenced by addr2bin(), and iniplist().

◆ iniplist()

Milter.utils.iniplist ( ipaddr,
iplist )
Return whether ip is in cidr list
>>> iniplist('66.179.26.146',['127.0.0.1','66.179.26.128/26'])
True
>>> iniplist('127.0.0.1',['127.0.0.1','66.179.26.128/26'])
True
>>> iniplist('192.168.0.45',['192.168.0.*'])
True
>>> iniplist('4.2.2.2',['b.resolvers.Level3.net'])
True
>>> iniplist('2606:2800:220:1::',['example.com/40'])
True
>>> iniplist('4.2.2.2',['nothing.example.com'])
False
>>> iniplist('2001:610:779:0:223:6cff:fe9a:9cf3',['127.0.0.1','172.20.1.0/24','2001:610:779::/48'])
True
>>> iniplist('2G01:610:779:0:223:6cff:fe9a:9cf3',['127.0.0.1','172.20.1.0/24','2001:610:779::/48'])
Traceback (most recent call last):
  ...
ValueError: Invalid ip syntax:2G01:610:779:0:223:6cff:fe9a:9cf3

References addr2bin(), bin2long6(), and iniplist().

Referenced by iniplist().

◆ parse_addr()

Milter.utils.parse_addr ( t)
Split email into user,domain.

>>> parse_addr('user@example.com')
['user', 'example.com']
>>> parse_addr('"user@example.com"')
['user@example.com']
>>> parse_addr('"user@bar"@example.com')
['user@bar', 'example.com']
>>> parse_addr('foo')
['foo']
>>> parse_addr('@mx.example.com:user@example.com')
['user', 'example.com']
>>> parse_addr('@user@example.com')
['@user', 'example.com']

◆ parse_header()

Milter.utils.parse_header ( val)

Decode headers gratuitously encoded to hide the content.

Spammers often encode headers to obscure the content from spam filters. This function decodes gratuitously encoded headers.

Parameters
valthe raw header value
Returns
the decoded value or the original raw value
Decode headers gratuitously encoded to hide the content.

◆ parseaddr()

Milter.utils.parseaddr ( t)

Split email into Fullname and address.

This replaces email.utils.parseaddr but fixes some tricky test cases. Additional tricky cases are still broken. Patches welcome.

Split email into Fullname and address.

>>> parseaddr('user@example.com')
('', 'user@example.com')
>>> parseaddr('"Full Name" <foo@example.com>')
('Full Name', 'foo@example.com')
>>> parseaddr('spam@spammer.com <foo@example.com>')
('spam@spammer.com', 'foo@example.com')
>>> parseaddr('God@heaven <@hop1.org,@hop2.net:jeff@spec.org>')
('God@heaven', 'jeff@spec.org')
>>> parseaddr('Real Name ((comment)) <addr...@example.com>')
('Real Name (comment)', 'addr...@example.com')

Variable Documentation

◆ ip6re

Milter.utils.ip6re
Initial value:
1= re.compile( '(?:%(hex4)s:){6}%(ls32)s$'
2 '|::(?:%(hex4)s:){5}%(ls32)s$'
3 '|(?:%(hex4)s)?::(?:%(hex4)s:){4}%(ls32)s$'
4 '|(?:(?:%(hex4)s:){0,1}%(hex4)s)?::(?:%(hex4)s:){3}%(ls32)s$'
5 '|(?:(?:%(hex4)s:){0,2}%(hex4)s)?::(?:%(hex4)s:){2}%(ls32)s$'
6 '|(?:(?:%(hex4)s:){0,3}%(hex4)s)?::%(hex4)s:%(ls32)s$'
7 '|(?:(?:%(hex4)s:){0,4}%(hex4)s)?::%(ls32)s$'
8 '|(?:(?:%(hex4)s:){0,5}%(hex4)s)?::%(hex4)s$'
9 '|(?:(?:%(hex4)s:){0,6}%(hex4)s)?::$'
10 % {
11 'ls32': r'(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|%s)'%PAT_IP4,
12 'hex4': r'[0-9a-f]{1,4}'
13 }, re.IGNORECASE)