def notify(self, plaintext, signature): try: success = self.db.messages.save_message(plaintext.sender_guid.encode("hex"), plaintext.handle, plaintext.pubkey, plaintext.subject, PlaintextMessage.Type.Name(plaintext.type), plaintext.message, plaintext.timestamp, plaintext.avatar_hash, signature, False) if plaintext.subject != "": self.db.purchases.update_unread(plaintext.subject) self.db.sales.update_unread(plaintext.subject) self.db.cases.update_unread(plaintext.subject) if success: message_json = { "message": { "sender": plaintext.sender_guid.encode("hex"), "subject": plaintext.subject, "message_type": PlaintextMessage.Type.Name(plaintext.type), "message": plaintext.message, "timestamp": plaintext.timestamp, "avatar_hash": plaintext.avatar_hash.encode("hex"), "public_key": plaintext.pubkey.encode("hex") } } if plaintext.handle: message_json["message"]["handle"] = plaintext.handle self.ws.push(json.dumps(sanitize_html(message_json), indent=4)) except Exception: pass
def parse_profile(profile, node): if profile is not None: # TODO: should check signatures here before entering in database self.factory.db.moderators.save_moderator( node.id.encode("hex"), node.pubkey, profile.bitcoin_key.public_key, profile.bitcoin_key.signature, profile.name, profile.avatar_hash, profile.moderation_fee, profile.handle, profile.short_description) moderator = { "id": message_id, "moderator": { "guid": node.id.encode("hex"), "name": profile.name, "handle": profile.handle, "short_description": profile.short_description, "avatar_hash": profile.avatar_hash.encode("hex"), "about": profile.about, "fee": profile.moderation_fee } } self.transport.write( json.dumps(sanitize_html(moderator), indent=4)) else: self.factory.db.moderators.delete_moderator(node.id)
def notify(self, plaintext, signature): try: success = self.db.messages.save_message(plaintext.sender_guid.encode("hex"), plaintext.handle, plaintext.pubkey, plaintext.subject, PlaintextMessage.Type.Name(plaintext.type), plaintext.message, plaintext.timestamp, plaintext.avatar_hash, signature, False) # TODO: should probably resolve the handle and make sure it matches the guid if success: message_json = { "message": { "sender": plaintext.sender_guid.encode("hex"), "subject": plaintext.subject, "message_type": PlaintextMessage.Type.Name(plaintext.type), "message": plaintext.message, "timestamp": plaintext.timestamp, "avatar_hash": plaintext.avatar_hash.encode("hex"), "public_key": plaintext.pubkey.encode("hex") } } if plaintext.handle: message_json["message"]["handle"] = plaintext.handle self.ws.push(json.dumps(sanitize_html(message_json), indent=4)) except Exception: pass
def notify(self, plaintext, signature): try: success = self.db.messages.save_message( plaintext.sender_guid.encode("hex"), plaintext.handle, plaintext.pubkey, plaintext.subject, PlaintextMessage.Type.Name(plaintext.type), plaintext.message, plaintext.timestamp, plaintext.avatar_hash, signature, False) # TODO: should probably resolve the handle and make sure it matches the guid if success: message_json = { "message": { "sender": plaintext.sender_guid.encode("hex"), "subject": plaintext.subject, "message_type": PlaintextMessage.Type.Name(plaintext.type), "message": plaintext.message, "timestamp": plaintext.timestamp, "avatar_hash": plaintext.avatar_hash.encode("hex"), "public_key": plaintext.pubkey.encode("hex") } } if plaintext.handle: message_json["message"]["handle"] = plaintext.handle self.ws.push(json.dumps(sanitize_html(message_json), indent=4)) except Exception: pass
def notify(self, guid, message): # pull the metadata for this node from the db f = Following() ser = self.db.follow.get_following() if ser is not None: f.ParseFromString(ser) for user in f.users: if user.guid == guid: avatar_hash = user.metadata.avatar_hash handle = user.metadata.handle timestamp = int(time.time()) broadcast_id = digest(random.getrandbits(255)).encode("hex") self.db.broadcasts.save_broadcast(broadcast_id, guid.encode("hex"), handle, message, timestamp, avatar_hash) broadcast_json = { "broadcast": { "id": broadcast_id, "guid": guid.encode("hex"), "handle": handle, "message": message, "timestamp": timestamp, "avatar_hash": avatar_hash.encode("hex") } } self.ws.push(json.dumps(sanitize_html(broadcast_json), indent=4))
def notify(self, plaintext, signature): try: success = self.db.messages.save_message( plaintext.sender_guid.encode("hex"), plaintext.handle, plaintext.pubkey, plaintext.subject, PlaintextMessage.Type.Name(plaintext.type), plaintext.message, plaintext.timestamp, plaintext.avatar_hash, signature, False) if plaintext.subject != "": self.db.purchases.update_unread(plaintext.subject) self.db.sales.update_unread(plaintext.subject) self.db.cases.update_unread(plaintext.subject) if success: message_json = { "message": { "sender": plaintext.sender_guid.encode("hex"), "subject": plaintext.subject, "message_type": PlaintextMessage.Type.Name(plaintext.type), "message": plaintext.message, "timestamp": plaintext.timestamp, "avatar_hash": plaintext.avatar_hash.encode("hex"), "public_key": plaintext.pubkey.encode("hex") } } if plaintext.handle: message_json["message"]["handle"] = plaintext.handle self.ws.push(json.dumps(sanitize_html(message_json), indent=4)) except Exception as e: self.log.error('Market.Listener.notify Exception: %s' % e)
def handle_response(listings, node): count = 0 if listings is not None: for l in listings.listing: try: if l.contract_hash not in self.factory.outstanding_listings[message_id]: listing_json = { "id": message_id, "listing": { "guid": node.id.encode("hex"), "handle": listings.handle, "avatar_hash": listings.avatar_hash.encode("hex"), "title": l.title, "contract_hash": l.contract_hash.encode("hex"), "thumbnail_hash": l.thumbnail_hash.encode("hex"), "category": l.category, "price": l.price, "currency_code": l.currency_code, "nsfw": l.nsfw, "origin": str(CountryCode.Name(l.origin)), "ships_to": [] } } if l.contract_type != 0: listing_json["contract_type"] = str(Listings.ContractType.Name(l.contract_type)) for country in l.ships_to: listing_json["listing"]["ships_to"].append(str(CountryCode.Name(country))) if not os.path.isfile(os.path.join( \ DATA_FOLDER, 'cache', l.thumbnail_hash.encode("hex"))): self.factory.mserver.get_image(node, l.thumbnail_hash) if not os.path.isfile(os.path.join( \ DATA_FOLDER, 'cache', listings.avatar_hash.encode("hex"))): self.factory.mserver.get_image(node, listings.avatar_hash) self.transport.write(json.dumps(sanitize_html(listing_json), indent=4)) count += 1 self.factory.outstanding_listings[message_id].append(l.contract_hash) if count == 3: break except Exception: pass if node.id in vendors: del vendors[node.id] else: if node.id in vendors: del vendors[node.id] if node.id in self.factory.mserver.protocol.multiplexer.vendors: del self.factory.mserver.protocol.multiplexer.vendors[node.id] self.factory.db.vendors.delete_vendor(node.id.encode("hex")) if only_following: vendor_list = get_following_from_vendors(vendors) else: vendor_list = vendors.values() if len(vendor_list) > 0: shuffle(vendor_list) node_to_ask = vendor_list[0] if node_to_ask is not None: self.factory.mserver.get_listings(node_to_ask).addCallback(handle_response, node_to_ask)
def get_conversations(self): """ Get all 'conversations' composed of messages of type 'CHAT'. Returns: Array of dictionaries, one element for each guid. Dictionaries include last message only. """ conn = Database.connect_database(self.PATH) cursor = conn.cursor() cursor.execute('''SELECT DISTINCT guid FROM messages''', ) guids = cursor.fetchall() ret = [] unread = self.get_unread() for g in guids: cursor.execute( '''SELECT avatarHash, message, max(timestamp), pubkey FROM messages WHERE guid=? and messageType=?''', (g[0], "CHAT")) val = cursor.fetchone() avatar_hash = None handle = "" if val[0] is not None: try: with open(join(DATA_FOLDER, 'cache', g[0] + ".profile"), "r") as filename: profile = filename.read() p = objects.Profile() p.ParseFromString(profile) avatar_hash = p.avatar_hash.encode("hex") handle = p.handle except Exception: cursor.execute( '''SELECT avatarHash FROM messages WHERE guid=? and messageType=? and avatarHash NOT NULL''', (g[0], "CHAT")) avi = cursor.fetchone() if avi[0] is not None: avatar_hash = avi[0].encode("hex") ret.append({ "guid": g[0], "avatar_hash": avatar_hash, "handle": handle, "last_message": val[1], "timestamp": val[2], "public_key": val[3].encode("hex"), "unread": 0 if g[0] not in unread else unread[g[0]] }) conn.close() return sanitize_html(ret)
def notify(self, guid, handle, notif_type, order_id, title, image_hash): timestamp = int(time.time()) notif_id = digest(random.getrandbits(255)).encode("hex") self.db.notifications.save_notification(notif_id, guid.encode("hex"), handle, notif_type, order_id, title, timestamp, image_hash) notification_json = { "notification": { "id": notif_id, "guid": guid.encode("hex"), "handle": handle, "type": notif_type, "order_id": order_id, "title": title, "timestamp": timestamp, "image_hash": image_hash.encode("hex") } } self.ws.push(json.dumps(sanitize_html(notification_json), indent=4))
def get_conversations(self): """ Get all 'conversations' composed of messages of type 'CHAT'. Returns: Array of dictionaries, one element for each guid. Dictionaries include last message only. """ conn = Database.connect_database(self.PATH) cursor = conn.cursor() cursor.execute('''SELECT DISTINCT guid FROM messages''',) guids = cursor.fetchall() ret = [] unread = self.get_unread() for g in guids: cursor.execute('''SELECT avatarHash, message, max(timestamp), pubkey FROM messages WHERE guid=? and messageType=?''', (g[0], "CHAT")) val = cursor.fetchone() avatar_hash = None handle = "" if val[0] is not None: try: with open(join(DATA_FOLDER, 'cache', g[0] + ".profile"), "r") as filename: profile = filename.read() p = objects.Profile() p.ParseFromString(profile) avatar_hash = p.avatar_hash.encode("hex") handle = p.handle except Exception: cursor.execute('''SELECT avatarHash FROM messages WHERE guid=? and messageType=? and avatarHash NOT NULL''', (g[0], "CHAT")) avi = cursor.fetchone() if avi[0] is not None: avatar_hash = avi[0].encode("hex") ret.append({"guid": g[0], "avatar_hash": avatar_hash, "handle": handle, "last_message": val[1], "timestamp": val[2], "public_key": val[3].encode("hex"), "unread": 0 if g[0] not in unread else unread[g[0]]}) conn.close() return sanitize_html(ret)
def respond(l, node): if l is not None: listing_json = { "id": message_id, "listing": { "guid": node.id.encode("hex"), "title": l.title, "contract_hash": l.contract_hash.encode("hex"), "thumbnail_hash": l.thumbnail_hash.encode("hex"), "category": l.category, "price": l.price, "currency_code": l.currency_code, "nsfw": l.nsfw, "origin": str(CountryCode.Name(l.origin)), "ships_to": [], "avatar_hash": l.avatar_hash.encode("hex"), "handle": l.handle } } for country in l.ships_to: listing_json["listing"]["ships_to"].append(str(CountryCode.Name(country))) self.transport.write(json.dumps(sanitize_html(listing_json), indent=4))
def handle_response(metadata, node): to_query.remove(node) if metadata is not None: vendor = { "id": message_id, "vendor": { "guid": node.id.encode("hex"), "name": metadata.name, "short_description": metadata.short_description, "handle": metadata.handle, "avatar_hash": metadata.avatar_hash.encode("hex"), "nsfw": metadata.nsfw } } self.transport.write(json.dumps(sanitize_html(vendor), indent=4)) queried.append(node.id) return True else: if node.id in self.factory.mserver.protocol.multiplexer.vendors: del self.factory.mserver.protocol.multiplexer.vendors[node.id] self.factory.db.vendors.delete_vendor(node.id.encode("hex")) return False
def handle_response(metadata, node): to_query.remove(node) if metadata is not None: vendor = { "id": message_id, "vendor": { "guid": node.id.encode("hex"), "name": metadata.name, "short_description": metadata.short_description, "handle": metadata.handle, "avatar_hash": metadata.avatar_hash.encode("hex"), "nsfw": metadata.nsfw } } self.transport.write( json.dumps(sanitize_html(vendor), indent=4)) queried.append(node.id) return True else: if node.id in self.factory.mserver.protocol.multiplexer.vendors: del self.factory.mserver.protocol.multiplexer.vendors[ node.id] self.factory.db.vendors.delete_vendor(node.id.encode("hex")) return False
def respond(l, node): if l is not None: listing_json = { "id": message_id, "listing": { "guid": node.id.encode("hex"), "title": l.title, "contract_hash": l.contract_hash.encode("hex"), "thumbnail_hash": l.thumbnail_hash.encode("hex"), "category": l.category, "price": l.price, "currency_code": l.currency_code, "nsfw": l.nsfw, "origin": str(CountryCode.Name(l.origin)), "ships_to": [], "avatar_hash": l.avatar_hash.encode("hex"), "handle": l.handle } } for country in l.ships_to: listing_json["listing"]["ships_to"].append( str(CountryCode.Name(country))) self.transport.write( json.dumps(sanitize_html(listing_json), indent=4))
def parse_profile(profile, node): if profile is not None: # TODO: should check signatures here before entering in database self.factory.db.moderators.save_moderator(node.id.encode("hex"), node.pubkey, profile.bitcoin_key.public_key, profile.bitcoin_key.signature, profile.name, profile.avatar_hash, profile.moderation_fee, profile.handle, profile.short_description) moderator = { "id": message_id, "moderator": { "guid": node.id.encode("hex"), "name": profile.name, "handle": profile.handle, "short_description": profile.short_description, "avatar_hash": profile.avatar_hash.encode("hex"), "about": profile.about, "fee": profile.moderation_fee } } self.transport.write(json.dumps(sanitize_html(moderator), indent=4)) else: self.factory.db.moderators.delete_moderator(node.id)
def push_ws(self, json_obj): self.ws.push(json.dumps(sanitize_html(json_obj), indent=4))
def handle_response(listings, node): count = 0 if listings is not None: for l in listings.listing: try: if l.contract_hash not in self.factory.outstanding_listings[ message_id]: listing_json = { "id": message_id, "listing": { "guid": node.id.encode("hex"), "handle": listings.handle, "avatar_hash": listings.avatar_hash.encode("hex"), "title": l.title, "contract_hash": l.contract_hash.encode("hex"), "thumbnail_hash": l.thumbnail_hash.encode("hex"), "category": l.category, "price": l.price, "currency_code": l.currency_code, "nsfw": l.nsfw, "origin": str(CountryCode.Name(l.origin)), "ships_to": [] } } if l.contract_type != 0: listing_json["contract_type"] = str( Listings.ContractType.Name( l.contract_type)) for country in l.ships_to: listing_json["listing"]["ships_to"].append( str(CountryCode.Name(country))) if not os.path.isfile(os.path.join( \ DATA_FOLDER, 'cache', l.thumbnail_hash.encode("hex"))): self.factory.mserver.get_image( node, l.thumbnail_hash) if not os.path.isfile(os.path.join( \ DATA_FOLDER, 'cache', listings.avatar_hash.encode("hex"))): self.factory.mserver.get_image( node, listings.avatar_hash) self.transport.write( json.dumps(sanitize_html(listing_json), indent=4)) count += 1 self.factory.outstanding_listings[ message_id].append(l.contract_hash) if count == 3: break except Exception: pass if node.id in vendors: del vendors[node.id] else: if node.id in vendors: del vendors[node.id] if node.id in self.factory.mserver.protocol.multiplexer.vendors: del self.factory.mserver.protocol.multiplexer.vendors[ node.id] self.factory.db.vendors.delete_vendor( node.id.encode("hex")) if only_following: vendor_list = get_following_from_vendors(vendors) else: vendor_list = vendors.values() if len(vendor_list) > 0: shuffle(vendor_list) node_to_ask = vendor_list[0] if node_to_ask is not None: self.factory.mserver.get_listings( node_to_ask).addCallback(handle_response, node_to_ask)