Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- ####--------------------------------####
- #--# Author: by uriid1 #--#
- #--# License: GNU GPLv3 #--#
- #--# Telegram: @main_moderator #--#
- #--# E-mail: appdurov@gmail.com #--#
- ####--------------------------------####
- --]]
- local function email_validator(str)
- if type(str) ~= 'string' then
- return false, 'E-mail is not a string'
- end
- -- Capture
- str = str:lower()
- local re_local_part = '([a-z0-9%.!#%$%%&\'%*%+%-/=%?^_`{|}~%(%) "]+)'
- local re_domain = '([a-z0-9%(%)%-%.:%[%]]+)'
- local local_part, domain = str:match('^'..re_local_part..'@'..re_domain..'$')
- -- Check exists local-part and domain
- if not local_part or not domain then
- return false, 'Invalid local-part or domain name'
- end
- -- Check length
- if #local_part > 64 then
- return false, 'Local-part over 64 symbol'
- elseif #domain > 255 then
- return false, 'Domain over 255 symbol'
- end
- -- Check local-part in quotes
- local text_in_quotes = local_part:match('^"(.+)"$')
- if text_in_quotes then
- if text_in_quotes:match('[^a-z0-9!#$%%&\'%*%+%-/=%?^_ `{|}~%.]') then
- return false, 'Invalid characters in quotes local_part'
- end
- else
- if local_part:find('"') then
- return false, 'Invalid character " in local-part'
- end
- end
- -- Check comment
- local comment_in_local_part = local_part:match('(%(.+%))')
- if comment_in_local_part then
- local some_text, count_char = comment_in_local_part:gsub('[()]', '')
- if count_char > 2 then
- return false, 'Multiple comment in local-part are not allowed'
- end
- local_part = local_part:gsub('%('..some_text..'%)', '')
- end
- local comment_in_domain = domain:match('(%(.+%))')
- if comment_in_domain then
- local some_text, count_char = comment_in_domain:gsub('[()]', '')
- if count_char > 2 then
- return false, 'Multiple comment in domain are not allowed'
- end
- domain = domain:gsub('%('..some_text..'%)', '')
- end
- -- Check [] characters in local-part
- if local_part:match('%[.+%]') then
- return false, 'Invalid local-part, characters []'
- end
- -- Check correct local-part
- if local_part:find("%.%.") or
- local_part:find("%-%-") or
- local_part:find("%+%+") then
- return false, 'Too many periods in local-part'
- end
- -- Dot pos check in local-part
- if local_part:sub(1, 1) == '.' then
- return false, 'Dot cannot be the first character'
- elseif local_part:sub(-1) == '.' then
- return false, 'Dot cannot be the last character'
- end
- -- DNS domain check
- local hostname = domain:match('^[%d%a%-%.]+$')
- if hostname then
- if domain:sub(0, 1) == '.' or
- domain:sub(-1) == '.' or
- domain:find('%.%.')
- then
- return false, 'Invalid domain name'
- end
- end
- -- IpV4
- local ipv4 = domain:match('^%[(.+)%]$')
- if ipv4 then
- local a, b, c, d = domain:match('(%d+)%.(%d+)%.(%d+)%.(%d+)')
- if a and b and c and d then
- if not ((tonumber(a) < 256) and
- (tonumber(b) < 256) and
- (tonumber(c) < 256) and
- (tonumber(d) < 256))
- then
- return false, 'Invalid ipV4 domain'
- end
- end
- end
- -- ipV6
- local ipv6 = domain:match('^%[ipv6:(.-)%]$')
- if ipv6 then
- if not ipv6:find('^[a-f0-9]+'..(':[a-f0-9]+'):rep(7)..'$') then
- return false, 'Invalid IPv6 domain'
- end
- end
- return true
- end
- -- Test
- print('[-- Valid --]')
- local valid = {
- 'appdurov@domain.co.tk';
- 'disposable.style.email.with+symbol@example.com';
- 'very.common@example.com';
- 'simple@[127.128.0.255]';
- 'postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]';
- 'postmaster(coment)@domain.lol';
- 'postmaster@(comment)domain.lol';
- }
- for i = 1, #valid do
- local res, err = email_validator(valid[i])
- if res then
- print('Test:', i, res)
- else
- print('Test:', i, res, valid[i], err)
- end
- end
- print('\n[-- Invalid --]')
- local invalid = {
- 'blablabla';
- ' appdurov@gmail.com ';
- '.John.Doe@example.com';
- 'John.Doe.@example.com';
- 'Abc.example.com';
- 'A@b@c@example.com';
- 'a"b(c)d,e:f;g<h>i[j\\k]l@example.com';
- 'just"not"right@example.com';
- 'this is"not\allowed@example.com';
- 'this\\ still\\"not\\allowed@example.com';
- '1234567890123456789012345678901234567890123456789012345678901234+x@example.com';
- 'i_like_underscore@but_its_not_allowed_in_this_part.example.com';
- 'QA[icon]CHOCOLATE[icon]@test.com';
- '[]!@#$%^&@gmail.com';
- }
- for i = 1, #invalid do
- local res, err = email_validator(invalid[i])
- if not res then
- print('Test:', i, res)
- else
- print('Test:', i, res, invalid[i], err)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement