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
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "pastebindotcomprotocol.h"
#include "pastebindotcomsettings.h"
#include <coreplugin/icore.h>
#include <utils/qtcassert.h>
#include <QtCore/QDebug>
#include <QtCore/QTextStream>
#include <QtCore/QXmlStreamReader>
#include <QtCore/QXmlStreamAttributes>
#include <QtCore/QByteArray>
#include <QtNetwork/QNetworkReply>
enum { debug = 0 };
static const char pastePhpScriptpC[] = "api_public.php";
static const char fetchPhpScriptpC[] = "raw.php";
namespace CodePaster {
PasteBinDotComProtocol::PasteBinDotComProtocol(const NetworkAccessManagerProxyPtr &nw) :
NetworkProtocol(nw),
m_settings(new PasteBinDotComSettings),
m_fetchReply(0),
m_pasteReply(0),
m_listReply(0),
m_fetchId(-1),
m_postId(-1),
m_hostChecked(false)
{
}
QString PasteBinDotComProtocol::protocolName()
{
return QLatin1String("Pastebin.Com");
}
unsigned PasteBinDotComProtocol::capabilities() const
{
return ListCapability;
}
bool PasteBinDotComProtocol::checkConfiguration(QString *errorMessage)
{
if (m_hostChecked) // Check the host once.
return true;
const bool ok = httpStatus(hostName(false), errorMessage);
if (ok)
m_hostChecked = true;
return ok;
}
QString PasteBinDotComProtocol::hostName(bool withSubDomain) const
{
QString rc;
if (withSubDomain) {
rc = m_settings->hostPrefix();
if (!rc.isEmpty())
rc.append(QLatin1Char('.'));
}
rc.append(QLatin1String("pastebin.com"));
return rc;
}
static inline QByteArray format(Protocol::ContentType ct)
{
switch (ct) {
case Protocol::Text:
break;
case Protocol::C:
return "paste_format=cpp";
break;
case Protocol::JavaScript:
return "paste_format=javascript";
break;
case Protocol::Diff:
return "paste_format=diff"; // v3.X 'dff' -> 'diff'
break;
case Protocol::Xml:
return "paste_format=xml";
break;
}
return QByteArray();
}
void PasteBinDotComProtocol::paste(const QString &text,
ContentType ct,
const QString &username,
const QString & /* comment */,
const QString & /* description */)
{
QTC_ASSERT(!m_pasteReply, return;)
// Format body
QByteArray pasteData = format(ct);
if (!pasteData.isEmpty())
pasteData.append('&');
pasteData += "paste_name=";
pasteData += QUrl::toPercentEncoding(username);
const QString subDomain = m_settings->hostPrefix();
if (!subDomain.isEmpty()) {
pasteData += "&paste_subdomain=";
pasteData += QUrl::toPercentEncoding(subDomain);
}
pasteData += "&paste_code=";
pasteData += QUrl::toPercentEncoding(fixNewLines(text));
// fire request
QString link;
QTextStream(&link) << "http://" << hostName(false) << '/' << pastePhpScriptpC;
m_pasteReply = httpPost(link, pasteData);
connect(m_pasteReply, SIGNAL(finished()), this, SLOT(pasteFinished()));
if (debug)
qDebug() << "paste: sending " << m_pasteReply << link << pasteData;
}
void PasteBinDotComProtocol::pasteFinished()
{
if (m_pasteReply->error()) {
qWarning("Pastebin.com protocol error: %s", qPrintable(m_pasteReply->errorString()));
} else {
emit pasteDone(QString::fromAscii(m_pasteReply->readAll()));
}
m_pasteReply->deleteLater();
m_pasteReply = 0;
}
void PasteBinDotComProtocol::fetch(const QString &id)
{
const QString httpProtocolPrefix = QLatin1String("http://");
QTC_ASSERT(!m_fetchReply, return;)
// Did we get a complete URL or just an id. Insert a call to the php-script
QString link;
if (id.startsWith(httpProtocolPrefix)) {
// Change "http://host/id" -> "http://host/script?i=id".
const int lastSlashPos = id.lastIndexOf(QLatin1Char('/'));
link = id.mid(0, lastSlashPos);
QTextStream(&link) << '/' << fetchPhpScriptpC<< "?i=" << id.mid(lastSlashPos + 1);
} else {
// format "http://host/script?i=id".
QTextStream(&link) << "http://" << hostName(true) << '/' << fetchPhpScriptpC<< "?i=" << id;
}
if (debug)
qDebug() << "fetch: sending " << link;
m_fetchReply = httpGet(link);
connect(m_fetchReply, SIGNAL(finished()), this, SLOT(fetchFinished()));
m_fetchId = id;
}
void PasteBinDotComProtocol::fetchFinished()
{
QString title;
QString content;
const bool error = m_fetchReply->error();
if (error) {
content = m_fetchReply->errorString();
if (debug)
qDebug() << "fetchFinished: error" << m_fetchId << content;
} else {
title = QString::fromLatin1("Pastebin.com: %1").arg(m_fetchId);
content = QString::fromAscii(m_fetchReply->readAll());
// Cut out from '<pre>' formatting
const int preEnd = content.lastIndexOf(QLatin1String("</pre>"));
if (preEnd != -1)
content.truncate(preEnd);
const int preStart = content.indexOf(QLatin1String("<pre>"));
if (preStart != -1)
content.remove(0, preStart + 5);
// Make applicable as patch.
content = Protocol::textFromHtml(content);
content += QLatin1Char('\n');
// -------------
if (debug) {
QDebug nsp = qDebug().nospace();
nsp << "fetchFinished: " << content.size() << " Bytes";
if (debug > 1)
nsp << content;
}
}
m_fetchReply->deleteLater();
m_fetchReply = 0;
emit fetchDone(title, content, error);
}
void PasteBinDotComProtocol::list()
{
QTC_ASSERT(!m_listReply, return;)
// fire request
const QString url = QLatin1String("http://") + hostName(true) + QLatin1String("/archive");
m_listReply = httpGet(url);
connect(m_listReply, SIGNAL(finished()), this, SLOT(listFinished()));
if (debug)
qDebug() << "list: sending " << url << m_listReply;
}
static inline void padString(QString *s, int len)
{
const int missing = len - s->length();
if (missing > 0)
s->append(QString(missing, QLatin1Char(' ')));
}
/* Quick & dirty: Parse out the 'archive' table as of 16.3.2011:
\code
<table class="maintable" cellspacing="0">
<tr class="top">
<th scope="col" align="left">Name / Title</th>
<th scope="col" align="left">Posted</th>
<th scope="col" align="left">Expires</th>
<th scope="col" align="left">Size</th>
<th scope="col" align="left">Syntax</th>
<th scope="col" align="left">User</th>
</tr>
<tr class="g">
<td class="icon"><a href="/8ZRqkcaP">Untitled</a></td>
<td>2 sec ago</td>
<td>Never</td>
<td>9.41 KB</td>
<td><a href="/archive/text">None</a></td>
<td>a guest</td>
</tr>
<tr>
\endcode */
enum ParseState
{
OutSideTable, WithinTable, WithinTableRow, WithinTableHeaderElement,
WithinTableElement, WithinTableElementAnchor, ParseError
};
QDebug operator<<(QDebug d, const QXmlStreamAttributes &al)
{
QDebug nospace = d.nospace();
foreach (const QXmlStreamAttribute &a, al)
nospace << a.name().toString() << '=' << a.value().toString() << ' ';
return d;
}
static inline ParseState nextOpeningState(ParseState current, const QXmlStreamReader &reader)
{
const QStringRef element = reader.name();
switch (current) {
case OutSideTable:
// Trigger on main table only.
if (element == QLatin1String("table")
&& reader.attributes().value(QLatin1String("class")) == QLatin1String("maintable"))
return WithinTable;
return OutSideTable;
case WithinTable:
if (element == QLatin1String("tr"))
return WithinTableRow;
break;
case WithinTableRow:
if (element == QLatin1String("td"))
return WithinTableElement;
if (element == QLatin1String("th"))
return WithinTableHeaderElement;
break;
case WithinTableElement:
if (element == QLatin1String("a"))
return WithinTableElementAnchor;
break;
case WithinTableHeaderElement:
case WithinTableElementAnchor:
case ParseError:
break;
}
return ParseError;
}
static inline ParseState nextClosingState(ParseState current, const QStringRef &element)
{
switch (current) {
case OutSideTable:
return OutSideTable;
case WithinTable:
if (element == QLatin1String("table"))
return OutSideTable;
break;
case WithinTableRow:
if (element == QLatin1String("tr"))
return WithinTable;
break;
case WithinTableElement:
if (element == QLatin1String("td"))
return WithinTableRow;
break;
case WithinTableHeaderElement:
if (element == QLatin1String("th"))
return WithinTableRow;
break;
case WithinTableElementAnchor:
if (element == QLatin1String("a"))
return WithinTableElement;
break;
case ParseError:
break;
}
return ParseError;
}
static inline QStringList parseLists(QIODevice *io)
{
enum { maxEntries = 200 }; // Limit the archive, which can grow quite large.
QStringList rc;
QXmlStreamReader reader(io);
ParseState state = OutSideTable;
int tableRow = 0;
int tableColumn = 0;
const QString hrefAttribute = QLatin1String("href");
//: Unknown user of paste.
const QString unknownUser = PasteBinDotComProtocol::tr("<Unknown>");
QString link;
QString user;
QString description;
while (!reader.atEnd()) {
switch(reader.readNext()) {
case QXmlStreamReader::StartElement:
state = nextOpeningState(state, reader);
switch (state) {
case WithinTableRow:
tableColumn = 0;
break;
case OutSideTable:
case WithinTable:
case WithinTableHeaderElement:
case WithinTableElement:
break;
case WithinTableElementAnchor: // 'href="/svb5K8wS"'
if (tableColumn == 0) {
link = reader.attributes().value(hrefAttribute).toString();
if (link.startsWith(QLatin1Char('/')))
link.remove(0, 1);
}
break;
case ParseError:
return rc;
} // switch startelement state
break;
case QXmlStreamReader::EndElement:
state = nextClosingState(state, reader.name());
switch (state) {
case OutSideTable:
if (tableRow) // Seen the table, bye.
return rc;
break;
case WithinTable:
// User can occasionally be empty.
if (tableRow && !link.isEmpty() && !description.isEmpty()) {
QString entry = link;
entry += QLatin1Char(' ');
entry += user.isEmpty() ? unknownUser : user;
entry += QLatin1Char(' ');
entry += description;
rc.push_back(entry);
if (rc.size() >= maxEntries)
return rc;
}
tableRow++;
user.clear();
link.clear();
description.clear();
break;
case WithinTableRow:
tableColumn++;
break;
case WithinTableHeaderElement:
case WithinTableElement:
case WithinTableElementAnchor:
break;
case ParseError:
return rc;
} // switch endelement state
break;
case QXmlStreamReader::Characters:
switch (state) {
break;
case WithinTableElement:
if (tableColumn == 5)
user = reader.text().toString();
break;
case WithinTableElementAnchor:
if (tableColumn == 0)
description = reader.text().toString();
break;
default:
break;
} // switch characters read state
break;
default:
break;
} // switch reader state
}
return rc;
}
void PasteBinDotComProtocol::listFinished()
{
const bool error = m_listReply->error();
if (error) {
if (debug)
qDebug() << "listFinished: error" << m_listReply->errorString();
} else {
QStringList list = parseLists(m_listReply);
emit listDone(name(), list);
if (debug)
qDebug() << list;
}
m_listReply->deleteLater();
m_listReply = 0;
}
Core::IOptionsPage *PasteBinDotComProtocol::settingsPage() const
{
return m_settings;
}
} // namespace CodePaster
|