summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVasil Vangelovski <vvangelovski@gmail.com>2012-01-25 16:25:05 +0100
committerVasil Vangelovski <vvangelovski@gmail.com>2012-01-25 16:25:05 +0100
commitd23553c24c4a8f71d130ac5d5d78f335a49a2ccd (patch)
treee66fae061f793d5f3c4f8490e9baffdd6339b363
parentfb4522c75780562df3abfaf16d1cea5c81ffe5e7 (diff)
downloadaskbot-d23553c24c4a8f71d130ac5d5d78f335a49a2ccd.tar.gz
askbot-d23553c24c4a8f71d130ac5d5d78f335a49a2ccd.tar.bz2
askbot-d23553c24c4a8f71d130ac5d5d78f335a49a2ccd.zip
Adapted reply by email feature to work with django lamson
-rw-r--r--askbot/doc/source/optional-modules.rst14
-rw-r--r--askbot/lamson_handlers.py75
-rw-r--r--askbot/models/reply_by_email.py63
-rw-r--r--askbot/tests/reply_by_email_tests.py4
4 files changed, 87 insertions, 69 deletions
diff --git a/askbot/doc/source/optional-modules.rst b/askbot/doc/source/optional-modules.rst
index 6ab381ba..b574aeae 100644
--- a/askbot/doc/source/optional-modules.rst
+++ b/askbot/doc/source/optional-modules.rst
@@ -169,12 +169,11 @@ Then run **supervisorctl update** and it will be started. For more information a
Receiving replies for email notifications
===========================================
-Askbot supports posting replies by email. For this feature to work ``Lamson`` and ``django-lamson`` need to be installed on the system.
-To install all the necessery dependencies execute the following command:
+Askbot supports posting replies by email. For this feature to work ``Lamson`` and ``django-lamson`` need to be installed on the system. To install all the necessery dependencies execute the following command:
pip install django-lamson
-The lamson daemon needs a folder to store files, create a folder named ``run`` within your project folder by executing the following command:
+The lamson daemon needs a folder to store it's mail queue files, create a folder named ``run`` within your project folder by executing the following command:
mkdir run
@@ -182,11 +181,14 @@ The minimum settings required to enable this feature are defining the port and b
and the email handlers withing askbot. Edit your settings.py file to include the following:
LAMSON_RECEIVER_CONFIG = {'host': 'your.ip.address', 'port': 25}
+
LAMSON_HANDLERS = ['askbot.lamson_handlers']
+
LAMSON_ROUTER_DEFAULTS = {'host': '.+'}
-To recieve internet email you will need to bind to your external ip address and port 25. If you just want to test the feature
-by sending eamil from the same system you could bind to 127.0.0.1 and any higher port.
+In the list of ``installed_apps`` add the app ``django-lamson``.
+
+The ``LAMSON_RECEIVER_CONFIG`` parameter defines the binding address/port for the SMTP daemon. To recieve internet email you will need to bind to your external ip address and port 25. If you just want to test the feature by sending eamil from the same system you could bind to 127.0.0.1 and any higher port.
To run the lamson SMTP daemon you will need to execute the following management command:
@@ -204,3 +206,5 @@ Within the askbot admin interface there are 4 significant configuration points f
* The "reply by email hostname" needs to be set to the email hostname where you want to receive the email replies. If for example this is set to "myaskbot.com" the users will post replies to addresses such as "4wffsw345wsf@myaskbot.com", you need to point the MX DNS record for that domain to the address where you will run the lamson SMTP daemon.
* The last setting in this section controls the threshold for minimum length of the reply that is posted as an answer to a question. If the user is replying to a notification for a question and the reply body is shorter than this threshold the reply will be posted as a comment to the question.
* In the karma thresholds section the "Post answers and comments by email" defines the minimum karma for users to be able to post replies by email.
+
+If you want to run the lamson daemon on a port other than 25 you can use a mail proxy server such as ``nginx`` that will listen on port 25 and forward any SMTP requests to lamson. Using nginx you can also setup more complex email handling rules, such as for example if the same server where askbot is installed acts as an email server for other domains you can configure nginx to forward any emails directed to your askbot installation to lamson and any other emails to the mail server you're using, such as ``postfix``. For more information on how to use nginx for this please consult the nginx mail module documentation `nginx mail module documentation <http://wiki.nginx.org/MailCoreModule>`_ .
diff --git a/askbot/lamson_handlers.py b/askbot/lamson_handlers.py
new file mode 100644
index 00000000..d91749c8
--- /dev/null
+++ b/askbot/lamson_handlers.py
@@ -0,0 +1,75 @@
+import re
+import logging
+from lamson.routing import route, route_like, stateless
+from django.utils.translation import ugettext as _
+from askbot.models import ReplyAddress
+from askbot.conf import settings as askbot_settings
+
+
+
+
+#we might end up needing to use something like this
+#to distinguish the reply text from the quoted original message
+"""
+def _strip_message_qoute(message_text):
+ import re
+ result = message_text
+ pattern = "(?P<qoute>" + \
+ "On ([a-zA-Z0-9, :/<>@\.\"\[\]]* wrote:.*)|" + \
+ "From: [\w@ \.]* \[mailto:[\w\.]*@[\w\.]*\].*|" + \
+ "From: [\w@ \.]*(\n|\r\n)+Sent: [\*\w@ \.,:/]*(\n|\r\n)+To:.*(\n|\r\n)+.*|" + \
+ "[- ]*Forwarded by [\w@ \.,:/]*.*|" + \
+ "From: [\w@ \.<>\-]*(\n|\r\n)To: [\w@ \.<>\-]*(\n|\r\n)Date: [\w@ \.<>\-:,]*\n.*|" + \
+ "From: [\w@ \.<>\-]*(\n|\r\n)To: [\w@ \.<>\-]*(\n|\r\n)Sent: [\*\w@ \.,:/]*(\n|\r\n).*|" + \
+ "From: [\w@ \.<>\-]*(\n|\r\n)To: [\w@ \.<>\-]*(\n|\r\n)Subject:.*|" + \
+ "(-| )*Original Message(-| )*.*)"
+ groups = re.search(pattern, email_text, re.IGNORECASE + re.DOTALL)
+ qoute = None
+ if not groups is None:
+ if groups.groupdict().has_key("qoute"):
+ qoute = groups.groupdict()["qoute"]
+ if qoute:
+ result = reslut.split(qoute)[0]
+ #if the last line contains an email message remove that one too
+ lines = result.splitlines(True)
+ if re.search(r'[\w\.]*@[\w\.]*\].*', lines[-1]):
+ result = '\n'.join(lines[:-1])
+ return result
+"""
+
+
+
+@route("(address)@(host)", address=".+")
+@stateless
+def PROCESS(message, address = None, host = None):
+
+ error = None
+ try:
+ reply_address = ReplyAddress.objects.get_unused(address, message.From)
+ separator = _("======= Reply above this line. ====-=-=")
+ parts = message.body().split(separator)
+ if len(parts) != 2 :
+ error = _("Your message was malformed. Please make sure to qoute \
+ the original notification you received at the end of your reply.")
+ else:
+ reply_part = parts[0]
+ reply_part = '\n'.join(reply_part.splitlines(True)[:-3])
+ reply_address.create_reply(reply_part.strip())
+ except ReplyAddress.DoesNotExist:
+ error = _("You were replying to an email address\
+ unknown to the system or you were replying from a different address from the one where you\
+ received the notification.")
+ if error is not None:
+ from askbot.utils import mail
+ from django.template import Context
+ from askbot.skins.loaders import get_template
+
+ template = get_template('reply_by_email_error.html')
+ body_text = template.render(Context({'error':error}))
+ mail.send_mail(
+ subject_line = "Error posting your reply",
+ body_text = body_text,
+ recipient_list = [message.From],
+ )
+
+
diff --git a/askbot/models/reply_by_email.py b/askbot/models/reply_by_email.py
index 5c92f0a6..98f86caf 100644
--- a/askbot/models/reply_by_email.py
+++ b/askbot/models/reply_by_email.py
@@ -4,7 +4,7 @@ import string
from django.db import models
from django.contrib.auth.models import User
-from django.utils.translation import ugettext as _
+
from askbot.models.post import Post
from askbot.models.base import BaseQuerySetManager
@@ -59,66 +59,5 @@ class ReplyAddress(models.Model):
return result
-#we might end up needing to use something like this
-#to distinguish the reply text from the quoted original message
-"""
-def _strip_message_qoute(message_text):
- import re
- result = message_text
- pattern = "(?P<qoute>" + \
- "On ([a-zA-Z0-9, :/<>@\.\"\[\]]* wrote:.*)|" + \
- "From: [\w@ \.]* \[mailto:[\w\.]*@[\w\.]*\].*|" + \
- "From: [\w@ \.]*(\n|\r\n)+Sent: [\*\w@ \.,:/]*(\n|\r\n)+To:.*(\n|\r\n)+.*|" + \
- "[- ]*Forwarded by [\w@ \.,:/]*.*|" + \
- "From: [\w@ \.<>\-]*(\n|\r\n)To: [\w@ \.<>\-]*(\n|\r\n)Date: [\w@ \.<>\-:,]*\n.*|" + \
- "From: [\w@ \.<>\-]*(\n|\r\n)To: [\w@ \.<>\-]*(\n|\r\n)Sent: [\*\w@ \.,:/]*(\n|\r\n).*|" + \
- "From: [\w@ \.<>\-]*(\n|\r\n)To: [\w@ \.<>\-]*(\n|\r\n)Subject:.*|" + \
- "(-| )*Original Message(-| )*.*)"
- groups = re.search(pattern, email_text, re.IGNORECASE + re.DOTALL)
- qoute = None
- if not groups is None:
- if groups.groupdict().has_key("qoute"):
- qoute = groups.groupdict()["qoute"]
- if qoute:
- result = reslut.split(qoute)[0]
- #if the last line contains an email message remove that one too
- lines = result.splitlines(True)
- if re.search(r'[\w\.]*@[\w\.]*\].*', lines[-1]):
- result = '\n'.join(lines[:-1])
- return result
-"""
-
-
-#TODO move this function to a more appropriate module
-def process_reply_email(message, address, host):
-
- error = None
- try:
- reply_address = ReplyAddress.objects.get_unused(address, message.From)
- separator = _("======= Reply above this line. ====-=-=")
- parts = message.body().split(separator)
- if len(parts) != 2 :
- error = _("Your message was malformed. Please make sure to qoute \
- the original notification you received at the end of your reply.")
- else:
- reply_part = parts[0]
- reply_part = '\n'.join(reply_part.splitlines(True)[:-3])
- reply_address.create_reply(reply_part.strip())
- except ReplyAddress.DoesNotExist:
- error = _("You were replying to an email address\
- unknown to the system or you were replying from a different address from the one where you\
- received the notification.")
- if error is not None:
- from askbot.utils import mail
- from django.template import Context
- from askbot.skins.loaders import get_template
-
- template = get_template('reply_by_email_error.html')
- body_text = template.render(Context({'error':error}))
- mail.send_mail(
- subject_line = "Error posting your reply",
- body_text = body_text,
- recipient_list = [message.From],
- )
diff --git a/askbot/tests/reply_by_email_tests.py b/askbot/tests/reply_by_email_tests.py
index 6aca55ec..76097362 100644
--- a/askbot/tests/reply_by_email_tests.py
+++ b/askbot/tests/reply_by_email_tests.py
@@ -1,6 +1,6 @@
from django.utils.translation import ugettext as _
from askbot.models import ReplyAddress
-from askbot.models.reply_by_email import process_reply_email
+from askbot.lamson_handlers import PROCESS
from askbot.tests.utils import AskbotTestCase
@@ -45,7 +45,7 @@ class EmailProcessingTests(AskbotTestCase):
separator = _("======= Reply above this line. ====-=-=")
msg = MockMessage("This is a test reply \n\nOn such and such someone\
wrote something \n\n%s\nlorem ipsum "%(separator), "user1@domain.com")
- process_reply_email(msg, addr, '')
+ PROCESS(msg, addr, '')
self.assertEquals(self.answer.comments.count(), 2)
self.assertEquals(self.answer.comments.all().order_by('-pk')[0].text.strip(), "This is a test reply")