def handle_noargs(self, **options): """reads all emails from the INBOX of imap server and posts questions based on those emails. Badly formatted questions are bounced, as well as emails from unknown users all messages are deleted thereafter """ if not askbot_settings.ALLOW_ASKING_BY_EMAIL: raise CommandError("Asking by email is not enabled") # open imap server and select the inbox if django_settings.IMAP_USE_TLS: imap_getter = imaplib.IMAP4_SSL else: imap_getter = imaplib.IMAP4 imap = imap_getter(django_settings.IMAP_HOST, django_settings.IMAP_PORT) imap.login(django_settings.IMAP_HOST_USER, django_settings.IMAP_HOST_PASSWORD) imap.select("INBOX") # get message ids status, ids = imap.search(None, "ALL") if len(ids[0].strip()) == 0: # with empty inbox - close and exit imap.close() imap.logout() return # for each id - read a message, parse it and post a question for id in ids[0].split(" "): t, data = imap.fetch(id, "(RFC822)") message_body = data[0][1] msg = email.message_from_string(data[0][1]) imap.store(id, "+FLAGS", "\\Deleted") try: (sender, subject, body) = parse_message(msg) except CannotParseEmail, e: continue data = {"sender": sender, "subject": subject, "body_text": body} form = AskByEmailForm(data) print data if form.is_valid(): email_address = form.cleaned_data["email"] try: user = models.User.objects.get(email__iexact=email_address) except models.User.DoesNotExist: bounce_email(email_address, subject, reason="unknown_user") except models.User.MultipleObjectsReturned: bounce_email(email_address, subject, reason="problem_posting") tagnames = form.cleaned_data["tagnames"] title = form.cleaned_data["title"] body_text = form.cleaned_data["body_text"] try: user.post_question(title=title, tags=tagnames, body_text=body_text) except exceptions.PermissionDenied, e: bounce_email(email_address, subject, reason="permission_denied", body_text=unicode(e))
def process_emailed_question(from_address, subject, body_text, stored_files, tags=None, group_id=None): """posts question received by email or bounces the message""" # a bunch of imports here, to avoid potential circular import issues from askbot.forms import AskByEmailForm from askbot.models import ReplyAddress, User from askbot.mail import messages reply_to = None try: # todo: delete uploaded files when posting by email fails!!! data = {"sender": from_address, "subject": subject, "body_text": body_text} form = AskByEmailForm(data) if form.is_valid(): email_address = form.cleaned_data["email"] user = User.objects.get(email__iexact=email_address) if user.can_post_by_email() is False: raise PermissionDenied(messages.insufficient_reputation(user)) body_text = form.cleaned_data["body_text"] stripped_body_text = user.strip_email_signature(body_text) signature_not_detected = stripped_body_text == body_text and user.email_signature # ask for signature response if user's email has not been # validated yet or if email signature could not be found if user.email_isvalid is False or signature_not_detected: reply_to = ReplyAddress.objects.create_new(user=user, reply_action="validate_email").as_email_address() message = messages.ask_for_signature(user, footer_code=reply_to) raise PermissionDenied(message) tagnames = form.cleaned_data["tagnames"] title = form.cleaned_data["title"] # defect - here we might get "too many tags" issue if tags: tagnames += " " + " ".join(tags) user.post_question( title=title, tags=tagnames.strip(), body_text=stripped_body_text, by_email=True, email_address=from_address, group_id=group_id, ) else: raise ValidationError() except User.DoesNotExist: bounce_email(email_address, subject, reason="unknown_user") except User.MultipleObjectsReturned: bounce_email(email_address, subject, reason="problem_posting") except PermissionDenied, error: bounce_email(email_address, subject, reason="permission_denied", body_text=unicode(error), reply_to=reply_to)
def process_emailed_question(from_address, subject, parts, tags = None): """posts question received by email or bounces the message""" #a bunch of imports here, to avoid potential circular import issues from askbot.forms import AskByEmailForm from askbot.models import User try: #todo: delete uploaded files when posting by email fails!!! body, stored_files = process_parts(parts) data = { 'sender': from_address, 'subject': subject, 'body_text': body } form = AskByEmailForm(data) if form.is_valid(): email_address = form.cleaned_data['email'] user = User.objects.get( email__iexact = email_address ) tagnames = form.cleaned_data['tagnames'] title = form.cleaned_data['title'] body_text = form.cleaned_data['body_text'] #defect - here we might get "too many tags" issue if tags: tagnames += ' ' + ' '.join(tags) user.post_question( title = title, tags = tagnames, body_text = body_text, by_email = True, email_address = from_address ) else: raise ValidationError() except User.DoesNotExist: bounce_email(email_address, subject, reason = 'unknown_user') except User.MultipleObjectsReturned: bounce_email(email_address, subject, reason = 'problem_posting') except PermissionDenied, error: bounce_email( email_address, subject, reason = 'permission_denied', body_text = unicode(error) )
def process_emailed_question(from_address, subject, parts, tags = None): """posts question received by email or bounces the message""" #a bunch of imports here, to avoid potential circular import issues from askbot.forms import AskByEmailForm from askbot.models import User try: #todo: delete uploaded files when posting by email fails!!! body, stored_files = process_parts(parts) data = { 'sender': from_address, 'subject': subject, 'body_text': body } form = AskByEmailForm(data) if form.is_valid(): email_address = form.cleaned_data['email'] user = User.objects.get( email__iexact = email_address ) tagnames = form.cleaned_data['tagnames'] title = form.cleaned_data['title'] body_text = form.cleaned_data['body_text'] #defect - here we might get "too many tags" issue if tags: tagnames += ' ' + ' '.join(tags) user.post_question( title = title, tags = tagnames.strip(), body_text = body_text, by_email = True, email_address = from_address ) else: raise ValidationError() except User.DoesNotExist: bounce_email(email_address, subject, reason = 'unknown_user') except User.MultipleObjectsReturned: bounce_email(email_address, subject, reason = 'problem_posting') except PermissionDenied, error: bounce_email( email_address, subject, reason = 'permission_denied', body_text = unicode(error) )
def process_emailed_question(from_address, subject, body, attachments = None): """posts question received by email or bounces the message""" #a bunch of imports here, to avoid potential circular import issues from askbot.forms import AskByEmailForm from askbot.models import User data = { 'sender': from_address, 'subject': subject, 'body_text': body } form = AskByEmailForm(data) if form.is_valid(): email_address = form.cleaned_data['email'] try: user = User.objects.get( email__iexact = email_address ) except User.DoesNotExist: bounce_email(email_address, subject, reason = 'unknown_user') except User.MultipleObjectsReturned: bounce_email(email_address, subject, reason = 'problem_posting') tagnames = form.cleaned_data['tagnames'] title = form.cleaned_data['title'] body_text = form.cleaned_data['body_text'] try: body_text += process_attachments(attachments) user.post_question( title = title, tags = tagnames, body_text = body_text ) except PermissionDenied, error: bounce_email( email_address, subject, reason = 'permission_denied', body_text = unicode(error) )
def process_emailed_question(from_address, subject, body_text, stored_files, tags=None, group_id=None): """posts question received by email or bounces the message""" #a bunch of imports here, to avoid potential circular import issues from askbot.forms import AskByEmailForm from askbot.models import ReplyAddress, User from askbot.mail.messages import (AskForSignature, InsufficientReputation) reply_to = None try: #todo: delete uploaded files when posting by email fails!!! data = { 'sender': from_address, 'subject': subject, 'body_text': body_text } user = User.objects.get(email__iexact=from_address) form = AskByEmailForm(data, user=user) if form.is_valid(): email_address = form.cleaned_data['email'] if user.can_post_by_email() is False: email = InsufficientReputation({'user': user}) raise PermissionDenied(email.render_body()) body_text = form.cleaned_data['body_text'] stripped_body_text = user.strip_email_signature(body_text) #note that signature '' means it is unset and 'empty signature' is a sentinel #because there is no other way to indicate unset signature without adding #another field to the user model signature_changed = (stripped_body_text == body_text and user.email_signature != 'empty signature') need_new_signature = (user.email_isvalid is False or user.email_signature == '' or signature_changed) #ask for signature response if user's email has not been #validated yet or if email signature could not be found if need_new_signature: footer_code = ReplyAddress.objects.create_new( user=user, reply_action='validate_email').as_email_address( prefix='welcome-') email = AskForSignature({ 'user': user, 'footer_code': footer_code }) raise PermissionDenied(email.render_body()) tagnames = form.cleaned_data['tagnames'] title = form.cleaned_data['title'] #defect - here we might get "too many tags" issue if tags: tagnames += ' ' + ' '.join(tags) user.post_question(title=title, tags=tagnames.strip(), body_text=stripped_body_text, by_email=True, email_address=from_address, group_id=group_id) else: raise ValidationError() except User.DoesNotExist: bounce_email(email_address, subject, reason='unknown_user') except User.MultipleObjectsReturned: bounce_email(email_address, subject, reason='problem_posting') except PermissionDenied, error: bounce_email(email_address, subject, reason='permission_denied', body_text=unicode(error), reply_to=reply_to)
def process_emailed_question(from_address, subject, body_text, stored_files, tags=None, group_id=None): """posts question received by email or bounces the message""" #a bunch of imports here, to avoid potential circular import issues from askbot.forms import AskByEmailForm from askbot.models import ReplyAddress, User from askbot.mail import messages reply_to = None try: #todo: delete uploaded files when posting by email fails!!! data = { 'sender': from_address, 'subject': subject, 'body_text': body_text } form = AskByEmailForm(data) if form.is_valid(): email_address = form.cleaned_data['email'] user = User.objects.get(email__iexact=email_address) if user.can_post_by_email() is False: raise PermissionDenied(messages.insufficient_reputation(user)) body_text = form.cleaned_data['body_text'] stripped_body_text = user.strip_email_signature(body_text) signature_not_detected = (stripped_body_text == body_text and user.email_signature) #ask for signature response if user's email has not been #validated yet or if email signature could not be found if user.email_isvalid is False or signature_not_detected: reply_to = ReplyAddress.objects.create_new( user=user, reply_action='validate_email').as_email_address() message = messages.ask_for_signature(user, footer_code=reply_to) raise PermissionDenied(message) tagnames = form.cleaned_data['tagnames'] title = form.cleaned_data['title'] #defect - here we might get "too many tags" issue if tags: tagnames += ' ' + ' '.join(tags) user.post_question(title=title, tags=tagnames.strip(), body_text=stripped_body_text, by_email=True, email_address=from_address, group_id=group_id) else: raise ValidationError() except User.DoesNotExist: bounce_email(email_address, subject, reason='unknown_user') except User.MultipleObjectsReturned: bounce_email(email_address, subject, reason='problem_posting') except PermissionDenied, error: bounce_email(email_address, subject, reason='permission_denied', body_text=unicode(error), reply_to=reply_to)
def process_emailed_question( from_address, subject, body_text, stored_files, tags=None, group_id=None ): """posts question received by email or bounces the message""" #a bunch of imports here, to avoid potential circular import issues from askbot.forms import AskByEmailForm from askbot.models import ReplyAddress, User from askbot.mail import messages reply_to = None try: #todo: delete uploaded files when posting by email fails!!! data = { 'sender': from_address, 'subject': subject, 'body_text': body_text } user = User.objects.get(email__iexact=from_address) form = AskByEmailForm(data, user=user) if form.is_valid(): email_address = form.cleaned_data['email'] if user.can_post_by_email() is False: raise PermissionDenied(messages.insufficient_reputation(user)) body_text = form.cleaned_data['body_text'] stripped_body_text = user.strip_email_signature(body_text) #note that signature '' means it is unset and 'empty signature' is a sentinel #because there is no other way to indicate unset signature without adding #another field to the user model signature_changed = ( stripped_body_text == body_text and user.email_signature != 'empty signature' ) need_new_signature = ( user.email_isvalid is False or user.email_signature == '' or signature_changed ) #ask for signature response if user's email has not been #validated yet or if email signature could not be found if need_new_signature: reply_to = ReplyAddress.objects.create_new( user = user, reply_action = 'validate_email' ).as_email_address(prefix='welcome-') message = messages.ask_for_signature(user, footer_code = reply_to) raise PermissionDenied(message) tagnames = form.cleaned_data['tagnames'] title = form.cleaned_data['title'] #defect - here we might get "too many tags" issue if tags: tagnames += ', ' + ', '.join(tags) user.post_question( title=title, tags=tagnames.strip(), body_text=stripped_body_text, by_email=True, email_address=from_address, group_id=group_id ) else: raise ValidationError() except User.DoesNotExist: bounce_email(email_address, subject, reason = 'unknown_user') except User.MultipleObjectsReturned: bounce_email(email_address, subject, reason = 'problem_posting') except PermissionDenied, error: bounce_email( email_address, subject, reason = 'permission_denied', body_text = unicode(error), reply_to = reply_to )
def handle_noargs(self, **options): """reads all emails from the INBOX of imap server and posts questions based on those emails. Badly formatted questions are bounced, as well as emails from unknown users all messages are deleted thereafter """ if not askbot_settings.ALLOW_ASKING_BY_EMAIL: raise CommandError('Asking by email is not enabled') #open imap server and select the inbox if django_settings.IMAP_USE_TLS: imap_getter = imaplib.IMAP4_SSL else: imap_getter = imaplib.IMAP4 imap = imap_getter(django_settings.IMAP_HOST, django_settings.IMAP_PORT) imap.login(django_settings.IMAP_HOST_USER, django_settings.IMAP_HOST_PASSWORD) imap.select('INBOX') #get message ids status, ids = imap.search(None, 'ALL') if len(ids[0].strip()) == 0: #with empty inbox - close and exit imap.close() imap.logout() return #for each id - read a message, parse it and post a question for id in ids[0].split(' '): t, data = imap.fetch(id, '(RFC822)') message_body = data[0][1] msg = email.message_from_string(data[0][1]) imap.store(id, '+FLAGS', '\\Deleted') try: (sender, subject, body) = parse_message(msg) except CannotParseEmail, e: continue data = {'sender': sender, 'subject': subject, 'body_text': body} form = AskByEmailForm(data) print data if form.is_valid(): email_address = form.cleaned_data['email'] try: user = models.User.objects.get(email__iexact=email_address) except models.User.DoesNotExist: bounce_email(email_address, subject, reason='unknown_user') except models.User.MultipleObjectsReturned: bounce_email(email_address, subject, reason='problem_posting') tagnames = form.cleaned_data['tagnames'] title = form.cleaned_data['title'] body_text = form.cleaned_data['body_text'] try: user.post_question(title=title, tags=tagnames, body_text=body_text) except exceptions.PermissionDenied, e: bounce_email(email_address, subject, reason='permission_denied', body_text=unicode(e))
def handle_noargs(self, **options): """reads all emails from the INBOX of imap server and posts questions based on those emails. Badly formatted questions are bounced, as well as emails from unknown users all messages are deleted thereafter """ if not askbot_settings.ALLOW_ASKING_BY_EMAIL: raise CommandError('Asking by email is not enabled') #open imap server and select the inbox if django_settings.IMAP_USE_TLS: imap_getter = imaplib.IMAP4_SSL else: imap_getter = imaplib.IMAP4 imap = imap_getter( django_settings.IMAP_HOST, django_settings.IMAP_PORT ) imap.login( django_settings.IMAP_HOST_USER, django_settings.IMAP_HOST_PASSWORD ) imap.select('INBOX') #get message ids status, ids = imap.search(None, 'ALL') if len(ids[0].strip()) == 0: #with empty inbox - close and exit imap.close() imap.logout() return #for each id - read a message, parse it and post a question for id in ids[0].split(' '): t, data = imap.fetch(id, '(RFC822)') message_body = data[0][1] msg = email.message_from_string(data[0][1]) imap.store(id, '+FLAGS', '\\Deleted') try: (sender, subject, body) = parse_message(msg) err_str = "\nEMAIL SUBMIT from %s: '%s'" % (sender, subject) logging.info(err_str) except CannotParseEmail, e: err_str = "\nCan't parse Email '%s'" % (msg) logging.critical(err_str) #print "Could not parse ", msg continue if check_for_invalid_subject(subject): continue data = { 'sender': sender, 'subject': subject, 'body_text': body } form = AskByEmailForm(data) #print data if form.is_valid(): email_address = form.cleaned_data['email'] try: user = models.User.objects.get( email__iexact = email_address ) except models.User.DoesNotExist: bounce_email(email_address, subject, reason = 'unknown_user') continue except models.User.MultipleObjectsReturned: bounce_email(email_address, subject, reason = 'problem_posting') continue tagnames = form.cleaned_data['tagnames'] title = form.cleaned_data['title'] body_text = form.cleaned_data['body_text'] try: user.post_question( title = title, tags = tagnames, body_text = body_text ) info_str = "\nPosted Email Question from %s - Tags: %s - Subject '%s'" % (email_address, tagnames, title) logging.info(info_str) except exceptions.PermissionDenied, e: bounce_email( email_address, subject, reason = 'permission_denied', body_text = unicode(e) )
def handle_noargs(self, **options): """reads all emails from the INBOX of imap server and posts questions based on those emails. Badly formatted questions are bounced, as well as emails from unknown users all messages are deleted thereafter """ if askbot_settings.ALLOW_ASKING_BY_EMAIL: raise CommandError('Asking by email is not enabled') #open imap server and select the inbox if django_settings.IMAP_USE_TLS: imap_getter = imaplib.IMAP4_SSL else: imap_getter = imaplib.IMAP4 imap = imap_getter( django_settings.IMAP_HOST, django_settings.IMAP_PORT ) imap.login( django_settings.IMAP_HOST_USER, django_settings.IMAP_HOST_PASSWORD ) imap.select('INBOX') #get message ids status, ids = imap.search(None, 'ALL') #for each id - read a message, parse it and post a question for id in ids[0].split(' '): t, data = imap.fetch(id, '(RFC822)') message_body = data[0][1] msg = email.message_from_string(data[0][1]) imap.store(id, '+FLAGS', '\\Deleted') try: (sender, subject, body) = parse_message(msg) except CannotParseEmail, e: continue data = { 'sender': sender, 'subject': subject, 'body_text': body } form = AskByEmailForm(data) print data if form.is_valid(): email_address = form.cleaned_data['email'] try: print 'looking for ' + email_address user = models.User.objects.get(email = email_address) except models.User.DoesNotExist: bounce_email(email_address, subject, reason = 'unknown_user') except models.User.MultipleObjectsReturned: bounce_email(email_address, subject, reason = 'problem_posting') tagnames = form.cleaned_data['tagnames'] title = form.cleaned_data['title'] body_text = form.cleaned_data['body_text'] try: print 'posting question' print title print tagnames print body_text user.post_question( title = title, tags = tagnames, body_text = body_text ) except exceptions.PermissionDenied, e: bounce_email( email_address, subject, reason = 'permission_denied', body_text = unicode(e) )
def process_emailed_question( from_address, subject, body_text, stored_files, tags = None ): """posts question received by email or bounces the message""" #a bunch of imports here, to avoid potential circular import issues from askbot.forms import AskByEmailForm from askbot.models import ReplyAddress, User from askbot.mail import messages reply_to = None try: #todo: delete uploaded files when posting by email fails!!! data = { 'sender': from_address, 'subject': subject, 'body_text': body_text } form = AskByEmailForm(data) if form.is_valid(): email_address = form.cleaned_data['email'] user = User.objects.get( email__iexact = email_address ) if user.can_post_by_email() == False: raise PermissionDenied(messages.insufficient_reputation(user)) if user.email_isvalid == False: reply_to = ReplyAddress.objects.create_new( user = user, reply_action = 'validate_email' ).as_email_address() message = messages.ask_for_signature(user, footer_code = reply_to) raise PermissionDenied(message) tagnames = form.cleaned_data['tagnames'] title = form.cleaned_data['title'] body_text = form.cleaned_data['body_text'] #defect - here we might get "too many tags" issue if tags: tagnames += ' ' + ' '.join(tags) stripped_body_text = user.strip_email_signature(body_text) if stripped_body_text == body_text and user.email_signature: #todo: send an email asking to update the signature raise ValueError('email signature changed') user.post_question( title = title, tags = tagnames.strip(), body_text = stripped_body_text, by_email = True, email_address = from_address ) else: raise ValidationError() except User.DoesNotExist: bounce_email(email_address, subject, reason = 'unknown_user') except User.MultipleObjectsReturned: bounce_email(email_address, subject, reason = 'problem_posting') except PermissionDenied, error: bounce_email( email_address, subject, reason = 'permission_denied', body_text = unicode(error), reply_to = reply_to )