Python, bpython and my university’s email

Almost a year ago I posted in my past blog (in portuguese, automated translation) a tutorial on how to use greasemonkey to workaround the ugly interface of my university’s webmail. More specifically, in Firefox it looses the ability to select all emails. I took that just for an example… the real goal was to delete all emails since I access them through Gmail and periodically I get the email:  “your mailbox is full…”. So, how could I delete them without having to download them all? Simple: connect through POP3 and issue the “dele” command. I could do that even with plain telnet, but I decided to come back to python, since it was awhile I didn’t use it.

In the same week I saw two interesting posts in my RSS reader:

  • the first came from BR-Linux, and it’s about bpython, a better CLI to python. It’s amazing!! You can get a lot of IDE-like functionalities in your command line. You can view a short video made by its author.
  • The second is about pydev and came from “Linux Today”. With PyDev you can use Eclipse to edit your python code and get all functionalities you may out of an IDE. I still didn’t use it (and I think I’ll continue with vim) but it’s an interesting post to read as it talks about IDE vs vim/eclipse/other_text_editors.

And now, the really-stupid-delete-all-mail-from-poli.py:

#!/usr/bin/python

import getpass, poplib

strServer = 'pop3.poli.usp.br'
user = raw_input('user: ')
server = poplib.POP3(strServer)
server.user(user)
server.pass_(getpass.getpass())
numMsg = len(server.list()[1])
while True:
    resp = raw_input('%d messages in Inbox. Delete all? (yes/no) ' % numMsg)
    if resp == 'yes':
        for i in range(1,numMsg+1):
            server.dele(i)
        break
    elif resp == 'no':
        break
    else:
        print 'Answer yes or no\n'

server.quit()

It will ask the user and password to connect to pop3.poli.usp.br, show the current number of messages and ask for confirmation.