yavsc/src/Abstract/Chat/HubInputValidator.cs

90 lines
3.1 KiB
C#
Raw Normal View History

2019-06-18 14:01:33 +01:00
//
// ChatHub.cs
//
// Author:
// Paul Schneider <paul@pschneider.fr>
//
// Copyright (c) 2016-2019 GNU GPL
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Linq;
2019-10-08 23:41:19 +01:00
using Yavsc;
2019-06-18 14:01:33 +01:00
namespace Yavsc
{
public class HubInputValidator {
public Action<string,string,string> NotifyUser {get;set;}
public bool ValidateRoomName (string roomName)
{
bool valid = ValidateStringLength(roomName,1,25);
if (valid) valid = IsLetterOrDigit(roomName);
2019-11-14 14:04:33 +00:00
if (!valid) NotifyUser(NotificationTypes.Error, "roomName", ChatHubLabels.InvalidRoomName);
2019-06-18 14:01:33 +01:00
return valid;
}
public bool ValidateUserName (string userName)
{
bool valid = true;
if (userName.Length<1 || userName[0] == '?' && userName.Length<2) valid = false;
if (valid) {
string suname = (userName[0] == '?') ? userName.Substring(1) : userName;
if (valid) valid = ValidateStringLength(suname, 1,12);
if (valid) valid = IsLetterOrDigit(userName);
}
2019-11-14 14:04:33 +00:00
if (!valid) NotifyUser(NotificationTypes.Error, "userName" , ChatHubLabels.InvalidUserName);
2019-06-18 14:01:33 +01:00
return valid;
}
public bool ValidateMessage (string message)
{
if (!ValidateStringLength(message, 1, 10240))
2019-06-18 14:01:33 +01:00
{
2019-11-14 14:04:33 +00:00
NotifyUser(NotificationTypes.Error, "message", ChatHubLabels.InvalidMessage);
2019-06-18 14:01:33 +01:00
return false;
}
return true;
}
public bool ValidateReason (string reason)
{
if (!ValidateStringLength(reason, 1,240))
{
2019-11-14 14:04:33 +00:00
NotifyUser(NotificationTypes.Error, "reason", ChatHubLabels.InvalidReason);
2019-06-18 14:01:33 +01:00
return false;
}
return true;
}
static bool ValidateStringLength(string str, int minLen, int maxLen)
{
if (string.IsNullOrEmpty(str))
{
if (minLen<=0) {
return true;
} else {
return false;
}
}
if (str.Length>maxLen||str.Length<minLen) return false;
return true;
}
static bool IsLetterOrDigit(string s)
{
foreach (var c in s)
if (!char.IsLetterOrDigit(c))
return false;
return true;
}
}
}