Anonymous avatar Anonymous committed c114f08

Started working on the 'articles from email' feature

Comments (0)

Files changed (2)

articles/__init__.py

-__version__ = '1.8.1a'
+__version__ = '1.9.0'
 
 """
 The Pygments reStructuredText directive

articles/management/commands/check_for_articles_from_email.py

+from django.conf import settings
+from django.core.management.base import BaseCommand
+
+from optparse import make_option
+
+MB_IMAP4 = 'IMAP4'
+MB_POP3 = 'POP3'
+
+class Command(BaseCommand):
+    help = "Checks special e-mail inboxes for emails that should be posted as articles"
+    option_list = BaseCommand.option_list + (
+        make_option('--protocol', dest='protocol', default=MB_IMAP4, help='Protocol to use to check for email'),
+        make_option('--host', dest='host', default=None),
+        make_option('--port', dest='port', default=None),
+        make_option('--keyfile', dest='keyfile', default=None),
+        make_option('--certfile', dest='certfile', default=None),
+        make_option('--username', dest='username', default=None),
+        make_option('--password', dest='password', default=None),
+        make_option('--ssl', action='store_true', dest='ssl', default=False),
+    )
+
+    def handle(self, *args, **options):
+        protocol = options['protocol']
+        host = options['host']
+        port = options['port']
+        keyfile = options['keyfile']
+        certfile = options['certfile']
+        username = options['username']
+        password = options['password']
+        ssl = options['ssl']
+
+        params = (host, port, username, password, keyfile, certfile, ssl)
+        if protocol == MB_IMAP4:
+            messages = self.handle_imap4(*params)
+        elif protocol == MB_POP3:
+            messages = self.handle_pop3(*params)
+
+        print messages
+
+    def handle_imap4(self, host, port, username, password, keyfile, certfile, ssl):
+        import imaplib
+
+        messages = []
+        if (keyfile and certfile) or ssl:
+            M = imaplib.IMAP4_SSL(host, port, keyfile, certfile)
+        else:
+            M = imaplib.IMAP4(host, port)
+
+        M.login(username, password)
+        M.select()
+
+        typ, data = M.search(None, 'ALL')
+
+        for num in data[0].split():
+            typ, data = M.fetch(num, '(RFC822)')
+            messages.append((num, data))
+
+        M.close()
+        M.logout()
+
+        return messages
+
+    def handle_pop3(self, host, port, username, password, keyfile, certfile, ssl):
+        import poplib
+
+        messages = []
+        if (keyfile and certfile) or ssl:
+            M = poplib.POP3_SSL(host, port, keyfile, certfile)
+        else:
+            M = poplib.POP3(host, port)
+
+        M.user(username)
+        M.pass_(password)
+        num = len(M.list()[1])
+        for i in range(num):
+            for msg in M.retr(i + 1)[1]:
+                messages.append(msg)
+
+        M.quit()
+
+        return messages
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.