Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # sends me an sms for utsa new mail
  3.  
  4. __author__ = "Cody Lee <platinummonkey@archlinux.us>"
  5.  
  6. import os               # Miscellaneous OS interfaces.
  7. import sys              # System-specific parameters and functions.
  8. import libgmail
  9. import time
  10.  
  11. username = 'username@gmail.com'
  12. password = 'c0mputerspeck'
  13. SEARCHLABEL = 'TAMU'
  14. PHONENUMBER = '5551234567'
  15. LOGFILE = '/path/to/utsa-gmail.log'
  16. SLEEPTIME = 60*5 # 5 min
  17.  
  18. # Default daemon parameters.
  19. # File mode creation mask of the daemon.
  20. UMASK = 0
  21.  
  22. # Default working directory for the daemon.
  23. WORKDIR = "/path/to/whatever/"
  24.  
  25. # Default maximum for the number of available file descriptors.
  26. MAXFD = 1024
  27.  
  28. # The standard I/O file descriptors are redirected to /dev/null by default.
  29. if (hasattr(os, "devnull")):
  30.    REDIRECT_TO = os.devnull
  31. else:
  32.    REDIRECT_TO = "/dev/null"
  33.  
  34. def createDaemon():
  35.    """Detach a process from the controlling terminal and run it in the
  36.   background as a daemon.
  37.   """
  38.  
  39.    try:
  40.       pid = os.fork()
  41.    except OSError, e:
  42.       raise Exception, "%s [%d]" % (e.strerror, e.errno)
  43.  
  44.    if (pid == 0):       # The first child.
  45.       os.setsid()
  46.  
  47.       try:
  48.          pid = os.fork()        # Fork a second child.
  49.       except OSError, e:
  50.          raise Exception, "%s [%d]" % (e.strerror, e.errno)
  51.  
  52.       if (pid == 0):    # The second child.
  53.          os.chdir(WORKDIR)
  54.          os.umask(UMASK)
  55.       else:
  56.          os._exit(0)    # Exit parent (the first child) of the second child.
  57.    else:
  58.       os._exit(0)       # Exit parent of the first child.
  59.  
  60.    import resource              # Resource usage information.
  61.    maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
  62.    if (maxfd == resource.RLIM_INFINITY):
  63.       maxfd = MAXFD
  64.  
  65.    # Iterate through and close all file descriptors.
  66.    for fd in range(0, maxfd):
  67.       try:
  68.          os.close(fd)
  69.       except OSError:   # ERROR, fd wasn't open to begin with (ignored)
  70.          pass
  71.  
  72.    os.open(REDIRECT_TO, os.O_RDWR)      # standard input (0)
  73.  
  74.    os.dup2(0, 1)                        # standard output (1)
  75.    os.dup2(0, 2)                        # standard error (2)
  76.  
  77.    return(0)
  78.  
  79. if __name__ == "__main__":
  80.  
  81.   retCode = createDaemon()
  82.  
  83.   ga = libgmail.GmailAccount(username, password)
  84.   ga.login()
  85.  
  86.   while True:
  87.     MESSAGES = []
  88.  
  89.     WRITELINES = []
  90.     f = open(LOGFILE,'r')
  91.     for line in f.readlines():
  92.       MESSAGES.append(line.rstrip())
  93.       WRITELINES.append(line)
  94.  
  95.     f.close()
  96.     os.system('cat /dev/null > %s' % LOGFILE)
  97.     f = open(LOGFILE, 'w')
  98.  
  99.     NEWMESSAGES = {}
  100.  
  101.     folder = ga.getMessagesByLabel(SEARCHLABEL, True)
  102.     for thread in folder:
  103.       threadSubject = thread.subject
  104.       for msg in thread:
  105.         if str(msg.id) not in MESSAGES:
  106.           NEWMESSAGES[msg.id] = [msg.author, msg.subject]
  107.           WRITELINES.append(str(msg.id) + '\n')
  108.  
  109.     for msg in NEWMESSAGES:
  110.       os.system("/path/to/googlevoice.pl -c sms -p %s '%s %s: %s'" % (PHONENUMBER, SEARCHLABEL, NEWMESSAGES[msg][0],NEWMESSAGES[msg][1]))
  111.  
  112.     f.writelines(WRITELINES)
  113.     f.close()
  114.     time.sleep(SLEEPTIME)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement