-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpastebin.cpp
464 lines (423 loc) · 18.6 KB
/
pastebin.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include "pastebin.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include "pasteinfo.h"
#include "userinfo.h"
#include <QDebug>
const QString PASTEBIN_URL = "http://pastebin.com/api/";
const QString PASTEBIN_LOGIN = "api_login.php";
const QString PASTEBIN_POST = "api_post.php";
Pastebin::Pastebin(QObject *parent)
: QObject(parent)
{
m_networkAccess = new QNetworkAccessManager(this);
connect(m_networkAccess, SIGNAL(finished(QNetworkReply*)), this, SLOT(readData(QNetworkReply*)));
}
Pastebin::~Pastebin()
{
delete m_networkAccess;
}
QString Pastebin::userKey() const
{
return m_userKey;
}
void Pastebin::setUserKey(const QString &key)
{
m_userKey = key;
}
QString Pastebin::userName() const
{
return m_userName;
}
void Pastebin::setUserName(const QString &name)
{
m_userName = name;
}
QString Pastebin::password() const
{
return m_password;
}
void Pastebin::setPassword(const QString &password)
{
m_password = password;
}
QString Pastebin::developerKey() const
{
return m_developerKey;
}
void Pastebin::setDeveloperKey(const QString &key)
{
m_developerKey = key;
}
QByteArray Pastebin::data() const
{
return m_data;
}
void Pastebin::setData(const QByteArray &data)
{
m_data = data;
}
Pastebin::Language Pastebin::language() const
{
return m_language;
}
void Pastebin::setLanguage(Pastebin::Language lang)
{
m_language = lang;
}
QString Pastebin::errorString() const
{
return m_errorString;
}
void Pastebin::deletePaste(const QString& pasteKey)
{
QUrl requestUrl(PASTEBIN_URL + PASTEBIN_POST);
QUrl requestArgs;
QNetworkRequest request(requestUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
requestArgs.addEncodedQueryItem("api_dev_key", m_developerKey.toUtf8());
requestArgs.addEncodedQueryItem("api_option", "delete");
requestArgs.addEncodedQueryItem("api_paste_key", pasteKey.toUtf8());
if (!m_userKey.isEmpty())
requestArgs.addEncodedQueryItem("api_user_key", m_userKey.toUtf8());
m_networkAccess->post(request, requestArgs.encodedQuery());
}
void Pastebin::postPaste(const QByteArray& data, Language lang, const QString& title, const QString& expire, quint32 vis)
{
QUrl requestUrl(PASTEBIN_URL + PASTEBIN_POST);
QUrl requestArgs;
QNetworkRequest request(requestUrl);
QString langOpt = "text";
if (lang != PlainText)
langOpt = languageString(lang);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
requestArgs.addEncodedQueryItem("api_dev_key", m_developerKey.toUtf8());
requestArgs.addEncodedQueryItem("api_option", "paste");
requestArgs.addEncodedQueryItem("api_paste_expire_date", expire.toUtf8());
requestArgs.addEncodedQueryItem("api_paste_private", QString("%1").arg(vis).toUtf8());
requestArgs.addEncodedQueryItem("api_paste_format", langOpt.toUtf8());
requestArgs.addEncodedQueryItem("api_paste_code", QUrl::toPercentEncoding(QString(data).toAscii()));
if (!title.isEmpty())
requestArgs.addEncodedQueryItem("api_paste_name", QUrl::toPercentEncoding(title.toAscii()));
if (!m_userKey.isEmpty())
requestArgs.addEncodedQueryItem("api_user_key", m_userKey.toUtf8());
m_networkAccess->post(request, requestArgs.encodedQuery());
}
void Pastebin::login()
{
QUrl requestUrl(PASTEBIN_URL + PASTEBIN_LOGIN);
QUrl requestArgs;
QNetworkRequest request(requestUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
requestArgs.addEncodedQueryItem("api_dev_key", m_developerKey.toUtf8());
requestArgs.addEncodedQueryItem("api_user_name", QUrl::toPercentEncoding(m_userName));
requestArgs.addEncodedQueryItem("api_user_password", QUrl::toPercentEncoding(m_password));
m_networkAccess->post(request, requestArgs.encodedQuery());
}
void Pastebin::userInfo()
{
QUrl requestUrl(PASTEBIN_URL + PASTEBIN_POST);
QUrl requestArgs;
QNetworkRequest request(requestUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
requestArgs.addEncodedQueryItem("api_dev_key", m_developerKey.toUtf8());
requestArgs.addEncodedQueryItem("api_user_key", m_userKey.toUtf8());
requestArgs.addEncodedQueryItem("api_option", "userdetails");
m_networkAccess->post(request, requestArgs.encodedQuery());
}
void Pastebin::trendingPastes()
{
QUrl requestUrl(PASTEBIN_URL + PASTEBIN_POST);
QUrl requestArgs;
QNetworkRequest request(requestUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
requestArgs.addEncodedQueryItem("api_dev_key", m_developerKey.toUtf8());
requestArgs.addEncodedQueryItem("api_option", "trends");
m_networkAccess->post(request, requestArgs.encodedQuery());
}
void Pastebin::readData(QNetworkReply* reply)
{
QByteArray response = reply->readAll();
qDebug() << reply->header(QNetworkRequest::ContentTypeHeader);
if (response.contains("Bad API request"))
{
m_errorString = response;
QString code = response.split(',')[1].simplified().toLower();
if (code == "use post request, not get")
emit error(InvalidRequestType);
else if (code == "invalid api_dev_key")
emit error(InvalidDevKey);
else if (code == "invalid login")
emit error(InvalidLogin);
else if (code == "account not active")
emit error(InactiveAccount);
else if (code == "invalid post parameters")
emit error(InvalidPostParameters);
else if (code == "invalid permission to remove paste")
emit error(InvalidPermissions);
else if (code == "invalid api_option")
emit error(InvalidOption);
else if (code == "invalid api_user_key")
emit error(InvalidUserKey);
else if (code == "ip blocked")
emit error(BlockedIP);
else if (code.contains("maximum number of 10"))
emit error(Max10Exceeded);
else if (code.contains("maximum number of 25"))
emit error(Max25Exceeded);
else if (code == "invalid_expire_date")
emit error(InvalidExpireDate);
else if (code == "maximum paste file size exceeded")
emit error(MaxSizeExceeded);
else if (code == "invalid api_paste_format")
emit error(InvalidFormat);
else if (code == "api_paste_code was empty")
emit error(EmptyCode);
else
emit unknownError(response);
}
else if (reply->url().toString().contains(PASTEBIN_LOGIN))
{
m_userKey = QString(response).simplified();
emit loggedIn(m_userKey);
}
else if (reply->url().toString().contains(PASTEBIN_POST))
{
if (response.indexOf(PASTEBIN_URL) == 0)
emit pasteSuccessful(QUrl(response.simplified()));
if (response.indexOf("Paste Removed") == 0)
emit deleteSuccessful();
if (response.contains("<user>"))
emit user(UserInfo::fromData(response));
if (response.contains("<paste>"))
// We have to add opening and closing tags so QXmlStreamReader reads the data correctly.
// For some reason if the data is not enclosed, the reader flips a shit and goes into an
// infinite loop.
emit pastes(PasteInfo::fromList("<trends>\n"+response +"\n</trends>"));
}
}
QString Pastebin::languageString(Pastebin::Language lang) const
{
switch(lang)
{
case FourCS: return "4cs";
case ACME6502: return "6502acme";
case KickAss6502: return "6502kickass";
case TASM6502: return "6502tasm";
case ABAP: return "abap";
case ActionScript: return "actionscript";
case ActionScript3: return "actionscript3";
case Ada: return "ada";
case Algol68: return "algol68";
case Apache: return "apache";
case AppleScript: return "applescript";
case APTSource: return "apt_sources";
case ARM: return "arm";
case NASM: return "asm";
case ASP: return "asp";
case Asymptote: return "asymptote";
case AutoConf: return "autoconf";
case AutoHotKey: return "autohotkey";
case AutoIt: return "autoit";
case AviSynth: return "avisynth";
case Awk: return "awk";
case BASCOM_AVR: return "bascomavr";
case Bash: return "bash";
case Basic4GL: return "basic4gl";
case BibTeX: return "bibtex";
case BlitzBasic: return "blitzbasic";
case BNF: return "bnf";
case BOO: return "boo";
case BrainFuck: return "bf";
case CLang: return "c";
case CMAC: return "c_mac";
case CIL: return "cil";
case CSharp: return "csharp";
case CPP: return "cpp";
case CPPQt: return "cpp-qt";
case CLoadRunner: return "c_loadrunner";
case CADDCL: return "caddcl";
case CADLisp: return "cadlisp";
case CFDG: return "cfdg";
case ChaiScript: return "chaiscript";
case Clojure: return "clojure";
case KloneC: return "klonec";
case KloneCPP: return "klonecpp";
case CMake: return "cmake";
case COBOL: return "cobol";
case CoffeeScript: return "coffeescript";
case ColdFusion: return "cfm";
case CSS: return "css";
case CueSheet: return "cuesheet";
case DLang: return "d";
case DCL: return "dcl";
case DCPU16: return "dcpu16";
case DCS: return "dcs";
case Delphi: return "delphi";
case Oxygene: return "oxygene";
case Diff: return "diff";
case DIV: return "div";
case DOS: return "dos";
case DOT: return "dot";
case ELang: return "e";
case ECMAScript: return "ecmascript";
case Eiffel: return "eiffel";
case Email: return "email";
case EPC: return "epc";
case Erlang: return "erlang";
case FSharp: return "fsharp";
case Falcon: return "falcon";
case FOLang: return "fo";
case FormulaOne: return "f1";
case Fortran: return "fortran";
case FreeBasic: return "freebasic";
case FreeSWITCH: return "freeswitch";
case GAMBAS: return "gambas";
case GameMaker: return "gml";
case GDB: return "gdb";
case Genero: return "genero";
case Genie: return "genie";
case GetText: return "gettext";
case Go: return "go";
case Groovy: return "groovy";
case GwBasic: return "gwbasic";
case Haskell: return "haskell";
case Haxe: return "haxe";
case HicEst: return "hicest";
case HQ9Plus: return "hq9plus";
case HTML4: return "html4strict";
case HTML5: return "html5";
case Icon: return "icon";
case IDL: return "idl";
case INI: return "ini";
case INNO: return "inno";
case INTERCAL: return "intercal";
case IO: return "io";
case JLang: return "j";
case Java: return "java";
case Java5: return "java5";
case JavaScript: return "javascript";
case jQuery: return "jquery";
case KiXtart: return "kixtart";
case Latex: return "latex";
case LDIF: return "ldif";
case LibertyBasic: return "lb";
case LindenScript: return "lsl2";
case Lisp: return "lisp";
case LLVM: return "llvm";
case LocoBasic: return "locobasic";
case Logtalk: return "logtalk";
case LOLCode: return "lolcode";
case LotusFormulas: return "lotusformulas";
case LotusScript: return "lotusscript";
case LScript: return "lscript";
case Lua: return "lua";
case M68KAssembly: return "m68k";
case MagikSF: return "magiksf";
case Make: return "make";
case MapBasic: return "mapbasic";
case MatLab: return "matlab";
case mIRC: return "mirc";
case MIXAssember: return "mmix";
case Modula2: return "modula2";
case Modula3: return "modula3";
case Motorola68KHiSoftDev: return "68000devpac";
case MPASM: return "mpasm";
case MXML: return "mxml";
case MySQL: return "mysql";
case Nagios: return "nagios";
case NewLISP: return "newlisp";
case NSIS: return "nsis";
case Oberon2: return "oberon2";
case ObjeckLang: return "objeck";
case ObjectiveC: return "objc";
case OCamlBrief: return "ocaml-brief";
case OCaml: return "ocaml";
case Octave: return "octave";
case OpenBSDPacketFilter: return "pf";
case GLSL: return "glsl";
case OOBasic: return "oobas";
case Oracle11: return "oracle11";
case Oracle8: return "oracle8";
case Oz: return "oz";
case ParaSail: return "parasail";
case PariGP: return "parigp";
case Pascal: return "pascal";
case PAWN: return "pawn";
case PCRE: return "pcre";
case Per: return "per";
case Perl: return "perl";
case Perl6: return "perl6";
case PHP: return "php";
case PHPBrief: return "php-brief";
case Pic16: return "pic16";
case Pike: return "pike";
case PixelBender: return "pixelbender";
case PLSQL: return "plsql";
case PostgreSQL: return "postgresql";
case POVRay: return "povray";
case PowerShell: return "powershell";
case PowerBuilder: return "powerbuilder";
case ProFTPd: return "proftpd";
case Progress: return "progress";
case Prolog: return "prolog";
case Properties: return "properties";
case ProvideX: return "provodex";
case PureBasic: return "purebasic";
case PyCon: return "pycon";
case Python: return "python";
case PyS60: return "pys60";
case QKdb: return "q";
case QBasic: return "qbasic";
case RSPlus: return "rsplus";
case Rails: return "rails";
case REBOL: return "rebol";
case REG: return "reg";
case Rexx: return "rexx";
case Robots: return "robots";
case RPMSpec: return "rpmspec";
case Ruby: return "ruby";
case GNUPlot: return "gnuplot";
case SAS: return "sas";
case Scala: return "scala";
case Scheme: return "scheme";
case Scilab: return "scilab";
case SdlBasic: return "sdlbasic";
case Smarty: return "smarty";
case SPARK: return "spark";
case SPARQL: return "sparql";
case SQL: return "sql";
case StoneScript: return "stonescript";
case SystemVerilog: return "systemverilog";
case TSQL: return "tsql";
case TCL: return "tcl";
case TeraTerm: return "teraterm";
case ThinBasic: return "thinbasic";
case TypoScript: return "typoscript";
case Unicon: return "unicon";
case UnrealScript: return "uscript";
case UPC: return "ups";
case Urbi: return "urbi";
case Vala: return "vala";
case VBNet: return "vbnet";
case Vedit: return "vedit";
case VeriLog: return "verilog";
case VHDL: return "vhdl";
case VIM: return "vim";
case VisualProLog: return "visualprolog";
case VB: return "vb";
case VisualFoxPro: return "visualfoxpro";
case WhiteSpace: return "whitespace";
case WHOIS: return "whois";
case Winbatch: return "winbatch";
case XBasic: return "xbasic";
case XML: return "xml";
case XorgConfig: return "xorg_conf";
case XPP: return "xpp";
case YAML: return "yaml";
case Z80Assembly: return "z80";
case ZXBasic: return "zxbasic";
default: return "text";
}
}