def testEmailAddresses(arr): for email in arr: print "\tEmail Address: ", email print "\t---------------------------------" #Strip any name information from email address email = Utils.parseaddr(email)[1] if isvalidEmailOne(email): print "\t\tisvalidEmailOne PASSED" else: print "\t\tisvalidEmailOne FAILED" if isvalidEmailTwo(email): print "\t\tisvalidEmailTwo PASSED" else: print "\t\tisvalidateEmailTwo FAILED" if isvalidEmailThree(email): print "\t\tisvalidateEmailThree PASSED" else: print "\t\tisvalidateEmailThree FAILED" if isvalidEmailFour(email): print "\t\tisvalidateEmailFour PASSED" else: print "\t\tisvalidateEmailFour FAILED" if isvalidEmailFive(email): print "\t\tisvalidateEmailFive PASSED" else: print "\t\tisvalidateEmailFive FAILED" print "\n"
def __call__(self, mi, log): self._load_if_needed() frm = mi.msg["from"] realname, frm = Utils.parseaddr(frm) status = (frm is not None) and (frm.lower() in self.addresses) if status: log.pass_test(SPAM) return "it is in 'from' white list" return False
def getPeer(view, messageObject): # Get the from address ie. Sender. emailAddr = messageObject.get("From") if not emailAddr: return None name, addr = emailUtils.parseaddr(emailAddr) return EmailAddress.getEmailAddress(view, getUnicodeValue(addr), decodeHeader(name))
def emailAddressesAreEqual(cls, emailAddressOne, emailAddressTwo): """ This method tests whether two email addresses are the same. Addresses can be in the form [email protected] or John Jones <*****@*****.**>. The method strips off the username and <> brakets if they exist and just compares the actual email addresses for equality. It will not look to see if each address is RFC 822 compliant only that the strings match. Use C{EmailAddress.isValidEmailAddress} to test for validity. @param emailAddressOne: A string containing a email address to compare. @type emailAddressOne: C{String} @param emailAddressTwo: A string containing a email address to compare. @type emailAddressTwo: C{String} @return: C{Boolean} """ assert isinstance(emailAddressOne, (str, unicode)) assert isinstance(emailAddressTwo, (str, unicode)) emailAddressOne = Utils.parseaddr(emailAddressOne)[1] emailAddressTwo = Utils.parseaddr(emailAddressTwo)[1] return emailAddressOne.lower() == emailAddressTwo.lower()
def address_to_encoded_header(address): """RFC2047-encode an address if necessary. :param address: An unicode string, or UTF-8 byte string. :return: A possibly RFC2047-encoded string. """ # Can't call Header over all the address, because that encodes both the # name and the email address, which is not permitted by RFCs. user, email = Utils.parseaddr(address) if not user: return email else: return Utils.formataddr((str(Header.Header(safe_unicode(user))), email))
def isValidEmailAddress(cls, emailAddress): """ This method tests an email address for valid syntax as defined RFC 822. The method validates addresses in the form 'John Jones <*****@*****.**>' and '*****@*****.**' @param emailAddress: A string containing a email address to validate. @type addr: C{String} @return: C{Boolean} """ assert isinstance(emailAddress, (str, unicode)), "Email Address must be in string or unicode format" #XXX: Strip any name information. i.e. John test <*****@*****.**>`from the email address emailAddress = Utils.parseaddr(emailAddress)[1] return re.match("^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$", emailAddress) is not None
def get_message_addresses(message): """Get the origin and destination addresses of a message. :param message: A message object supporting get() to access its headers, like email.Message or bzrlib.email_message.EmailMessage. :return: A pair (from_email, to_emails), where from_email is the email address in the From header, and to_emails a list of all the addresses in the To, Cc, and Bcc headers. """ from_email = Utils.parseaddr(message.get('From', None))[1] to_full_addresses = [] for header in ['To', 'Cc', 'Bcc']: value = message.get(header, None) if value: to_full_addresses.append(value) to_emails = [pair[1] for pair in Utils.getaddresses(to_full_addresses)] return from_email, to_emails
def get_message_addresses(message): """Get the origin and destination addresses of a message. :param message: A message object supporting get() to access its headers, like email.Message or bzrlib.email_message.EmailMessage. :return: A pair (from_email, to_emails), where from_email is the email address in the From header, and to_emails a list of all the addresses in the To, Cc, and Bcc headers. """ from_email = Utils.parseaddr(message.get('From', None))[1] to_full_addresses = [] for header in ['To', 'Cc', 'Bcc']: value = message.get(header, None) if value: to_full_addresses.append(value) to_emails = [ pair[1] for pair in Utils.getaddresses(to_full_addresses) ] return from_email, to_emails
def __assignToKind(view, kindVar, messageObject, key, hType, attr, decode, makeUnicode): header = messageObject.get(key) if header is None: return None if decode: header = decodeHeader(header) elif makeUnicode: header = getUnicodeValue(header) if hType == "String": setattr(kindVar, attr, header) elif hType == "EmailAddress": name, addr = emailUtils.parseaddr(messageObject.get(key)) if decode: name = decodeHeader(name) if makeUnicode: addr = getUnicodeValue(addr) ea = EmailAddress.getEmailAddress(view, addr, name) if ea is not None: setattr(kindVar, attr, ea) elif hType == "EmailAddressList": for name, addr in emailUtils.getaddresses( messageObject.get_all(key, [])): if decode: name = decodeHeader(name) if makeUnicode: addr = getUnicodeValue(addr) ea = EmailAddress.getEmailAddress(view, addr, name) if ea is not None: kindVar.append(ea)
def __assignToKind(view, kindVar, messageObject, key, hType, attr, decode, makeUnicode): header = messageObject.get(key) if header is None: return None if decode: header = decodeHeader(header) elif makeUnicode: header = getUnicodeValue(header) if hType == "String": setattr(kindVar, attr, header) elif hType == "EmailAddress": name, addr = emailUtils.parseaddr(messageObject.get(key)) if decode: name = decodeHeader(name) if makeUnicode: addr = getUnicodeValue(addr) ea = EmailAddress.getEmailAddress(view, addr, name) if ea is not None: setattr(kindVar, attr, ea) elif hType == "EmailAddressList": for name, addr in emailUtils.getaddresses(messageObject.get_all(key, [])): if decode: name = decodeHeader(name) if makeUnicode: addr = getUnicodeValue(addr) ea = EmailAddress.getEmailAddress(view, addr, name) if ea is not None: kindVar.append(ea)
def validate_message_headers(msg, check_subject): # Check the validity of the message. if check_subject and not msg['subject']: raise MessageFormatError('No Subject.') # Get all the recipient addresses. rcpts = [a for (n, a) in unicode_getalladdrs(msg, 'to', 'cc', 'bcc')] if not rcpts: raise MessageFormatError('No Recipient.') if not msg['from']: raise MessageFormatError('No From.') (_, fromaddr) = Utils.parseaddr(msg['from']) for addr in [fromaddr] + rcpts: if '@' not in addr: raise MessageFormatError('Invalid address: %s' % addr) try: addr.encode('ascii', 'strict') except UnicodeError: raise MessageFormatError('Non-ascii address: %r' % addr) fromaddr = str(fromaddr) rcpts = map(str, rcpts) return (fromaddr, rcpts)
def validate_message_headers(msg, check_subject): # Check the validity of the message. if check_subject and not msg["subject"]: raise MessageFormatError("No Subject.") # Get all the recipient addresses. rcpts = [a for (n, a) in unicode_getalladdrs(msg, "to", "cc", "bcc")] if not rcpts: raise MessageFormatError("No Recipient.") if not msg["from"]: raise MessageFormatError("No From.") (_, fromaddr) = Utils.parseaddr(msg["from"]) for addr in [fromaddr] + rcpts: if "@" not in addr: raise MessageFormatError("Invalid address: %s" % addr) try: addr.encode("ascii", "strict") except UnicodeError: raise MessageFormatError("Non-ascii address: %r" % addr) fromaddr = str(fromaddr) rcpts = map(str, rcpts) return (fromaddr, rcpts)
def __assignToKind(view, kindVar, messageObject, key, type, attr=None, decode=True): header = messageObject.get(key) if header is None: return if decode: header = decodeHeader(header) if type == "String": setattr(kindVar, attr, header) # XXX: This logic will need to be expanded elif type == "StringList": kindVar.append(header) elif type == "EmailAddress": name, addr = emailUtils.parseaddr(messageObject.get(key)) ea = __getEmailAddress(view, decodeHeader(name), addr) if ea is not None: setattr(kindVar, attr, ea) elif __debug__: logging.error("__assignToKind: invalid email address found %s: %s" % (key, addr)) elif type == "EmailAddressList": for name, addr in emailUtils.getaddresses(messageObject.get_all(key, [])): ea = __getEmailAddress(view, decodeHeader(name), addr) if ea is not None: kindVar.append(ea) elif __debug__: logging.error("__assignToKind: invalid email address found %s: %s" % (key, addr)) del messageObject[key]
def previewQuickConvert(view, headers, body, eimml, ics): # 1.0 Case # 1. Standard mail message # a. headers (build a decoded headers dict using m.keys()) # b. body # 2. ICS # a. headers (build a decoded headers dict using m.keys()) # b. body # c. decoded ics attachment # 3. EIM # a. headers (build a decoded headers dict using m.keys()) # b. decoded eim attachment mailStamp = icsDesc = icsSummary = None if eimml: # Get the from address ie. Sender. emailAddr = headers.get("From") if not emailAddr: # A peer address is required for eimml # deserialization. If there is no peer # then ignore the eimml data. return (-1, None) name, addr = emailUtils.parseaddr(emailAddr) peer = EmailAddress.getEmailAddress(view, addr, name) matchingAddresses = [] for address in addressMatchGenerator(peer): matchingAddresses.append(address) # the matchingAddresses list will at least contain the # peer address since it is an EmailAddress Item and # there for will be in the EmailAddressCollection index. statusCode, mailStamp = parseEIMML(view, peer, matchingAddresses, eimml) if statusCode != 1: # There was either an error during # processing of the eimml or the # eimml was older than the current # Item's state so it was ignored. return (statusCode, None) elif ics: result = parseICS(view, ics) if result is not None: # If the result is None then there # was an error that prevented converting # the ics text to a Chandler Item # in which case the ics is ignored and # the rest of the message parsed. mailStamp, icsDesc, icsSummary = result if not mailStamp: mailStamp = MailMessage(itsView=view) mailStamp.fromEIMML = False if getattr(mailStamp, "messageId", None): # The presence a messageId indicated that # this message has already been sent or # received and thus is an update. mailStamp.isUpdated = True # Setting these values here reduces Observer # processing time when calculating if the # message should appear in the In Collection mailStamp.viaMailService = True # Look at the from address if it matches a me address # and the ignore me attribute enabled then set toMe to # false otherwise set to True mailStamp.toMe = True if not mailStamp.fromEIMML: mailStamp.body = body if icsSummary or icsDesc: # If ics summary or ics description exist then # add them to the message body mailStamp.body += buildICSInfo(mailStamp, icsSummary, icsDesc) __parseHeaders(view, headers, mailStamp, False, False) return (1, mailStamp)
def update_list (mlist, **kw): archive = mlist.data.get ('list_archive') mboxes = [] if archive != None: links = LinkExtractor (archive).get_links () for link in links: if link.endswith ('.txt.gz'): mboxes.append (urlparse.urljoin (archive, link)) mlist.data.setdefault ('archives', {}) for url in mboxes: parsed = urlparse.urlparse (url) con = httplib.HTTPConnection (parsed[1]) headers = {'Accept-encoding': 'gzip'} if kw.get('timestamps', True): dats = mlist.data['archives'].get (url, {}) mod = dats.get ('modified') if mod != None: headers['If-Modified-Since'] = mod etag = dats.get ('etag') if etag != None: headers['Etag'] =etag con.request ('GET', parsed[2], None, headers) res = con.getresponse () if res.status != 200: continue urlfile = url urlslash = urlfile.rfind ('/') if urlslash >= 0: urlfile = urlfile[urlslash+1:] pulse.utils.log ('Processing list archive %s %s' % (mlist.ident, urlfile)) urlbase = url[:-7] + '/' tmp = pulse.utils.tmpfile() fd = open (tmp, 'w') fd.write (gzip.GzipFile (fileobj=StringIO.StringIO (res.read ())).read ()) fd.close () mbox = mailbox.PortableUnixMailbox (open(tmp, 'rb')) i = -1 for msg in mbox: i += 1 msgfrom = msgdate = msgsubject = msgid = msgparent = None for hdr in msg.headers: lower = hdr.lower() if lower.startswith ('from:'): msgfrom = hdr[5:] elif lower.startswith ('date:'): msgdate = hdr[5:] elif lower.startswith ('subject:'): msgsubject = hdr[8:].strip() elif lower.startswith ('message-id:'): msgid = hdr[11:] elif lower.startswith ('in-reply-to:'): msgparent = hdr[12:] if msgid == None: continue msgid = pulse.utils.utf8dec (emailutils.parseaddr (msgid)[1]) ident = mlist.ident + u'/' + msgid post = pulse.db.ForumPost.get_or_create (ident, u'ListPost') postdata = {'forum': mlist, 'name': msgsubject} if msgparent != None: msgparent = pulse.utils.utf8dec (emailutils.parseaddr (msgparent)[1]) pident = mlist.ident + '/' + msgparent parent = pulse.db.ForumPost.get_or_create (pident, u'ListPost') parent.forum = mlist postdata['parent_ident'] = parent.ident msgfrom = emailutils.parseaddr (msgfrom) personident = u'/person/' + pulse.utils.utf8dec (msgfrom[1]) person = pulse.db.Entity.get_or_create (personident, u'Person') person.extend (name=msgfrom[0]) postdata['author_ident'] = person.ident if person.ident != personident: postdata['alias_ident'] = personident pulse.db.Queue.push (u'people', person.ident) msgdate = emailutils.parsedate_tz (msgdate) try: dt = datetime.datetime (*msgdate[:6]) if msgdate[-1] != None: dt = dt + datetime.timedelta (seconds=msgdate[-1]) except: dt = None postdata['datetime'] = dt postdata['weeknum'] = pulse.utils.weeknum (dt) postdata['web'] = urlbase + 'msg%05i.html' % i post.update (postdata) try: os.remove (tmp) except: pass mlist.data['archives'].setdefault (url, {}) mlist.data['archives'][url]['modified'] = res.getheader ('Last-Modified') mlist.data['archives'][url]['etag'] = res.getheader ('Etag') update_graphs (mlist, 100, **kw)
else: return False def add_pair(local_addr, remote_addr): log = open('%s%s' % (vacation_log_path, local_addr), 'a') log.write(remote_addr) log.write("\n") log.close() raw_email = sys.stdin.read() p = Parser.Parser() msg = p.parsestr(raw_email) from_addr = Utils.parseaddr(msg.__getitem__('From')) to_addr = Utils.parseaddr(msg.__getitem__('To')) subject = msg.__getitem__('Subject') l = ldap.initialize('ldap://%s:%d' % (ldap_host, int(ldap_port))) try: vacation_search = l.search_s(ldap_base, ldap.SCOPE_SUBTREE, "(mail=%s)" % to_addr[1], ['vacationActive', 'vacationInfo']) except ldap.NO_SUCH_OBJECT: log(syslog.LOG_ERR, "The user with email: %s was not found in the LDAP tree" % to_addr[1]) sys.exit(1) except ldap.SERVER_DOWN: log(syslog.LOG_ERR, "Can't connect to ldap server on %s address and %s port" % (ldap_host, ldap_port.__str__())) sys.exit(1) if not vacation_search:
def test_parseaddr_empty(self): self.assertEqual(Utils.parseaddr('<>'), ('', '')) self.assertEqual(Utils.dump_address_pair(Utils.parseaddr('<>')), '')
def _parse_id(self, id): if id.find('<') == -1: return id, "" else: return Utils.parseaddr(id)
# Copyright (C) 2001 Python Software Foundation