#!/usr/bin/env python import os, re import sys import time import xmpp from optparse import OptionParser name = {'1234' : 'Caller 1', '5678':'Caller2'} def send_it(tojid, text): jidparams={} if os.access(os.environ['HOME']+'/.xsend',os.R_OK): for ln in open(os.environ['HOME']+'/.xsend').readlines(): if not ln[0] in ('#',';'): key,val=ln.strip().split('=',1) jidparams[key.lower()]=val for mandatory in ['jid','password']: if mandatory not in jidparams.keys(): open(os.environ['HOME']+'/.xsend','w').write('#Uncomment fields before use and type in correct credentials.\n#JID=romeo@montague.net/resource (/resource is optional)\n#PASSWORD=juliet\n') print 'Please point ~/.xsend config file to valid JID for sending messages.' sys.exit(0) jid=xmpp.protocol.JID(jidparams['jid']) cl=xmpp.Client(jid.getDomain(),debug=[]) con=cl.connect(('127.0.0.1',5222)) if not con: print 'could not connect!' sys.exit() print 'connected with',con auth=cl.auth(jid.getNode(),jidparams['password'],resource=jid.getResource()) if not auth: print 'could not authenticate!' sys.exit() print 'authenticated using',auth #cl.SendInitPresence(requestRoster=0) # you may need to uncomment this for old server id=cl.send(xmpp.protocol.Message(tojid,text)) print 'sent message with id',id #time.sleep(1) # some older servers will not send the message if you disconnect immediately after sending #cl.disconnect() def tail_lines(fd, linesback = 10): # Contributed to Python Cookbook by Ed Pascoe (2003) avgcharsperline = 75 while 1: try: fd.seek(-1 * avgcharsperline * linesback, 2) except IOError: fd.seek(0) if fd.tell() == 0: atstart = 1 else: atstart = 0 lines = fd.read().split("\n") if (len(lines) > (linesback+1)) or atstart: break avgcharsperline=avgcharsperline * 1.3 if len(lines) > linesback: start = len(lines) - linesback - 1 else: start = 0 return lines[start:len(lines)-1] def handle_line(line): if re.compile(".*isdn_tty.*").match(line): if line.find("555"): bla = line.split() try: result = "Call from " + name[bla[8]] + "(" + bla[8] + ")" except KeyError: result = "Call from Unknown" + "(" + bla[8] + ")" print result send_it('adrian@jabberserver', result) def do_tail(filename, lines, follow, func = handle_line): fd = open(filename, 'r') for line in tail_lines(fd, lines): func(line + "\n") if not follow: return while 1: where = fd.tell() line = fd.readline() if not line: fd_results = os.fstat(fd.fileno()) try: st_results = os.stat(filename) except OSError: st_results = fd_results if st_results[1] == fd_results[1]: time.sleep(1) fd.seek(where) else: print "%s changed inode numbers from %d to %d" % (filename, fd_results[1], st_results[1]) fd = open(filename, 'r') else: func(line) def main(argv = sys.argv): parser = OptionParser() parser.add_option("-n", "--number", action="store", type="int", dest = "number", default=10) parser.add_option("-f", "--follow", action="store_true", dest = "follow", default=0) (options, args) = parser.parse_args() do_tail('/var/log/messages', options.number, 1, handle_line) if __name__ == "__main__": try: main(sys.argv) except KeyboardInterrupt: pass