messagenet/mncli.py

113 lines
3.5 KiB
Python
Raw Normal View History

2014-05-18 19:22:22 +02:00
import json
import sys
from itertools import imap
2014-05-18 19:26:34 +02:00
import os.path
2016-12-15 13:44:29 +01:00
from pprint import pprint
2016-12-15 14:21:07 +01:00
import atexit
2014-05-18 17:30:25 +02:00
2014-05-18 19:22:22 +02:00
from dirset import DirSet
2016-12-15 13:44:29 +01:00
from messagenet import Messagenet
2014-05-18 19:22:22 +02:00
2014-05-18 17:30:25 +02:00
def read_pass_file(fname):
with open(fname) as buf:
user = buf.readline().strip()
pwd = buf.readline().strip()
return user, pwd
2014-05-18 19:22:22 +02:00
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
2014-05-18 17:30:25 +02:00
2016-12-15 13:44:29 +01:00
def replhelp():
return '''
2016-12-15 14:20:49 +01:00
help - show this message
calllog - show every call, done and received
IN - show every inbound call
OUT - show every outbound call
2016-12-15 13:44:29 +01:00
forward $NUMBER - forward calls to number
forward STOP - disable forwarding
2016-12-15 14:20:49 +01:00
currency - how rich are you ?
2016-12-15 13:44:29 +01:00
'''
2016-12-15 14:20:49 +01:00
def main():
2014-05-18 19:26:34 +02:00
conf = json.load(open(os.path.join(
os.path.dirname(sys.argv[0]),
'defaultconf.json')))
2014-05-18 19:22:22 +02:00
if len(sys.argv) == 2:
conf.update(json.load(open(sys.argv[1])))
user, password = read_pass_file(conf['credfile'])
2016-12-15 13:44:29 +01:00
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"
2016-12-15 14:21:07 +01:00
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)
2016-12-15 13:44:29 +01:00
# }}}
2016-12-15 14:21:07 +01:00
mn.login(user, password)
2016-12-15 13:44:29 +01:00
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])
2016-12-15 14:20:49 +01:00
elif cmd == 'currency':
print mn.get_currency()
2016-12-15 13:44:29 +01:00
else:
print " Invalid command '%s'; type 'help' to get help" % cmd
2016-12-15 14:21:07 +01:00
if __name__ == '__main__':
main()
2016-12-15 13:44:29 +01:00
# vim: set fdm=marker: