def read_cookie(): try: cookie = open('.tn-cli-cookie', 'r') params = json.load(cookie) cookie.close() return params.get("token") except Exception as err: printerr("Missing or invalid cookie file '.tn-cli-cookie'", err) return None
def stdin(InputQueue): partial_input = "" try: for cmd in readLinesFromStdin(): cmd = cmd.strip() # Check for continuation symbol \ in the end of the line. if len(cmd) > 0 and cmd[-1] == "\\": cmd = cmd[:-1].rstrip() if cmd: if partial_input: partial_input += " " + cmd else: partial_input = cmd if tn_globals.IsInteractive: sys.stdout.write("... ") sys.stdout.flush() continue # Check if we have cached input from a previous multiline command. if partial_input: if cmd: partial_input += " " + cmd InputQueue.append(partial_input) partial_input = "" continue InputQueue.append(cmd) # Stop processing input if cmd == 'exit' or cmd == 'quit' or cmd == '.exit' or cmd == '.quit': return except Exception as ex: printerr("Exception in stdin", ex) InputQueue.append("exit")
def run(args, schema, secret): try: if tn_globals.IsInteractive: tn_globals.Prompt = PromptSession() # Create secure channel with default credentials. channel = None if args.ssl: opts = (('grpc.ssl_target_name_override', args.ssl_host),) if args.ssl_host else None channel = grpc.secure_channel(args.host, grpc.ssl_channel_credentials(), opts) else: channel = grpc.insecure_channel(args.host) # Call the server stream = pbx.NodeStub(channel).MessageLoop(gen_message(schema, secret, args)) # Read server responses for msg in stream: if tn_globals.Verbose: stdoutln("\r<= " + to_json(msg)) if msg.HasField("ctrl"): handle_ctrl(msg.ctrl) elif msg.HasField("meta"): what = [] if len(msg.meta.sub) > 0: what.append("sub") if msg.meta.HasField("desc"): what.append("desc") if msg.meta.HasField("del"): what.append("del") if len(msg.meta.tags) > 0: what.append("tags") stdoutln("\r<= meta " + ",".join(what) + " " + msg.meta.topic) if tn_globals.WaitingFor and tn_globals.WaitingFor.await_id == msg.meta.id: if 'varname' in tn_globals.WaitingFor: tn_globals.Variables[tn_globals.WaitingFor.varname] = msg.meta tn_globals.WaitingFor = None elif msg.HasField("data"): stdoutln("\n\rFrom: " + msg.data.from_user_id) stdoutln("Topic: " + msg.data.topic) stdoutln("Seq: " + str(msg.data.seq_id)) if msg.data.head: stdoutln("Headers:") for key in msg.data.head: stdoutln("\t" + key + ": "+str(msg.data.head[key])) stdoutln(json.loads(msg.data.content)) elif msg.HasField("pres"): # 'ON', 'OFF', 'UA', 'UPD', 'GONE', 'ACS', 'TERM', 'MSG', 'READ', 'RECV', 'DEL', 'TAGS' what = pb.ServerPres.What.Name(msg.pres.what) stdoutln("\r<= pres " + what + " " + msg.pres.topic) elif msg.HasField("info"): switcher = { pb.READ: 'READ', pb.RECV: 'RECV', pb.KP: 'KP' } stdoutln("\rMessage #" + str(msg.info.seq_id) + " " + switcher.get(msg.info.what, "unknown") + " by " + msg.info.from_user_id + "; topic=" + msg.info.topic + " (" + msg.topic + ")") else: stdoutln("\rMessage type not handled" + str(msg)) except grpc.RpcError as err: # print(err) printerr("gRPC failed with {0}: {1}".format(err.code(), err.details())) except Exception as ex: printerr("Request failed: {0}".format(ex)) # print(traceback.format_exc()) finally: printout('Shutting down...') channel.close() if tn_globals.InputThread != None: tn_globals.InputThread.join(0.3)
printout("Logging in with token", args.login_token) elif args.login_basic: """Use username:password""" schema = 'basic' secret = args.login_basic printout("Logging in with login:password", args.login_basic) elif tn_globals.IsInteractive: """Interactive mode only: try reading the cookie file""" try: schema = 'token' secret = read_cookie() printout("Logging in with cookie file") except Exception as err: printerr("Failed to read authentication cookie", err) # Attempt to load the macro file if available. macros = None if args.load_macros: import importlib macros = importlib.import_module( 'macros', args.load_macros) if args.load_macros else None # Check if background session is specified explicitly. If not set it to # True for non-interactive sessions. if args.background is None and not tn_globals.IsInteractive: args.background = True run(args, schema, secret)