aboutsummaryrefslogtreecommitdiff
path: root/source/QXmppTransferManager.cpp
blob: f86fcf09f1f2e343e69bebe2730dc1d266c4fe9e (plain) (blame)
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
/*
 * Copyright (C) 2010 Bolloré telecom
 *
 * Author:
 *	Jeremy Lainé
 *
 * Source:
 *	http://code.google.com/p/qxmpp
 *
 * This file is a part of QXmpp library.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 */

#include <QDomElement>
#include <QFile>
#include <QFileInfo>
#include <QNetworkInterface>
#include <QTimer>

#include "QXmppByteStreamIq.h"
#include "QXmppConstants.h"
#include "QXmppIbbIq.h"
#include "QXmppLogger.h"
#include "QXmppSocks.h"
#include "QXmppStream.h"
#include "QXmppStreamInitiationIq.h"
#include "QXmppTransferManager.h"
#include "QXmppUtils.h"

// time to try to connect to a SOCKS host (7 seconds)
const int socksTimeout = 7000;

static QString streamHash(const QString &sid, const QString &initiatorJid, const QString &targetJid)
{
    QCryptographicHash hash(QCryptographicHash::Sha1);
    QString str = sid + initiatorJid + targetJid;
    hash.addData(str.toAscii());
    return hash.result().toHex();
}

QXmppTransferFileInfo::QXmppTransferFileInfo()
    : m_size(0)
{
}

QDateTime QXmppTransferFileInfo::date() const
{
    return m_date;
}

void QXmppTransferFileInfo::setDate(const QDateTime &date)
{
    m_date = date;
}

QByteArray QXmppTransferFileInfo::hash() const
{
    return m_hash;
}

void QXmppTransferFileInfo::setHash(const QByteArray &hash)
{
    m_hash = hash;
}

QString QXmppTransferFileInfo::name() const
{
    return m_name;
}

void QXmppTransferFileInfo::setName(const QString &name)
{
    m_name = name;
}

qint64 QXmppTransferFileInfo::size() const
{
    return m_size;
}

void QXmppTransferFileInfo::setSize(qint64 size)
{
    m_size = size;
}

bool QXmppTransferFileInfo::operator==(const QXmppTransferFileInfo &other) const
{
    return other.m_size == m_size &&
        other.m_hash == m_hash &&
        other.m_name == m_name;
}

QXmppTransferJob::QXmppTransferJob(const QString &jid, QXmppTransferJob::Direction direction, QObject *parent)
    : QObject(parent),
    m_blockSize(16384),
    m_direction(direction),
    m_done(0),
    m_error(NoError),
    m_hash(QCryptographicHash::Md5),
    m_iodevice(0),
    m_jid(jid),
    m_method(NoMethod),
    m_state(OfferState),
    m_ibbSequence(0),
    m_socksSocket(0)
{
}

/// Call this method if you wish to abort on ongoing transfer job.
///

void QXmppTransferJob::abort()
{
    terminate(AbortError);
}

/// Call this method if you wish to accept an incoming transfer job.
///

void QXmppTransferJob::accept(QIODevice *iodevice)
{
    if (m_direction == IncomingDirection && m_state == OfferState && !m_iodevice)
    {
        m_iodevice = iodevice;
        setState(QXmppTransferJob::StartState);
    }
}

void QXmppTransferJob::checkData()
{
    if ((m_fileInfo.size() && m_done != m_fileInfo.size()) ||
        (!m_fileInfo.hash().isEmpty() && m_hash.result() != m_fileInfo.hash()))
        terminate(QXmppTransferJob::FileCorruptError);
    else
        terminate(QXmppTransferJob::NoError);
}

/// Returns the job's data for a given role.
///
/// You can associate arbitrary data with the role using setData().

QVariant QXmppTransferJob::data(int role) const
{
    return m_data.value(role);
}

/// Sets the data for a given role to the given value.
///
/// You can set any data you want for use in your application, this
/// data will not be used internally by QXmppTransferManager.

void QXmppTransferJob::setData(int role, const QVariant &value)
{
    m_data.insert(role, value);
}

/// Returns the job's transfer direction.
///

QXmppTransferJob::Direction QXmppTransferJob::direction() const
{
    return m_direction;
}

/// Returns the last error that was encoutered.
///

QXmppTransferJob::Error QXmppTransferJob::error() const
{
    return m_error;
}

QString QXmppTransferJob::jid() const
{
    return m_jid;
}

QXmppTransferFileInfo QXmppTransferJob::fileInfo() const
{
    return m_fileInfo;
}

QDateTime QXmppTransferJob::fileDate() const
{
    return m_fileInfo.date();
}

QByteArray QXmppTransferJob::fileHash() const
{
    return m_fileInfo.hash();
}

QString QXmppTransferJob::fileName() const
{
    return m_fileInfo.name();
}

qint64 QXmppTransferJob::fileSize() const
{
    return m_fileInfo.size();
}

QXmppTransferJob::Method QXmppTransferJob::method() const
{
    return m_method;
}

QString QXmppTransferJob::sid() const
{
    return m_sid;
}

QXmppTransferJob::State QXmppTransferJob::state() const
{
    return m_state;
}

void QXmppTransferJob::setState(QXmppTransferJob::State state)
{
    if (m_state != state)
    {
        m_state = state;
        emit stateChanged(m_state);
    }
}

void QXmppTransferJob::disconnected()
{
    if (m_state == QXmppTransferJob::FinishedState)
        return;

    // terminate transfer
    if (m_direction == QXmppTransferJob::IncomingDirection)
    {
        checkData();
    } else {
        if (fileSize() && m_done != fileSize())
            terminate(QXmppTransferJob::ProtocolError);
        else
            terminate(QXmppTransferJob::NoError);
    }
}

void QXmppTransferJob::receiveData()
{
    if (m_state != QXmppTransferJob::TransferState)
        return;

    // receive data block
    if (m_direction == QXmppTransferJob::IncomingDirection)
    {
        writeData(m_socksSocket->readAll());

        // if we have received all the data, stop here
        if (fileSize() && m_done >= fileSize())
            checkData();
    }
}

void QXmppTransferJob::sendData()
{
    if (m_state != QXmppTransferJob::TransferState)
        return;

    // don't saturate the outgoing socket
    if (m_socksSocket->bytesToWrite() > 2 * m_blockSize)
        return;

    // check whether we have written the whole file
    if (m_fileInfo.size() && m_done >= m_fileInfo.size())
    {
        if (!m_socksSocket->bytesToWrite())
            terminate(QXmppTransferJob::NoError);
        return;
    }

    char *buffer = new char[m_blockSize];
    qint64 length = m_iodevice->read(buffer, m_blockSize);
    if (length < 0)
    {
        delete [] buffer;
        terminate(QXmppTransferJob::FileAccessError);
        return;
    }
    if (length > 0)
    {
        m_socksSocket->write(buffer, length);
        delete [] buffer;
        m_done += length;
        emit progress(m_done, fileSize());
    }
}

void QXmppTransferJob::slotTerminated()
{
    emit stateChanged(m_state);
    if (m_error != NoError)
        emit error(m_error);
    emit finished();
}

void QXmppTransferJob::terminate(QXmppTransferJob::Error cause)
{
    if (m_state == FinishedState)
        return;

    // change state
    m_error = cause;
    m_state = FinishedState;

    // close IO device
    if (m_iodevice)
        m_iodevice->close();

    // close socket
    if (m_socksSocket)
    {
        m_socksSocket->flush();
        m_socksSocket->close();
    }

    // emit signals later
    QTimer::singleShot(0, this, SLOT(slotTerminated()));
}

bool QXmppTransferJob::writeData(const QByteArray &data)
{
    const qint64 written = m_iodevice->write(data);
    if (written < 0)
        return false;
    m_done += written;
    if (!m_fileInfo.hash().isEmpty())
        m_hash.addData(data);
    progress(m_done, m_fileInfo.size());
    return true;
}

QXmppTransferManager::QXmppTransferManager(QXmppStream *stream, QObject *parent)
    : QObject(parent),
    m_stream(stream),
    m_ibbBlockSize(4096),
    m_proxyOnly(false),
    m_socksServer(0),
    m_supportedMethods(QXmppTransferJob::AnyMethod)
{
    // XEP-0047: In-Band Bytestreams
    bool check = QObject::connect(m_stream, SIGNAL(iqReceived(const QXmppIq&)),
        this, SLOT(iqReceived(const QXmppIq&)));
    Q_ASSERT(check);

    check = QObject::connect(m_stream, SIGNAL(ibbCloseIqReceived(const QXmppIbbCloseIq&)),
        this, SLOT(ibbCloseIqReceived(const QXmppIbbCloseIq&)));
    Q_ASSERT(check);

    check = QObject::connect(m_stream, SIGNAL(ibbDataIqReceived(const QXmppIbbDataIq&)),
        this, SLOT(ibbDataIqReceived(const QXmppIbbDataIq&)));
    Q_ASSERT(check);

    check = QObject::connect(m_stream, SIGNAL(ibbOpenIqReceived(const QXmppIbbOpenIq&)),
        this, SLOT(ibbOpenIqReceived(const QXmppIbbOpenIq&)));
    Q_ASSERT(check);

    // XEP-0065: SOCKS5 Bytestreams
    check = QObject::connect(m_stream, SIGNAL(byteStreamIqReceived(const QXmppByteStreamIq&)),
        this, SLOT(byteStreamIqReceived(const QXmppByteStreamIq&)));
    Q_ASSERT(check);

    // XEP-0095: Stream Initiation
    check = QObject::connect(m_stream, SIGNAL(streamInitiationIqReceived(const QXmppStreamInitiationIq&)),
        this, SLOT(streamInitiationIqReceived(const QXmppStreamInitiationIq&)));
    Q_ASSERT(check);

    // start SOCKS server
    m_socksServer = new QXmppSocksServer(this);
    if (m_socksServer->listen())
    {
        connect(m_socksServer, SIGNAL(newConnection(QTcpSocket*, const QString&, quint16)),
            this, SLOT(socksServerConnected(QTcpSocket*, const QString&, quint16)));
    } else {
        qWarning() << "QXmppSocksServer could not start listening";
    }
}

void QXmppTransferManager::byteStreamIqReceived(const QXmppByteStreamIq &iq)
{
    // handle IQ from proxy
    foreach (QXmppTransferJob *job, m_jobs)
    {
        if (job->m_socksProxy.jid() == iq.from() && job->m_requestId == iq.id())
        {
            if (iq.type() == QXmppIq::Result && iq.streamHosts().size() > 0)
            {
                job->m_socksProxy = iq.streamHosts().first();
                socksServerSendOffer(job);
                return;
            }
        }
    }

    if (iq.type() == QXmppIq::Result)
        byteStreamResultReceived(iq);
    else if (iq.type() == QXmppIq::Set)
        byteStreamSetReceived(iq);
}

/// Handle a response to a bystream set, i.e. after we informed the remote party
/// that we connected to a stream host.
void QXmppTransferManager::byteStreamResponseReceived(const QXmppIq &iq)
{
    QXmppTransferJob *job = getJobByRequestId(iq.from(), iq.id());
    if (!job ||
        job->direction() != QXmppTransferJob::IncomingDirection ||
        job->method() != QXmppTransferJob::SocksMethod ||
        job->state() != QXmppTransferJob::StartState)
        return;

    if (iq.type() == QXmppIq::Error)
        job->terminate(QXmppTransferJob::ProtocolError);
}

/// Handle a bytestream result, i.e. after the remote party has connected to
/// a stream host.
void QXmppTransferManager::byteStreamResultReceived(const QXmppByteStreamIq &iq)
{
    QXmppTransferJob *job = getJobByRequestId(iq.from(), iq.id());
    if (!job ||
        job->direction() != QXmppTransferJob::OutgoingDirection ||
        job->method() != QXmppTransferJob::SocksMethod ||
        job->state() != QXmppTransferJob::StartState)
        return;

    // check the stream host
    if (iq.streamHostUsed() == job->m_socksProxy.jid())
    {
        const QXmppByteStreamIq::StreamHost streamHost = job->m_socksProxy;
        m_stream->logger()->log(QXmppLogger::InformationMessage,
            QString("Connecting to proxy: %1 (%2:%3)").arg(
                streamHost.jid(),
                streamHost.host().toString(),
                QString::number(streamHost.port())));

        // connect to proxy
        const QString hostName = streamHash(job->m_sid,
                                            m_stream->configuration().jid(),
                                            job->m_jid);

        QXmppSocksClient *socksClient = new QXmppSocksClient(streamHost.host(), streamHost.port(), job);
        socksClient->connectToHost(hostName, 0);
        // FIXME : this should probably be made asynchronous as it blocks XMPP packet handling
        if (!socksClient->waitForReady(socksTimeout))
        {
            m_stream->logger()->log(QXmppLogger::WarningMessage,
                QString("Failed to connect to proxy: %1 (%2:%3)").arg(
                    streamHost.jid(),
                    streamHost.host().toString(),
                    QString::number(streamHost.port())));
            delete socksClient;
            job->terminate(QXmppTransferJob::ProtocolError);
            return;
        }
        job->m_socksSocket = socksClient;
        connect(job->m_socksSocket, SIGNAL(disconnected()), job, SLOT(disconnected()));

        // activate stream
        QXmppByteStreamIq streamIq;
        streamIq.setType(QXmppIq::Set);
        streamIq.setFrom(m_stream->configuration().jid());
        streamIq.setTo(streamHost.jid());
        streamIq.setSid(job->m_sid);
        streamIq.setActivate(job->m_jid);
        job->m_requestId = streamIq.id();
        m_stream->sendPacket(streamIq);
        return;
    }

    // direction connection, start sending data
    if (!job->m_socksSocket)
    {
        qWarning("Client says they connected to our SOCKS server, but they did not");
        job->terminate(QXmppTransferJob::ProtocolError);
        return;
    }
    job->setState(QXmppTransferJob::TransferState);
    connect(job->m_socksSocket, SIGNAL(disconnected()), job, SLOT(disconnected()));
    connect(job->m_socksSocket, SIGNAL(bytesWritten(qint64)), job, SLOT(sendData()));
    connect(job->m_iodevice, SIGNAL(readyRead()), job, SLOT(sendData()));
    job->sendData();
}

/// Handle a bytestream set, i.e. an invitation from the remote party to connect
/// to a stream host.
void QXmppTransferManager::byteStreamSetReceived(const QXmppByteStreamIq &iq)
{
    QXmppIq response;
    response.setId(iq.id());
    response.setTo(iq.from());

    QXmppTransferJob *job = getJobBySid(iq.from(), iq.sid());
    if (!job ||
        job->direction() != QXmppTransferJob::IncomingDirection ||
        job->method() != QXmppTransferJob::SocksMethod ||
        job->state() != QXmppTransferJob::StartState)
    {
        // the stream is unknown
        QXmppStanza::Error error(QXmppStanza::Error::Auth, QXmppStanza::Error::NotAcceptable);
        error.setCode(406);
        response.setType(QXmppIq::Error);
        response.setError(error);
        m_stream->sendPacket(response);
        return;
    }

    // try connecting to the offered stream hosts
    foreach (const QXmppByteStreamIq::StreamHost &streamHost, iq.streamHosts())
    {
        m_stream->logger()->log(QXmppLogger::InformationMessage,
            QString("Connecting to streamhost: %1 (%2:%3)").arg(
                streamHost.jid(),
                streamHost.host().toString(),
                QString::number(streamHost.port())));

        const QString hostName = streamHash(job->m_sid,
                                            job->m_jid,
                                            m_stream->configuration().jid());

        // try to connect to stream host
        QXmppSocksClient *socksClient = new QXmppSocksClient(streamHost.host(), streamHost.port(), job);
        socksClient->connectToHost(hostName, 0);
        // FIXME : this should probably be made asynchronous as it blocks XMPP packet handling
        if (socksClient->waitForReady(socksTimeout))
        {
            job->setState(QXmppTransferJob::TransferState);
            job->m_socksSocket = socksClient;
            connect(job->m_socksSocket, SIGNAL(readyRead()), job, SLOT(receiveData()));
            connect(job->m_socksSocket, SIGNAL(disconnected()), job, SLOT(disconnected()));

            QXmppByteStreamIq ackIq;
            ackIq.setId(iq.id());
            ackIq.setTo(iq.from());
            ackIq.setType(QXmppIq::Result);
            ackIq.setSid(job->m_sid);
            ackIq.setStreamHostUsed(streamHost.jid());
            m_stream->sendPacket(ackIq);
            return;
        } else {
            m_stream->logger()->log(QXmppLogger::WarningMessage,
                QString("Failed to connect to streamhost: %1 (%2:%3)").arg(
                    streamHost.jid(),
                    streamHost.host().toString(),
                    QString::number(streamHost.port())));
            delete socksClient;
        }
    }

    // could not connect to any stream host
    QXmppStanza::Error error(QXmppStanza::Error::Cancel, QXmppStanza::Error::ItemNotFound);
    error.setCode(404);
    response.setType(QXmppIq::Error);
    response.setError(error);
    m_stream->sendPacket(response);

    job->terminate(QXmppTransferJob::ProtocolError);
}

QXmppTransferJob* QXmppTransferManager::getJobByRequestId(const QString &jid, const QString &id)
{
    foreach (QXmppTransferJob *job, m_jobs)
        if (job->m_jid == jid && job->m_requestId == id)
            return job;
    return 0;
}

QXmppTransferJob* QXmppTransferManager::getJobBySid(const QString &jid, const QString &sid)
{
    foreach (QXmppTransferJob *job, m_jobs)
        if (job->m_jid == jid && job->m_sid == sid)
            return job;
    return 0;
}

void QXmppTransferManager::ibbCloseIqReceived(const QXmppIbbCloseIq &iq)
{
    QXmppIq response;
    response.setTo(iq.from());
    response.setId(iq.id());

    QXmppTransferJob *job = getJobBySid(iq.from(), iq.sid());
    if (!job ||
        job->direction() != QXmppTransferJob::IncomingDirection ||
        job->method() != QXmppTransferJob::InBandMethod)
    {
        // the job is unknown, cancel it
        QXmppStanza::Error error(QXmppStanza::Error::Cancel, QXmppStanza::Error::ItemNotFound);
        response.setType(QXmppIq::Error);
        response.setError(error);
        m_stream->sendPacket(response);
        return;
    }

    // acknowledge the packet
    response.setType(QXmppIq::Result);
    m_stream->sendPacket(response);

    // check received data
    job->checkData();
}

void QXmppTransferManager::ibbDataIqReceived(const QXmppIbbDataIq &iq)
{
    QXmppIq response;
    response.setTo(iq.from());
    response.setId(iq.id());

    QXmppTransferJob *job = getJobBySid(iq.from(), iq.sid());
    if (!job ||
        job->direction() != QXmppTransferJob::IncomingDirection ||
        job->method() != QXmppTransferJob::InBandMethod ||
        job->state() != QXmppTransferJob::TransferState)
    {
        // the job is unknown, cancel it
        QXmppStanza::Error error(QXmppStanza::Error::Cancel, QXmppStanza::Error::ItemNotFound);
        response.setType(QXmppIq::Error);
        response.setError(error);
        m_stream->sendPacket(response);
        return;
    }

    if (iq.sequence() != job->m_ibbSequence)
    {
        // the packet is out of sequence
        QXmppStanza::Error error(QXmppStanza::Error::Cancel, QXmppStanza::Error::UnexpectedRequest);
        response.setType(QXmppIq::Error);
        response.setError(error);
        m_stream->sendPacket(response);
        return;
    }

    // write data
    job->writeData(iq.payload());
    job->m_ibbSequence++;

    // acknowledge the packet
    response.setType(QXmppIq::Result);
    m_stream->sendPacket(response);
}

void QXmppTransferManager::ibbOpenIqReceived(const QXmppIbbOpenIq &iq)
{
    QXmppIq response;
    response.setTo(iq.from());
    response.setId(iq.id());

    QXmppTransferJob *job = getJobBySid(iq.from(), iq.sid());
    if (!job ||
        job->direction() != QXmppTransferJob::IncomingDirection ||
        job->method() != QXmppTransferJob::InBandMethod)
    {
        // the job is unknown, cancel it
        QXmppStanza::Error error(QXmppStanza::Error::Cancel, QXmppStanza::Error::ItemNotFound);
        response.setType(QXmppIq::Error);
        response.setError(error);
        m_stream->sendPacket(response);
        return;
    }

    if (iq.blockSize() > m_ibbBlockSize)
    {
        // we prefer a smaller block size
        QXmppStanza::Error error(QXmppStanza::Error::Modify, QXmppStanza::Error::ResourceConstraint);
        response.setType(QXmppIq::Error);
        response.setError(error);
        m_stream->sendPacket(response);
        return;
    }

    job->m_blockSize = iq.blockSize();
    job->setState(QXmppTransferJob::TransferState);

    // accept transfer
    response.setType(QXmppIq::Result);
    m_stream->sendPacket(response);
}

void QXmppTransferManager::ibbResponseReceived(const QXmppIq &iq)
{
    QXmppTransferJob *job = getJobByRequestId(iq.from(), iq.id());
    if (!job ||
        job->direction() != QXmppTransferJob::OutgoingDirection ||
        job->method() != QXmppTransferJob::InBandMethod ||
        job->state() == QXmppTransferJob::FinishedState)
        return;

    // if the IO device is closed, do nothing
    if (!job->m_iodevice->isOpen())
        return;

    if (iq.type() == QXmppIq::Result)
    {
        const QByteArray buffer = job->m_iodevice->read(job->m_blockSize);
        job->setState(QXmppTransferJob::TransferState);
        if (buffer.size())
        {
            // send next data block
            QXmppIbbDataIq dataIq;
            dataIq.setTo(job->m_jid);
            dataIq.setSid(job->m_sid);
            dataIq.setSequence(job->m_ibbSequence++);
            dataIq.setPayload(buffer);
            job->m_requestId = dataIq.id();
            m_stream->sendPacket(dataIq);

            job->m_done += buffer.size();
            job->progress(job->m_done, job->fileSize());
        } else {
            // close the bytestream
            QXmppIbbCloseIq closeIq;
            closeIq.setTo(job->m_jid);
            closeIq.setSid(job->m_sid);
            job->m_requestId = closeIq.id();
            m_stream->sendPacket(closeIq);

            job->terminate(QXmppTransferJob::NoError);
        }
    }
    else if (iq.type() == QXmppIq::Error)
    {
        // close the bytestream
        QXmppIbbCloseIq closeIq;
        closeIq.setTo(job->m_jid);
        closeIq.setSid(job->m_sid);
        job->m_requestId = closeIq.id();
        m_stream->sendPacket(closeIq);

        job->terminate(QXmppTransferJob::ProtocolError);
    }
}

void QXmppTransferManager::iqReceived(const QXmppIq &iq)
{
    // handle IQ from proxy
    foreach (QXmppTransferJob *job, m_jobs)
    {
        if (job->m_socksProxy.jid() == iq.from() && job->m_requestId == iq.id())
        {
            if (job->m_socksSocket)
            {
                // proxy connection activation result
                if (iq.type() == QXmppIq::Result)
                {
                    // proxy stream activated, start sending data
                    job->setState(QXmppTransferJob::TransferState);
                    connect(job->m_socksSocket, SIGNAL(bytesWritten(qint64)), job, SLOT(sendData()));
                    connect(job->m_iodevice, SIGNAL(readyRead()), job, SLOT(sendData()));
                    job->sendData();
                } else if (iq.type() == QXmppIq::Error) {
                    // proxy stream not activated, terminate
                    qWarning("Could not activate SOCKS5 proxy bytestream");
                    job->terminate(QXmppTransferJob::ProtocolError);
                }
            } else {
                // we could not get host/port from proxy, procede without a proxy
                if (iq.type() == QXmppIq::Error)
                    socksServerSendOffer(job);
            }
            return;
        }
    }

    QXmppTransferJob *job = getJobByRequestId(iq.from(), iq.id());
    if (!job)
        return;

    if (job->method() == QXmppTransferJob::InBandMethod)
        ibbResponseReceived(iq);
    else if (job->method() == QXmppTransferJob::SocksMethod)
        byteStreamResponseReceived(iq);
    else if (iq.type() == QXmppIq::Error) {
        // remote party cancelled stream initiation
        job->terminate(QXmppTransferJob::AbortError);
    }
}

void QXmppTransferManager::jobDestroyed(QObject *object)
{
    m_jobs.removeAll(static_cast<QXmppTransferJob*>(object));
}

void QXmppTransferManager::jobError(QXmppTransferJob::Error error)
{
    QXmppTransferJob *job = qobject_cast<QXmppTransferJob *>(sender());
    if (!job || !m_jobs.contains(job))
        return;

    if (job->direction() == QXmppTransferJob::OutgoingDirection &&
        job->method() == QXmppTransferJob::InBandMethod &&
        error == QXmppTransferJob::AbortError)
    {
        // close the bytestream
        QXmppIbbCloseIq closeIq;
        closeIq.setTo(job->m_jid);
        closeIq.setSid(job->m_sid);
        job->m_requestId = closeIq.id();
        m_stream->sendPacket(closeIq);
    }
}

void QXmppTransferManager::jobFinished()
{
    QXmppTransferJob *job = qobject_cast<QXmppTransferJob *>(sender());
    if (!job || !m_jobs.contains(job))
        return;

    emit finished(job);
}

void QXmppTransferManager::jobStateChanged(QXmppTransferJob::State state)
{
    QXmppTransferJob *job = qobject_cast<QXmppTransferJob *>(sender());
    if (!job || !m_jobs.contains(job))
        return;

    if (job->direction() != QXmppTransferJob::IncomingDirection)
        return;

    // disconnect from the signal
    disconnect(job, SIGNAL(stateChanged(QXmppTransferJob::State)), this, SLOT(jobStateChanged(QXmppTransferJob::State)));

    QXmppStreamInitiationIq response;
    response.setTo(job->jid());
    response.setId(job->m_offerId);

    // the job was refused by the local party
    if (state != QXmppTransferJob::StartState || !job->m_iodevice || !job->m_iodevice->isWritable())
    {
        QXmppStanza::Error error(QXmppStanza::Error::Cancel, QXmppStanza::Error::Forbidden);
        error.setCode(403);

        response.setType(QXmppIq::Error);
        response.setError(error);
        m_stream->sendPacket(response);

        job->terminate(QXmppTransferJob::AbortError);
        return;
    }

    // the job was accepted by the local party
    connect(job, SIGNAL(error(QXmppTransferJob::Error)), this, SLOT(jobError(QXmppTransferJob::Error)));

    QXmppElement value;
    value.setTagName("value");
    if (job->method() == QXmppTransferJob::InBandMethod)
        value.setValue(ns_ibb);
    else if (job->method() == QXmppTransferJob::SocksMethod)
        value.setValue(ns_bytestreams);

    QXmppElement field;
    field.setTagName("field");
    field.setAttribute("var", "stream-method");
    field.appendChild(value);

    QXmppElement x;
    x.setTagName("x");
    x.setAttribute("xmlns", "jabber:x:data");
    x.setAttribute("type", "submit");
    x.appendChild(field);

    QXmppElement feature;
    feature.setTagName("feature");
    feature.setAttribute("xmlns", ns_feature_negotiation);
    feature.appendChild(x);

    response.setType(QXmppIq::Result);
    response.setProfile(QXmppStreamInitiationIq::FileTransfer);
    response.setSiItems(feature);

    m_stream->sendPacket(response);
}

/// Send file to a remote party.
///
/// The remote party will be given the choice to accept or refuse the transfer.
///
QXmppTransferJob *QXmppTransferManager::sendFile(const QString &jid, const QString &fileName, const QString &sid)
{
    QFileInfo info(fileName);

    QXmppTransferFileInfo fileInfo;
    fileInfo.setDate(info.lastModified());
    fileInfo.setName(info.fileName());
    fileInfo.setSize(info.size());

    // open file
    QIODevice *device = new QFile(fileName);
    if (!device->open(QIODevice::ReadOnly))
    {
        delete device;
        device = 0;
    }

    // hash file
    if (device && !device->isSequential())
    {
        QCryptographicHash hash(QCryptographicHash::Md5);
        QByteArray buffer;
        while (device->bytesAvailable())
        {
            buffer = device->read(16384);
            hash.addData(buffer);
        }
        device->reset();
        fileInfo.setHash(hash.result());
    }

    // create job
    return sendFile(jid, device, fileInfo, sid);
}

/// Send file to a remote party.
///
/// The remote party will be given the choice to accept or refuse the transfer.
///
QXmppTransferJob *QXmppTransferManager::sendFile(const QString &jid, QIODevice *device, const QXmppTransferFileInfo &fileInfo, const QString &sid)
{
    QXmppTransferJob *job = new QXmppTransferJob(jid, QXmppTransferJob::OutgoingDirection, this);
    if (sid.isEmpty())
        job->m_sid = generateStanzaHash();
    else
        job->m_sid = sid;
    job->m_fileInfo = fileInfo;
    job->m_iodevice = device;
    if (device)
        device->setParent(job);

    // check file is open
    if (!device || !device->isReadable())
    {
        job->terminate(QXmppTransferJob::FileAccessError);
        return job;
    }

    // check we support some methods
    if (m_supportedMethods == QXmppTransferJob::NoMethod)
    {
        job->terminate(QXmppTransferJob::ProtocolError);
        return job;
    }

    // prepare negotiation
    QXmppElementList items;

    QXmppElement file;
    file.setTagName("file");
    file.setAttribute("xmlns", ns_stream_initiation_file_transfer);
    file.setAttribute("date", datetimeToString(job->fileDate()));
    file.setAttribute("hash", job->fileHash().toHex());
    file.setAttribute("name", job->fileName());
    file.setAttribute("size", QString::number(job->fileSize()));
    items.append(file);
 
    QXmppElement feature;
    feature.setTagName("feature");
    feature.setAttribute("xmlns", ns_feature_negotiation);

    QXmppElement x;
    x.setTagName("x");
    x.setAttribute("xmlns", "jabber:x:data");
    x.setAttribute("type", "form");
    feature.appendChild(x);

    QXmppElement field;
    field.setTagName("field");
    field.setAttribute("var", "stream-method");
    field.setAttribute("type", "list-single");
    x.appendChild(field);

    // add supported stream methods
    if (m_supportedMethods & QXmppTransferJob::InBandMethod)
    {
        QXmppElement option;
        option.setTagName("option");
        field.appendChild(option);

        QXmppElement value;
        value.setTagName("value");
        value.setValue(ns_ibb);
        option.appendChild(value);
    }
    if (m_supportedMethods & QXmppTransferJob::SocksMethod)
    {
        QXmppElement option;
        option.setTagName("option");
        field.appendChild(option);

        QXmppElement value;
        value.setTagName("value");
        value.setValue(ns_bytestreams);
        option.appendChild(value);
    }

    items.append(feature);

    // start job
    m_jobs.append(job);
    connect(job, SIGNAL(destroyed(QObject*)), this, SLOT(jobDestroyed(QObject*)));
    connect(job, SIGNAL(error(QXmppTransferJob::Error)), this, SLOT(jobError(QXmppTransferJob::Error)));
    connect(job, SIGNAL(finished()), this, SLOT(jobFinished()));

    QXmppStreamInitiationIq request;
    request.setType(QXmppIq::Set);
    request.setTo(jid);
    request.setProfile(QXmppStreamInitiationIq::FileTransfer);
    request.setSiItems(items);
    request.setSiId(job->m_sid);
    job->m_requestId = request.id();
    m_stream->sendPacket(request);

    return job;
}

void QXmppTransferManager::socksServerConnected(QTcpSocket *socket, const QString &hostName, quint16 port)
{
    const QString ownJid = m_stream->configuration().jid();
    foreach (QXmppTransferJob *job, m_jobs)
    {
        if (hostName == streamHash(job->m_sid, ownJid, job->jid()) && port == 0)
        {
            job->m_socksSocket = socket;
            return;
        }
    }
    qWarning("QXmppSocksServer got a connection for a unknown stream");
    socket->close();
}

void QXmppTransferManager::socksServerSendOffer(QXmppTransferJob *job)
{
    const QString ownJid = m_stream->configuration().jid();
    QList<QXmppByteStreamIq::StreamHost> streamHosts;

    // discover local IPs
    if (!m_proxyOnly)
    {
        foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces())
        {
            if (!(interface.flags() & QNetworkInterface::IsRunning) ||
                interface.flags() & QNetworkInterface::IsLoopBack)
                continue;

            foreach (const QNetworkAddressEntry &entry, interface.addressEntries())
            {
                if (entry.ip().protocol() != QAbstractSocket::IPv4Protocol ||
                    entry.netmask().isNull() ||
                    entry.netmask() == QHostAddress::Broadcast)
                    continue;

                QXmppByteStreamIq::StreamHost streamHost;
                streamHost.setHost(entry.ip());
                streamHost.setPort(m_socksServer->serverPort());
                streamHost.setJid(ownJid);
                streamHosts.append(streamHost);
            }
        }
    }

    // add proxy
    if (!job->m_socksProxy.jid().isEmpty())
        streamHosts.append(job->m_socksProxy);

    // check we have some stream hosts
    if (!streamHosts.size())
    {
        qWarning("Could not determine local stream hosts");
        job->terminate(QXmppTransferJob::ProtocolError);
        return;
    }

    // send offer
    QXmppByteStreamIq streamIq;
    streamIq.setType(QXmppIq::Set);
    streamIq.setTo(job->m_jid);
    streamIq.setSid(job->m_sid);
    streamIq.setStreamHosts(streamHosts);
    job->m_requestId = streamIq.id();
    m_stream->sendPacket(streamIq);
}

void QXmppTransferManager::streamInitiationIqReceived(const QXmppStreamInitiationIq &iq)
{
    if (iq.type() == QXmppIq::Result)
        streamInitiationResultReceived(iq);
    else if (iq.type() == QXmppIq::Set)
        streamInitiationSetReceived(iq);
}

// The remote party has accepted an outgoing transfer.
void QXmppTransferManager::streamInitiationResultReceived(const QXmppStreamInitiationIq &iq)
{
    QXmppTransferJob *job = getJobByRequestId(iq.from(), iq.id());
    if (!job ||
        job->direction() != QXmppTransferJob::OutgoingDirection ||
        job->state() != QXmppTransferJob::OfferState)
        return;

    foreach (const QXmppElement &item, iq.siItems())
    {
        if (item.tagName() == "feature" && item.attribute("xmlns") == ns_feature_negotiation)
        {
            QXmppElement field = item.firstChildElement("x").firstChildElement("field");
            while (!field.isNull())
            {
                if (field.attribute("var") == "stream-method")
                {
                    if ((field.firstChildElement("value").value() == ns_ibb) &&
                        (m_supportedMethods & QXmppTransferJob::InBandMethod))
                        job->m_method = QXmppTransferJob::InBandMethod;
                    else if ((field.firstChildElement("value").value() == ns_bytestreams) &&
                             (m_supportedMethods & QXmppTransferJob::SocksMethod))
                        job->m_method = QXmppTransferJob::SocksMethod;
                }
                field = field.nextSiblingElement("field");
            }
        }
    }

    // remote party accepted stream initiation
    job->setState(QXmppTransferJob::StartState);
    if (job->method() == QXmppTransferJob::InBandMethod)
    {
        // lower block size for IBB
        job->m_blockSize = m_ibbBlockSize;

        QXmppIbbOpenIq openIq;
        openIq.setTo(job->m_jid);
        openIq.setSid(job->m_sid);
        openIq.setBlockSize(job->m_blockSize);
        job->m_requestId = openIq.id();
        m_stream->sendPacket(openIq);
    } else if (job->method() == QXmppTransferJob::SocksMethod) {
        if (!m_socksServer->isListening())
        {
            qWarning() << "QXmppSocksServer is not listening";
            job->terminate(QXmppTransferJob::ProtocolError);
            return;
        }
        if (!m_proxy.isEmpty())
        {
            job->m_socksProxy.setJid(m_proxy);

            // query proxy
            QXmppByteStreamIq streamIq;
            streamIq.setType(QXmppIq::Get);
            streamIq.setTo(job->m_socksProxy.jid());
            streamIq.setSid(job->m_sid);
            job->m_requestId = streamIq.id();
            m_stream->sendPacket(streamIq);
        } else {
            socksServerSendOffer(job);
        }
    } else {
        qWarning("We received an unsupported method");
        job->terminate(QXmppTransferJob::ProtocolError);
    }
}

