예제 #1
0
    def setpath(self, socket, request):
        decoded_header = self._decode_header_data(request)
        createdir = not bool(request.flags & request.DontCreateDir)
        toparent = bool(request.flags & request.NavigateToParent)
        logger.info("createdir = %r" % createdir)
        logger.info("toparent = %r" % toparent)
        # TODO: set_phonebook, to_root is not yet supported
        # This is just a overloaded version of obex setpath
        if toparent:
            if self.vfolder.curdir == self.vfolder.rootdir:
                logger.error(
                    "Current directory is same as Root dir, so can't go to parent"
                )
                self.send_response(socket, responses.Forbidden())
                return
            else:
                self.vfolder.curdir = self.vfolder.join(
                    self.vfolder.curdir, "..")
                logger.info("Setting current directory = %s",
                            self.vfolder.curdir)
                if decoded_header["Name"] == "":
                    logger.debug("Sending response success")
                    self.send_response(socket, responses.Success())
                    return

        requested_dir = self.vfolder.join(self.vfolder.curdir,
                                          decoded_header["Name"])
        if createdir:
            if self.vfolder.isdir(requested_dir):
                logger.error(
                    "Requested path already exists, so can't create it again.")
                self.send_response(socket, responses.Precondition_Failed())
                return
            else:
                logger.info("Creating new directory = %s", requested_dir)
                self.vfolder.makedirs(requested_dir)
                self.vfolder.curdir = requested_dir
                logger.info("Setting current directory = %s",
                            self.vfolder.curdir)
                logger.debug("Sending response success")
                self.send_response(socket, responses.Success())
                return

        if not self.vfolder.isdir(requested_dir):
            logger.error(
                "Requested path not exists, so can't set it to current dir")
            self.send_response(socket, responses.Precondition_Failed())
            return
        else:
            self.vfolder.curdir = requested_dir
            logger.info("Setting current directory = %s", self.vfolder.curdir)
            logger.debug("Sending response success")
            self.send_response(socket, responses.Success())
            return
예제 #2
0
 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)])
예제 #3
0
 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)])
예제 #4
0
    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)
예제 #5
0
    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)
            ])