def pull_vcard_listing(self, name, order=0, search_value=None, search_attribute="N", max_list_count=65535, list_startoffset=0): """Retrieves phonebook listing object from current folder""" logger.info("Requesting pull_vcard_listing with parameters %s", str(locals())) data = { "Order": headers.Order(order), "MaxListCount": headers.MaxListCount(max_list_count), "ListStartOffset": headers.ListStartOffset(list_startoffset) } if search_value is not None and search_attribute is not None: data.update({ "SearchValue": headers.SearchValue(search_value), "SearchAttribute": headers.SearchAttribute(search_attribute), }) application_parameters = headers.App_Parameters(data, encoded=False) header_list = [headers.Type("x-bt/vcard-listing")] if application_parameters.data: header_list.append(application_parameters) response = self.get(name, header_list) if not isinstance(response, tuple) and isinstance( response, responses.FailureResponse): logger.error( "pull_vcard_listing failed for pbobject '%s'. reason = %s", name, response) return return response
def pull_phonebook(self, name, filter_=0, format_=0, max_list_count=65535, list_startoffset=0): """Retrieves entire phonebook object from current folder""" logger.info( "Requesting pull_phonebook for pbobject '%s' with appl parameters %s", name, str(locals())) data = { "Filter": headers.Filter(filter_), "Format": headers.Format(format_), "MaxListCount": headers.MaxListCount(max_list_count), "ListStartOffset": headers.ListStartOffset(list_startoffset) } application_parameters = headers.App_Parameters(data, encoded=False) header_list = [headers.Type("x-bt/phonebook")] if application_parameters.data: header_list.append(application_parameters) response = self.get(name, header_list) if not isinstance(response, tuple) and isinstance( response, responses.FailureResponse): logger.error( "pull_phonebook failed for pbobject '%s'. reason = %s", name, response) return return response
def _respond_phonebook_size(self, socket, phonebook_size): # MaxListCount = 0 signifies to the PSE that the PCE wants to know the number of used # indexes in the phone book of interest. # When MaxListCount = 0, the PSE shall ignore all other application parameters that may # be present in the request. The response shall include the PhonebookSize application # parameter (see Section 5.1.4.5). The response shall not contain any Body header logger.debug("MaxListCount is 0, so responding with PhonebookSize = {}".format( phonebook_size)) response_dict = {'PhonebookSize': headers.PhonebookSize(phonebook_size)} self.send_response(socket, responses.Success(), [ headers.App_Parameters(response_dict)])
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, name, filter_=0, format_=0): """Retrieves specific vcard from pbap server""" logger.info( "Requesting pull_vcard_entry for pbobject with parameters %s", str(locals())) data = { "Filter": headers.Filter(filter_), "Format": headers.Format(format_) } application_parameters = headers.App_Parameters(data, encoded=False) header_list = [headers.Type("x-bt/vcard")] if application_parameters.data: header_list.append(application_parameters) response = self.get(name, header_list) if not isinstance(response, tuple) and isinstance( response, responses.FailureResponse): logger.error( "pull_vcard_entry failed for pbobject '%s'. reason = %s", name, response) return return response
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)