void QXmppTransferManager::streamInitiationSetReceived(const QXmppStreamInitiationIq &iq)
{
    QXmppStreamInitiationIq response;
    response.setTo(iq.from());
    response.setId(iq.id());

    // check we support the profile
    if (iq.profile() != QXmppStreamInitiationIq::FileTransfer)
    {
        // FIXME : we should add:
        // <bad-profile xmlns='http://jabber.org/protocol/si'/>
        QXmppStanza::Error error(QXmppStanza::Error::Cancel, QXmppStanza::Error::BadRequest);
        error.setCode(400);

        response.setType(QXmppIq::Error);
        response.setError(error);
        m_stream->sendPacket(response);
        return;
    }

    // check there is a receiver connected to the fileReceived() signal
    if (!receivers(SIGNAL(fileReceived(QXmppTransferJob*))))
    {
        QXmppStanza::Error error(QXmppStanza::Error::Cancel, QXmppStanza::Error::Forbidden);
        error.setCode(403);

        response.setType(QXmppIq::Error);
        response.setError(error);
        m_stream->sendPacket(response);
        return;
    }

    // check the stream type
    QXmppTransferJob *job = new QXmppTransferJob(iq.from(), QXmppTransferJob::IncomingDirection, this);
    int offeredMethods = QXmppTransferJob::NoMethod;
    job->m_offerId = iq.id();
    job->m_sid = iq.siId();
    job->m_mimeType = iq.mimeType();
    foreach (const QXmppElement &item, iq.siItems())
    {
        if (item.tagName() == "feature" && item.attribute("xmlns") == ns_feature_negotiation)
        {
            QXmppElement field = item.firstChildElement("x").firstChildElement("field");
            while (!field.isNull())
            {
                if (field.attribute("var") == "stream-method" && field.attribute("type") == "list-single")
                {
                    QXmppElement option = field.firstChildElement("option");
                    while (!option.isNull())
                    {
                        if (option.firstChildElement("value").value() == ns_ibb)
                            offeredMethods = offeredMethods | QXmppTransferJob::InBandMethod;
                        else if (option.firstChildElement("value").value() == ns_bytestreams)
                            offeredMethods = offeredMethods | QXmppTransferJob::SocksMethod;
                        option = option.nextSiblingElement("option");
                    }
                }
                field = field.nextSiblingElement("field");
            }
        }
        else if (item.tagName() == "file" && item.attribute("xmlns") == ns_stream_initiation_file_transfer)
        {
            job->m_fileInfo.setDate(datetimeFromString(item.attribute("date")));
            job->m_fileInfo.setHash(QByteArray::fromHex(item.attribute("hash").toAscii()));
            job->m_fileInfo.setName(item.attribute("name"));
            job->m_fileInfo.setSize(item.attribute("size").toLongLong());
        }
    }

    // select a method supported by both parties
    int sharedMethods = (offeredMethods & m_supportedMethods);
    if (sharedMethods & QXmppTransferJob::SocksMethod)
        job->m_method = QXmppTransferJob::SocksMethod;
    else if (sharedMethods & QXmppTransferJob::InBandMethod)
        job->m_method = QXmppTransferJob::InBandMethod;
    else
    {
        // FIXME : we should add:
        // <no-valid-streams xmlns='http://jabber.org/protocol/si'/>
        QXmppStanza::Error error(QXmppStanza::Error::Cancel, QXmppStanza::Error::BadRequest);
        error.setCode(400);

        response.setType(QXmppIq::Error);
        response.setError(error);
        m_stream->sendPacket(response);
    
        delete job;
        return;
    }

    // register job
    m_jobs.append(job);
    connect(job, SIGNAL(destroyed(QObject*)), this, SLOT(jobDestroyed(QObject*)));
    connect(job, SIGNAL(finished()), this, SLOT(jobFinished()));
    connect(job, SIGNAL(stateChanged(QXmppTransferJob::State)), this, SLOT(jobStateChanged(QXmppTransferJob::State)));

    // allow user to accept or decline the job
    emit fileReceived(job);
}

/// Return the JID of the bytestream proxy to use for
/// outgoing transfers.
///

QString QXmppTransferManager::proxy() const
{
    return m_proxy;
}

/// Set the JID of the SOCKS5 bytestream proxy to use for
/// outgoing transfers.
///
/// If you set a proxy, when you send a file the proxy will
/// be offered to the recipient in addition to your own IP
/// addresses.
///

void QXmppTransferManager::setProxy(const QString &proxyJid)
{
    m_proxy = proxyJid;
}

/// Return whether the proxy will systematically be used for
/// outgoing SOCKS5 bytestream transfers.
///

bool QXmppTransferManager::proxyOnly() const
{
    return m_proxyOnly;
}

/// Set whether the proxy should systematically be used for
/// outgoing SOCKS5 bytestream transfers.
///
/// \note If you set this to true and do not provide a proxy
/// using setProxy(), your outgoing transfers will fail!
///

void QXmppTransferManager::setProxyOnly(bool proxyOnly)
{
    m_proxyOnly = proxyOnly;
}

/// Return the supported stream methods.
///
/// The methods are a combination of zero or more QXmppTransferJob::Method.
///

int QXmppTransferManager::supportedMethods() const
{
    return m_supportedMethods;
}

/// Set the supported stream methods. This allows you to selectively
/// enable or disable stream methods (In-Band or SOCKS5 bytestreams).
///
/// The methods argument is a combination of zero or more
/// QXmppTransferJob::Method.
///

void QXmppTransferManager::setSupportedMethods(int methods)
{
    m_supportedMethods = (methods & QXmppTransferJob::AnyMethod);
}