diff --git a/tools/test-backend b/tools/test-backend index 814d989115..2778098f6f 100755 --- a/tools/test-backend +++ b/tools/test-backend @@ -90,6 +90,7 @@ not_yet_fully_covered = { 'zerver/lib/timeout.py', 'zerver/lib/unminify.py', 'zerver/lib/utils.py', + 'zerver/lib/zephyr.py', # Low priority for coverage 'zerver/lib/ccache.py', 'zerver/lib/generate_test_data.py', diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index f7c8410f91..134863ca61 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -67,6 +67,7 @@ from zerver.lib.users import ( user_ids_to_users ) from zerver.lib.user_groups import create_user_group, access_user_group_by_id + from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity, \ RealmDomain, Service, SubMessage, \ Subscription, Recipient, Message, Attachment, UserMessage, RealmAuditLog, \ @@ -126,16 +127,14 @@ from zerver.lib.exceptions import JsonableError, ErrorCode, BugdownRenderingExce from zerver.lib.sessions import delete_user_sessions from zerver.lib.upload import attachment_url_re, attachment_url_to_path_id, \ claim_attachment, delete_message_image, upload_emoji_image -from zerver.lib.str_utils import NonBinaryStr, force_str +from zerver.lib.str_utils import NonBinaryStr from zerver.tornado.event_queue import request_event_queue, send_event from zerver.lib.types import ProfileFieldData from analytics.models import StreamCount -import DNS import ujson import time -import traceback import re import datetime import os @@ -858,26 +857,6 @@ def compute_irc_user_fullname(email: NonBinaryStr) -> NonBinaryStr: def compute_jabber_user_fullname(email: NonBinaryStr) -> NonBinaryStr: return email.split("@")[0] + " (XMPP)" -def compute_mit_user_fullname(email: NonBinaryStr) -> NonBinaryStr: - try: - # Input is either e.g. username@mit.edu or user|CROSSREALM.INVALID@mit.edu - match_user = re.match(r'^([a-zA-Z0-9_.-]+)(\|.+)?@mit\.edu$', email.lower()) - if match_user and match_user.group(2) is None: - answer = DNS.dnslookup( - "%s.passwd.ns.athena.mit.edu" % (match_user.group(1),), - DNS.Type.TXT) - hesiod_name = force_str(answer[0][0]).split(':')[4].split(',')[0].strip() - if hesiod_name != "": - return hesiod_name - elif match_user: - return match_user.group(1).lower() + "@" + match_user.group(2).upper()[1:] - except DNS.Base.ServerError: - pass - except Exception: - print("Error getting fullname for %s:" % (email,)) - traceback.print_exc() - return email.lower() - @cache_with_key(lambda realm, email, f: user_profile_by_email_cache_key(email), timeout=3600*24*7) def create_mirror_user_if_needed(realm: Realm, email: str, diff --git a/zerver/lib/zephyr.py b/zerver/lib/zephyr.py new file mode 100644 index 0000000000..19a34963e2 --- /dev/null +++ b/zerver/lib/zephyr.py @@ -0,0 +1,25 @@ +import re +import traceback +import DNS + +from zerver.lib.str_utils import NonBinaryStr, force_str + +def compute_mit_user_fullname(email: NonBinaryStr) -> NonBinaryStr: + try: + # Input is either e.g. username@mit.edu or user|CROSSREALM.INVALID@mit.edu + match_user = re.match(r'^([a-zA-Z0-9_.-]+)(\|.+)?@mit\.edu$', email.lower()) + if match_user and match_user.group(2) is None: + answer = DNS.dnslookup( + "%s.passwd.ns.athena.mit.edu" % (match_user.group(1),), + DNS.Type.TXT) + hesiod_name = force_str(answer[0][0]).split(':')[4].split(',')[0].strip() + if hesiod_name != "": + return hesiod_name + elif match_user: + return match_user.group(1).lower() + "@" + match_user.group(2).upper()[1:] + except DNS.Base.ServerError: + pass + except Exception: + print("Error getting fullname for %s:" % (email,)) + traceback.print_exc() + return email.lower() diff --git a/zerver/tests/test_external.py b/zerver/tests/test_external.py index ff79441ff3..b9b6f33370 100644 --- a/zerver/tests/test_external.py +++ b/zerver/tests/test_external.py @@ -11,8 +11,8 @@ from zerver.lib.rate_limiter import ( remove_ratelimit_rule, RateLimitedUser, ) +from zerver.lib.zephyr import compute_mit_user_fullname -from zerver.lib.actions import compute_mit_user_fullname from zerver.lib.test_classes import ( ZulipTestCase, ) diff --git a/zerver/views/messages.py b/zerver/views/messages.py index fa72b2f9a5..40a6b66a97 100644 --- a/zerver/views/messages.py +++ b/zerver/views/messages.py @@ -15,7 +15,7 @@ from django.utils.html import escape as escape_html from zerver.lib import bugdown from zerver.lib.zcommand import process_zcommands from zerver.lib.actions import recipient_for_emails, do_update_message_flags, \ - compute_mit_user_fullname, compute_irc_user_fullname, compute_jabber_user_fullname, \ + compute_irc_user_fullname, compute_jabber_user_fullname, \ create_mirror_user_if_needed, check_send_message, do_update_message, \ extract_recipients, truncate_body, render_incoming_message, do_delete_message, \ do_mark_all_as_read, do_mark_stream_messages_as_read, \ @@ -36,6 +36,7 @@ from zerver.lib.topic_mutes import exclude_topic_mutes from zerver.lib.utils import statsd from zerver.lib.validator import \ check_list, check_int, check_dict, check_string, check_bool +from zerver.lib.zephyr import compute_mit_user_fullname from zerver.models import Message, UserProfile, Stream, Subscription, Client,\ Realm, RealmDomain, Recipient, UserMessage, bulk_get_recipients, get_personal_recipient, \ get_stream, email_to_domain, get_realm, get_active_streams, \ diff --git a/zerver/views/registration.py b/zerver/views/registration.py index f01cbd824f..cd99e19250 100644 --- a/zerver/views/registration.py +++ b/zerver/views/registration.py @@ -20,7 +20,7 @@ from zerver.lib.send_email import send_email, FromAddress from zerver.lib.events import do_events_register from zerver.lib.actions import do_change_password, do_change_full_name, do_change_is_admin, \ do_activate_user, do_create_user, do_create_realm, \ - email_not_system_bot, compute_mit_user_fullname, validate_email_for_realm, \ + email_not_system_bot, validate_email_for_realm, \ do_set_user_display_setting, lookup_default_stream_groups, bulk_add_subscriptions from zerver.forms import RegistrationForm, HomepageForm, RealmCreationForm, \ CreateUserForm, FindMyTeamForm @@ -33,6 +33,7 @@ from zerver.lib.response import json_success from zerver.lib.subdomains import get_subdomain, is_root_domain_available from zerver.lib.timezone import get_all_timezones from zerver.lib.users import get_accounts_for_email +from zerver.lib.zephyr import compute_mit_user_fullname from zerver.views.auth import create_preregistration_user, \ redirect_and_log_into_subdomain, \ redirect_to_deactivation_notice