comparison libervia/backend/plugins/plugin_xep_0292.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 4b842c1fb686
children
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
49 C.PI_DESCRIPTION: _("""XEP-0292 (vCard4 Over XMPP) implementation"""), 49 C.PI_DESCRIPTION: _("""XEP-0292 (vCard4 Over XMPP) implementation"""),
50 } 50 }
51 51
52 NS_VCARD4 = "urn:ietf:params:xml:ns:vcard-4.0" 52 NS_VCARD4 = "urn:ietf:params:xml:ns:vcard-4.0"
53 VCARD4_NODE = "urn:xmpp:vcard4" 53 VCARD4_NODE = "urn:xmpp:vcard4"
54 text_fields = { 54 text_fields = {"fn": "name", "nickname": "nicknames", "note": "description"}
55 "fn": "name", 55 text_fields_inv = {v: k for k, v in text_fields.items()}
56 "nickname": "nicknames",
57 "note": "description"
58 }
59 text_fields_inv = {v: k for k,v in text_fields.items()}
60 56
61 57
62 class XEP_0292: 58 class XEP_0292:
63 namespace = NS_VCARD4 59 namespace = NS_VCARD4
64 node = VCARD4_NODE 60 node = VCARD4_NODE
72 # from a future version of the XEP. 68 # from a future version of the XEP.
73 log.info(_("vCard4 Over XMPP initialization")) 69 log.info(_("vCard4 Over XMPP initialization"))
74 host.register_namespace("vcard4", NS_VCARD4) 70 host.register_namespace("vcard4", NS_VCARD4)
75 self.host = host 71 self.host = host
76 self._p = host.plugins["XEP-0060"] 72 self._p = host.plugins["XEP-0060"]
77 self._i = host.plugins['IDENTITY'] 73 self._i = host.plugins["IDENTITY"]
78 self._i.register( 74 self._i.register(
79 IMPORT_NAME, 75 IMPORT_NAME,
80 'nicknames', 76 "nicknames",
81 partial(self.getValue, field="nicknames"), 77 partial(self.getValue, field="nicknames"),
82 partial(self.set_value, field="nicknames"), 78 partial(self.set_value, field="nicknames"),
83 priority=1000 79 priority=1000,
84 ) 80 )
85 self._i.register( 81 self._i.register(
86 IMPORT_NAME, 82 IMPORT_NAME,
87 'description', 83 "description",
88 partial(self.getValue, field="description"), 84 partial(self.getValue, field="description"),
89 partial(self.set_value, field="description"), 85 partial(self.set_value, field="description"),
90 priority=1000 86 priority=1000,
91 ) 87 )
92 88
93 def get_handler(self, client): 89 def get_handler(self, client):
94 return XEP_0292_Handler() 90 return XEP_0292_Handler()
95 91
104 if metadata_elt.text is not None: 100 if metadata_elt.text is not None:
105 dest_type = self._i.get_field_type(dest_field) 101 dest_type = self._i.get_field_type(dest_field)
106 value = str(metadata_elt.text) 102 value = str(metadata_elt.text)
107 if dest_type is str: 103 if dest_type is str:
108 if dest_field in vcard: 104 if dest_field in vcard:
109 vcard[dest_field] += value 105 vcard[dest_field] += value
110 else: 106 else:
111 vcard[dest_field] = value 107 vcard[dest_field] = value
112 elif dest_type is list: 108 elif dest_type is list:
113 vcard.setdefault(dest_field, []).append(value) 109 vcard.setdefault(dest_field, []).append(value)
114 else: 110 else:
133 if isinstance(value, list): 129 if isinstance(value, list):
134 for v in value: 130 for v in value:
135 field_elt = vcard_elt.addElement(elt_name) 131 field_elt = vcard_elt.addElement(elt_name)
136 field_elt.addElement("text", content=v) 132 field_elt.addElement("text", content=v)
137 else: 133 else:
138 log.warning( 134 log.warning(f"ignoring unexpected value: {value!r}")
139 f"ignoring unexpected value: {value!r}"
140 )
141 135
142 return vcard_elt 136 return vcard_elt
143 137
144 @async_lru(5) 138 @async_lru(5)
145 async def get_card(self, client: SatXMPPEntity, entity: jid.JID) -> dict: 139 async def get_card(self, client: SatXMPPEntity, entity: jid.JID) -> dict:
161 155
162 async def update_vcard_elt( 156 async def update_vcard_elt(
163 self, 157 self,
164 client: SatXMPPEntity, 158 client: SatXMPPEntity,
165 vcard_elt: domish.Element, 159 vcard_elt: domish.Element,
166 entity: Optional[jid.JID] = None 160 entity: Optional[jid.JID] = None,
167 ) -> None: 161 ) -> None:
168 """Update VCard 4 of given entity, create node if doesn't already exist 162 """Update VCard 4 of given entity, create node if doesn't already exist
169 163
170 @param vcard_elt: whole vCard element to update 164 @param vcard_elt: whole vCard element to update
171 @param entity: entity for which the vCard must be updated 165 @param entity: entity for which the vCard must be updated
172 None to update profile's own vCard 166 None to update profile's own vCard
173 """ 167 """
174 service = entity or client.jid.userhostJID() 168 service = entity or client.jid.userhostJID()
175 node_options = { 169 node_options = {
176 self._p.OPT_ACCESS_MODEL: self._p.ACCESS_OPEN, 170 self._p.OPT_ACCESS_MODEL: self._p.ACCESS_OPEN,
177 self._p.OPT_PUBLISH_MODEL: self._p.PUBLISH_MODEL_PUBLISHERS 171 self._p.OPT_PUBLISH_MODEL: self._p.PUBLISH_MODEL_PUBLISHERS,
178 } 172 }
179 await self._p.create_if_new_node(client, service, VCARD4_NODE, node_options) 173 await self._p.create_if_new_node(client, service, VCARD4_NODE, node_options)
180 await self._p.send_item( 174 await self._p.send_item(
181 client, service, VCARD4_NODE, vcard_elt, item_id=self._p.ID_SINGLETON 175 client, service, VCARD4_NODE, vcard_elt, item_id=self._p.ID_SINGLETON
182 ) 176 )
223 async def set_value( 217 async def set_value(
224 self, 218 self,
225 client: SatXMPPEntity, 219 client: SatXMPPEntity,
226 value: Union[str, List[str]], 220 value: Union[str, List[str]],
227 entity: jid.JID, 221 entity: jid.JID,
228 field: str 222 field: str,
229 ) -> None: 223 ) -> None:
230 """Set generic value 224 """Set generic value
231 225
232 @param entity: entity from who the vCard comes 226 @param entity: entity from who the vCard comes
233 @param field: name of the field to get 227 @param field: name of the field to get