Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # sends me an sms for utsa new mail
- __author__ = "Cody Lee <platinummonkey@archlinux.us>"
- import os # Miscellaneous OS interfaces.
- import sys # System-specific parameters and functions.
- import libgmail
- import time
- username = 'username@gmail.com'
- password = 'c0mputerspeck'
- SEARCHLABEL = 'TAMU'
- PHONENUMBER = '5551234567'
- LOGFILE = '/path/to/utsa-gmail.log' #
- SLEEPTIME = 60*5 # 5 min
- MARKASUNREAD = True # True to keep messages "unread"; false otherwise.
- # Default daemon parameters.
- # File mode creation mask of the daemon.
- UMASK = 0
- # Default working directory for the daemon.
- WORKDIR = "/path/to/whatever/"
- # Default maximum for the number of available file descriptors.
- MAXFD = 1024
- # The standard I/O file descriptors are redirected to /dev/null by default.
- if (hasattr(os, "devnull")):
- REDIRECT_TO = os.devnull
- else:
- REDIRECT_TO = "/dev/null"
- def createDaemon():
- """Detach a process from the controlling terminal and run it in the
- background as a daemon.
- """
- try:
- pid = os.fork()
- except OSError, e:
- raise Exception, "%s [%d]" % (e.strerror, e.errno)
- if (pid == 0): # The first child.
- os.setsid()
- try:
- pid = os.fork() # Fork a second child.
- except OSError, e:
- raise Exception, "%s [%d]" % (e.strerror, e.errno)
- if (pid == 0): # The second child.
- os.chdir(WORKDIR)
- os.umask(UMASK)
- else:
- os._exit(0) # Exit parent (the first child) of the second child.
- else:
- os._exit(0) # Exit parent of the first child.
- import resource # Resource usage information.
- maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
- if (maxfd == resource.RLIM_INFINITY):
- maxfd = MAXFD
- # Iterate through and close all file descriptors.
- for fd in range(0, maxfd):
- try:
- os.close(fd)
- except OSError: # ERROR, fd wasn't open to begin with (ignored)
- pass
- os.open(REDIRECT_TO, os.O_RDWR) # standard input (0)
- os.dup2(0, 1) # standard output (1)
- os.dup2(0, 2) # standard error (2)
- return(0)
- if __name__ == "__main__":
- retCode = createDaemon()
- ga = libgmail.GmailAccount(username, password)
- ga.login()
- while True:
- MESSAGES = []
- WRITELINES = []
- f = open(LOGFILE,'r')
- for line in f.readlines():
- MESSAGES.append(line.rstrip())
- WRITELINES.append(line)
- f.close()
- os.system('cat /dev/null > %s' % LOGFILE)
- f = open(LOGFILE, 'w')
- NEWMESSAGES = {}
- folder = ga.getMessagesByQuery("is:unread label:%s" % SEARCHLABEL, True) # grrrrr libgmail marking everything as read x.x
- for thread in folder:
- threadSubject = thread.subject
- for msg in thread:
- if str(msg.id) not in MESSAGES:
- NEWMESSAGES[msg.id] = [msg.author, msg.subject]
- WRITELINES.append(str(msg.id) + '\n')
- # fix default libgmail action of marking as read. remark as unread.
- if MARKASUNREAD:
- ga._doThreadAction(libgmail.U_MARKUNREAD_ACTION, thread)
- for msg in NEWMESSAGES:
- os.system("/path/to/googlevoice.pl -c sms -p %s '%s %s: %s'" % (PHONENUMBER, SEARCHLABEL, NEWMESSAGES[msg][0],NEWMESSAGES[msg][1]))
- f.writelines(WRITELINES)
- f.close()
- time.sleep(SLEEPTIME)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement