#!/usr/bin/env python import sys, os, getopt, time, string, commands MAILS = [ 'samfarm@hepmail.phys.sinica.edu.tw', 'cdf_production_farm@listserv.fnal.gov', ] def ts( t ): return time.strftime("%Y/%m/%d %H:%M:%S", time.localtime( t )) try: optlist, args = getopt.getopt(sys.argv[1:], 't:b:m:scf:', ['title=', 'body=', 'message=', 'send', 'clear', 'file=', ]) except getopt.GetoptError, e: print ts(time.time()) + 'Messenger: argument error' sys.stdout.flush() sys.exit(1) message = '.messenger' title = '' body = '' send = 0 clear = 0 file = '' for key, val in optlist: if key == '-t': title = val elif key == '-b': body = val elif key == '-m': message = val elif key == '-f': file = val elif key == '-s': send = 1 elif key == '-c': clear = 1 if clear == 1: # empty file fp = open(message, 'wt') fp.close() if title != '': # replace the record of title buf = [] if os.path.exists(message) == 1: lines = open(message).readlines() lines = map(string.strip, lines) else: lines = [] for line in lines: try: timestamp, act, val = map(string.strip, string.split(line, ':', 2)) except: continue # exclude title if act != 'title': buf.append( line ) timestamp = int( time.time() ) act = 'title' val = title line = '%d:%s:%s' % (timestamp, act, val) buf.append( line ) fp = open(message, 'wt') for line in buf: fp.write( line + '\n' ) fp.flush() fp.close() if body != '': timestamp = int( time.time() ) act = 'body' val = body line = '%d:%s:%s' % (timestamp, act, val) fp = open(message, 'at') fp.write( line + '\n' ) fp.flush() fp.close() if file != '' and os.path.exists(file) == 1: timestamp = int( time.time() ) act = 'file' val = file line = '%d:%s:%s' % (timestamp, act, val) fp = open(message, 'at') fp.write( line + '\n' ) fp.flush() fp.close() if send == 1: title = '' body = [] file = [] lines = open(message).readlines() for line in lines: try: timestamp, act, val = map(string.strip, string.split(line, ':', 2)) timestamp = int( timestamp ) except: continue if act == 'title': title = val elif act == 'body': body.append( (timestamp, val) ) elif act == 'file': file.append( (timestamp, val) ) #body.sort() file.sort() if title == '' or (len(body) == 0 and len(file) == 0): print ts(time.time()) + 'no title or message assigned' sys.stdout.flush() sys.exit(1) for email in MAILS: s, user = commands.getstatusoutput("id -un") s, host = commands.getstatusoutput("hostname") p = os.popen("/usr/sbin/sendmail " + email, "w") p.write( "Subject: %s\n" % title ) p.write( "From: %s at %s <%s@%s>\n" % (user, host, user, host) ) p.write( "To: %s\n" % email ) for timestamp, val in body: timestamp = ts( timestamp ) p.write( '%s: %s\n' % (timestamp, val) ) for timestamp, val in file: timestamp = ts( timestamp ) try: buf = open(val).read() except: continue p.write( 'File %s:\n%s\n\n' % (val, buf) ) p.close() sys.stdout.flush() sys.exit(0)