def serialize_cmd(string, id): """Take string read from the command line, convert in into a protobuf message""" # Convert string into a dictionary cmd = parse_cmd(string) if cmd == None: return None # Process dictionary if cmd.cmd == "acc": return accMsg(id, cmd.user, cmd.scheme, cmd.secret, cmd.uname, cmd.password, cmd.do_login, cmd.fn, cmd.photo, cmd.private, cmd.auth, cmd.anon, cmd.tags, cmd.cred) elif cmd.cmd == "login": return loginMsg(id, cmd.scheme, cmd.secret, cmd.cred, cmd.uname, cmd.password) elif cmd.cmd == "sub": return subMsg(id, cmd.topic, cmd.fn, cmd.photo, cmd.private, cmd.auth, cmd.anon, cmd.mode, cmd.tags, cmd.get_query) elif cmd.cmd == "leave": return pb.ClientMsg(leave=pb.ClientLeave(id=str(id), topic=cmd.topic)) elif cmd.cmd == "pub": return pb.ClientMsg(pub=pb.ClientPub(id=str(id), topic=cmd.topic, no_echo=True, content=json.dumps(cmd.content))) elif cmd.cmd == "get": return getMsg(id, cmd.topic, cmd.desc, cmd.sub, cmd.tags, cmd.data) elif cmd.cmd == "set": return setMsg(id, cmd.topic, cmd.user, cmd.fn, cmd.photo, cmd.public, cmd.private, cmd.auth, cmd.anon, cmd.mode, cmd.tags) elif cmd.cmd == "del": return delMsg(id, cmd.topic, cmd.what, cmd.param, cmd.hard) elif cmd.cmd == "note": return noteMsg(id, cmd.topic, cmd.what, cmd.seq) else: print("Unrecognized: " + cmd.cmd) return None
def subscribe(topic): tid = next_id() add_future(tid, { 'arg': topic, 'action': lambda topicName, unused: add_subscription(topicName), }) return pb.ClientMsg(sub=pb.ClientSub(id=tid, topic=topic))
def loginMsg(id, cmd, args): if cmd.secret == None: if cmd.uname == None: cmd.uname = '' if cmd.password == None: cmd.password = '' cmd.secret = str(cmd.uname) + ":" + str(cmd.password) cmd.secret = cmd.secret.encode('utf-8') elif cmd.scheme == "basic": # Assuming secret is a uname:password string. cmd.secret = str(cmd.secret).encode('utf-8') else: # All other schemes: assume secret is a base64-encoded string cmd.secret = base64.b64decode(cmd.secret) msg = pb.ClientMsg(login=pb.ClientLogin(id=str(id), scheme=cmd.scheme, secret=cmd.secret, cred=parse_cred(cmd.cred))) if args.no_cookie or not tn_globals.IsInteractive: tn_globals.OnCompletion[str(id)] = lambda params: handle_login(params) else: tn_globals.OnCompletion[str(id)] = lambda params: save_cookie(params) return msg
def accMsg(id, user, scheme, secret, uname, password, do_login, fn, photo, private, auth, anon, tags, cred): if secret == None and uname != None: if password == None: password = '' secret = str(uname) + ":" + str(password) if secret: secret = secret.encode('utf-8') else: secret = b'' print(default_user) public = encode_to_bytes(make_vcard(fn, photo)) if (fn or photo) else None private = encode_to_bytes(private) if private else None return pb.ClientMsg(acc=pb.ClientAcc( id=str(id), user_id=user, scheme=scheme, secret=secret, login=do_login, tags=tags.split(",") if tags else None, desc=pb.SetDesc(default_acs=pb.DefaultAcsMode(auth=auth, anon=anon), public=public, private=private), cred=parse_cred(cred)), on_behalf_of=default_user)
def login(cookie_file_name, scheme, secret): tid = next_id() add_future(tid, { 'arg': cookie_file_name, 'action': lambda fname, params: on_login(fname, params), }) return pb.ClientMsg(login=pb.ClientLogin(id=tid, scheme=scheme, secret=secret))
def setMsg(id, cmd, ignored): if not cmd.topic: cmd.topic = tn_globals.DefaultTopic if cmd.public == None: cmd.public = encode_to_bytes(make_vcard(cmd.fn, cmd.photo)) else: cmd.public = encode_to_bytes(cmd.public) cmd.private = encode_to_bytes(cmd.private) cred = parse_cred(cmd.cred) if cred: if len(cred) > 1: stdoutln( 'Warning: multiple credentials specified. Will use only the first one.' ) cred = cred[0] return pb.ClientMsg(set=pb.ClientSet( id=str(id), topic=cmd.topic, query=pb.SetQuery(desc=pb.SetDesc(default_acs=pb.DefaultAcsMode( auth=cmd.auth, anon=cmd.anon), public=cmd.public, private=cmd.private), sub=pb.SetSub(user_id=cmd.user, mode=cmd.mode), tags=cmd.tags.split(",") if cmd.tags else None, cred=cred)), on_behalf_of=tn_globals.DefaultUser)
def leaveMsg(id, topic, unsub): if not topic: topic = default_topic return pb.ClientMsg(leave=pb.ClientLeave(id=str(id), topic=topic, unsub=unsub), on_behalf_of=default_user)
def leaveMsg(id, cmd, ignored): if not cmd.topic: cmd.topic = DefaultTopic return pb.ClientMsg(leave=pb.ClientLeave(id=str(id), topic=cmd.topic, unsub=cmd.unsub), on_behalf_of=DefaultUser)
def publish(topic, text): tid = next_id() return pb.ClientMsg( pub=pb.ClientPub(id=tid, topic=topic, no_echo=True, content=json.dumps(text).encode('utf-8')))
def pubMsg(id, cmd, ignored): if not cmd.topic: cmd.topic = DefaultTopic head = {} if cmd.drafty or cmd.image or cmd.attachment: head['mime'] = encode_to_bytes('text/x-drafty') # Excplicitly provided 'mime' will override the one assigned above. if cmd.head: for h in cmd.head.split(","): key, val = h.split(":") head[key] = encode_to_bytes(val) content = json.loads(cmd.drafty) if cmd.drafty \ else inline_image(cmd.image) if cmd.image \ else attachment(cmd.attachment) if cmd.attachment \ else cmd.content if not content: return None return pb.ClientMsg(pub=pb.ClientPub(id=str(id), topic=cmd.topic, no_echo=True, head=head, content=encode_to_bytes(content)), on_behalf_of=DefaultUser)
def accMsg(id, cmd, ignored): if cmd.uname: cmd.scheme = 'basic' if cmd.password == None: cmd.password = '' cmd.secret = str(cmd.uname) + ":" + str(cmd.password) if cmd.secret: if cmd.scheme == None: cmd.scheme = 'basic' cmd.secret = cmd.secret.encode('utf-8') else: cmd.secret = b'' state = None if cmd.suspend == 'true': state = 'susp' elif cmd.suspend == 'false': state = 'ok' cmd.public = encode_to_bytes(make_vcard(cmd.fn, cmd.photo)) cmd.private = encode_to_bytes(cmd.private) return pb.ClientMsg(acc=pb.ClientAcc(id=str(id), user_id=cmd.user, state=state, scheme=cmd.scheme, secret=cmd.secret, login=cmd.do_login, tags=cmd.tags.split(",") if cmd.tags else None, desc=pb.SetDesc(default_acs=pb.DefaultAcsMode(auth=cmd.auth, anon=cmd.anon), public=cmd.public, private=cmd.private), cred=parse_cred(cmd.cred)), on_behalf_of=tn_globals.DefaultUser)
def leave(topic): tid = next_id() add_future(tid, { 'arg': topic, 'onsuccess': lambda topicName, unused: del_subscription(topicName) }) return pb.ClientMsg(leave=pb.ClientLeave(id=tid, topic=topic))
def login(cookie_file_name, scheme, secret): tid = next_id() add_future(tid, { 'arg': cookie_file_name, 'onsuccess': lambda fname, params: on_login(fname, params), 'onerror': lambda unused, errcode: login_error(unused, errcode), }) return pb.ClientMsg(login=pb.ClientLogin(id=tid, scheme=scheme, secret=secret))
def subscribe(topic): tid = next_id() add_future(tid, { 'arg': topic, 'onsuccess': lambda topicName, unused: add_subscription(topicName), 'onerror': lambda topicName, errcode: subscription_failed(topicName, errcode), }) return pb.ClientMsg(sub=pb.ClientSub(id=tid, topic=topic))
def hello(): tid = next_id() add_future(tid, { 'onsuccess': lambda unused, params: server_version(params), }) return pb.ClientMsg(hi=pb.ClientHi(id=tid, user_agent=APP_NAME + "/" + APP_VERSION + " (" + platform.system() + "/" + platform.release() + "); gRPC-python/" + LIB_VERSION, ver=LIB_VERSION, lang="EN"))
def pubMsg(id, topic, content): if not topic: topic = default_topic return pb.ClientMsg(pub=pb.ClientPub(id=str(id), topic=topic, no_echo=True, content=encode_to_bytes(content)), on_behalf_of=default_user)
def hiMsg(id): OnCompletion[str(id)] = lambda params: print_server_params(params) return pb.ClientMsg( hi=pb.ClientHi(id=str(id), user_agent=APP_NAME + "/" + APP_VERSION + " (" + platform.system() + "/" + platform.release() + "); gRPC-python/" + LIB_VERSION + "-" + GRPC_VERSION, ver=LIB_VERSION, lang="EN"))
def subMsg(id, topic, fn, photo, private, auth, anon, mode, tags, get_query): if get_query: get_query = pb.GetQuery(what=get_query.split(",").join(" ")) public = encode_to_bytes(make_vcard(fn, photo)) private = encode_to_bytes(private) return pb.ClientMsg(sub=pb.ClientSub(id=str(id), topic=topic, set_query=pb.SetQuery( desc=pb.SetDesc(public=public, private=private, default_acs=pb.DefaultAcsMode(auth=auth, anon=anon)), sub=pb.SetSub(mode=mode), tags=tags.split(",") if tags else None), get_query=get_query))
def loginMsg(id, scheme, secret, cred, uname, password): if secret == None and uname != None: if password == None: password = '' secret = str(uname) + ":" + str(password) if type(secret) is str: secret = secret.encode('utf-8') onCompletion[str(id)] = lambda params: save_cookie(params) return pb.ClientMsg(login=pb.ClientLogin( id=str(id), scheme=scheme, secret=secret, cred=parse_cred(cred)))
def noteMsg(id, topic, what, seq): enum_what = None if what == 'kp': enum_what = pb.KP seq = None elif what == 'read': enum_what = pb.READ seq = int(seq) elif what == 'recv': enum_what = pb.READ seq = int(seq) return pb.ClientMsg(note=pb.ClientNote(topic=topic, what=enum_what, seq_id=seq))
def getMsg(id, topic, desc, sub, tags, data): what = [] if desc: what.append("desc") if sub: what.append("sub") if tags: what.append("tags") if data: what.append("data") return pb.ClientMsg(get=pb.ClientGet( id=str(id), topic=topic, query=pb.GetQuery(what=" ".join(what))))
def setMsg(id, topic, user, fn, photo, public, private, auth, anon, mode, tags): if public == None: public = encode_to_bytes(make_vcard(fn, photo)) else: public = encode_to_bytes(public) private = encode_to_bytes(private) return pb.ClientMsg(set=pb.ClientSet(id=str(id), topic=topic, query=pb.SetQuery( desc=pb.SetDesc(default_acs=pb.DefaultAcsMode(auth=auth, anon=anon), public=public, private=private), sub=pb.SetSub(user_id=user, mode=mode), tags=tags)))
def subMsg(id, cmd, ignored): if not cmd.topic: cmd.topic = tn_globals.DefaultTopic if cmd.get_query: cmd.get_query = pb.GetQuery(what=" ".join(cmd.get_query.split(","))) cmd.public = encode_to_bytes(make_vcard(cmd.fn, cmd.photo)) cmd.private = encode_to_bytes(cmd.private) return pb.ClientMsg(sub=pb.ClientSub(id=str(id), topic=cmd.topic, set_query=pb.SetQuery( desc=pb.SetDesc(public=cmd.public, private=cmd.private, default_acs=pb.DefaultAcsMode(auth=cmd.auth, anon=cmd.anon)), sub=pb.SetSub(mode=cmd.mode), tags=cmd.tags.split(",") if cmd.tags else None), get_query=cmd.get_query), on_behalf_of=tn_globals.DefaultUser)
def getMsg(id, topic, desc, sub, tags, data): if not topic: topic = default_topic what = [] if desc: what.append("desc") if sub: what.append("sub") if tags: what.append("tags") if data: what.append("data") return pb.ClientMsg(get=pb.ClientGet(id=str(id), topic=topic, query=pb.GetQuery(what=" ".join(what))), on_behalf_of=default_user)
def noteMsg(id, cmd, ignored): if not cmd.topic: cmd.topic = tn_globals.DefaultTopic enum_what = None if cmd.what == 'kp': enum_what = pb.KP cmd.seq = None elif cmd.what == 'read': enum_what = pb.READ cmd.seq = int(cmd.seq) elif what == 'recv': enum_what = pb.RECV cmd.seq = int(cmd.seq) return pb.ClientMsg(note=pb.ClientNote(topic=cmd.topic, what=enum_what, seq_id=cmd.seq), on_behalf_of=tn_globals.DefaultUser)
def noteMsg(id, topic, what, seq): if not topic: topic = default_topic enum_what = None if what == 'kp': enum_what = pb.KP seq = None elif what == 'read': enum_what = pb.READ seq = int(seq) elif what == 'recv': enum_what = pb.READ seq = int(seq) return pb.ClientMsg(note=pb.ClientNote(topic=topic, what=enum_what, seq_id=seq), on_behalf_of=default_user)
def loginMsg(id, scheme, secret, cred, uname, password): if secret == None: if uname == None: uname = '' if password == None: password = '' secret = str(uname) + ":" + str(password) secret = secret.encode('utf-8') else: # Assuming secret is a base64-encoded string secret = base64.b64decode(secret) onCompletion[str(id)] = lambda params: save_cookie(params) return pb.ClientMsg(login=pb.ClientLogin( id=str(id), scheme=scheme, secret=secret, cred=parse_cred(cred)))
def getMsg(id, cmd, ignored): if not cmd.topic: cmd.topic = DefaultTopic what = [] if cmd.desc: what.append("desc") if cmd.sub: what.append("sub") if cmd.tags: what.append("tags") if cmd.data: what.append("data") return pb.ClientMsg(get=pb.ClientGet( id=str(id), topic=cmd.topic, query=pb.GetQuery(what=" ".join(what))), on_behalf_of=DefaultUser)
def delMsg(id, topic, what, param, hard): if topic == None and param != None: topic = param param = None if not topic: topic = default_topic stdoutln(id, topic, what, param, hard) enum_what = None before = None seq_list = None user = None if what == 'msg': enum_what = pb.ClientDel.MSG if param == 'all': seq_list = [pb.DelQuery(range=pb.SeqRange(low=1, hi=0x8FFFFFF))] elif param != None: seq_list = [ pb.DelQuery(seq_id=int(x.strip())) for x in param.split(',') ] stdoutln(seq_list) elif what == 'sub': enum_what = pb.ClientDel.SUB user = param elif what == 'topic': enum_what = pb.ClientDel.TOPIC # Field named 'del' conflicts with the keyword 'del. This is a work around. msg = pb.ClientMsg(on_behalf_of=default_user) xdel = getattr(msg, 'del') """ setattr(msg, 'del', pb.ClientDel(id=str(id), topic=topic, what=enum_what, hard=hard, del_seq=seq_list, user_id=user)) """ xdel.id = str(id) xdel.topic = topic xdel.what = enum_what if hard != None: xdel.hard = hard if seq_list != None: xdel.del_seq.extend(seq_list) if user != None: xdel.user_id = user return msg
def setMsg(id, cmd, ignored): if not cmd.topic: cmd.topic = DefaultTopic if cmd.public == None: cmd.public = encode_to_bytes(make_vcard(cmd.fn, cmd.photo)) else: cmd.public = encode_to_bytes(cmd.public) cmd.private = encode_to_bytes(cmd.private) return pb.ClientMsg(set=pb.ClientSet( id=str(id), topic=cmd.topic, query=pb.SetQuery(desc=pb.SetDesc(default_acs=pb.DefaultAcsMode( auth=cmd.auth, anon=cmd.anon), public=cmd.public, private=cmd.private), sub=pb.SetSub(user_id=cmd.user, mode=cmd.mode), tags=cmd.tags.split(",") if cmd.tags else None)), on_behalf_of=DefaultUser)