| 
									
										
										
										
											2020-06-29 15:07:22 +00:00
										 |  |  | __filename__ = "petnames.py" | 
					
						
							|  |  |  | __author__ = "Bob Mottram" | 
					
						
							|  |  |  | __license__ = "AGPL3+" | 
					
						
							| 
									
										
										
										
											2024-12-22 23:37:30 +00:00
										 |  |  | __version__ = "1.6.0" | 
					
						
							| 
									
										
										
										
											2020-06-29 15:07:22 +00:00
										 |  |  | __maintainer__ = "Bob Mottram" | 
					
						
							| 
									
										
										
										
											2021-09-10 16:14:50 +00:00
										 |  |  | __email__ = "bob@libreserver.org" | 
					
						
							| 
									
										
										
										
											2020-06-29 15:07:22 +00:00
										 |  |  | __status__ = "Production" | 
					
						
							| 
									
										
										
										
											2021-06-26 11:16:41 +00:00
										 |  |  | __module_group__ = "Core" | 
					
						
							| 
									
										
										
										
											2020-06-29 15:07:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							| 
									
										
										
										
											2021-12-26 12:02:29 +00:00
										 |  |  | from utils import acct_dir | 
					
						
							| 
									
										
										
										
											2020-06-29 15:07:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  | def set_pet_name(base_dir: str, nickname: str, domain: str, | 
					
						
							|  |  |  |                  handle: str, petname: str) -> bool: | 
					
						
							| 
									
										
										
										
											2020-06-29 15:07:22 +00:00
										 |  |  |     """Adds a new petname
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     if '@' not in handle: | 
					
						
							|  |  |  |         return False | 
					
						
							| 
									
										
										
										
											2020-06-29 15:23:52 +00:00
										 |  |  |     if ' ' in petname: | 
					
						
							|  |  |  |         petname = petname.replace(' ', '_') | 
					
						
							| 
									
										
										
										
											2020-06-29 15:07:22 +00:00
										 |  |  |     if handle.startswith('@'): | 
					
						
							|  |  |  |         handle = handle[1:] | 
					
						
							|  |  |  |     if petname.startswith('@'): | 
					
						
							|  |  |  |         petname = petname[1:] | 
					
						
							| 
									
										
										
										
											2022-01-03 15:06:00 +00:00
										 |  |  |     petnames_filename = acct_dir(base_dir, nickname, domain) + '/petnames.txt' | 
					
						
							| 
									
										
										
										
											2020-06-29 15:07:22 +00:00
										 |  |  |     entry = petname + ' ' + handle + '\n' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # does this entry already exist? | 
					
						
							| 
									
										
										
										
											2022-01-03 15:06:00 +00:00
										 |  |  |     if os.path.isfile(petnames_filename): | 
					
						
							| 
									
										
										
										
											2024-07-02 19:55:58 +00:00
										 |  |  |         petnames_str = '' | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             with open(petnames_filename, 'r', | 
					
						
							| 
									
										
										
										
											2024-07-14 13:01:46 +00:00
										 |  |  |                       encoding='utf-8') as fp_petnames: | 
					
						
							|  |  |  |                 petnames_str = fp_petnames.read() | 
					
						
							| 
									
										
										
										
											2024-07-02 19:55:58 +00:00
										 |  |  |         except OSError: | 
					
						
							|  |  |  |             print('EX: set_pet_name unable to read ' + petnames_filename) | 
					
						
							|  |  |  |         if entry in petnames_str: | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  |         if ' ' + handle + '\n' in petnames_str: | 
					
						
							|  |  |  |             petnames_list = petnames_str.split('\n') | 
					
						
							|  |  |  |             new_petnames_str = '' | 
					
						
							|  |  |  |             for pet in petnames_list: | 
					
						
							|  |  |  |                 if not pet.endswith(' ' + handle): | 
					
						
							|  |  |  |                     new_petnames_str += pet + '\n' | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     new_petnames_str += entry | 
					
						
							|  |  |  |             # save the updated petnames file | 
					
						
							| 
									
										
										
										
											2021-11-25 21:18:53 +00:00
										 |  |  |             try: | 
					
						
							| 
									
										
										
										
											2024-07-02 19:55:58 +00:00
										 |  |  |                 with open(petnames_filename, 'w+', | 
					
						
							| 
									
										
										
										
											2024-07-14 13:01:46 +00:00
										 |  |  |                           encoding='utf-8') as fp_petnames: | 
					
						
							|  |  |  |                     fp_petnames.write(new_petnames_str) | 
					
						
							| 
									
										
										
										
											2021-11-25 21:18:53 +00:00
										 |  |  |             except OSError: | 
					
						
							| 
									
										
										
										
											2024-07-02 19:55:58 +00:00
										 |  |  |                 print('EX: set_pet_name unable to save ' + petnames_filename) | 
					
						
							| 
									
										
										
										
											2021-11-25 21:18:53 +00:00
										 |  |  |                 return False | 
					
						
							| 
									
										
										
										
											2020-06-29 15:07:22 +00:00
										 |  |  |             return True | 
					
						
							| 
									
										
										
										
											2024-07-02 19:55:58 +00:00
										 |  |  |         # entry does not exist in the petnames file | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             with open(petnames_filename, 'a+', | 
					
						
							| 
									
										
										
										
											2024-07-14 13:01:46 +00:00
										 |  |  |                       encoding='utf-8') as fp_petnames: | 
					
						
							|  |  |  |                 fp_petnames.write(entry) | 
					
						
							| 
									
										
										
										
											2024-07-02 19:55:58 +00:00
										 |  |  |         except OSError: | 
					
						
							|  |  |  |             print('EX: set_pet_name unable to append ' + petnames_filename) | 
					
						
							|  |  |  |             return False | 
					
						
							|  |  |  |         return True | 
					
						
							| 
									
										
										
										
											2020-06-29 15:07:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # first entry | 
					
						
							| 
									
										
										
										
											2021-11-25 21:18:53 +00:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-07-14 13:01:46 +00:00
										 |  |  |         with open(petnames_filename, 'w+', encoding='utf-8') as fp_petnames: | 
					
						
							|  |  |  |             fp_petnames.write(entry) | 
					
						
							| 
									
										
										
										
											2021-11-25 21:18:53 +00:00
										 |  |  |     except OSError: | 
					
						
							| 
									
										
										
										
											2024-07-02 20:20:24 +00:00
										 |  |  |         print('EX: set_pet_name unable to write ' + petnames_filename) | 
					
						
							| 
									
										
										
										
											2021-11-25 21:18:53 +00:00
										 |  |  |         return False | 
					
						
							| 
									
										
										
										
											2020-06-29 15:07:22 +00:00
										 |  |  |     return True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  | def get_pet_name(base_dir: str, nickname: str, domain: str, | 
					
						
							|  |  |  |                  handle: str) -> str: | 
					
						
							| 
									
										
										
										
											2020-06-29 15:07:22 +00:00
										 |  |  |     """Given a handle returns the petname
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     if '@' not in handle: | 
					
						
							| 
									
										
										
										
											2020-06-29 16:25:28 +00:00
										 |  |  |         return '' | 
					
						
							| 
									
										
										
										
											2020-06-29 15:07:22 +00:00
										 |  |  |     if handle.startswith('@'): | 
					
						
							|  |  |  |         handle = handle[1:] | 
					
						
							| 
									
										
										
										
											2022-01-03 15:06:00 +00:00
										 |  |  |     petnames_filename = acct_dir(base_dir, nickname, domain) + '/petnames.txt' | 
					
						
							| 
									
										
										
										
											2020-06-29 15:07:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 15:06:00 +00:00
										 |  |  |     if not os.path.isfile(petnames_filename): | 
					
						
							| 
									
										
										
										
											2020-06-29 16:25:28 +00:00
										 |  |  |         return '' | 
					
						
							| 
									
										
										
										
											2024-07-02 19:59:47 +00:00
										 |  |  |     petnames_str = '' | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-07-14 13:01:46 +00:00
										 |  |  |         with open(petnames_filename, 'r', encoding='utf-8') as fp_petnames: | 
					
						
							|  |  |  |             petnames_str = fp_petnames.read() | 
					
						
							| 
									
										
										
										
											2024-07-02 19:59:47 +00:00
										 |  |  |     except OSError: | 
					
						
							|  |  |  |         print('EX: get_pet_name unable to read ' + petnames_filename) | 
					
						
							|  |  |  |     if ' ' + handle + '\n' in petnames_str: | 
					
						
							|  |  |  |         petnames_list = petnames_str.split('\n') | 
					
						
							|  |  |  |         for pet in petnames_list: | 
					
						
							|  |  |  |             if pet.endswith(' ' + handle): | 
					
						
							|  |  |  |                 return pet.replace(' ' + handle, '').strip() | 
					
						
							|  |  |  |     elif ' ' + handle.lower() + '\n' in petnames_str.lower(): | 
					
						
							|  |  |  |         petnames_list = petnames_str.split('\n') | 
					
						
							|  |  |  |         handle = handle.lower() | 
					
						
							|  |  |  |         for pet in petnames_list: | 
					
						
							|  |  |  |             if pet.lower().endswith(' ' + handle): | 
					
						
							|  |  |  |                 handle2 = pet.split(' ')[-1] | 
					
						
							|  |  |  |                 return pet.replace(' ' + handle2, '').strip() | 
					
						
							| 
									
										
										
										
											2020-06-29 16:25:28 +00:00
										 |  |  |     return '' | 
					
						
							| 
									
										
										
										
											2020-06-29 19:15:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  | def _get_pet_name_handle(base_dir: str, nickname: str, domain: str, | 
					
						
							|  |  |  |                          petname: str) -> str: | 
					
						
							| 
									
										
										
										
											2020-06-29 19:15:51 +00:00
										 |  |  |     """Given a petname returns the handle
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     if petname.startswith('@'): | 
					
						
							|  |  |  |         petname = petname[1:] | 
					
						
							| 
									
										
										
										
											2022-01-03 15:06:00 +00:00
										 |  |  |     petnames_filename = acct_dir(base_dir, nickname, domain) + '/petnames.txt' | 
					
						
							| 
									
										
										
										
											2020-06-29 19:15:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 15:06:00 +00:00
										 |  |  |     if not os.path.isfile(petnames_filename): | 
					
						
							| 
									
										
										
										
											2020-06-29 19:15:51 +00:00
										 |  |  |         return '' | 
					
						
							| 
									
										
										
										
											2024-07-02 20:01:26 +00:00
										 |  |  |     petnames_str = '' | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-07-14 13:01:46 +00:00
										 |  |  |         with open(petnames_filename, 'r', encoding='utf-8') as fp_petnames: | 
					
						
							|  |  |  |             petnames_str = fp_petnames.read() | 
					
						
							| 
									
										
										
										
											2024-07-02 20:01:26 +00:00
										 |  |  |     except OSError: | 
					
						
							|  |  |  |         print('EX: _get_pet_name_handle unable to read ' + petnames_filename) | 
					
						
							|  |  |  |     if petname + ' ' in petnames_str: | 
					
						
							|  |  |  |         petnames_list = petnames_str.split('\n') | 
					
						
							|  |  |  |         for pet in petnames_list: | 
					
						
							|  |  |  |             if pet.startswith(petname + ' '): | 
					
						
							|  |  |  |                 handle = pet.replace(petname + ' ', '').strip() | 
					
						
							|  |  |  |                 return handle | 
					
						
							| 
									
										
										
										
											2020-06-29 19:15:51 +00:00
										 |  |  |     return '' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  | def resolve_petnames(base_dir: str, nickname: str, domain: str, | 
					
						
							|  |  |  |                      content: str) -> str: | 
					
						
							| 
									
										
										
										
											2020-06-29 19:15:51 +00:00
										 |  |  |     """Replaces petnames with their full handles
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     if not content: | 
					
						
							|  |  |  |         return content | 
					
						
							|  |  |  |     if ' ' not in content: | 
					
						
							|  |  |  |         return content | 
					
						
							|  |  |  |     words = content.strip().split(' ') | 
					
						
							|  |  |  |     for wrd in words: | 
					
						
							|  |  |  |         # check initial words beginning with @ | 
					
						
							|  |  |  |         if not wrd.startswith('@'): | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |         # does a petname handle exist for this? | 
					
						
							| 
									
										
										
										
											2021-12-29 21:55:09 +00:00
										 |  |  |         handle = _get_pet_name_handle(base_dir, nickname, domain, wrd) | 
					
						
							| 
									
										
										
										
											2020-06-29 19:15:51 +00:00
										 |  |  |         if not handle: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         # replace the petname with the handle | 
					
						
							| 
									
										
										
										
											2020-06-29 19:39:15 +00:00
										 |  |  |         content = content.replace(wrd + ' ', '@' + handle + ' ') | 
					
						
							| 
									
										
										
										
											2020-06-29 19:15:51 +00:00
										 |  |  |     return content |