2018-08-11 16:26:46 +02:00
|
|
|
import re
|
|
|
|
import traceback
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2018-08-11 16:26:46 +02:00
|
|
|
import DNS
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2018-11-27 20:21:55 +01:00
|
|
|
def compute_mit_user_fullname(email: str) -> str:
|
2018-08-11 16:26:46 +02:00
|
|
|
try:
|
|
|
|
# Input is either e.g. username@mit.edu or user|CROSSREALM.INVALID@mit.edu
|
2021-02-12 08:20:45 +01:00
|
|
|
match_user = re.match(r"^([a-zA-Z0-9_.-]+)(\|.+)?@mit\.edu$", email.lower())
|
2018-08-11 16:26:46 +02:00
|
|
|
if match_user and match_user.group(2) is None:
|
2021-02-12 08:19:30 +01:00
|
|
|
answer = DNS.dnslookup(f"{match_user.group(1)}.passwd.ns.athena.mit.edu", DNS.Type.TXT)
|
2021-02-12 08:20:45 +01:00
|
|
|
hesiod_name = answer[0][0].split(":")[4].split(",")[0].strip()
|
2018-08-11 16:26:46 +02:00
|
|
|
if hesiod_name != "":
|
|
|
|
return hesiod_name
|
|
|
|
elif match_user:
|
2024-09-03 19:42:14 +02:00
|
|
|
return match_user.group(1).lower() + "@" + match_user.group(2).upper().removeprefix("|")
|
2018-08-11 16:26:46 +02:00
|
|
|
except DNS.Base.ServerError:
|
|
|
|
pass
|
|
|
|
except Exception:
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"Error getting fullname for {email}:")
|
2018-08-11 16:26:46 +02:00
|
|
|
traceback.print_exc()
|
|
|
|
return email.lower()
|