コード例 #1
0
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)
コード例 #2
0
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')))
コード例 #3
0
ファイル: tn-cli.py プロジェクト: vatsal78/chat
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
コード例 #4
0
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)