messagenet/mncli.py

112 lines
3.5 KiB
Python

import json
import sys
from itertools import imap
import os.path
from pprint import pprint
import atexit
from dirset import DirSet
from messagenet import Messagenet
def read_pass_file(fname):
with open(fname) as buf:
user = buf.readline().strip()
pwd = buf.readline().strip()
return user, pwd
def save_calls(calls, datadir):
s = DirSet(datadir)
for call in imap(lambda t: '\t'.join(t), calls):
if s.add(call): # wasn't existing before
print 'NEW: %s' % call
def replhelp():
return '''
help - show this message
calllog - show every call, done and received
IN - show every inbound call
OUT - show every outbound call
forward $NUMBER - forward calls to number
forward STOP - disable forwarding
currency - how rich are you ?
'''
def main():
conf = json.load(open(os.path.join(
os.path.dirname(sys.argv[0]),
'defaultconf.json')))
if len(sys.argv) == 2:
conf.update(json.load(open(sys.argv[1])))
user, password = read_pass_file(conf['credfile'])
with Messagenet() as mn: # conf['browserdriver'])
# readline {{{2
try:
import readline
readline.read_init_file()
except ImportError: # readline not supported on your system
pass
except:
print "Some error with readline; who cares"
histfile = os.path.join(os.path.dirname(__file__),
'.mncli_history')
if os.path.exists(histfile):
readline.read_history_file(histfile)
atexit.register(readline.write_history_file, histfile)
def _completer(text, state):
commands = ('help', 'quit', 'calllog', 'forward', 'currency')
options = [c for c in commands if c.startswith(text)]
if readline.get_begidx() != 0:
return None
if state < len(options):
return options[state]
return None
readline.parse_and_bind('tab: complete')
readline.set_completer(_completer)
# }}}
mn.login(user, password)
print "messagenet command-line. type help for help. type quit for quit"
while True:
try:
cmdline = raw_input('> ').strip()
except EOFError:
break
if not cmdline:
continue
cmd = cmdline.split()[0]
args = cmdline.split()[1:]
if cmd == 'help':
print replhelp()
elif cmd in ('quit', 'exit', ':wq'):
break
elif cmd == 'calllog':
if not args or 'IN' in args:
inb = tuple(mn.get_inbound_calls())
print "=== IN ==="
pprint(inb[:5])
if not args or 'OUT' in args:
outb = tuple(mn.get_outbound_calls())
print "=== OUT ==="
pprint(outb[:5])
elif cmd == 'forward':
if not args:
print mn.get_forward()
elif args[0] == 'STOP':
mn.set_forward(False)
else:
mn.set_forward(args[0])
elif cmd == 'currency':
print mn.get_currency()
else:
print " Invalid command '%s'; type 'help' to get help" % cmd
if __name__ == '__main__':
main()
# vim: set fdm=marker: