Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import imaplib
- import getpass
- import email
- import email.header
- import datetime
- import time
- import signal
- import socket
- import fcntl
- import struct
- def get_ip_address(ifname):
- # Get IP Address of machine
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- return socket.inet_ntoa(fcntl.ioctl(
- s.fileno(),
- 0x8915, # SIOCGIFADDR
- struct.pack('256s', ifname[:15])
- )[20:24])
- #get_ip_address('eth0')
- EMAIL_ACCOUNT = "test@gmail.com"
- EMAIL_FOLDER = "INBOX"
- def signal_handler(signal, frame):
- global done
- done = True
- signal.signal(signal.SIGINT, signal_handler)
- done = False
- def shutdown():
- import subprocess
- subprocess.call(["shutdown", "1"])
- def process_mailbox(M):
- """
- Do something with emails messages in the folder.
- """
- rv, data = M.search(None, "ALL")
- if rv != 'OK':
- print "No messages found!"
- return
- last_50_msg = data[0].split()[-50:]
- last_50_msg.reverse()
- for num in last_50_msg:
- rv, data = M.fetch(num, '(RFC822)')
- if rv != 'OK':
- print "ERROR getting message", num
- return
- msg = email.message_from_string(data[0][1])
- decode = email.header.decode_header(msg['Subject'])[0]
- subject = unicode(decode[0])
- if 'shutdown' in subject.lower():
- shutdown()
- def main(username, password):
- M = imaplib.IMAP4_SSL('imap.gmail.com')
- try:
- rv, data = M.login(username, password)
- except imaplib.IMAP4.error:
- print "LOGIN FAILED!!! "
- sys.exit(1)
- print rv, data
- rv, mailboxes = M.list()
- if rv == 'OK':
- print "Mailboxes:"
- print mailboxes
- rv, data = M.select(EMAIL_FOLDER)
- if rv == 'OK':
- print "Processing mailbox...\n"
- process_mailbox(M)
- M.close()
- else:
- print "ERROR: Unable to open mailbox ", rv
- if __name__ == "__main__":
- username = raw_input("Username:")
- password = getpass.getpass()
- while not done:
- main(username, password)
- time.sleep(300)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement