예제 #1
0
파일: cache.py 프로젝트: nolash/weechat-pss
    def load_store(self):

        entrycount = 0
        okcount = 0

        try:
            f = open(self.get_store_path(), "r", 0600)
            while 1:
                # if there is a record
                # split fields on tab and chop newline
                record = f.readline()
                if len(record) == 0:
                    break

                # add it to the map and report
                entrycount += 1
                try:
                    (nick, pubkeyhx, overlayhx, srckeyhx) = record.split("\t")
                    if ord(srckeyhx[len(srckeyhx) - 1]) == 0x0a:
                        srckeyhx = srckeyhx[:len(srckeyhx) - 1]
                    srckey = clean_pubkey(srckeyhx).decode("hex")
                    pubkey = clean_pubkey(pubkeyhx).decode("hex")
                    overlay = clean_overlay(overlayhx).decode("hex")
                    contact = PssContact(nick, srckey)
                    contact.set_public_key(pubkey)
                    contact.set_overlay(overlay)

                # \todo delete the record from the contact store
                except Exception as e:
                    pass

                try:
                    self.add_contact(contact)
                    okcount += 1
                    if not srckey in self.idx_src_contacts:
                        self.idx_src_contacts[srckey] = []
                    self.idx_src_contacts[srckey].append(contact)
                    self.idx_nick_contact[nick] = contact
                    self.idx_publickey_contact[pubkey] = contact

                except Exception as e:
                    sys.stderr.write("fail on " + str(nick) + ": " + repr(e) +
                                     "\n")
            f.close()
        except IOError as e:
            pass

        # \todo dump valid entries to new file and copy over old
        self.file = open(self.get_store_path(), "a", 0600)
        return (entrycount, okcount)
예제 #2
0
파일: pss.py 프로젝트: Mistra/weechat-pss
    def connect(self):

        base = ""
        key = ""

        self.ws = None
        try:
            self.ws = websocket.create_connection("ws://" + self.host + ":" +
                                                  self.port)
        except Exception as e:
            self.err = PSS_ESOCK
            self.errstr = "could not connect to pss " + self.name + " on " + self.host + ":" + self.port + ": " + repr(
                e)
            return False

        # get the node adress
        self.ws.send(rpc_call(self.seq, "baseAddr", []))
        self.seq += 1
        resp = rpc_parse(self.ws.recv())

        # verify address
        try:
            base = clean_address(resp['result'])
        except ValueError as e:
            self.err = PSS_EREMOTEINVAL
            self.errstr = "received bogus base address " + resp['result']
            return False

        # retrieve the node key	data
        self.ws.send(rpc_call(self.seq, "getPublicKey", []))
        self.seq += 1
        resp = rpc_parse(self.ws.recv())

        # verify key
        try:
            key = clean_pubkey(resp['result'])
        except ValueError as e:
            self.err = PSS_EREMOTEINVAL
            self.errstr = "received bogus pubkey " + resp['result']
            return False

        # subscribe to incoming
        self.ws.send(
            rpc_call(self.seq, "subscribe", ['receive', topic, False, False]))
        self.seq += 1
        resp = rpc_parse(self.ws.recv())
        self.sub = resp['result']

        # now we're in the clear
        # finish setting up object properties
        self.key = key
        self.base = base
        self.connected = True
        self.run = True

        return True
예제 #3
0
파일: room.py 프로젝트: Mistra/weechat-pss
    def add(self, nick, participant):

        # account reflects the peer's address / key
        acc = Account()
        # \todo fix this 04 prefix ambiguity bullshit
        acc.set_public_key(clean_pubkey(participant.key).decode("hex"))

        # incoming feed user is peer
        participantfeed_in = Feed(self.agent, acc, self.name, False)
        self.feedcollection_in.add(participant.nick, participantfeed_in)
        self.participants[nick] = participant
        self.hsh_room = self.save()
예제 #4
0
파일: room.py 프로젝트: Mistra/weechat-pss
    def extract_message(self, body, contact):
        participantcount = 0
        payloadoffset = -1
        payloadlength = 0
        ciphermsg = ""

        # retrieve update from swarm
        # body = self.bzz.get(hsh.encode("hex"))

        # if recipient list for the update matches the one in memory
        # we use the position of the participant in the list as the body offset index
        matchidx = -1
        idx = 0
        if self.hsh_room == body[37:69]:
            participantcount = len(self.participants)
            for p in self.participants.values():
                if p.key == contact.key:
                    matchidx = idx
                idx += 1
        # if not we need to retrieve the one that was relevant at the time of update
        # and match the index against that
        else:
            savedroom = json.loads(self.bzz.get(body[37:69].encode("hex")))
            participantcount = len(savedroom['participants'])
            for p in savedroom['participants']:
                if clean_hex(p) == clean_pubkey(contact.key):
                    matchidx = idx
                idx += 1

        # if no matches then this pubkey is not relevant for the room at that particular update
        if matchidx == -1:
            raise ValueError("pubkey " + contact.pubkey +
                             " not valid for this update")

        # parse the position of the update and extract it
        payloadthreshold = 69 + (participantcount * 3)
        payloadoffsetcrsr = 69 + (3 * matchidx)
        payloadoffsetbytes = body[payloadoffsetcrsr:payloadoffsetcrsr + 3]
        payloadoffset = struct.unpack("<I", payloadoffsetbytes + "\x00")[0]
        if participantcount - 1 == matchidx:
            ciphermsg = body[69 + (participantcount * 3) + payloadoffset:]
        else:
            payloadoffsetcrsr += 3
            payloadoffsetbytes = body[payloadoffsetcrsr:payloadoffsetcrsr + 3]
            payloadstop = struct.unpack("<I", payloadoffsetbytes + "\x00")[0]
            ciphermsg = body[payloadthreshold +
                             payloadoffset:payloadthreshold + payloadstop]

        return ciphermsg
예제 #5
0
파일: user.py 프로젝트: Mistra/weechat-pss
    def __init__(self, nick, key, addr, src):

        validnick = ""
        validkey = ""
        validaddr = ""

        validnick = clean_nick(nick)
        validkey = clean_pubkey(key)
        if len(addr) > 0:
            validaddr = clean_address(addr)

        self.nick = validnick
        self.key = "0x" + validkey
        self.address = "0x" + validaddr
        self.src = src
예제 #6
0
파일: room.py 프로젝트: Mistra/weechat-pss
    def load(self, hsh, owneraccount=None):
        savedJson = self.bzz.get(hsh.encode("hex"))
        print "savedj " + repr(savedJson)
        self.hsh_room = hsh
        r = json.loads(savedJson)
        self.name = clean_name(r['name'])
        for pubkeyhx in r['participants']:
            acc = Account()
            acc.set_public_key(clean_pubkey(pubkeyhx).decode("hex"))
            nick = acc.address.encode("hex")
            p = Participant(nick, "04" + acc.publickeybytes.encode("hex"),
                            acc.address.encode("hex"), "")
            self.add(nick, p)

        # outgoing feed user is room publisher
        if owneraccount == None:
            owneraccount = self.feed_room.account

        self.feed_out = Feed(self.agent, owneraccount, self.name, True)
        hd = self.feed_out.head()
        if len(hd) == 64:
            self.hsh_out = hd.decode("hex")
예제 #7
0
파일: room.py 프로젝트: nolash/weechat-pss
    def load(self, hsh, owneraccount=None):
        savedJson = self.bzz.get(hsh.encode("hex"))
        #sys.stderr.write("savedj " + repr(savedJson) + " from hash " + hsh.encode("hex") + "\n")
        self.hsh_room = hsh
        r = json.loads(savedJson)
        self.name = clean_name(r['name'])

        # outgoing feed user is room publisher
        if owneraccount == None:
            owneraccount = self.feed_room.account
        senderfeed = Feed(self.bzz, owneraccount,
                          new_topic_mask(self.name, "", "\x06"))
        self.feedcollection = FeedCollection("room:" + self.name, senderfeed)

        for pubkeyhx in r['participants']:
            pubkey = clean_pubkey(pubkeyhx).decode("hex")
            nick = publickey_to_address(pubkey)
            p = Participant(nick.encode("hex"), None)
            p.set_public_key(pubkey)
            try:
                self.add(nick, p, False)
            except Exception as e:
                sys.stderr.write("skipping already added feed: '" +
                                 pubkey.encode("hex"))