comparison sat/plugins/plugin_xep_0329.py @ 3362:02583a401e51

plugin XEP-0329: fix conflict check + directory creation in `onComponentCreateDir`
author Goffi <goffi@goffi.org>
date Thu, 17 Sep 2020 21:41:19 +0200
parents 57afccb91961
children 626046010a2d
comparison
equal deleted inserted replaced
3361:43e60c40de65 3362:02583a401e51
1096 f"path: {path}\nnamespace: {namespace!r}" 1096 f"path: {path}\nnamespace: {namespace!r}"
1097 ) 1097 )
1098 client.sendError(iq_elt, 'forbidden', "You can't create a directory there") 1098 client.sendError(iq_elt, 'forbidden', "You can't create a directory there")
1099 return 1099 return
1100 # when going further into the path, the permissions will be checked by getFiles 1100 # when going further into the path, the permissions will be checked by getFiles
1101 try: 1101 if len(path.parts) > 2:
1102 await self.host.memory.getFiles( 1102 files_data = await self.host.memory.getFiles(
1103 client, 1103 client,
1104 peer_jid=peer_jid, 1104 peer_jid=peer_jid,
1105 path=path.parent, 1105 path=path.parent,
1106 name=path.name,
1107 namespace=namespace, 1106 namespace=namespace,
1108 owner=owner, 1107 owner=owner,
1109 ) 1108 )
1110 except exceptions.NotFound: 1109 if path.name in [d['name'] for d in files_data]:
1111 # this is expected, nothing should be found at this path 1110 log.warning(
1112 pass 1111 f"Conflict when trying to create a directory (from: {peer_jid} "
1113 else: 1112 f"namespace: {namespace!r} path: {path!r})"
1114 log.warning( 1113 )
1115 f"Conflict when trying to create a directory (from: {peer_jid} " 1114 client.sendError(
1116 f"namespace: {namespace!r} path: {path!r})" 1115 iq_elt, 'conflict', "there is already a file or dir at this path")
1117 ) 1116 return
1118 client.sendError(
1119 iq_elt, 'conflict', "there is already a file or dir at this path")
1120 return
1121 1117
1122 try: 1118 try:
1123 configuration_elt = next( 1119 configuration_elt = next(
1124 create_dir_elt.elements(NS_FIS_CONFIGURATION, 'configuration')) 1120 create_dir_elt.elements(NS_FIS_CONFIGURATION, 'configuration'))
1125 except StopIteration: 1121 except StopIteration:
1126 configuration_elt = None 1122 configuration_elt = None
1127 1123
1128 await self.host.memory.setFile(client, path.name, path=path.parent, namespace=namespace, owner=owner, peer_jid=peer_jid) 1124 await self.host.memory.setFile(
1125 client,
1126 path.name,
1127 path=path.parent,
1128 type_=C.FILE_TYPE_DIRECTORY,
1129 namespace=namespace,
1130 owner=owner,
1131 peer_jid=peer_jid
1132 )
1129 1133
1130 if configuration_elt is not None: 1134 if configuration_elt is not None:
1131 file_data = (await self.host.memory.getFiles( 1135 file_data = (await self.host.memory.getFiles(
1132 client, 1136 client,
1133 peer_jid=peer_jid, 1137 peer_jid=peer_jid,