| 
									
										
										
										
											2024-03-03 13:25:00 +00:00
										 |  |  | __filename__ = "daemon_get_vcard.py" | 
					
						
							|  |  |  | __author__ = "Bob Mottram" | 
					
						
							|  |  |  | __license__ = "AGPL3+" | 
					
						
							| 
									
										
										
										
											2024-12-22 23:37:30 +00:00
										 |  |  | __version__ = "1.6.0" | 
					
						
							| 
									
										
										
										
											2024-03-03 13:25:00 +00:00
										 |  |  | __maintainer__ = "Bob Mottram" | 
					
						
							|  |  |  | __email__ = "bob@libreserver.org" | 
					
						
							|  |  |  | __status__ = "Production" | 
					
						
							| 
									
										
										
										
											2024-12-25 14:31:14 +00:00
										 |  |  | __module_group__ = "Daemon GET" | 
					
						
							| 
									
										
										
										
											2024-03-03 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | from daemon_utils import has_accept | 
					
						
							|  |  |  | from httpcodes import write2 | 
					
						
							|  |  |  | from httpcodes import http_400 | 
					
						
							|  |  |  | from httpcodes import http_404 | 
					
						
							|  |  |  | from httpcodes import http_503 | 
					
						
							|  |  |  | from httpheaders import set_headers | 
					
						
							|  |  |  | from utils import acct_dir | 
					
						
							|  |  |  | from utils import load_json | 
					
						
							| 
									
										
										
										
											2024-08-14 09:10:31 +00:00
										 |  |  | from utils import string_contains | 
					
						
							| 
									
										
										
										
											2024-03-03 13:25:00 +00:00
										 |  |  | from pgp import actor_to_vcard_xml | 
					
						
							|  |  |  | from pgp import actor_to_vcard | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def show_vcard(self, base_dir: str, path: str, calling_domain: str, | 
					
						
							| 
									
										
										
										
											2024-08-15 18:27:29 +00:00
										 |  |  |                referer_domain: str, domain: str, translate: {}) -> bool: | 
					
						
							| 
									
										
										
										
											2024-03-03 13:25:00 +00:00
										 |  |  |     """Returns a vcard for the given account
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     if not has_accept(self, calling_domain): | 
					
						
							|  |  |  |         return False | 
					
						
							| 
									
										
										
										
											2024-08-14 09:57:18 +00:00
										 |  |  |     if not path.startswith('/users/'): | 
					
						
							|  |  |  |         return False | 
					
						
							| 
									
										
										
										
											2024-03-03 13:25:00 +00:00
										 |  |  |     if path.endswith('.vcf'): | 
					
						
							|  |  |  |         path = path.split('.vcf')[0] | 
					
						
							|  |  |  |         accept_str = 'text/vcard' | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2024-08-14 09:18:47 +00:00
										 |  |  |         accept_str = self.headers['Accept'] | 
					
						
							| 
									
										
										
										
											2024-08-14 09:10:31 +00:00
										 |  |  |     vcard_mime_types = ( | 
					
						
							|  |  |  |         'text/vcard', | 
					
						
							| 
									
										
										
										
											2024-08-14 09:18:47 +00:00
										 |  |  |         'application/vcard+xml' | 
					
						
							| 
									
										
										
										
											2024-08-14 09:10:31 +00:00
										 |  |  |     ) | 
					
						
							|  |  |  |     if not string_contains(accept_str, vcard_mime_types): | 
					
						
							| 
									
										
										
										
											2024-03-03 13:25:00 +00:00
										 |  |  |         return False | 
					
						
							| 
									
										
										
										
											2024-08-14 09:28:16 +00:00
										 |  |  |     print('Downloading vcard ' + path) | 
					
						
							| 
									
										
										
										
											2024-03-03 13:25:00 +00:00
										 |  |  |     if path.startswith('/@'): | 
					
						
							|  |  |  |         if '/@/' not in path: | 
					
						
							|  |  |  |             path = path.replace('/@', '/users/', 1) | 
					
						
							|  |  |  |     if not path.startswith('/users/'): | 
					
						
							|  |  |  |         http_400(self) | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  |     nickname = path.split('/users/')[1] | 
					
						
							|  |  |  |     if '/' in nickname: | 
					
						
							|  |  |  |         nickname = nickname.split('/')[0] | 
					
						
							|  |  |  |     if '?' in nickname: | 
					
						
							|  |  |  |         nickname = nickname.split('?')[0] | 
					
						
							|  |  |  |     if self.server.vcard_is_active: | 
					
						
							|  |  |  |         print('vcard is busy during request from ' + str(referer_domain)) | 
					
						
							|  |  |  |         http_503(self) | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  |     self.server.vcard_is_active = True | 
					
						
							|  |  |  |     actor_json = None | 
					
						
							|  |  |  |     actor_filename = \ | 
					
						
							|  |  |  |         acct_dir(base_dir, nickname, domain) + '.json' | 
					
						
							|  |  |  |     if os.path.isfile(actor_filename): | 
					
						
							|  |  |  |         actor_json = load_json(actor_filename) | 
					
						
							|  |  |  |     if not actor_json: | 
					
						
							|  |  |  |         print('WARN: vcard actor not found ' + actor_filename) | 
					
						
							|  |  |  |         http_404(self, 3) | 
					
						
							|  |  |  |         self.server.vcard_is_active = False | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  |     if 'application/vcard+xml' in accept_str: | 
					
						
							| 
									
										
										
										
											2024-08-15 18:27:29 +00:00
										 |  |  |         vcard_str = actor_to_vcard_xml(actor_json, domain, translate) | 
					
						
							| 
									
										
										
										
											2024-03-03 13:25:00 +00:00
										 |  |  |         header_type = 'application/vcard+xml; charset=utf-8' | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2024-08-15 18:27:29 +00:00
										 |  |  |         vcard_str = actor_to_vcard(actor_json, domain, translate) | 
					
						
							| 
									
										
										
										
											2024-03-03 13:25:00 +00:00
										 |  |  |         header_type = 'text/vcard; charset=utf-8' | 
					
						
							|  |  |  |     if vcard_str: | 
					
						
							|  |  |  |         msg = vcard_str.encode('utf-8') | 
					
						
							|  |  |  |         msglen = len(msg) | 
					
						
							|  |  |  |         set_headers(self, header_type, msglen, | 
					
						
							|  |  |  |                     None, calling_domain, True) | 
					
						
							|  |  |  |         write2(self, msg) | 
					
						
							|  |  |  |         print('vcard sent to ' + str(referer_domain)) | 
					
						
							|  |  |  |         self.server.vcard_is_active = False | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  |     print('WARN: vcard string not returned') | 
					
						
							|  |  |  |     http_404(self, 4) | 
					
						
							|  |  |  |     self.server.vcard_is_active = False | 
					
						
							|  |  |  |     return True |