You are not logged in.

#1 2019-05-19 17:56:38

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 229
Website

Any of our gurus proficient in lua(string match question)?

Hi there everyone!

I need to check usernames on login for unwanted strings(http, www, .co) and my string check is not taking punctuation into account so "TomScott" matches to my check for ".co".

Here's my config string:

Config.illchar_strings 		= {"http", "=", "script", ".co", "www", ".net", ".org"}

and here's how I'm checking it:

                        local conName = string.gsub(name, "%s+", "") --TomScott
			for i, illstring in ipairs(Config.illchar_strings) do

				if string.find(string.lower(conName), string.lower(illstring)) then

					--Drop the player here

				end

			end

Can anyone tell me how I would alter this to take the punctuation into account when checking the name?


Schw.im! A social site with an identity crisis.

Offline

#2 2019-05-20 05:35:53

ohnonot
...again
Registered: 2015-09-29
Posts: 3,988
Website

Re: Any of our gurus proficient in lua(string match question)?

not a lua expert at all, but this:

schwim wrote:

I need to check usernames on login for unwanted strings(http, www, .co) and my string check is not taking punctuation into account so "TomScott" matches to my check for ".co".

sounds like a fairly common problem with regex or wildcard matching; the dot '.' denotes "any character".
try escaping it:

{"http", "=", "script", "\.co", "www", "\.net", "\.org"}

also it would make sense to match tld's only at the end of the string, yes? the syntax might or might not be this:

{"http", "=", "script", "\.co$", "www", "\.net$", "\.org$"}

do some websearches for "regex to block spammers" or something like that.

Offline

#3 2019-05-20 13:14:31

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 229
Website

Re: Any of our gurus proficient in lua(string match question)?

thanks very much for the help, ohno!

I did try escaping the character but that breaks my script, spitting errors causing it to report that the variable in question is missing.

Our spam links are often ".co/YouCouldWin" so I don't think matching at the end is the solution in this particular case.


Schw.im! A social site with an identity crisis.

Offline

#4 2019-05-20 19:01:58

twoion
ほやほや
Registered: 2015-08-10
Posts: 2,501

Re: Any of our gurus proficient in lua(string match question)?

Remember to read the Lua manual on each function diligently; string.find() by default finds the first occurrence of a pattern, which means that it has regex matches enabled. The punctuation in your case becomes the match-all symbol '.'. In your current implementation, you either have to escape the dot

%.co

(ohnonot is wrong in suggesting \ as the escape character; in Lua it's %) or disable the pattern matching in string.find() by specfying more arguments, in which case string.find() will, as you intended, just look for the substring given:

local function filterIllegals(name, ...)
  local normalizedName = name:lower()
  for _,s in ipairs(Config.illchar_strings) do
    if normalizedName:find(s, 1, true) then
      print("DROPPED", s, "because of", s)
    end
  end
end

as the full form of string.find() is

string.find (s, pattern [, init [, plain]])

as shown in https://www.lua.org/manual/5.3/manual.h … tring.find .

EDIT: Note that by keeping regex matching enabled, you could write stuff like

%.[Cc][oO]

to capture both the lower- and uppercased version of the illegal substring, though you probably know better than me what's best in this case.


A silent kite against the blue, blue sky

Offline

#5 2019-05-21 20:08:26

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 229
Website

Re: Any of our gurus proficient in lua(string match question)?

twoion, thanks so much for your help!  That made so much more sense to me than the info I tried to glean from the lua help docs.  I can't thank you enough for laymanising it for me!


Schw.im! A social site with an identity crisis.

Offline

Board footer

Powered by FluxBB