def obtain(uri): """Obtain the resource at the URI.""" uri = hashless(uri) res = None try: res = Resource.objects.get(uri=uri) return # XXX: Don't modify existing. except Resource.DoesNotExist: pass message = comms.get(uri) # XXX TODO: Handle resources. if message.is_image(): res = Image.new_from_message(message, save=True) elif message.is_html(): # TODO/XXX: Try web2feed extraction web = Web2Feed(uri) web.set_contents(message.get_body) feed = web.get_feed() # XXX: TODO #typ = message.get_content_type() #if typ in ['image/jpeg', 'image/png', 'image/gif']: # res = Image.new_from_message(message, save=True) return res
def push_profile_to_node(node_uri): """Send an updated copy of our profile to a specified node.""" print "push_profile_to_node %s" % node_uri node_uri = hashless(node_uri) node = None user = None try: user = User.objects.get(pk=settings.OUR_USER_PK) node = Node.objects.get(uri=node_uri) except User.DoesNotExist: raise Exception, "The main user MUST exist!" except Node.DoesNotExist: raise Exception, "The node requested does not exist." # TODO: Granularized Privacy message = SylphMessage(node_uri) message.set_post('dispatch', 'user_push') message.add(user) response = send(message) if not response.has_errors(): node.just_failed(save=True) print "User profile push failed." # TODO: Error log return node.just_pushed_to(save=True)
def give_key(request): """ Another node is telling us that they are granting us the ability to make privledged transactions with them. They are giving us a key we must store. (The key must be verified.) """ # TODO: THIS IS TERRIBLE! p = request.POST uri = hashless(p['uri']) key_theirs = p['key'] if 'key' in p else None # Lookup or create node. # TODO: For now assume it exists. Must deal with spam/fraud later. node = None try: node = Node.objects.get(uri=uri) except Node.DoesNotExist: node = Node(uri=uri) node.datetime_added = datetime.today() node.is_yet_to_resolve = True #node.save() # Maybe they want to change their key if node.key_theirs and node.key_theirs_confirmed: node.new_key_theirs = key_theirs node.save() # TODO: Schedule a job to confirm the key return node.key_theirs = key_theirs node.key_theirs_confirmed = False node.new_key_theirs = '' node.save()
def get_by_node(request): """ Get information on a user given their node resource URI. See: get(resource). """ if 'uri' not in request.POST: raise Exception, "No URI in post." uri = hashless(request.POST['uri']) try: user = User.objects.get(node__uri=uri) except User.DoesNotExist: raise Exception, "User does not exist." # XXX/TODO/FIXME: Privacy issues!! rs = RdfSerializer(user) return HttpResponse(rs.to_rdf(), mimetype='text/plain')
def get(request): """ Get information on a user given their user resource URI. This isn't authoratative, since we're not looking up the originating source. """ if 'uri' not in request.POST: raise Exception, "No URI in post." uri = hashless(request.POST['uri']) try: user = User.objects.get(uri=uri) except User.DoesNotExist: raise Exception, "User does not exist." # XXX/TODO/FIXME: Privacy issues!! rs = RdfSerializer(user) return HttpResponse(rs.to_rdf(), mimetype='text/plain')
def ask_to_add(request): """Ask to add a node. Just have to provide the node uri. Note: this is very insecure and subject to spamming, thus this should only be considered a temporary solution.""" print request print request.post uri = request.get_post('uri') print uri uri = hashless(uri) # XXX: BAD API DESIGN!! node = None try: node = Node.objects.get(uri=uri) return HttpResponse('ALREADY EXISTS') # TODO BAD except Node.DoesNotExist: pass node = Node(uri=uri) node.datetime_added = datetime.today() node.is_yet_to_resolve = True node.save() tasks.do_add_node_lookup.delay(uri) return HttpResponse('ACK')
def ask_key(request): """ Another node is asking us for a key so they can make priveledged transactions with us. They'll have to verify this with us. """ # TODO: THIS IS TERRIBLE! p = request.POST uri = hashless(p['uri']) #key_theirs = p['key'] if 'key' in p else None # Lookup or create node. # TODO: For now assume it exists. Must deal with spam/fraud later. node = None try: node = Node.objects.get(uri=uri) except Node.DoesNotExist: node = Node(uri=uri) node.datetime_added = datetime.today() node.is_yet_to_resolve = True #node.save() # XXX: We cannot give them the key in this transaction as anyone could # be making it. We'll have to request the node over HTTP. key = Node.generate_key() # Maybe they want a new key if node.key_ours and node.key_ours_confirmed: node.new_key_ours = key node.save() # TODO: Schedule a job to confirm the key return node.key_ours = key node.key_ours_confirmed = False node.new_key_ours = '' node.save()
def add_node(uri): """ Add Node Resolves the remote node and notifies it that we'll be adding them to our list of tracked nodes. (They are free to add us.) """ uri = hashless(uri) node = None try: node = Node.objects.get(uri=uri) except Node.DoesNotExist: # XXX: Technically, it should already be in the DB node = Node(uri=uri) node.datetime_added = datetime.today() node.is_yet_to_resolve = True node.status = 'U' node.save() # Now let's exchange doorbell key info... state = node.doorbell_state if not node.dispatch_key_ours: node.generate_key() node.save() if state not in ['0', '1', '2', '3']: raise Exception, "State is non-normative" # XXX ERROR LOG post = {'dispatch': 'node_add'} new_state = None # We're contacting them first. if state == '0': post['key_ours'] = node.dispatch_key_ours new_state = '1' # We're responding to their request. if state == '1': post['key_ours'] = node.display_key_ours post['key_yours'] = node.display_key_theirs new_state = '3' print "TODO"*50 return comm = Communicator(node.uri) ret = comm.send_post(post) time = datetime.today() if not ret or ret == 'NACK': print "No communication return data!!" # TODO: Error log node.status = 'EERR' # TODO node.datetime_last_failed = time node.save() return node.is_yet_to_resolve = False node.datetime_last_resolved = time node.datetime_last_pulled_from = time node.doorbell_state = new_state node.save() # Unknown other state... raise Exception, "Unknown state."
def old_add_DEPRECATED(request): """... giving us a key to The other node is asking that we mutually form a connection. This usually means that both endpoints will add each other. 1. NodeA (add)--> NodeB 2. NodeA <---(add) NodeB (Note: this could be maliciously taken advantage of.) FIXME. POSTDATA: * node = uri * key_ours = ... * key_yours = ... """ if not request.method != 'POST': raise Exception, "Not a post..." # TODO: THIS IS TERRIBLE! p = request.POST uri = hashless(p['uri']) key_ours = p['key_theirs'] if 'key_theirs' in p else None key_theirs = p['key_yours'] if 'key_yours' in p else None # Lookup or create node. # TODO: For now assume it exists. Deal with spam later. node = None try: node = Node.objects.get(uri=uri) except Node.DoesNotExist: node = Node(uri=uri) node.datetime_added = datetime.today() node.is_yet_to_resolve = True node.save() #tasks.do_add_node_lookup.delay(form.cleaned_data['uri']) state = node.doorbell_status if state not in ['0', '1', '2', '3']: raise Exception, "Invalid state." # They're asking to add us first if state == '0': node.key_theirs = key_theirs node.generate_key() node.doorbell_status = '2' node.save() return HttpResponse('ACK') # TODO: Acknowledge # They're responding back to our request to add them. if state == '1': if node.key_ours != key_ours: raise Exception, "Key does not match." node.key_theirs = key_theirs node.doorbell_status = 3 node.is_yet_to_resolve = False node.save() return HttpResponse('ACK') # TODO: Acknowledge # Both keys have been generated, but perhaps they're asking to # change them. # TODO pass