comparison sat/plugins/plugin_xep_0234.py @ 3314:5887fb414758

component file sharing: add/parse affiliation when possible
author Goffi <goffi@goffi.org>
date Fri, 17 Jul 2020 13:00:10 +0200
parents f17379123571
children ac9342f359e9
comparison
equal deleted inserted replaced
3313:624c60293deb 3314:5887fb414758
103 """ 103 """
104 return "{}_{}".format(session["id"], content_name) 104 return "{}_{}".format(session["id"], content_name)
105 105
106 # generic methods 106 # generic methods
107 107
108 def buildFileElement(self, name=None, file_hash=None, hash_algo=None, size=None, 108 def buildFileElement(
109 self, client, name=None, file_hash=None, hash_algo=None, size=None,
109 mime_type=None, desc=None, modified=None, transfer_range=None, path=None, 110 mime_type=None, desc=None, modified=None, transfer_range=None, path=None,
110 namespace=None, file_elt=None, **kwargs): 111 namespace=None, file_elt=None, **kwargs):
111 """Generate a <file> element with available metadata 112 """Generate a <file> element with available metadata
112 113
113 @param file_hash(unicode, None): hash of the file 114 @param file_hash(unicode, None): hash of the file
114 empty string to set <hash-used/> element 115 empty string to set <hash-used/> element
115 @param hash_algo(unicode, None): hash algorithm used 116 @param hash_algo(unicode, None): hash algorithm used
116 if file_hash is None and hash_algo is set, a <hash-used/> element will be generated 117 if file_hash is None and hash_algo is set, a <hash-used/> element will be
118 generated
117 @param transfer_range(Range, None): where transfer must start/stop 119 @param transfer_range(Range, None): where transfer must start/stop
118 @param modified(int, unicode, None): date of last modification 120 @param modified(int, unicode, None): date of last modification
119 0 to use current date 121 0 to use current date
120 int to use an unix timestamp 122 int to use an unix timestamp
121 else must be an unicode string which will be used as it (it must be an XMPP time) 123 else must be an unicode string which will be used as it (it must be an XMPP
124 time)
122 @param file_elt(domish.Element, None): element to use 125 @param file_elt(domish.Element, None): element to use
123 None to create a new one 126 None to create a new one
124 @param **kwargs: data for plugin extension (ignored by default) 127 @param **kwargs: data for plugin extension (ignored by default)
125 @return (domish.Element): generated element 128 @return (domish.Element): generated element
126 @trigger XEP-0234_buildFileElement(file_elt, extra_args): can be used to extend elements to add 129 @trigger XEP-0234_buildFileElement(file_elt, extra_args): can be used to extend
130 elements to add
127 """ 131 """
128 if file_elt is None: 132 if file_elt is None:
129 file_elt = domish.Element((NS_JINGLE_FT, "file")) 133 file_elt = domish.Element((NS_JINGLE_FT, "file"))
130 for name, value in ( 134 for name, value in (
131 ("name", name), 135 ("name", name),
157 file_elt.addChild(self._hash.buildHashUsedElt()) 161 file_elt.addChild(self._hash.buildHashUsedElt())
158 else: 162 else:
159 file_elt.addChild(self._hash.buildHashElt(file_hash, hash_algo)) 163 file_elt.addChild(self._hash.buildHashElt(file_hash, hash_algo))
160 elif hash_algo is not None: 164 elif hash_algo is not None:
161 file_elt.addChild(self._hash.buildHashUsedElt(hash_algo)) 165 file_elt.addChild(self._hash.buildHashUsedElt(hash_algo))
162 self.host.trigger.point("XEP-0234_buildFileElement", file_elt, extra_args=kwargs) 166 self.host.trigger.point(
167 "XEP-0234_buildFileElement", client, file_elt, extra_args=kwargs)
163 if kwargs: 168 if kwargs:
164 for kw in kwargs: 169 for kw in kwargs:
165 log.debug("ignored keyword: {}".format(kw)) 170 log.debug("ignored keyword: {}".format(kw))
166 return file_elt 171 return file_elt
167 172
168 def buildFileElementFromDict(self, file_data, **kwargs): 173 def buildFileElementFromDict(self, client, file_data, **kwargs):
169 """like buildFileElement but get values from a file_data dict 174 """like buildFileElement but get values from a file_data dict
170 175
171 @param file_data(dict): metadata to use 176 @param file_data(dict): metadata to use
172 @param **kwargs: data to override 177 @param **kwargs: data to override
173 """ 178 """
174 if kwargs: 179 if kwargs:
175 file_data = file_data.copy() 180 file_data = file_data.copy()
176 file_data.update(kwargs) 181 file_data.update(kwargs)
177 return self.buildFileElement(**file_data) 182 return self.buildFileElement(client, **file_data)
178 183
179 def parseFileElement(self, file_elt, file_data=None, given=False, parent_elt=None, 184 def parseFileElement(
180 keep_empty_range=False,): 185 self, client, file_elt, file_data=None, given=False, parent_elt=None,
186 keep_empty_range=False):
181 """Parse a <file> element and file dictionary accordingly 187 """Parse a <file> element and file dictionary accordingly
182 188
183 @param file_data(dict, None): dict where the data will be set 189 @param file_data(dict, None): dict where the data will be set
184 following keys will be set (and overwritten if they already exist): 190 following keys will be set (and overwritten if they already exist):
185 name, file_hash, hash_algo, size, mime_type, desc, path, namespace, range 191 name, file_hash, hash_algo, size, mime_type, desc, path, namespace, range
266 file_elt 272 file_elt
267 ) 273 )
268 except exceptions.NotFound: 274 except exceptions.NotFound:
269 pass 275 pass
270 276
271 self.host.trigger.point("XEP-0234_parseFileElement", file_elt, file_data) 277 self.host.trigger.point("XEP-0234_parseFileElement", client, file_elt, file_data)
272 278
273 return file_data 279 return file_data
274 280
275 # bridge methods 281 # bridge methods
276 282
421 file_data["size"] = os.path.getsize(filepath) 427 file_data["size"] = os.path.getsize(filepath)
422 if "namespace" in extra: 428 if "namespace" in extra:
423 file_data["namespace"] = extra["namespace"] 429 file_data["namespace"] = extra["namespace"]
424 if "path" in extra: 430 if "path" in extra:
425 file_data["path"] = extra["path"] 431 file_data["path"] = extra["path"]
426 self.buildFileElementFromDict(file_data, file_elt=file_elt, file_hash="") 432 self.buildFileElementFromDict(
433 client, file_data, file_elt=file_elt, file_hash="")
427 else: 434 else:
428 # we request a file 435 # we request a file
429 file_hash = extra.pop("file_hash", "") 436 file_hash = extra.pop("file_hash", "")
430 if not name and not file_hash: 437 if not name and not file_hash:
431 raise ValueError(_("you need to provide at least name or file hash")) 438 raise ValueError(_("you need to provide at least name or file hash"))
438 file_data["hash_algo"] = self._hash.getDefaultAlgo() 445 file_data["hash_algo"] = self._hash.getDefaultAlgo()
439 if "namespace" in extra: 446 if "namespace" in extra:
440 file_data["namespace"] = extra["namespace"] 447 file_data["namespace"] = extra["namespace"]
441 if "path" in extra: 448 if "path" in extra:
442 file_data["path"] = extra["path"] 449 file_data["path"] = extra["path"]
443 self.buildFileElementFromDict(file_data, file_elt=file_elt) 450 self.buildFileElementFromDict(client, file_data, file_elt=file_elt)
444 451
445 return desc_elt 452 return desc_elt
446 453
447 def jingleRequestConfirmation(self, client, action, session, content_name, desc_elt): 454 def jingleRequestConfirmation(self, client, action, session, content_name, desc_elt):
448 """This method request confirmation for a jingle session""" 455 """This method request confirmation for a jingle session"""
472 @defer.inlineCallbacks 479 @defer.inlineCallbacks
473 def _fileSendingRequestConf( 480 def _fileSendingRequestConf(
474 self, client, session, content_data, content_name, file_data, file_elt 481 self, client, session, content_data, content_name, file_data, file_elt
475 ): 482 ):
476 """parse file_elt, and handle file retrieving/permission checking""" 483 """parse file_elt, and handle file retrieving/permission checking"""
477 self.parseFileElement(file_elt, file_data) 484 self.parseFileElement(client, file_elt, file_data)
478 content_data["application_data"]["file_data"] = file_data 485 content_data["application_data"]["file_data"] = file_data
479 finished_d = content_data["finished_d"] = defer.Deferred() 486 finished_d = content_data["finished_d"] = defer.Deferred()
480 487
481 # confirmed_d is a deferred returning confimed value (only used if cont is False) 488 # confirmed_d is a deferred returning confimed value (only used if cont is False)
482 cont, confirmed_d = self.host.trigger.returnPoint( 489 cont, confirmed_d = self.host.trigger.returnPoint(
502 509
503 def _fileReceivingRequestConf( 510 def _fileReceivingRequestConf(
504 self, client, session, content_data, content_name, file_data, file_elt 511 self, client, session, content_data, content_name, file_data, file_elt
505 ): 512 ):
506 """parse file_elt, and handle user permission/file opening""" 513 """parse file_elt, and handle user permission/file opening"""
507 self.parseFileElement(file_elt, file_data, given=True) 514 self.parseFileElement(client, file_elt, file_data, given=True)
508 try: 515 try:
509 hash_algo, file_data["given_file_hash"] = self._hash.parseHashElt(file_elt) 516 hash_algo, file_data["given_file_hash"] = self._hash.parseHashElt(file_elt)
510 except exceptions.NotFound: 517 except exceptions.NotFound:
511 try: 518 try:
512 hash_algo = self._hash.parseHashUsedElt(file_elt) 519 hash_algo = self._hash.parseHashUsedElt(file_elt)