summaryrefslogtreecommitdiffstats
path: root/kftpgrabber/src/engine/socket.cpp
blob: afc5341b9d0e32bb60f61453cbffc7c0e71f2fb5 (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
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
/*
 * This file is part of the KFTPGrabber project
 *
 * Copyright (C) 2003-2006 by the KFTPGrabber developers
 * Copyright (C) 2003-2006 Jernej Kos <kostko@jweb-network.net>
 *
 * 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; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
 * NON-INFRINGEMENT.  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, write to the Free Software
 * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 *
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
#include "socket.h"
#include "thread.h"
#include "connectionretry.h"
#include "speedlimiter.h"
#include "cache.h"

#include "misc/config.h"

#include <tdelocale.h>

namespace KFTPEngine {

Socket::Socket(Thread *thread, const TQString &protocol)
 : m_remoteEncoding(new KRemoteEncoding()),
   m_cmdData(0),
   m_thread(thread),
   m_transferBytes(0),
   m_speedLastTime(0),
   m_speedLastBytes(0),
   m_protocol(protocol),
   m_currentCommand(Commands::CmdNone),
   m_errorReporting(true),
   m_shouldAbort(false)
{
  m_commandChain.setAutoDelete(true);
}

Socket::~Socket()
{
  delete m_remoteEncoding;
  
  if (m_connectionRetry)
    delete m_connectionRetry;
}

void Socket::initConfig()
{
  m_config.clear();
  
  // Fill in some default values
  setConfig("feat.epsv", 1);
  setConfig("feat.eprt", 1);
  setConfig("feat.pasv", 1);
  setConfig("feat.size", 1);
  setConfig("ssl.prot_mode", 2);
  setConfig("keepalive.enabled", 1);
  setConfig("keepalive.timeout", 60);
}

void Socket::emitError(ErrorCode code, const TQString &param1)
{
  // Intercept connect and login errors and pass them on to the ConnectionRetry class (if enabled)
  if (getConfigInt("retry") && (code == ConnectFailed || code == LoginFailed)) {
    if (!m_connectionRetry)
      m_connectionRetry = new ConnectionRetry(this);
      
    m_connectionRetry->startRetry();
    return;
  }
  
  TQValueList<EventParameter> params;
  params.append(EventParameter(code));
  params.append(EventParameter(param1));
  
  // Dispatch the event via socket thread
  m_thread->event(Event::EventError, params);
}

void Socket::emitEvent(Event::Type type, const TQString &param1, const TQString &param2)
{
  TQValueList<EventParameter> params;
  params.append(EventParameter(param1));
  params.append(EventParameter(param2));
  
  // Dispatch the event via socket thread
  m_thread->event(type, params);
}

void Socket::emitEvent(Event::Type type, DirectoryListing param1)
{
  TQValueList<EventParameter> params;
  params.append(EventParameter(param1));
  
  // Dispatch the event via socket thread
  m_thread->event(type, params);
}

void Socket::emitEvent(Event::Type type, filesize_t param1)
{
  TQValueList<EventParameter> params;
  params.append(EventParameter(param1));
  
  // Dispatch the event via socket thread
  m_thread->event(type, params);
}

void Socket::emitEvent(Event::Type type, void *param1)
{
  TQValueList<EventParameter> params;
  params.append(EventParameter(param1));
  
  // Dispatch the event via socket thread
  m_thread->event(type, params);
}

void Socket::changeEncoding(const TQString &encoding)
{
  // Alter encoding and change socket config
  m_remoteEncoding->setEncoding(encoding.ascii());
  setConfig("encoding", encoding);
}

void Socket::protoDisconnect()
{
  resetCommandClass(UserAbort);
  
  emitEvent(Event::EventMessage, i18n("Disconnected."));
  emitEvent(Event::EventDisconnect);
}

void Socket::timeoutWait(bool start)
{
  if (start) {
    m_timeoutCounter.start();
  } else {
    m_timeoutCounter = TQTime();
  }
}
    
void Socket::timeoutPing()
{
  m_timeoutCounter.restart();
}
    
void Socket::timeoutCheck()
{
  if (!isConnected())
    return;
  
  if (!m_timeoutCounter.isNull()) {
    Commands::Type command = getCurrentCommand();
    int timeout = 0;
    
    // Ignore timeouts for FXP transfers, since there is no way to do pings
    if (command == Commands::CmdFxp)
      return;
    
    if (command == Commands::CmdGet || command == Commands::CmdPut)
      timeout = KFTPCore::Config::dataTimeout();
    else
      timeout = KFTPCore::Config::controlTimeout();
    
    if (timeout > 0 && m_timeoutCounter.elapsed() > (timeout * 1000)) {
      timeoutWait(false);
      
      // We have a timeout, let's abort
      emitEvent(Event::EventMessage, i18n("Connection timed out."));
      protoDisconnect();
    }
  }
}

void Socket::keepaliveStart()
{
  m_keepaliveCounter.start();
}

void Socket::keepaliveCheck()
{
  // Ignore keepalive if the socket is busy
  if (isBusy() || !isConnected()) {
    m_keepaliveCounter.restart();
    return;
  }
  
  if (getConfigInt("keepalive.enabled") && m_keepaliveCounter.elapsed() > getConfigInt("keepalive.timeout") * 1000) {
    protoKeepAlive();
    
    // Reset the counter
    m_keepaliveCounter.restart();
  }
}

Commands::Type Socket::getCurrentCommand()
{
  if (m_commandChain.count() > 0) {
    TQPtrList<Commands::Base>::iterator chainEnd = m_commandChain.end();
    for (TQPtrList<Commands::Base>::iterator i = m_commandChain.begin(); i != chainEnd; i++) {
      if ((*i)->command() != Commands::CmdNone)
        return (*i)->command();
    }
  }
  
  return m_currentCommand;
}

Commands::Type Socket::getToplevelCommand()
{
  return m_currentCommand;
}

Commands::Type Socket::getPreviousCommand()
{
  if (!isChained())
    return Commands::CmdNone;
  
  if (m_commandChain.count() > 1) {
    Commands::Base *previous = m_commandChain.prev();
    m_commandChain.next();
    
    return previous->command();
  } else {
    return m_currentCommand;
  }
}

void Socket::resetCommandClass(ResetCode code)
{
  if (m_commandChain.count() > 0) {
    Commands::Base *current = m_commandChain.current();
    
    if (current->isProcessing()) {
      current->autoDestruct(code);
      return;
    } else {
      if (!current->isClean())
        current->cleanup();
          
      m_commandChain.remove();
    }
    
    if (code == Ok) {
      nextCommandAsync();
    } else {
      // Command has completed with an error code. We should abort the
      // complete chain.
      resetCommandClass(code);
    }
  } else {
    if (m_cmdData) {
      if (m_cmdData->isProcessing()) {
        m_cmdData->autoDestruct(code);
        return;
      } else {
        if (!m_cmdData->isClean())
          m_cmdData->cleanup();
          
        delete m_cmdData;
        m_cmdData = 0;
      }
    }
    
    if (code == Failed)
      emitError(OperationFailed);
    
    // Reset current command and emit a ready event
    if (getCurrentCommand() != Commands::CmdConnectRetry) {
      setCurrentCommand(Commands::CmdNone);
      emitEvent(Event::EventReady);
      emitEvent(Event::EventState, i18n("Idle."));
    }
    
    setErrorReporting(true);
    m_shouldAbort = false;
  }
}

void Socket::nextCommand()
{
  if (m_commandChain.count() > 0) {
    Commands::Base *current = m_commandChain.current();
    
    current->setProcessing(true);
    current->process();
    current->setProcessing(false);
    
    if (current->isDestructable())
      resetCommandClass(current->resetCode());
  } else if (m_cmdData) {
    m_cmdData->setProcessing(true);
    m_cmdData->process();
    m_cmdData->setProcessing(false);
    
    if (m_cmdData->isDestructable())
      resetCommandClass(m_cmdData->resetCode());
  }
}

void Socket::nextCommandAsync()
{
  m_thread->m_commandQueue.append(Commands::CmdNext);
}

void Socket::wakeup(WakeupEvent *event)
{
  if (m_commandChain.count() > 0) {
    Commands::Base *current = m_commandChain.current();
    
    if (current->isProcessing()) {
      tqDebug("WARNING: Attempted to wakeup a processing socket!");
      return;
    }
    
    current->setProcessing(true);
    current->wakeup(event);
    current->setProcessing(false);
    
    if (current->isDestructable())
      resetCommandClass(current->resetCode());
  } else if (m_cmdData) {
    if (m_cmdData->isProcessing()) {
      tqDebug("WARNING: Attempted to wakeup a processing socket!");
      return;
    }
    
    m_cmdData->setProcessing(true);
    m_cmdData->wakeup(event);
    m_cmdData->setProcessing(false);
    
    if (m_cmdData->isDestructable())
      resetCommandClass(m_cmdData->resetCode());
  }
}

filesize_t Socket::getTransferSpeed()
{
  time_t timeDelta = time(0) - m_speedLastTime;
  
  if (timeDelta == 0)
    return 0;
  
  if (m_speedLastBytes > m_transferBytes)
    m_speedLastBytes = 0;
  
  filesize_t speed = (m_transferBytes - m_speedLastBytes)/(time(0) - m_speedLastTime);
  
  m_speedLastBytes = m_transferBytes;
  m_speedLastTime = time(0);
  
  return speed;
}

void Socket::protoAbort()
{
  m_shouldAbort = true;
  
  if (m_connectionRetry && !m_cmdData)
    m_connectionRetry->abortRetry();
}

// *******************************************************************************************
// ******************************************* STAT ******************************************
// *******************************************************************************************

class FtpCommandStat : public Commands::Base {
public:
    enum State {
      None,
      WaitList
    };
    
    ENGINE_STANDARD_COMMAND_CONSTRUCTOR(FtpCommandStat, Socket, CmdNone)
    
    KURL path;
    
    void process()
    {
      switch (currentState) {
        case None: {
          // Issue a list of the parent directory
          currentState = WaitList;
          socket()->setErrorReporting(false);
          socket()->protoList(path.directory());
          break;
        }
        case WaitList: {
          // Now just extract what we need
          TQValueList<DirectoryEntry> list = socket()->getLastDirectoryListing().list();
          TQValueList<DirectoryEntry>::iterator listEnd = list.end();
          for (TQValueList<DirectoryEntry>::iterator i = list.begin(); i != listEnd; i++) {
            if ((*i).filename() == path.fileName()) {
              socket()->m_lastStatResponse = *i;
              socket()->resetCommandClass();
              return;
            }
          }
          
          // We found no such file
          socket()->m_lastStatResponse = DirectoryEntry();
          socket()->resetCommandClass();
          break;
        }
      }
    }
};

void Socket::protoStat(const KURL &path)
{
  // Lookup the cache first and don't even try to list if cached
  DirectoryListing cached = Cache::self()->findCached(this, path.directory());
  if (cached.isValid()) {
    TQValueList<DirectoryEntry> list = cached.list();
    TQValueList<DirectoryEntry>::iterator listEnd = list.end();
    for (TQValueList<DirectoryEntry>::iterator i = list.begin(); i != listEnd; i++) {
      if ((*i).filename() == path.fileName()) {
        m_lastStatResponse = *i;
        nextCommandAsync();
        return;
      }
    }
    
    // Cached is valid but file can't be found
    m_lastStatResponse = DirectoryEntry();
    nextCommandAsync();
    return;
  }
  
  // Not cached, let's do a real listing
  FtpCommandStat *stat = new FtpCommandStat(this);
  stat->path = path;
  addToCommandChain(stat);
  nextCommand();
}

// *******************************************************************************************
// ****************************************** SCAN *******************************************
// *******************************************************************************************

class FtpCommandScan : public Commands::Base {
public:
    enum State {
      None,
      SentList,
      ProcessList,
      ScannedDir
    };
    
    ENGINE_STANDARD_COMMAND_CONSTRUCTOR(FtpCommandScan, Socket, CmdNone)
    
    TQValueList<DirectoryEntry> currentList;
    TQValueList<DirectoryEntry>::const_iterator currentEntry;
    
    TQString currentDirectory;
    DirectoryTree *currentTree;
    
    void cleanup()
    {
      // We didn't emit the tree, so we should free it
      if (!socket()->isChained())
        delete currentTree;
    }
    
    void process()
    {
      // NOTE: The missing breaks are mising for a purpuse! Do not dare to add them ;)
      switch (currentState) {
        case None: {
          // We would like to disable error reporting
          socket()->setErrorReporting(false);
          
          // Issue a directory listing on the given URL
          currentState = SentList;
          socket()->protoList(currentDirectory);
          break;
        }
        case SentList: {
          currentList = socket()->getLastDirectoryListing().list();
          qHeapSort(currentList);
          
          currentEntry = currentList.begin();
          currentState = ProcessList;
          
          // Empty listing, we are done
          if (currentEntry == currentList.end()) {
            if (socket()->isChained()) {
              socket()->resetCommandClass();
            } else {
              // We are the toplevel scan command
              markClean();
              
              socket()->emitEvent(Event::EventScanComplete, currentTree);
              socket()->emitEvent(Event::EventMessage, i18n("Scan complete."));
              socket()->resetCommandClass();
            }
            
            return;
          }
        }
        case ProcessList: {
          if ((*currentEntry).isDirectory()) {
            // A directory entry
            DirectoryTree *tree = currentTree->addDirectory(*currentEntry);
            currentState = ScannedDir;
            
            FtpCommandScan *scan = new FtpCommandScan(socket());
            scan->currentDirectory = currentDirectory + "/" + (*currentEntry).filename();
            scan->currentTree = tree;
            socket()->addToCommandChain(scan);
            socket()->nextCommandAsync();
            return;
          } else {
            // A file entry
            currentTree->addFile(*currentEntry);
          }
        }
        case ScannedDir: {
          currentState = ProcessList;
          
          if (++currentEntry == currentList.end()) {
            // We are done
            if (socket()->isChained()) {
              socket()->resetCommandClass();
            } else {
              // We are the toplevel scan command
              markClean();
              
              socket()->emitEvent(Event::EventScanComplete, currentTree);
              socket()->emitEvent(Event::EventMessage, i18n("Scan complete."));
              socket()->resetCommandClass();
            }
          } else {
            socket()->nextCommandAsync();
          }
          break;
        }
      }
    }
};

void Socket::protoScan(const KURL &path)
{
  emitEvent(Event::EventMessage, i18n("Starting recursive directory scan..."));
  
  // We have to create a new command class manually, since we need to set the
  // currentTree parameter
  FtpCommandScan *scan = new FtpCommandScan(this);
  scan->currentDirectory = path.path();
  scan->currentTree = new DirectoryTree(DirectoryEntry());
  m_cmdData = scan;
  m_cmdData->process();
}

// *******************************************************************************************
// ***************************************** DELETE ******************************************
// *******************************************************************************************

class FtpCommandDelete : public Commands::Base {
public:
    enum State {
      None,
      VerifyDir,
      SimpleRemove,
      SentList,
      ProcessList,
      DeletedDir,
      DeletedFile
    };
    
    ENGINE_STANDARD_COMMAND_CONSTRUCTOR(FtpCommandDelete, Socket, CmdDelete)
    
    TQValueList<DirectoryEntry> currentList;
    TQValueList<DirectoryEntry>::const_iterator currentEntry;
    
    KURL destinationPath;
    
    void process()
    {
      switch (currentState) {
        case None: {
          // We have to determine if the destination is a file or a directory
          // TODO use cached information
          if (socket()->isChained()) {
            // We know that it is a directory
            currentState = SentList;
            socket()->protoList(destinationPath);
          } else {
            currentState = VerifyDir;
            socket()->protoStat(destinationPath);
          }
          break;
        }
        case VerifyDir: {
          DirectoryEntry entry = socket()->getStatResponse();
          
          if (entry.filename().isEmpty()) {
            // The file doesn't exist, abort
            socket()->resetCommandClass(Failed);
          } else {
            if (entry.isDirectory()) {
              // It is a directory, remove recursively
              currentState = SentList;
              socket()->protoList(destinationPath);
            } else {
              // A single file, a simple remove
              currentState = SimpleRemove;
              socket()->setConfig("params.remove.directory", 0);
              socket()->protoRemove(destinationPath);
            }
          }          
          break;
        }
        case SimpleRemove: {
          if (!socket()->isChained())
            socket()->emitEvent(Event::EventReloadNeeded);
          socket()->resetCommandClass();
          break;
        }
        case SentList: {
          currentList = socket()->getLastDirectoryListing().list();
          currentEntry = currentList.begin();
          currentState = ProcessList;
          
          // Empty listing, we are done
          if (currentEntry == currentList.end()) {
            if (socket()->isChained())
              socket()->resetCommandClass();
            else {
              // We are the top level command class, remove the destination dir
              currentState = SimpleRemove;
              socket()->setConfig("params.remove.directory", 1);
              socket()->protoRemove(destinationPath);
            }
            
            return;
          }
        }
        case ProcessList: {
          KURL childPath = destinationPath;
          childPath.addPath((*currentEntry).filename());
          
          if ((*currentEntry).isDirectory()) {
            // A directory, chain another delete command
            currentState = DeletedDir;
            
            // Chain manually, since we need to set some parameters
            FtpCommandDelete *del = new FtpCommandDelete(socket());
            del->destinationPath = childPath;
            socket()->addToCommandChain(del);
            socket()->nextCommand();
          } else {
            // A file entry - remove
            currentState = DeletedFile;
            socket()->setConfig("params.remove.directory", 0);
            socket()->protoRemove(childPath);
          }
          break;
        }
        case DeletedDir: {
          // We have to remove the empty directory
          KURL childPath = destinationPath;
          childPath.addPath((*currentEntry).filename());
          
          currentState = DeletedFile;
          socket()->setConfig("params.remove.directory", 1);
          socket()->protoRemove(childPath);
          break;
        }
        case DeletedFile: {
          currentState = ProcessList;
          
          if (++currentEntry == currentList.end()) {
            if (socket()->isChained())
              socket()->resetCommandClass();
            else {
              // We are the top level command class, remove the destination dir
              currentState = SimpleRemove;
              socket()->setConfig("params.remove.directory", 1);
              socket()->protoRemove(destinationPath);
            }
          } else
            socket()->nextCommand();
          break;
        }
      }
    }
};

void Socket::protoDelete(const KURL &path)
{
  // We have to create a new command class manually to set some parameter
  FtpCommandDelete *del = new FtpCommandDelete(this);
  del->destinationPath = path;
  m_cmdData = del;
  m_cmdData->process();
}

// *******************************************************************************************
// ***************************************** CHMOD *******************************************
// *******************************************************************************************

class FtpCommandRecursiveChmod : public Commands::Base {
public:
    enum State {
      None,
      VerifyDir,
      SimpleChmod,
      SentList,
      ProcessList,
      ChmodedDir,
      ChmodedFile
    };
    
    ENGINE_STANDARD_COMMAND_CONSTRUCTOR(FtpCommandRecursiveChmod, Socket, CmdChmod)
    
    TQValueList<DirectoryEntry> currentList;
    TQValueList<DirectoryEntry>::const_iterator currentEntry;
    
    KURL destinationPath;
    int mode;
    
    void process()
    {
      switch (currentState) {
        case None: {
          // We have to determine if the destination is a file or a directory
          if (socket()->isChained()) {
            // We know that it is a directory
            currentState = SentList;
            socket()->protoList(destinationPath);
          } else {
            currentState = VerifyDir;
            socket()->protoStat(destinationPath);
          }
          break;
        }
        case VerifyDir: {
          DirectoryEntry entry = socket()->getStatResponse();
          
          if (entry.filename().isEmpty()) {
            // The file doesn't exist, abort
            socket()->resetCommandClass(Failed);
          } else {
            if (entry.isDirectory()) {
              // It is a directory, chmod recursively
              currentState = SentList;
              socket()->protoList(destinationPath);
            } else {
              // A single file, a simple chmod
              currentState = SimpleChmod;
              socket()->protoChmodSingle(destinationPath, mode);
            }
          }          
          break;
        }
        case SimpleChmod: {
          socket()->resetCommandClass();
          break;
        }
        case SentList: {
          currentList = socket()->getLastDirectoryListing().list();
          currentEntry = currentList.begin();
          currentState = ProcessList;
          
          // Empty listing, we are done
          if (currentEntry == currentList.end()) {
            if (socket()->isChained())
              socket()->resetCommandClass();
            else {
              // We are the top level command class, chmod the destination dir
              currentState = SimpleChmod;
              socket()->protoChmodSingle(destinationPath, mode);
            }
            
            return;
          }
        }
        case ProcessList: {
          KURL childPath = destinationPath;
          childPath.addPath((*currentEntry).filename());
          
          if ((*currentEntry).isDirectory()) {
            // A directory, chain another recursive chmod command
            currentState = ChmodedDir;
            
            // Chain manually, since we need to set some parameters
            FtpCommandRecursiveChmod *cm = new FtpCommandRecursiveChmod(socket());
            cm->destinationPath = childPath;
            cm->mode = mode;
            socket()->addToCommandChain(cm);
            socket()->nextCommand();
          } else {
            // A file entry - remove
            currentState = ChmodedFile;
            socket()->protoChmodSingle(childPath, mode);
          }
          break;
        }
        case ChmodedDir: {
          // We have to chmod the directory
          KURL childPath = destinationPath;
          childPath.addPath((*currentEntry).filename());
          
          currentState = ChmodedFile;
          socket()->protoChmodSingle(childPath, mode);
          break;
        }
        case ChmodedFile: {
          currentState = ProcessList;
          
          if (++currentEntry == currentList.end()) {
            if (socket()->isChained())
              socket()->resetCommandClass();
            else {
              // We are the top level command class, chmod the destination dir
              currentState = SimpleChmod;
              socket()->protoChmodSingle(destinationPath, mode);
            }
          } else
            socket()->nextCommand();
          break;
        }
      }
    }
};

void Socket::protoChmod(const KURL &path, int mode, bool recursive)
{
  if (recursive) {
    // We have to create a new command class manually to set some parameters
    FtpCommandRecursiveChmod *cm = new FtpCommandRecursiveChmod(this);
    cm->destinationPath = path;
    cm->mode = mode;
    m_cmdData = cm;
    m_cmdData->process();
  } else {
    // No recursive, just chmod a single file
    protoChmodSingle(path, mode);
  }
}

}