Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 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. MARKASUNREAD = True # True to keep messages "unread"; false otherwise.
  18.  
  19. # Default daemon parameters.
  20. # File mode creation mask of the daemon.
  21. UMASK = 0
  22.  
  23. # Default working directory for the daemon.
  24. WORKDIR = "/path/to/whatever/"
  25.  
  26. # Default maximum for the number of available file descriptors.
  27. MAXFD = 1024
  28.  
  29. # The standard I/O file descriptors are redirected to /dev/null by default.
  30. if (hasattr(os, "devnull")):
  31.    REDIRECT_TO = os.devnull
  32. else:
  33.    REDIRECT_TO = "/dev/null"
  34.  
  35. def createDaemon():
  36.    """Detach a process from the controlling terminal and run it in the
  37.   background as a daemon.
  38.   """
  39.  
  40.    try:
  41.       pid = os.fork()
  42.    except OSError, e:
  43.       raise Exception, "%s [%d]" % (e.strerror, e.errno)
  44.  
  45.    if (pid == 0):       # The first child.
  46.       os.setsid()
  47.  
  48.       try:
  49.          pid = os.fork()        # Fork a second child.
  50.       except OSError, e:
  51.          raise Exception, "%s [%d]" % (e.strerror, e.errno)
  52.  
  53.       if (pid == 0):    # The second child.
  54.          os.chdir(WORKDIR)
  55.          os.umask(UMASK)
  56.       else:
  57.          os._exit(0)    # Exit parent (the first child) of the second child.
  58.    else:
  59.       os._exit(0)       # Exit parent of the first child.
  60.  
  61.    import resource              # Resource usage information.
  62.    maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
  63.    if (maxfd == resource.RLIM_INFINITY):
  64.       maxfd = MAXFD
  65.  
  66.    # Iterate through and close all file descriptors.
  67.    for fd in range(0, maxfd):
  68.       try:
  69.          os.close(fd)
  70.       except OSError:   # ERROR, fd wasn't open to begin with (ignored)
  71.          pass
  72.  
  73.    os.open(REDIRECT_TO, os.O_RDWR)      # standard input (0)
  74.  
  75.    os.dup2(0, 1)                        # standard output (1)
  76.    os.dup2(0, 2)                        # standard error (2)
  77.  
  78.    return(0)
  79.  
  80. if __name__ == "__main__":
  81.  
  82.   retCode = createDaemon()
  83.  
  84.   ga = libgmail.GmailAccount(username, password)
  85.   ga.login()
  86.  
  87.   while True:
  88.     MESSAGES = []
  89.  
  90.     WRITELINES = []
  91.     f = open(LOGFILE,'r')
  92.     for line in f.readlines():
  93.       MESSAGES.append(line.rstrip())
  94.       WRITELINES.append(line)
  95.  
  96.     f.close()
  97.     os.system('cat /dev/null > %s' % LOGFILE)
  98.     f = open(LOGFILE, 'w')
  99.  
  100.     NEWMESSAGES = {}
  101.  
  102.     folder = ga.getMessagesByQuery("is:unread label:%s" % SEARCHLABEL, True) # grrrrr libgmail marking everything as read x.x
  103.     for thread in folder:
  104.       threadSubject = thread.subject
  105.       for msg in thread:
  106.         if str(msg.id) not in MESSAGES:
  107.           NEWMESSAGES[msg.id] = [msg.author, msg.subject]
  108.           WRITELINES.append(str(msg.id) + '\n')
  109.           # fix default libgmail action of marking as read. remark as unread.
  110.           if MARKASUNREAD:
  111.             ga._doThreadAction(libgmail.U_MARKUNREAD_ACTION, thread)
  112.  
  113.     for msg in NEWMESSAGES:
  114.       os.system("/path/to/googlevoice.pl -c sms -p %s '%s %s: %s'" % (PHONENUMBER, SEARCHLABEL, NEWMESSAGES[msg][0],NEWMESSAGES[msg][1]))
  115.  
  116.     f.writelines(WRITELINES)
  117.     f.close()
  118.     time.sleep(SLEEPTIME)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement