#!/usr/bin/python

# try to find out who sent you last xtell message and reply
# to him/her

import sys, os, string

if len(sys.argv)>1:
    os.execvp("xtell", sys.argv)

try:
    f = open(os.path.join(os.environ["HOME"], ".xtell-log"))
except IOError:
    print "Sorry, cannot find out who called you last"
    sys.exit()

f.seek(0, 2) # to the end of file
buf = ""
for i in xrange(2000):
    f.seek(-1,1)
    ch = f.read(1)
    f.seek(-1,1)
    if ch in string.digits+string.letters+"-@.":
        buf = ch + buf
    else:
        if '@' in buf:
            print "Replying to", buf
            os.execvp("xtell", ("xtell", buf))
            sys.exit()
        buf = ''

