<commit_before>﻿
/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Wox and other plugins, 
 * like python or other self-execute program. But, we added addtional infos (proxy and so on) into rpc request. Also, we didn't use the
 * "id" and "jsonrpc" in the request, since it's not so useful in our request model.
 * 
 * When execute a query:
 *      Wox -------JsonRPCServerRequestModel--------> client
 *      Wox <------JsonRPCQueryResponseModel--------- client
 *      
 * When execute a action (which mean user select an item in reulst item):
 *      Wox -------JsonRPCServerRequestModel--------> client
 *      Wox <------JsonRPCResponseModel-------------- client
 * 
 */

using System.Collections.Generic;
using System.Linq;
using Wox.Plugin;

namespace Wox.Core.Plugin
{
    public class JsonRPCErrorModel
    {
        public int Code { get; set; }

        public string Message { get; set; }

        public string Data { get; set; }
    }

    public class JsonRPCModelBase
    {
        public int Id { get; set; }
    }

    public class JsonRPCResponseModel : JsonRPCModelBase
    {
        public string Result { get; set; }

        public JsonRPCErrorModel Error { get; set; }
    }

    public class JsonRPCQueryResponseModel : JsonRPCResponseModel
    {
        public new List<JsonRPCResult> Result { get; set; }
    }

    public class JsonRPCRequestModel : JsonRPCModelBase
    {
        public string Method { get; set; }

        public object[] Parameters { get; set; }

        public override string ToString()
        {
            string rpc = string.Empty;
            if (Parameters != null && Parameters.Length > 0)
            {
                string parameters = Parameters.Aggregate("[", (current, o) => current + (GetParamterByType(o) + ","));
                parameters = parameters.Substring(0, parameters.Length - 1) + "]";
                rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":{1}", Method, parameters);
            }
            else
            {
                rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":[]", Method);
            }

            return rpc;

        }

        private string GetParamterByType(object paramter)
        {

            if (paramter is string)
            {
                return string.Format(@"\""{0}\""", paramter);
            }
            if (paramter is int || paramter is float || paramter is double)
            {
                return string.Format(@"{0}", paramter);
            }
            if (paramter is bool)
            {
                return string.Format(@"{0}", paramter.ToString().ToLower());
            }
            return paramter.ToString();
        }
    }

    /// <summary>
    /// Json RPC Request that Wox sent to client
    /// </summary>
    public class JsonRPCServerRequestModel : JsonRPCRequestModel
    {
        public IHttpProxy HttpProxy { get; set; }

        public override string ToString()
        {
            string rpc = base.ToString();<commit_msg>fix #249, replace escapes before sending json rpc request to plugin [WIP]<commit_after>﻿
/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Wox and other plugins, 
 * like python or other self-execute program. But, we added addtional infos (proxy and so on) into rpc request. Also, we didn't use the
 * "id" and "jsonrpc" in the request, since it's not so useful in our request model.
 * 
 * When execute a query:
 *      Wox -------JsonRPCServerRequestModel--------> client
 *      Wox <------JsonRPCQueryResponseModel--------- client
 *      
 * When execute a action (which mean user select an item in reulst item):
 *      Wox -------JsonRPCServerRequestModel--------> client
 *      Wox <------JsonRPCResponseModel-------------- client
 * 
 */

using System.Collections.Generic;
using System.Linq;
using Wox.Plugin;

namespace Wox.Core.Plugin
{
    public class JsonRPCErrorModel
    {
        public int Code { get; set; }

        public string Message { get; set; }

        public string Data { get; set; }
    }

    public class JsonRPCModelBase
    {
        public int Id { get; set; }
    }

    public class JsonRPCResponseModel : JsonRPCModelBase
    {
        public string Result { get; set; }

        public JsonRPCErrorModel Error { get; set; }
    }

    public class JsonRPCQueryResponseModel : JsonRPCResponseModel
    {
        public new List<JsonRPCResult> Result { get; set; }
    }

    public class JsonRPCRequestModel : JsonRPCModelBase
    {
        public string Method { get; set; }

        public object[] Parameters { get; set; }

        public override string ToString()
        {
            string rpc = string.Empty;
            if (Parameters != null && Parameters.Length > 0)
            {
                string parameters = Parameters.Aggregate("[", (current, o) => current + (GetParamterByType(o) + ","));
                parameters = parameters.Substring(0, parameters.Length - 1) + "]";
                rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":{1}", Method, parameters);
            }
            else
            {
                rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":[]", Method);
            }

            return rpc;

        }

        private string GetParamterByType(object paramter)
        {

            if (paramter is string)
            {
                return string.Format(@"\""{0}\""", RepalceEscapes(paramter.ToString()));
            }
            if (paramter is int || paramter is float || paramter is double)
            {
                return string.Format(@"{0}", paramter);
            }
            if (paramter is bool)
            {
                return string.Format(@"{0}", paramter.ToString().ToLower());
            }
            return paramter.ToString();
        }

        private string RepalceEscapes(string str)
        {
            return str.Replace(@"\", @"\\")  //Escapes in ProcessStartInfo
                      .Replace(@"\", @"\\");  //Escapes itself when passed to client
            //todo: replace "
        }
    }

    /// <summary>
    /// Json RPC Request that Wox sent to client
    /// </summary>
    public class JsonRPCServerRequestModel : JsonRPCRequestModel
    {
        public IHttpProxy HttpProxy { get; set; }

        public override string ToString()
        {
            string rpc = base.ToString();
{'fraction_non_alphanumeric': 0.13106200122774708, 'fraction_numerical': 0.0038367096378146104, 'mean_word_length': 1.8620992534036012, 'pattern_counts': {'":': 8, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5693122', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1728, 'n_words': 524}