use super::{push_bounded, MAX_NAMESERVERS};
use crate::whois::parse_date;
use crate::whois::parser::WhoisResponse;
static_regex! {
DOMAIN_NAME = r"(?i)^Domain Name:\s*(.+)$";
REGISTRANT_SECTION = r"(?i)^Registrant:\s*$";
ADMIN_SECTION = r"(?i)^Administrative Contact:\s*$";
TECH_SECTION = r"(?i)^Technical Contact:\s*$";
NAME_SERVERS_SECTION = r"(?i)^Name Servers:\s*$";
ACTIVATED_DATE = r"(?i)^Domain record activated:\s*(.+)$";
UPDATED_DATE = r"(?i)^Domain record last updated:\s*(.+)$";
EXPIRES_DATE = r"(?i)^Domain expires:\s*(.+)$";
EMAIL_PATTERN = r"[\w.+-]+@[\w.-]+\.\w+";
PHONE_PATTERN = r"^\+[\d.]+$";
}
pub(super) const TLDS: &[&str] = &["edu"];
pub(super) fn parse(domain: &str, server: &str, raw: &str) -> WhoisResponse {
let mut registrant = None;
let mut nameservers = Vec::new();
let mut creation_date = None;
let mut expiration_date = None;
let mut updated_date = None;
let mut admin_name = None;
let mut admin_email = None;
let mut admin_phone = None;
let mut tech_name = None;
let mut tech_email = None;
let mut tech_phone = None;
#[derive(Clone, Copy)]
enum Section {
None,
Registrant,
AdminContact,
TechContact,
NameServers,
}
let mut current_section = Section::None;
let mut domain_found = false;
for line in raw.lines() {
let trimmed = line.trim();
if let Some(caps) = ACTIVATED_DATE.captures(trimmed) {
if creation_date.is_none() {
if let Some(m) = caps.get(1) {
creation_date = parse_date(m.as_str());
}
}
current_section = Section::None;
continue;
}
if let Some(caps) = UPDATED_DATE.captures(trimmed) {
if updated_date.is_none() {
if let Some(m) = caps.get(1) {
updated_date = parse_date(m.as_str());
}
}
current_section = Section::None;
continue;
}
if let Some(caps) = EXPIRES_DATE.captures(trimmed) {
if expiration_date.is_none() {
if let Some(m) = caps.get(1) {
expiration_date = parse_date(m.as_str());
}
}
current_section = Section::None;
continue;
}
if REGISTRANT_SECTION.is_match(trimmed) {
current_section = Section::Registrant;
continue;
} else if ADMIN_SECTION.is_match(trimmed) {
current_section = Section::AdminContact;
continue;
} else if TECH_SECTION.is_match(trimmed) {
current_section = Section::TechContact;
continue;
} else if NAME_SERVERS_SECTION.is_match(trimmed) {
current_section = Section::NameServers;
continue;
} else if DOMAIN_NAME.is_match(trimmed) {
domain_found = true;
current_section = Section::None;
continue;
}
if trimmed.is_empty() {
match current_section {
Section::NameServers => {
if !nameservers.is_empty() {
current_section = Section::None;
}
}
_ => {
current_section = Section::None;
}
}
continue;
}
if line.starts_with('\t') || line.starts_with(" ") {
let value = trimmed.to_string();
match current_section {
Section::Registrant if registrant.is_none() => {
registrant = Some(value);
}
Section::AdminContact => {
if EMAIL_PATTERN.is_match(trimmed) && admin_email.is_none() {
admin_email = Some(trimmed.to_string());
} else if PHONE_PATTERN.is_match(trimmed) && admin_phone.is_none() {
admin_phone = Some(trimmed.to_string());
} else if admin_name.is_none()
&& !EMAIL_PATTERN.is_match(trimmed)
&& !PHONE_PATTERN.is_match(trimmed)
{
admin_name = Some(value);
}
}
Section::TechContact => {
if EMAIL_PATTERN.is_match(trimmed) && tech_email.is_none() {
tech_email = Some(trimmed.to_string());
} else if PHONE_PATTERN.is_match(trimmed) && tech_phone.is_none() {
tech_phone = Some(trimmed.to_string());
} else if tech_name.is_none()
&& !EMAIL_PATTERN.is_match(trimmed)
&& !PHONE_PATTERN.is_match(trimmed)
{
tech_name = Some(value);
}
}
Section::NameServers => {
let ns = value.to_lowercase();
push_bounded(&mut nameservers, ns, MAX_NAMESERVERS);
}
_ => {}
}
} else {
current_section = Section::None;
}
}
let has_data = domain_found
&& (registrant.is_some()
|| !nameservers.is_empty()
|| creation_date.is_some()
|| expiration_date.is_some()
|| admin_name.is_some()
|| tech_name.is_some());
WhoisResponse {
domain: domain.to_string(),
registrar: has_data.then(|| "EDUCAUSE".to_string()),
registrant: registrant.clone(),
organization: registrant,
registrant_country: has_data.then(|| "US".to_string()),
admin_name,
admin_email,
admin_phone,
tech_name,
tech_email,
tech_phone,
creation_date,
expiration_date,
updated_date,
nameservers,
whois_server: server.to_string(),
raw_response: raw.to_string(),
..Default::default()
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::Datelike;
const SAMPLE_EDUCAUSE_RESPONSE: &str = r#"This Registry database contains ONLY .EDU domains.
The data in the EDUCAUSE Whois database is provided
by EDUCAUSE for information purposes in order to
assist in the process of obtaining information about
or related to .edu domain registration records.
The EDUCAUSE Whois database is authoritative for the
.EDU domain.
A Web interface for the .EDU EDUCAUSE Whois Server is
available at: http://whois.educause.edu
By submitting a Whois query, you agree that this information
will not be used to allow, enable, or otherwise support
the transmission of unsolicited commercial advertising or
solicitations via e-mail. The use of electronic processes to
harvest information from this server is generally prohibited
except as reasonably necessary to register or modify .edu
domain names.
-------------------------------------------------------------
Domain Name: UW.EDU
Registrant:
University of Washington
4545 15th Ave NE
Suite 400
Seattle, WA 98105-4527
USA
Administrative Contact:
Domain Admin
University of Washington
4545 15th Avenue NE
Box 354840 UW-IT NOC
Seattle, WA 98105-4527
USA
+1.2062216000
domain-admin@uw.edu
Technical Contact:
Domain Admin
University of Washington
4545 15th Avenue NE
Box 354840 UW-IT NOC
Seattle, WA 98105-4527
USA
+1.2062216000
domain-admin@uw.edu
Name Servers:
HOLLY.S.UW.EDU
MARGE.CAC.WASHINGTON.EDU
HANNA.CAC.WASHINGTON.EDU
Domain record activated: 05-Mar-1999
Domain record last updated: 31-Jul-2025
Domain expires: 31-Jul-2027"#;
#[test]
fn test_educause_parser_registrant() {
let result = parse("uw.edu", "whois.educause.edu", SAMPLE_EDUCAUSE_RESPONSE);
assert_eq!(result.domain, "uw.edu");
assert_eq!(
result.registrant,
Some("University of Washington".to_string())
);
assert_eq!(result.registrar, Some("EDUCAUSE".to_string()));
}
#[test]
fn test_educause_parser_nameservers() {
let result = parse("uw.edu", "whois.educause.edu", SAMPLE_EDUCAUSE_RESPONSE);
assert_eq!(result.nameservers.len(), 3);
assert!(result.nameservers.contains(&"holly.s.uw.edu".to_string()));
assert!(result
.nameservers
.contains(&"marge.cac.washington.edu".to_string()));
assert!(result
.nameservers
.contains(&"hanna.cac.washington.edu".to_string()));
}
#[test]
fn test_educause_parser_dates() {
let result = parse("uw.edu", "whois.educause.edu", SAMPLE_EDUCAUSE_RESPONSE);
assert!(result.creation_date.is_some());
let creation = result.creation_date.unwrap();
assert_eq!(creation.year(), 1999);
assert_eq!(creation.month(), 3);
assert_eq!(creation.day(), 5);
assert!(result.expiration_date.is_some());
let expiry = result.expiration_date.unwrap();
assert_eq!(expiry.year(), 2027);
assert_eq!(expiry.month(), 7);
assert_eq!(expiry.day(), 31);
assert!(result.updated_date.is_some());
let updated = result.updated_date.unwrap();
assert_eq!(updated.year(), 2025);
assert_eq!(updated.month(), 7);
assert_eq!(updated.day(), 31);
}
#[test]
fn test_educause_parser_contacts() {
let result = parse("uw.edu", "whois.educause.edu", SAMPLE_EDUCAUSE_RESPONSE);
assert_eq!(result.admin_name, Some("Domain Admin".to_string()));
assert_eq!(result.admin_email, Some("domain-admin@uw.edu".to_string()));
assert_eq!(result.admin_phone, Some("+1.2062216000".to_string()));
assert_eq!(result.tech_name, Some("Domain Admin".to_string()));
assert_eq!(result.tech_email, Some("domain-admin@uw.edu".to_string()));
assert_eq!(result.tech_phone, Some("+1.2062216000".to_string()));
}
#[test]
fn test_educause_parser_country() {
let result = parse("uw.edu", "whois.educause.edu", SAMPLE_EDUCAUSE_RESPONSE);
assert_eq!(result.registrant_country, Some("US".to_string()));
}
#[test]
fn test_educause_date_parsing() {
assert!(parse_date("05-Mar-1999").is_some());
assert!(parse_date("31-Jul-2027").is_some());
assert!(parse_date("31-July-2027").is_some());
}
const SAMPLE_LEADING_EMAIL: &str = r#"Domain Name: EX.EDU
Administrative Contact:
admin@ex.edu
+1.5551234567
Technical Contact:
noc@ex.edu
+1.5559876543
Name Servers:
NS1.EX.EDU
Domain record activated: 05-Mar-1999"#;
const SAMPLE_NO_MATCH: &str = r#"This Registry database contains ONLY .EDU domains.
The data in the EDUCAUSE Whois database is provided
by EDUCAUSE for information purposes in order to
assist in the process of obtaining information about
or related to .edu domain registration records.
-------------------------------------------------------------
No match for "NOTAREALDOMAIN123XYZ.EDU"."#;
const SAMPLE_NO_MATCH_CURRENT: &str = r#"Domain Name: SEER-SWEEP-ZK8QV3XW.EDU
The domain name you requested was not found in our database.
>>> Last update of WHOIS database: 2026-07-04T22:55:30+00:00 <<<"#;
#[test]
fn test_educause_current_not_found_reports_available() {
let result = parse(
"seer-sweep-zk8qv3xw.edu",
"whois.educause.edu",
SAMPLE_NO_MATCH_CURRENT,
);
assert_eq!(
result.registrar, None,
"domain-name echo alone must not stamp a registrar"
);
assert!(
result.is_available(),
"current-format not-found must be reported available"
);
}
#[test]
fn test_educause_no_match_reports_available() {
let result = parse(
"notarealdomain123xyz.edu",
"whois.educause.edu",
SAMPLE_NO_MATCH,
);
assert_eq!(
result.registrar, None,
"not-found response must not carry a registrar"
);
assert!(
result.is_available(),
"unregistered .edu domain should be reported available"
);
}
#[test]
fn test_educause_contact_leading_email_not_stored_as_name() {
let result = parse("ex.edu", "whois.educause.edu", SAMPLE_LEADING_EMAIL);
assert_ne!(result.admin_name.as_deref(), Some("admin@ex.edu"));
assert_ne!(result.tech_name.as_deref(), Some("noc@ex.edu"));
assert!(
result.admin_name.is_none(),
"no real name in block; admin_name should stay None, got {:?}",
result.admin_name
);
assert!(
result.tech_name.is_none(),
"no real name in block; tech_name should stay None, got {:?}",
result.tech_name
);
assert_eq!(result.admin_email.as_deref(), Some("admin@ex.edu"));
assert_eq!(result.admin_phone.as_deref(), Some("+1.5551234567"));
assert_eq!(result.tech_email.as_deref(), Some("noc@ex.edu"));
assert_eq!(result.tech_phone.as_deref(), Some("+1.5559876543"));
}
}