messagenet/mncli.py
2016-12-15 14:20:49 +01:00

96 lines
2.9 KiB
Python

import json
import sys
from itertools import imap
import os.path
from pprint import pprint
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'])
mn.login(user, password)
# readline {{{2
try:
import readline
readline.read_init_file()
readline.read_history_file(os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'.mncli_history'))
except ImportError: # readline not supported on your system
pass
except:
print "Some error with readline; who cares"
# }}}
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
# save_calls(calls, conf['datadir'])
# vim: set fdm=marker: