ShareX/ShareX.UploadersLib/Helpers/CustomUploaderParser.cs

342 lines
10 KiB
C#
Raw Normal View History

2018-10-08 19:10:59 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2018 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using Newtonsoft.Json.Linq;
using ShareX.HelpersLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2018-10-08 19:10:59 +13:00
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
2018-10-08 19:10:59 +13:00
using System.Xml.XPath;
namespace ShareX.UploadersLib
{
public class CustomUploaderParser
{
public const char SyntaxChar = '$';
public const char SyntaxParameterChar = '|';
2018-10-31 23:21:13 +13:00
public const char SyntaxEscapeChar = '\\';
2018-10-08 19:10:59 +13:00
public bool IsOutput { get; set; }
public string Filename { get; private set; }
public string Input { get; private set; }
2018-10-31 23:21:13 +13:00
public string Response { get; private set; }
public List<Match> RegexMatches { get; private set; }
2018-10-08 19:10:59 +13:00
public CustomUploaderParser()
{
IsOutput = false;
}
2018-10-31 23:21:13 +13:00
public CustomUploaderParser(string filename, string input)
{
Filename = filename;
Input = input;
IsOutput = false;
}
2018-10-08 19:10:59 +13:00
public CustomUploaderParser(string response, List<string> regexList)
{
Response = response;
RegexMatches = new List<Match>();
if (regexList != null)
{
foreach (string regex in regexList)
{
Match match = Regex.Match(response, regex);
RegexMatches.Add(match);
}
}
IsOutput = true;
}
public string Parse(string text)
{
if (string.IsNullOrEmpty(text))
{
return "";
}
StringBuilder sbResult = new StringBuilder();
StringBuilder sbSyntax = new StringBuilder();
bool escapeNext = false;
bool parsingSyntax = false;
2018-10-08 19:10:59 +13:00
for (int i = 0; i < text.Length; i++)
{
if (!escapeNext && text[i] == SyntaxChar)
2018-10-08 19:10:59 +13:00
{
if (!parsingSyntax)
2018-10-08 19:10:59 +13:00
{
parsingSyntax = true;
sbSyntax.Clear();
2018-10-08 19:10:59 +13:00
}
else
{
parsingSyntax = false;
2018-10-08 19:10:59 +13:00
string syntax = sbSyntax.ToString();
if (!string.IsNullOrEmpty(syntax))
2018-10-08 19:10:59 +13:00
{
string syntaxResult = ParseSyntax(syntax);
if (!string.IsNullOrEmpty(syntaxResult))
{
sbResult.Append(syntaxResult);
2018-10-08 19:10:59 +13:00
}
}
}
}
else if (!escapeNext && text[i] == SyntaxEscapeChar)
2018-10-08 19:10:59 +13:00
{
escapeNext = true;
2018-10-08 19:10:59 +13:00
}
2018-11-01 00:45:55 +13:00
else
2018-10-08 19:10:59 +13:00
{
escapeNext = false;
if (!parsingSyntax)
2018-11-01 00:45:55 +13:00
{
sbResult.Append(text[i]);
}
else
{
sbSyntax.Append(text[i]);
2018-11-01 00:45:55 +13:00
}
2018-10-08 19:10:59 +13:00
}
}
return sbResult.ToString();
2018-10-08 19:10:59 +13:00
}
private string ParseSyntax(string syntax)
{
string value;
2018-10-08 19:10:59 +13:00
if (IsOutput)
{
if (CheckKeyword(syntax, "response")) // Example: $response$
2018-10-08 19:10:59 +13:00
{
return Response;
}
else if (CheckKeyword(syntax, "regex", out value)) // Examples: $regex:1$ $regex:1|1$ $regex:1|thumbnail$
2018-10-08 19:10:59 +13:00
{
return ParseSyntaxRegex(value);
2018-10-08 19:10:59 +13:00
}
else if (CheckKeyword(syntax, "json", out value)) // Example: $json:Files[0].URL$
2018-10-08 19:10:59 +13:00
{
return ParseSyntaxJson(value);
2018-10-08 19:10:59 +13:00
}
else if (CheckKeyword(syntax, "xml", out value)) // Example: $xml:/Files/File[1]/URL$
2018-10-08 19:10:59 +13:00
{
return ParseSyntaxXml(value);
2018-10-08 19:10:59 +13:00
}
}
2018-10-31 23:21:13 +13:00
if (CheckKeyword(syntax, "input")) // Example: $input$
{
2018-10-31 23:21:13 +13:00
return Input;
}
2018-10-31 23:21:13 +13:00
else if (CheckKeyword(syntax, "filename")) // Example: $filename$
2018-10-08 19:10:59 +13:00
{
2018-10-31 23:21:13 +13:00
return Filename;
}
else if (CheckKeyword(syntax, "random", out value)) // Example: $random:domain1.com|domain2.com$
{
return ParseSyntaxRandom(value);
2018-10-08 19:10:59 +13:00
}
else if (CheckKeyword(syntax, "select", out value)) // Example: $select:domain1.com|domain2.com$
{
return ParseSyntaxSelect(value);
}
else if (CheckKeyword(syntax, "prompt", out value)) // Examples: $prompt$ $prompt:default value$
{
return ParseSyntaxPrompt(value);
}
2018-10-08 19:10:59 +13:00
// Invalid syntax
2018-10-08 19:10:59 +13:00
return null;
}
private bool CheckKeyword(string syntax, string keyword)
{
return CheckKeyword(syntax, keyword, out _);
}
private bool CheckKeyword(string syntax, string keyword, out string value)
{
value = null;
if (syntax.Equals(keyword, StringComparison.InvariantCultureIgnoreCase))
{
return true;
}
else if (syntax.StartsWith(keyword + ":", StringComparison.InvariantCultureIgnoreCase))
{
2018-10-31 23:21:13 +13:00
value = syntax.Substring(keyword.Length + 1);
return true;
}
return false;
}
private string ParseSyntaxRegex(string syntax)
2018-10-08 19:10:59 +13:00
{
if (!string.IsNullOrEmpty(syntax))
{
2018-10-08 19:34:29 +13:00
string regexIndexString = "";
bool isGroupRegex = false;
int i;
2018-10-08 19:10:59 +13:00
2018-10-08 19:34:29 +13:00
for (i = 0; i < syntax.Length; i++)
2018-10-08 19:10:59 +13:00
{
2018-10-08 19:34:29 +13:00
if (char.IsDigit(syntax[i]))
2018-10-08 19:10:59 +13:00
{
2018-10-08 19:34:29 +13:00
regexIndexString += syntax[i];
2018-10-08 19:10:59 +13:00
}
2018-10-08 19:34:29 +13:00
else
{
if (syntax[i] == SyntaxParameterChar || syntax[i] == ',') // , for backward compatibility
{
isGroupRegex = true;
}
2018-10-08 19:10:59 +13:00
2018-10-08 19:34:29 +13:00
break;
}
2018-10-08 19:10:59 +13:00
}
if (regexIndexString.Length > 0 && int.TryParse(regexIndexString, out int regexIndex))
2018-10-08 19:10:59 +13:00
{
2018-10-08 19:34:29 +13:00
Match match = RegexMatches[regexIndex - 1];
2018-10-08 19:10:59 +13:00
2018-10-08 19:34:29 +13:00
if (isGroupRegex && i + 1 < syntax.Length)
2018-10-08 19:10:59 +13:00
{
2018-10-08 19:34:29 +13:00
string group = syntax.Substring(i + 1);
if (int.TryParse(group, out int groupNumber))
2018-10-08 19:34:29 +13:00
{
return match.Groups[groupNumber].Value;
}
return match.Groups[group].Value;
2018-10-08 19:10:59 +13:00
}
2018-10-08 19:34:29 +13:00
return match.Value;
2018-10-08 19:10:59 +13:00
}
}
return null;
}
// http://goessner.net/articles/JsonPath/
private string ParseSyntaxJson(string syntaxJsonPath)
2018-10-08 19:10:59 +13:00
{
2018-10-08 19:34:29 +13:00
if (!string.IsNullOrEmpty(syntaxJsonPath))
{
return (string)JToken.Parse(Response).SelectToken("$." + syntaxJsonPath);
}
return null;
2018-10-08 19:10:59 +13:00
}
// http://www.w3schools.com/xsl/xpath_syntax.asp
// https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx
private string ParseSyntaxXml(string syntaxXPath)
2018-10-08 19:10:59 +13:00
{
2018-10-08 19:34:29 +13:00
if (!string.IsNullOrEmpty(syntaxXPath))
2018-10-08 19:10:59 +13:00
{
2018-10-08 19:34:29 +13:00
using (StringReader sr = new StringReader(Response))
2018-10-08 19:10:59 +13:00
{
2018-10-08 19:34:29 +13:00
XPathDocument doc = new XPathDocument(sr);
XPathNavigator nav = doc.CreateNavigator();
XPathNavigator node = nav.SelectSingleNode(syntaxXPath);
if (node != null)
{
return node.Value;
}
2018-10-08 19:10:59 +13:00
}
}
return null;
}
private string ParseSyntaxRandom(string syntax)
2018-10-08 19:10:59 +13:00
{
2018-10-08 19:34:29 +13:00
if (!string.IsNullOrEmpty(syntax))
2018-10-08 19:10:59 +13:00
{
2018-10-08 19:34:29 +13:00
string[] values = syntax.Split(SyntaxParameterChar);
if (values.Length > 0)
{
return values[MathHelpers.Random(values.Length - 1)];
}
2018-10-08 19:10:59 +13:00
}
2018-10-08 19:34:29 +13:00
return null;
2018-10-08 19:10:59 +13:00
}
private string ParseSyntaxSelect(string syntax)
{
if (!string.IsNullOrEmpty(syntax))
{
string[] values = syntax.Split(SyntaxParameterChar).Where(x => !string.IsNullOrEmpty(x)).ToArray();
if (values.Length > 0)
{
using (ParserSelectForm form = new ParserSelectForm(values))
{
form.ShowDialog();
return form.SelectedText;
}
}
}
return null;
}
2018-10-31 20:17:58 +13:00
private string ParseSyntaxPrompt(string defaultValue = null)
{
2018-10-31 20:17:58 +13:00
using (InputBox inputBox = new InputBox("ShareX - Prompt", defaultValue))
{
if (inputBox.ShowDialog() == DialogResult.OK)
{
return inputBox.InputText;
}
}
return defaultValue;
}
2018-10-08 19:10:59 +13:00
}
}