Remove Unsafe Characters

Please post any questions about developing your plugin here. Please use the search function before posting!
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Remove Unsafe Characters

Postby decompile » Tue Aug 23, 2016 9:39 am

Hey,

Im currently on thinking about adding a "player connect/disconnect announcer" and thought about what fixes are needed since players can completly modify their names.

After abit of google research I created a function which returns a save name which i can use in Saytext2

Syntax: Select all

unsafeLetters = [r"\a", r"\b", r"\f", r"\n", r"\r", r"\t", r"\v", r"%s"]
def getSaveString(string):
""" Strips colorcodes and newlinecharacters"""
newstring = ""
for x in string:
if ord(x) > 8:
newstring += x
newstring = repr(newstring)
for x in unsafeLetters:
newstring = newstring.replace(x, r"\\"+x)
newstring = eval(newstring)
return newstring


I havent tested it yet. + I dont know what happens when a player joins with "%s"
kalle
Junior Member
Posts: 26
Joined: Sun Jun 26, 2016 9:48 am
Contact:

Re: Remove Unsafe Characters

Postby kalle » Tue Aug 23, 2016 5:44 pm

Wouldn't it be easier to allow only certain characters? Would be better to allow things you know that there are not harmful, eg a-zA-Z0-9-_ and so on. Is called regular expressions and should work beautiful with python :)
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Re: Remove Unsafe Characters

Postby arawra » Tue Aug 23, 2016 6:10 pm

If you're worried about unsafe characters, you may also want to replace many unicode characters.
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Remove Unsafe Characters

Postby decompile » Wed Aug 24, 2016 12:45 am

I guess there are no other unsafe characters than those above. Maybe %s needs to be fixed kinda, seems it doesnt work with the top function.

Lets take an example connect message.

PlayerName: %s test

Code: Select all

Player %s test connected from Reserved.


Using this in a SayText2 message, just prints me

Code: Select all

Player


So how could I actually prevent this message to get corrupted without replacing %s
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Remove Unsafe Characters

Postby iPlayer » Wed Aug 24, 2016 5:22 pm

Syntax: Select all

from filters.players import PlayerIter
from messages import SayText2


def load():
for player in PlayerIter('human'):
SayText2("Player {} connected from Reserved.".format(player.name)).send(player.index)
break

Works fine for me.
As for color codes, I haven't been able to include "\x07" byte nor, for example, "\x02" byte in my steam nickname. It was just stripped. I tried both desktop app and web interface.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 10 guests