def __init__(self, m, to, sender, subject=None, cc=None, bcc=None): super().__init__() self.m = check_m(m) to = to_list(to) cc = to_list(cc) bcc = to_list(bcc) if not subject: subject = '%s mailer' % (IDENT) subject = '%s - %s' % (subject, get_timestamp()) self.__sender = sender self.__recipients = list(_chain(to, cc, bcc)) _charset.add_charset('utf-8', _charset.QP, _charset.QP, 'UTF-8') self.__message = _MIMEMultipart() self.__message.add_header('To', ', '.join(to)) if cc: self.__message.add_header('CC', ', '.join(cc)) self.__message.add_header('From', sender) self.__message.add_header('Subject', subject) self.__message.add_header('Date', _formatdate()) self.__message.add_header('X-Mailer', '%s mailer' % (IDENT)) self.m( 'mail tool startup done', more=dict(to=to, cc=cc, bcc=bcc, sender=sender, subject=subject), verbose=False )
def __init__(self, m, to, sender, subject=None, cc=None, bcc=None): super().__init__() self.m = check_m(m) to = to_list(to) cc = to_list(cc) bcc = to_list(bcc) if not subject: subject = '%s mailer' % (IDENT) subject = '%s - %s' % (subject, get_timestamp()) self.__sender = sender self.__recipients = list(_chain(to, cc, bcc)) _charset.add_charset('utf-8', _charset.QP, _charset.QP, 'UTF-8') self.__message = _MIMEMultipart() self.__message.add_header('To', ', '.join(to)) if cc: self.__message.add_header('CC', ', '.join(cc)) self.__message.add_header('From', sender) self.__message.add_header('Subject', subject) self.__message.add_header('Date', _formatdate()) self.__message.add_header('X-Mailer', '%s mailer' % (IDENT)) self.m('mail tool startup done', more=dict(to=to, cc=cc, bcc=bcc, sender=sender, subject=subject), verbose=False)
def _get_entry_date(self, entry): datetime = _time.gmtime() if self.date_header: for datetype in self.date_header_order: kind = datetype + '_parsed' if entry.get(kind, None): datetime = entry[kind] break return _formatdate(_calendar.timegm(datetime))
def resolve(self, uriRef, baseUri=None): """ Takes a URI or a URI reference plus a base URI, produces a absolutized URI if a base URI was given, then attempts to obtain access to an entity representing the resource identified by the resulting URI, returning the entity as a stream (a file-like object). Raises a IriError if the URI scheme is unsupported or if a stream could not be obtained for any reason. """ if not isinstance(uriRef, urllib.request.Request): if baseUri is not None: uri = self.absolutize(uriRef, baseUri) scheme = get_scheme(uri) else: uri = uriRef scheme = get_scheme(uriRef) # since we didn't use absolutize(), we need to verify here if scheme not in self._supported_schemes: if scheme is None: raise ValueError('When the URI to resolve is a relative ' 'reference, it must be accompanied by a base URI.') else: raise IriError(IriError.UNSUPPORTED_SCHEME, scheme=scheme, resolver=self.__class__.__name__) req = urllib.request.Request(uri) else: req, uri = uriRef, uriRef.get_full_url() if self.authorizations and not self.authorize(uri): raise IriError(IriError.DENIED_BY_RULE, uri=uri) # Bypass urllib for opening local files. if scheme == 'file': path = uri_to_os_path(uri, attemptAbsolute=False) try: stream = open(path, 'rb') except IOError as e: raise IriError(IriError.RESOURCE_ERROR, loc='%s (%s)' % (uri, path), uri=uri, msg=str(e)) # Add the extra metadata that urllib normally provides (sans # the poorly guessed Content-Type header). stats = os.stat(path) size = stats.st_size mtime = _formatdate(stats.st_mtime) headers = email.Message(io.StringIO( 'Content-Length: %s\nLast-Modified: %s\n' % (size, mtime))) stream = urllib.addinfourl(stream, headers, uri) else: # urllib.request.urlopen, wrapped by us, will suffice for http, ftp, # data and gopher try: stream = urllib.request.urlopen(req) except IOError as e: raise IriError(IriError.RESOURCE_ERROR, uri=uri, loc=uri, msg=str(e)) return stream
def run(basedir, course, message, person, subject, max_late=0, dry_run=None, **kwargs): """ >>> from pgp_mime.email import encodedMIMEText >>> from ..test.course import StubCourse >>> from . import Response >>> course = StubCourse() >>> person = list( ... course.course.find_people(email='*****@*****.**'))[0] >>> message = encodedMIMEText('The answer is 42.') >>> message['Message-ID'] = '<*****@*****.**>' >>> message['Received'] = ( ... 'from smtp.home.net (smtp.home.net [123.456.123.456]) ' ... 'by smtp.mail.uu.edu (Postfix) with ESMTP id 5BA225C83EF ' ... 'for <*****@*****.**>; Sun, 09 Oct 2011 11:50:46 -0400 (EDT)') >>> subject = '[submit] assignment 1' >>> try: ... run(basedir=course.basedir, course=course.course, message=message, ... person=person, subject=subject, max_late=0) ... except Response as e: ... print('respond with:') ... print(e.message.as_string()) ... # doctest: +ELLIPSIS, +REPORT_UDIFF respond with: Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline Subject: Received Assignment 1 submission <BLANKLINE> We received your submission for Assignment 1 on .... >>> course.cleanup() """ time = _message_time(message=message) assignment = _get_assignment(course=course, subject=subject) assignment_path = _assignment_path(basedir, assignment, person) _save_local_message_copy( msg=message, person=person, assignment_path=assignment_path, dry_run=dry_run) _extract_mime(message=message, output=assignment_path, dry_run=dry_run) _check_late( basedir=basedir, assignment=assignment, person=person, time=time, max_late=max_late, dry_run=dry_run) if time: time_str = 'on {}'.format(_formatdate(time)) else: time_str = 'at an unknown time' message = _pgp_mime.encodedMIMEText(( 'We received your submission for {} {}.' ).format( assignment.name, time_str)) message['Subject'] = 'Received {} submission'.format(assignment.name) raise _Response(message=message)
def resolve(self, uriRef, baseUri=None): """ Takes a URI or a URI reference plus a base URI, produces a absolutized URI if a base URI was given, then attempts to obtain access to an entity representing the resource identified by the resulting URI, returning the entity as a stream (a file-like object). Raises a IriError if the URI scheme is unsupported or if a stream could not be obtained for any reason. """ if not isinstance(uriRef, urllib.request.Request): if baseUri is not None: uri = self.absolutize(uriRef, baseUri) scheme = get_scheme(uri) else: uri = uriRef scheme = get_scheme(uriRef) # since we didn't use absolutize(), we need to verify here if scheme not in self._supported_schemes: if scheme is None: raise ValueError( 'When the URI to resolve is a relative ' 'reference, it must be accompanied by a base URI.') else: raise IriError(IriError.UNSUPPORTED_SCHEME, scheme=scheme, resolver=self.__class__.__name__) req = urllib.request.Request(uri) else: req, uri = uriRef, uriRef.get_full_url() if self.authorizations and not self.authorize(uri): raise IriError(IriError.DENIED_BY_RULE, uri=uri) # Bypass urllib for opening local files. if scheme == 'file': path = uri_to_os_path(uri, attemptAbsolute=False) try: stream = open(path, 'rb') except IOError as e: raise IriError(IriError.RESOURCE_ERROR, loc='%s (%s)' % (uri, path), uri=uri, msg=str(e)) # Add the extra metadata that urllib normally provides (sans # the poorly guessed Content-Type header). stats = os.stat(path) size = stats.st_size mtime = _formatdate(stats.st_mtime) headers = email.Message( io.StringIO('Content-Length: %s\nLast-Modified: %s\n' % (size, mtime))) stream = urllib.addinfourl(stream, headers, uri) else: # urllib.request.urlopen, wrapped by us, will suffice for http, ftp, # data and gopher try: stream = urllib.request.urlopen(req) except IOError as e: raise IriError(IriError.RESOURCE_ERROR, uri=uri, loc=uri, msg=str(e)) return stream
def run(basedir, course, message, person, subject, max_late=0, dry_run=None, **kwargs): """ >>> from pgp_mime.email import encodedMIMEText >>> from ..test.course import StubCourse >>> from . import Response >>> course = StubCourse() >>> person = list( ... course.course.find_people(email='*****@*****.**'))[0] >>> message = encodedMIMEText('The answer is 42.') >>> message['Message-ID'] = '<*****@*****.**>' >>> message['Received'] = ( ... 'from smtp.home.net (smtp.home.net [123.456.123.456]) ' ... 'by smtp.mail.uu.edu (Postfix) with ESMTP id 5BA225C83EF ' ... 'for <*****@*****.**>; Sun, 09 Oct 2011 11:50:46 -0400 (EDT)') >>> subject = '[submit] assignment 1' >>> try: ... run(basedir=course.basedir, course=course.course, message=message, ... person=person, subject=subject, max_late=0) ... except Response as e: ... print('respond with:') ... print(e.message.as_string()) ... # doctest: +ELLIPSIS, +REPORT_UDIFF respond with: Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline Subject: Received Assignment 1 submission <BLANKLINE> We received your submission for Assignment 1 on .... >>> course.cleanup() """ time = _message_time(message=message) assignment = _get_assignment(course=course, subject=subject) assignment_path = _assignment_path(basedir, assignment, person) _save_local_message_copy(msg=message, person=person, assignment_path=assignment_path, dry_run=dry_run) _extract_mime(message=message, output=assignment_path, dry_run=dry_run) _check_late(basedir=basedir, assignment=assignment, person=person, time=time, max_late=max_late, dry_run=dry_run) if time: time_str = 'on {}'.format(_formatdate(time)) else: time_str = 'at an unknown time' message = _pgp_mime.encodedMIMEText( ('We received your submission for {} {}.').format( assignment.name, time_str)) message['Subject'] = 'Received {} submission'.format(assignment.name) raise _Response(message=message)