summaryrefslogtreecommitdiff
path: root/plugins/PasteIt/src/PasteToWeb2.cpp
blob: e77def7eb07383c32aa089ff21bd8744ac55beff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
Paste It plugin
Copyright (C) 2011 Krzysztof Kral

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 version 2
of the License.

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, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"

PasteFormat PasteToWeb2::defFormats[] =
{
	{ L"text", L"Text" },
	{ L"xml", L"XML" },
	{ L"html", L"HTML" },
	{ L"js", L"JavaScript" },
	{ L"php", L"PHP" },
	{ L"c", L"C" },
	{ L"csharp", L"C#" },
	{ L"cpp", L"C++" },
	{ L"java", L"Java" },
	{ L"ini", L"INI" },
	{ L"css", L"CSS" },
	{ L"sql", L"SQL" },
	{ L"nasm", L"NASM" },
	{ L"delphi", L"Delphi" },
	{ L"diff", L"Diff" },
	{ L"tex", L"TeX" },
	{ L"perl", L"Perl" },
	{ L"python", L"Python" },
	{ L"vb.net", L"VB.net" }
};

PasteToWeb2::PasteToWeb2()
{
}


PasteToWeb2::~PasteToWeb2()
{
}

void PasteToWeb2::SendToServer(std::wstring str, std::wstring fileName, std::wstring format)
{
	std::map<std::string, std::string> headers;
	headers["Content-Type"] = "text/xml";
	std::wstring content = L"<?xml version=\"1.0\"?>\r\n<methodCall><methodName>create_paste</methodName><params><param><value>";
	if (fileName == L"") {
		content += format;
		content += L"</value></param><param><value>";
	}
	else content += L"</value></param><param><value>";

	for (std::wstring::iterator it = str.begin(); it != str.end(); ++it) {
		if (*it == L'&') {
			content += L"&amp;";
		}
		else if (*it == L'<') {
			content += L"&lt;";
		}
		else {
			content += *it;
		}
	}

	content += L"</value></param><param><value></value></param><param><value>";
	if (fileName != L"") {
		for (std::wstring::iterator it = fileName.begin(); it != fileName.end(); ++it) {
			if (*it == L'&') {
				content += L"&amp;";
			}
			else if (*it == L'<') {
				content += L"&lt;";
			}
			else {
				content += *it;
			}
		}
	}
	content += L"</value></param><param><value></value></param><param><value><double>1.5</double></value></param></params></methodCall>";

	char *resCont = SendToWeb("http://wklej.to/api/", headers, content);
	error = TranslateT("Error during sending text to web page");
	if (resCont != nullptr) {
		TiXmlDocument doc;
		if (0 == doc.Parse(resCont)) {
			auto *pData = TiXmlConst(&doc)["methodResponse"]["params"]["param"]["value"]["array"]["data"].ToElement();
			for (auto *it : TiXmlFilter(pData, "value")) {
				auto *pString = it->FirstChildElement("string");
				if (pString) {
					mir_strncpy(szFileLink, pString->GetText(), _countof(szFileLink));
					error = nullptr;
				}
			}
		}
		mir_free(resCont);
	}
}

std::list<PasteFormat> PasteToWeb2::GetFormats()
{
	std::list<PasteFormat> ret;

	std::map<std::string, std::string> headers;
	headers["Content-Type"] = "text/xml";
	std::wstring content = L"<?xml version=\"1.0\"?>\r\n<methodCall><methodName>types</methodName></methodCall>";

	char* resCont = SendToWeb("http://wklej.to/api/", headers, content);
	if (resCont != nullptr) {
		TiXmlDocument doc;
		if (0 == doc.Parse(resCont)) {
			auto *pData = TiXmlConst(&doc)["methodResponse"]["params"]["param"]["value"]["array"]["data"].ToElement();
			for (auto *it : TiXmlFilter(pData, "value")) {
				auto *pString = it->FirstChildElement("string");
				if (pString == nullptr)
					continue;

				std::wstring str = Utf2T(pString->GetText()).get();
				std::wstring::size_type pos = str.find(L'\n');
				if (pos < str.length()) {
					str = str.substr(pos + 1);
				}
				pos = str.find(L'\n');
				if (pos < str.length()) {
					str = str.substr(pos + 1);
				}
				pos = str.find(L'\n');
				while (pos < str.length()) {
					std::wstring line = str.substr(0, pos);
					std::wstring::size_type sep = line.find(L':');
					if (sep < line.length()) {
						PasteFormat pf;
						pf.name = line.substr(0, sep);
						std::wstring::size_type sep2 = line.find(L',');
						if (sep2 < line.length()) {
							pf.id = line.substr(sep + 2, sep2 - sep - 2);
						}
						else {
							pf.id = line.substr(sep + 2);
						}
						ret.push_back(pf);
					}

					if (pos < str.length() - 1) {
						str = str.substr(pos + 1);
					}
					else {
						str = L"";
					}
					pos = str.find(L'\n');
				}
				{
					std::wstring line = str;
					std::wstring::size_type sep = line.find(L':');
					if (sep < line.length()) {
						PasteFormat pf;
						pf.name = line.substr(0, sep);
						std::wstring::size_type sep2 = line.find(L',');
						if (sep2 < line.length()) {
							pf.id = line.substr(sep + 2, sep2 - sep - 2);
						}
						else {
							pf.id = line.substr(sep + 2);
						}
						ret.push_back(pf);
					}
				}
			}
		}
		mir_free(resCont);
	}

	return ret;
}

std::list<PasteFormat> PasteToWeb2::GetDefFormats()
{
	int count = sizeof(defFormats) / sizeof(defFormats[0]);
	std::list<PasteFormat> ret(defFormats, defFormats + count);

	return ret;
}

void PasteToWeb2::ConfigureSettings()
{
}