def _pull_vcard_listing(self, socket, request, decoded_header): mch_size = 0 # mch_size for phonebook folder (Don't optimize) abs_name = self.vfolder.join(self.vfolder.curdir, decoded_header["Name"]) logger.info("Absolute path of requested vcard_listing object = %s", abs_name) app_params = self._decode_app_params(decoded_header.get("App_Parameters", {})) if not self.vfolder.isdir(abs_name): logger.error("Requested vcard-listing dir doesn't exists") self.send_response(socket, responses.Not_Found()) else: phonebook_size = self.vfolder.count(abs_name) search_query = self._get_search_query(app_params["SearchAttribute"], app_params["SearchValue"]) # TODO: sorting based on order not works, since handle is not part of db record. it is not proper # make the "handle" as part of db record sort_key = self._get_sort_key(app_params["Order"]) vcard_list = self.vfolder.listdir(abs_name, query=search_query) vcard_list = self._sort_vcard_list(vcard_list, sort_key) if app_params["MaxListCount"] == 0: self._respond_phonebook_size(socket, phonebook_size) return data = "" res_vcard_list = self._limit_phonebook(vcard_list, app_params["MaxListCount"], app_params["ListStartOffset"]) res_vcard_list_range = range(app_params["ListStartOffset"], min((app_params["ListStartOffset"] + app_params["MaxListCount"]), 65535)) # "NewMissedCalls": This application parameter shall be used in the response when and only when the # phone book object is mch. It indicates the number of missed calls that have been # received on the PSE since the last PullPhoneBook request on the mch folder, at the # point of the request. if "mch" in abs_name: response_dict = {'NewMissedCalls': headers.NewMissedCalls(phonebook_size - mch_size)} mch_size = phonebook_size else: response_dict = {} vcard_listing_object_tmpl = ('<?xml version="1.0"?>\r\n' '<!DOCTYPE vcard-listing SYSTEM "vcard-listing.dtd">\r\n' '<vCard-listing version="1.0">\r\n' '{cards}' '</vCard-listing>\r\n') card_tag_tmpl = '<card handle="{handle}" name="{name}"/>\r\n' cards = "" # TODO: As per spec the handles should be hex?? for index, vcard in zip(res_vcard_list_range, res_vcard_list): cards += card_tag_tmpl.format(handle="{}.vcf".format(index), name=self._get_param_values(vcard, "N")) data = vcard_listing_object_tmpl.format(cards=cards) logger.debug("Sending response success with following data") logger.debug("vcard-listing data: \r\n%s", data) self.send_response(socket, responses.Success(), [headers.End_Of_Body(data), headers.App_Parameters(response_dict)])
def _pull_vcard_entry(self, socket, request, decoded_header): abs_name = self.vfolder.join(self.vfolder.curdir, decoded_header["Name"]) logger.info("Absolute path of requested vcard_entry object = %s", abs_name) app_params = self._decode_app_params(decoded_header.get("App_Parameters", {})) if not self.vfolder.isfile(abs_name): logger.error("Requested vcard file doesn't exists") self.send_response(socket, responses.Not_Found()) else: filtered_data = self._filter_attributes(app_params["Filter"], self.vfolder.read(abs_name), app_params["Format"]) data = VCard(filtered_data, parsed=True).serialize(app_params["Format"]) logger.debug("Sending response success with following data") logger.debug("vcard data: \r\n%s", data) self.send_response(socket, responses.Success(), [ headers.End_Of_Body(data)])
def _pull_phonebook(self, socket, request, decoded_header): mch_size = 0 # mch_size for phonebook folder (Don't optimize) abs_name = self.vfolder.join(self.vfolder.curdir, decoded_header["Name"]) logger.info("Absolute path of requested phonebook object = %s", abs_name) app_params = self._decode_app_params(decoded_header.get("App_Parameters", {})) if not self.vfolder.isfile(abs_name): logger.error("Requested phonebook file doesn't exists") self.send_response(socket, responses.Not_Found()) else: phonebook_size = self.vfolder.count(os.path.splitext(abs_name)[0]) vcard_list = self.vfolder.listdir(os.path.splitext(abs_name)[0]) if app_params["MaxListCount"] == 0: self._respond_phonebook_size(socket, phonebook_size) return data = "" res_vcard_list = self._limit_phonebook(vcard_list, app_params["MaxListCount"], app_params["ListStartOffset"]) # "NewMissedCalls": This application parameter shall be used in the response when and only when the # phone book object is mch. It indicates the number of missed calls that have been # received on the PSE since the last PullPhoneBook request on the mch folder, at the # point of the request. if "mch" in abs_name: response_dict = {'NewMissedCalls': headers.NewMissedCalls(phonebook_size - mch_size)} mch_size = phonebook_size else: response_dict = {} for item in res_vcard_list: filtered_data = self._filter_attributes(app_params["Filter"], item, app_params["Format"]) data += VCard(filtered_data, parsed=True).serialize(app_params["Format"]) logger.debug("Sending response success with following data") logger.debug("phonebook data: \r\n%s", data) # Sends the chunked response # TODO: This needs to be handled properly in pyobex: server.py: send_response bytes_transferred = 0 datasize = len(data) # ideally max data length per packet should be as follows # max_datalen = self._max_length() - Message().minimum_length # but because of some unknown reasons we could only able to transmit ~700 bytes # TODO: figure out exactly what is the reason max_datalen = 700 if datasize < max_datalen: data_last_chunk = data else: while bytes_transferred < datasize: data_chunk = data[bytes_transferred: (bytes_transferred + max_datalen)] header_list = [headers.App_Parameters(response_dict), headers.Body(data_chunk)] # 'continue' response and process the subsequent requests self.send_response(socket, responses.Continue(), header_list) while True: request = self.request_handler.decode(self.connection) if not isinstance(request, requests.Get_Final): self.process_request(self.connection, request) continue else: break bytes_transferred += max_datalen data_last_chunk = "" header_list = [headers.App_Parameters(response_dict), headers.End_Of_Body(data_last_chunk)] self.send_response(socket, responses.Success(), header_list)