betabug... Sascha Welter

home english | home deutsch | Site Map | Sascha | Kontakt | Pro | Weblog | Wiki

31 January 2008

Moblog Posting Through Procmail in COREBlog

Why wait for POP3?
 

The COREBlog blogging software for Zope has a nice "moblog" feature, which allows to post by email. Unfortunately it uses a POP3 account, and therefore has to be polled through a cronjob to check that POP3 account. That introduces a delay on mobile posts and results in a lot of unnecessary jobs being run on the server. Instead of this it's fairly easy to set up procmail to post through mail more directly...


This recipe involves three things:

Adding receive_from_msg() to COREblog.py

When thinking about this, at first I was carrying big plans about having to parse mails, check everything myself and splice together a method that adds a post. All of that wasn't really necessary, because the base of it is already in the current moblog implementation. All I had to do was to reuse 3 lines of code almost unchanged (and of course debug it for a while to find out which little part I had to change).

So, here is the patch:

Index: COREBlog.py
===================================================================
RCS file: /home/betabug/work/cvs/COREBlog/COREBlog.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- COREBlog.py 23 Oct 2007 13:12:40 -0000      1.11
+++ COREBlog.py 30 Jan 2008 15:14:35 -0000      1.12
@@ -1529,6 +1529,15 @@

         m.quit()

+    security.declareProtected(AddCOREBlogEntries,'receive_from_msg')
+    def receive_from_msg(self, msg):
+        """
+        Receive mails from a mail msg,
+        used e.g. to post messages throuh procmail.
+        """
+        buf = StringIO(msg)
+        mail = Message(buf,self.getProperty("management_page_charset"))
+        self.add_mail_entry(mail)

     security.declarePrivate('add_mail_entry')
     def add_mail_entry(self,mail):

All the other setup in COREBlog can be left as-is. We use the same accounts you would have set up for the normal moblog setup, along with settings / permissions / passwords. The method picks up a mail message in standard Unix mbox format (or however that's called), processes it and adds a post if everything is OK. As with standards moblog, you can add one image.

Mail server setup

I'm not going into this too much. My server is set up to allow users to process their mail through procmail, by adding a file called .procmailrc to their home directory. Obviously it also successfully receives mail through SMTP. The whole point of this recipe is to have your own server where you can play like this and don't need to go through POP3 accounts.

Mail account and procmail

The same account I had previously used for POP3 moblog mails now receives a .procmailrc with something like this inside:

DEFAULT=/var/mail/account_mailbox

:0 HB
* From:.*user@example\.org
* ^secretPassword123$
| /usr/local/bin/curl -u 'zopeaccount:secretZopePassword123' -F 'msg=<-' \
   http://example.org/path/to/receive_from_msg

The last 2 lines are actually one single line - I don't know if you really can wrap lines in a procmailrc, this is just for blog readability. So you might want to join them into one big line.

What this does:

It sets up a rule that searches header and body (HB), it processes only mails coming from a certain account, it checks for the moblog password (which in standard COREBlog moblog format appears on a line by itself) and then it pipes the resulting message into curl. In turn, curl posts the message to our method, using the form parameter msg and giving the username/password of a special Zope user account.

The checking of the password is performed by coreblog too, so it wouldn't be necessary here. But I like this check to be performed as early as possible, so Zope has less work to process any arbitrary mails which might arrive on this account. Keep in mind that the moblog password isn't really secure: It appears in plain text mails, so if you have high security standards, don't use this setup. In such a case you could use a script that checks a PGP signature and pipes into coreblog only when the signature matches.

The result

Now when I post to my blog using my mobile phone, the posts appear almost instantaneously, no need to wait for the cron job to come around. All the other setup remains the same.

See also: CoreBlogChanges

Posted by betabug at 09:33 | Comments (0) | Trackbacks (0)
ch athens
Life in Athens (Greece) for a foreigner from the other side of the mountains. And with an interest in digital life and the feeling of change in a big city. Multilingual English - German - Greek.
Main blog page
Recent Entries
Best of
Some of the most sought after posts, judging from access logs and search engine queries.

Apple & Macintosh:
Security & Privacy:
Misc technical:
Athens for tourists and visitors:
Life in general:
<< Testing a new moblog setup for COREBlog | Main | Χαμός στο παζάρι βιβλίου >>
Comments
There are no comments.
Trackbacks
You can trackback to: http://betabug.ch/blogs/ch-athens/769/tbping
There are no trackbacks.
Leave a comment