| 
									
										
										
										
											2020-04-04 14:17:48 +00:00
										 |  |  | __filename__ = "xmpp.py" | 
					
						
							|  |  |  | __author__ = "Bob Mottram" | 
					
						
							|  |  |  | __license__ = "AGPL3+" | 
					
						
							| 
									
										
										
										
											2024-12-22 23:37:30 +00:00
										 |  |  | __version__ = "1.6.0" | 
					
						
							| 
									
										
										
										
											2020-04-04 14:17:48 +00:00
										 |  |  | __maintainer__ = "Bob Mottram" | 
					
						
							| 
									
										
										
										
											2021-09-10 16:14:50 +00:00
										 |  |  | __email__ = "bob@libreserver.org" | 
					
						
							| 
									
										
										
										
											2020-04-04 14:17:48 +00:00
										 |  |  | __status__ = "Production" | 
					
						
							| 
									
										
										
										
											2021-06-15 15:08:12 +00:00
										 |  |  | __module_group__ = "Profile Metadata" | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-11 17:17:23 +00:00
										 |  |  | from utils import get_attachment_property_value | 
					
						
							| 
									
										
										
										
											2023-07-13 08:39:35 +00:00
										 |  |  | from utils import remove_html | 
					
						
							| 
									
										
										
										
											2022-05-11 17:17:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-28 17:33:54 +00:00
										 |  |  | def get_xmpp_address(actor_json: {}) -> str: | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |     """Returns xmpp address for the given actor
 | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2021-12-26 10:29:52 +00:00
										 |  |  |     if not actor_json.get('attachment'): | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |         return '' | 
					
						
							| 
									
										
										
										
											2023-11-29 10:58:48 +00:00
										 |  |  |     if not isinstance(actor_json['attachment'], list): | 
					
						
							|  |  |  |         return '' | 
					
						
							| 
									
										
										
										
											2021-12-26 10:32:45 +00:00
										 |  |  |     for property_value in actor_json['attachment']: | 
					
						
							| 
									
										
										
										
											2022-05-11 16:10:38 +00:00
										 |  |  |         name_value = None | 
					
						
							|  |  |  |         if property_value.get('name'): | 
					
						
							|  |  |  |             name_value = property_value['name'].lower() | 
					
						
							|  |  |  |         elif property_value.get('schema:name'): | 
					
						
							|  |  |  |             name_value = property_value['schema:name'].lower() | 
					
						
							|  |  |  |         if not name_value: | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2022-05-11 16:10:38 +00:00
										 |  |  |         if not (name_value.startswith('xmpp') or | 
					
						
							| 
									
										
										
										
											2023-07-09 10:41:51 +00:00
										 |  |  |                 name_value.startswith('jabber') or | 
					
						
							|  |  |  |                 name_value == 'chat'): | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2021-12-26 10:32:45 +00:00
										 |  |  |         if not property_value.get('type'): | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2022-05-11 17:17:23 +00:00
										 |  |  |         prop_value_name, _ = \ | 
					
						
							|  |  |  |             get_attachment_property_value(property_value) | 
					
						
							|  |  |  |         if not prop_value_name: | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2023-07-09 10:41:51 +00:00
										 |  |  |         if not property_value['type'].endswith('PropertyValue') and \ | 
					
						
							|  |  |  |            not property_value['type'] == 'Link': | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2024-08-15 22:16:53 +00:00
										 |  |  |         address_text = remove_html(property_value[prop_value_name]) | 
					
						
							| 
									
										
										
										
											2024-08-12 09:34:27 +00:00
										 |  |  |         if 'matrix' in address_text.lower(): | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2024-08-12 09:34:27 +00:00
										 |  |  |         if '@' not in address_text: | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2024-08-12 09:34:27 +00:00
										 |  |  |         if '.' not in address_text: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if '"' in address_text: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if address_text.startswith('xmpp://'): | 
					
						
							|  |  |  |             address_text = address_text.split('xmpp://', 1)[1] | 
					
						
							| 
									
										
										
										
											2024-08-15 22:16:53 +00:00
										 |  |  |         elif address_text.startswith('xmpp:'): | 
					
						
							|  |  |  |             address_text = address_text.split('xmpp:', 1)[1] | 
					
						
							|  |  |  |         return address_text | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |     return '' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 14:17:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-31 18:11:17 +00:00
										 |  |  | def set_xmpp_address(actor_json: {}, xmpp_address: str) -> None: | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |     """Sets an xmpp address for the given actor
 | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2022-01-03 21:19:01 +00:00
										 |  |  |     not_xmpp_address = False | 
					
						
							| 
									
										
										
										
											2021-12-31 18:11:17 +00:00
										 |  |  |     if '@' not in xmpp_address: | 
					
						
							| 
									
										
										
										
											2022-01-03 21:19:01 +00:00
										 |  |  |         not_xmpp_address = True | 
					
						
							| 
									
										
										
										
											2021-12-31 18:11:17 +00:00
										 |  |  |     if '.' not in xmpp_address: | 
					
						
							| 
									
										
										
										
											2022-01-03 21:19:01 +00:00
										 |  |  |         not_xmpp_address = True | 
					
						
							| 
									
										
										
										
											2021-12-31 18:11:17 +00:00
										 |  |  |     if '"' in xmpp_address: | 
					
						
							| 
									
										
										
										
											2022-01-03 21:19:01 +00:00
										 |  |  |         not_xmpp_address = True | 
					
						
							| 
									
										
										
										
											2021-12-31 18:11:17 +00:00
										 |  |  |     if '<' in xmpp_address: | 
					
						
							| 
									
										
										
										
											2022-01-03 21:19:01 +00:00
										 |  |  |         not_xmpp_address = True | 
					
						
							| 
									
										
										
										
											2020-07-06 10:27:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-26 10:29:52 +00:00
										 |  |  |     if not actor_json.get('attachment'): | 
					
						
							| 
									
										
										
										
											2024-12-23 18:23:47 +00:00
										 |  |  |         actor_json['attachment']: list[dict] = [] | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-17 23:35:59 +00:00
										 |  |  |     # remove any existing value | 
					
						
							| 
									
										
										
										
											2022-01-03 10:27:55 +00:00
										 |  |  |     property_found = None | 
					
						
							| 
									
										
										
										
											2021-12-26 10:32:45 +00:00
										 |  |  |     for property_value in actor_json['attachment']: | 
					
						
							| 
									
										
										
										
											2022-05-11 16:10:38 +00:00
										 |  |  |         name_value = None | 
					
						
							|  |  |  |         if property_value.get('name'): | 
					
						
							|  |  |  |             name_value = property_value['name'] | 
					
						
							|  |  |  |         elif property_value.get('schema:name'): | 
					
						
							|  |  |  |             name_value = property_value['schema:name'] | 
					
						
							|  |  |  |         if not name_value: | 
					
						
							| 
									
										
										
										
											2019-12-17 23:35:59 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2021-12-26 10:32:45 +00:00
										 |  |  |         if not property_value.get('type'): | 
					
						
							| 
									
										
										
										
											2019-12-17 23:35:59 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2022-05-11 16:10:38 +00:00
										 |  |  |         if not (name_value.lower().startswith('xmpp') or | 
					
						
							| 
									
										
										
										
											2023-07-09 10:41:51 +00:00
										 |  |  |                 name_value.lower().startswith('jabber') or | 
					
						
							|  |  |  |                 name_value == 'chat'): | 
					
						
							| 
									
										
										
										
											2019-12-17 23:35:59 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2023-07-09 10:41:51 +00:00
										 |  |  |         if name_value == 'chat': | 
					
						
							|  |  |  |             if not property_value.get('href'): | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             if not isinstance(property_value['href'], str): | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             if not property_value['href'].startswith('xmpp://'): | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2022-01-03 10:27:55 +00:00
										 |  |  |         property_found = property_value | 
					
						
							| 
									
										
										
										
											2019-12-17 23:35:59 +00:00
										 |  |  |         break | 
					
						
							| 
									
										
										
										
											2022-01-03 10:27:55 +00:00
										 |  |  |     if property_found: | 
					
						
							|  |  |  |         actor_json['attachment'].remove(property_found) | 
					
						
							| 
									
										
										
										
											2022-01-03 21:19:01 +00:00
										 |  |  |     if not_xmpp_address: | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |         return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-26 10:32:45 +00:00
										 |  |  |     for property_value in actor_json['attachment']: | 
					
						
							| 
									
										
										
										
											2022-05-11 16:10:38 +00:00
										 |  |  |         name_value = None | 
					
						
							|  |  |  |         if property_value.get('name'): | 
					
						
							|  |  |  |             name_value = property_value['name'] | 
					
						
							|  |  |  |         elif property_value.get('schema:name'): | 
					
						
							|  |  |  |             name_value = property_value['schema:name'] | 
					
						
							|  |  |  |         if not name_value: | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2021-12-26 10:32:45 +00:00
										 |  |  |         if not property_value.get('type'): | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2022-05-11 16:10:38 +00:00
										 |  |  |         name_value = name_value.lower() | 
					
						
							|  |  |  |         if not (name_value.startswith('xmpp') or | 
					
						
							| 
									
										
										
										
											2023-07-09 10:41:51 +00:00
										 |  |  |                 name_value.startswith('jabber') or | 
					
						
							|  |  |  |                 name_value == 'chat'): | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2023-07-09 10:41:51 +00:00
										 |  |  |         if not property_value['type'].endswith('PropertyValue') and \ | 
					
						
							|  |  |  |            not property_value['type'] == 'Link': | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2022-05-11 17:17:23 +00:00
										 |  |  |         prop_value_name, _ = \ | 
					
						
							|  |  |  |             get_attachment_property_value(property_value) | 
					
						
							|  |  |  |         if not prop_value_name: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         property_value[prop_value_name] = xmpp_address | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |         return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-09 10:41:51 +00:00
										 |  |  |     if not xmpp_address.startswith('xmpp://'): | 
					
						
							|  |  |  |         xmpp_address = 'xmpp://' + xmpp_address | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # https://codeberg.org/fediverse/fep/src/branch/main/fep/1970/fep-1970.md | 
					
						
							| 
									
										
										
										
											2022-01-03 21:19:01 +00:00
										 |  |  |     new_xmpp_address = { | 
					
						
							| 
									
										
										
										
											2023-07-09 10:41:51 +00:00
										 |  |  |         "type": "Link", | 
					
						
							|  |  |  |         "name": "Chat", | 
					
						
							|  |  |  |         "rel": "discussion", | 
					
						
							|  |  |  |         "href": xmpp_address | 
					
						
							| 
									
										
										
										
											2019-12-17 14:57:16 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-03 21:19:01 +00:00
										 |  |  |     actor_json['attachment'].append(new_xmpp_address) |