Advertisement
Guest User

Untitled

a guest
Jun 15th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. import sys
  2. import imaplib
  3. import getpass
  4. import email
  5. import email.header
  6. import datetime
  7. import time
  8. import signal
  9.  
  10. import socket
  11. import fcntl
  12. import struct
  13.  
  14. def get_ip_address(ifname):
  15. # Get IP Address of machine
  16. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  17. return socket.inet_ntoa(fcntl.ioctl(
  18. s.fileno(),
  19. 0x8915, # SIOCGIFADDR
  20. struct.pack('256s', ifname[:15])
  21. )[20:24])
  22.  
  23. #get_ip_address('eth0')
  24.  
  25.  
  26. EMAIL_ACCOUNT = "test@gmail.com"
  27. EMAIL_FOLDER = "INBOX"
  28.  
  29. def signal_handler(signal, frame):
  30. global done
  31. done = True
  32. signal.signal(signal.SIGINT, signal_handler)
  33. done = False
  34.  
  35. def shutdown():
  36. import subprocess
  37. subprocess.call(["shutdown", "1"])
  38.  
  39. def process_mailbox(M):
  40. """
  41. Do something with emails messages in the folder.
  42. """
  43.  
  44. rv, data = M.search(None, "ALL")
  45. if rv != 'OK':
  46. print "No messages found!"
  47. return
  48. last_50_msg = data[0].split()[-50:]
  49. last_50_msg.reverse()
  50. for num in last_50_msg:
  51. rv, data = M.fetch(num, '(RFC822)')
  52. if rv != 'OK':
  53. print "ERROR getting message", num
  54. return
  55.  
  56. msg = email.message_from_string(data[0][1])
  57. decode = email.header.decode_header(msg['Subject'])[0]
  58. subject = unicode(decode[0])
  59. if 'shutdown' in subject.lower():
  60. shutdown()
  61.  
  62. def main(username, password):
  63. M = imaplib.IMAP4_SSL('imap.gmail.com')
  64. try:
  65. rv, data = M.login(username, password)
  66. except imaplib.IMAP4.error:
  67. print "LOGIN FAILED!!! "
  68. sys.exit(1)
  69.  
  70. print rv, data
  71.  
  72. rv, mailboxes = M.list()
  73. if rv == 'OK':
  74. print "Mailboxes:"
  75. print mailboxes
  76.  
  77. rv, data = M.select(EMAIL_FOLDER)
  78. if rv == 'OK':
  79. print "Processing mailbox...\n"
  80. process_mailbox(M)
  81. M.close()
  82. else:
  83. print "ERROR: Unable to open mailbox ", rv
  84.  
  85.  
  86. if __name__ == "__main__":
  87. username = raw_input("Username:")
  88. password = getpass.getpass()
  89. while not done:
  90. main(username, password)
  91. time.sleep(300)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement