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;
|
2018-10-08 22:33:20 +13:00
|
|
|
|
using System.Linq;
|
2018-10-08 19:10:59 +13:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2018-10-10 00:25:27 +13:00
|
|
|
|
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 = '|';
|
|
|
|
|
|
|
|
|
|
public bool IsOutput { get; set; }
|
|
|
|
|
public string Response { get; private set; }
|
|
|
|
|
public List<Match> RegexMatches { get; private set; }
|
|
|
|
|
|
|
|
|
|
public CustomUploaderParser()
|
|
|
|
|
{
|
|
|
|
|
IsOutput = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 result = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
bool syntaxStart = false;
|
|
|
|
|
int syntaxStartIndex = 0;
|
|
|
|
|
bool escape = false;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < text.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (text[i] == SyntaxChar && !escape)
|
|
|
|
|
{
|
|
|
|
|
if (!syntaxStart)
|
|
|
|
|
{
|
|
|
|
|
syntaxStart = true;
|
|
|
|
|
syntaxStartIndex = i + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
syntaxStart = false;
|
|
|
|
|
int syntaxLength = i - syntaxStartIndex;
|
|
|
|
|
|
|
|
|
|
if (syntaxLength > 0)
|
|
|
|
|
{
|
|
|
|
|
string syntax = text.Substring(syntaxStartIndex, syntaxLength);
|
|
|
|
|
string syntaxResult = ParseSyntax(syntax);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(syntaxResult))
|
|
|
|
|
{
|
|
|
|
|
result.Append(syntaxResult);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
escape = false;
|
|
|
|
|
}
|
|
|
|
|
else if (text[i] == '\\' && !escape)
|
|
|
|
|
{
|
|
|
|
|
escape = true;
|
|
|
|
|
}
|
|
|
|
|
else if (!syntaxStart)
|
|
|
|
|
{
|
|
|
|
|
result.Append(text[i]);
|
|
|
|
|
escape = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string ParseSyntax(string syntax)
|
|
|
|
|
{
|
|
|
|
|
if (IsOutput)
|
|
|
|
|
{
|
|
|
|
|
if (syntax.Equals("response", StringComparison.InvariantCultureIgnoreCase)) // Example: $response$
|
|
|
|
|
{
|
|
|
|
|
return Response;
|
|
|
|
|
}
|
|
|
|
|
else if (syntax.StartsWith("regex:", StringComparison.InvariantCultureIgnoreCase)) // Example: $regex:1|1$
|
|
|
|
|
{
|
2018-10-08 22:33:20 +13:00
|
|
|
|
return ParseSyntaxRegex(syntax.Substring(6));
|
2018-10-08 19:10:59 +13:00
|
|
|
|
}
|
|
|
|
|
else if (syntax.StartsWith("json:", StringComparison.InvariantCultureIgnoreCase)) // Example: $json:Files[0].URL$
|
|
|
|
|
{
|
2018-10-08 22:33:20 +13:00
|
|
|
|
return ParseSyntaxJson(syntax.Substring(5));
|
2018-10-08 19:10:59 +13:00
|
|
|
|
}
|
|
|
|
|
else if (syntax.StartsWith("xml:", StringComparison.InvariantCultureIgnoreCase)) // Example: $xml:/Files/File[1]/URL$
|
|
|
|
|
{
|
2018-10-08 22:33:20 +13:00
|
|
|
|
return ParseSyntaxXml(syntax.Substring(4));
|
2018-10-08 19:10:59 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (syntax.StartsWith("random:", StringComparison.InvariantCultureIgnoreCase)) // Example: $random:domain1.com|domain2.com$
|
|
|
|
|
{
|
2018-10-08 22:33:20 +13:00
|
|
|
|
return ParseSyntaxRandom(syntax.Substring(7));
|
|
|
|
|
}
|
|
|
|
|
else if (syntax.StartsWith("select:", StringComparison.InvariantCultureIgnoreCase)) // Example: $select:domain1.com|domain2.com$
|
|
|
|
|
{
|
|
|
|
|
return ParseSyntaxSelect(syntax.Substring(7));
|
2018-10-08 19:10:59 +13:00
|
|
|
|
}
|
2018-10-10 00:25:27 +13:00
|
|
|
|
else if (syntax.Equals("input", StringComparison.InvariantCultureIgnoreCase)) // Example: $input$
|
|
|
|
|
{
|
|
|
|
|
return ParseSyntaxInput();
|
|
|
|
|
}
|
|
|
|
|
else if (syntax.StartsWith("input:", StringComparison.InvariantCultureIgnoreCase)) // Example: $input:default value$
|
|
|
|
|
{
|
|
|
|
|
return ParseSyntaxInput(syntax.Substring(6));
|
|
|
|
|
}
|
2018-10-08 19:10:59 +13:00
|
|
|
|
|
2018-10-08 22:33:20 +13:00
|
|
|
|
// Invalid syntax
|
2018-10-08 19:10:59 +13:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-08 22:33:20 +13:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-10-08 22:33:20 +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);
|
|
|
|
|
|
2018-10-08 22:33:20 +13:00
|
|
|
|
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/
|
2018-10-08 22:33:20 +13:00
|
|
|
|
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
|
2018-10-08 22:33:20 +13:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-08 22:33:20 +13:00
|
|
|
|
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
|
|
|
|
}
|
2018-10-08 22:33:20 +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-10 00:25:27 +13:00
|
|
|
|
|
|
|
|
|
private string ParseSyntaxInput(string defaultValue = null)
|
|
|
|
|
{
|
|
|
|
|
using (InputBox inputBox = new InputBox("ShareX - Input", defaultValue))
|
|
|
|
|
{
|
|
|
|
|
if (inputBox.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
return inputBox.InputText;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
2018-10-08 19:10:59 +13:00
|
|
|
|
}
|
|
|
|
|
}
|