This code is the driver script for dryrun.py, documented seperately.
Basically what this script does is sets the IMAP server in IDLE mode so
as to be notified when a new email arrives. However before doing that,
it first invokes dryrun_invoke() to check for any existing unprocessed
Amit Saha < email@example.com>
Version PoC: 27 August, 2011
# uses impalib2: https://www.it.usyd.edu.au/~piers/python/imaplib2
import imaplib2, time
from threading import *
from dryrun import *
# Global configs for the mailbox which acts
# as the receiever for code execution requestions
IMAP_SERVER = 'imap.gmail.com'
username = 'your username'
password = 'your password'
# This is the threading object that does all the waiting on
# the event
def __init__(self, conn):
self.thread = Thread(target=self.idle)
self.M = conn
self.event = Event()
# This is a neat trick to make thread end. Took me a
# while to figure that one out!
# Starting an unending loop here
# This is part of the trick to make the loop stop
# when the stop() command is given
self.needsync = False
# A callback method that gets called when a new
# email arrives. Very basic, but that's good.
if not self.event.isSet():
self.needsync = True
# Do the actual idle call. This returns immediately,
# since it's asynchronous.
# This waits until the event is set. The event is
# set by the callback, when the server 'answers'
# the idle call and the callback function gets
# Because the function sets the needsync variable,
# this helps escape the loop without doing
# anything if the stop() is called. Kinda neat
# The method that gets called when a new email arrives.
# Replace it with something better.
logging.info('Got a request for processing..')
# invoke dryrun
# Register a IDLE for the mailbox
# Init. logger
# Check for any unattended new requests
# Register IDLE mode with the IMAP server and
M = imaplib2.IMAP4_SSL(IMAP_SERVER)
# We need to get out of the AUTH state, so we just select
# the INBOX.
# Start the Idler thread
idler = Idler(M)
#keep alive for ever
# Clean up.
# This is important!