summaryrefslogtreecommitdiffstats
path: root/kioslave/smtp/smtp.h
blob: 571375bd4a7ddc0fab634bbcfa694e25056d5ebb (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
/* -*- c++ -*-
 * Copyright (c) 2000, 2001 Alex Zepeda <zipzippy@sonic.net>
 * Copyright (c) 2001 Michael Häckel <Michael@Haeckel.Net>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#ifndef _SMTP_H
#define _SMTP_H

#include <kio/tcpslavebase.h>

#include "capabilities.h"

#include <qstring.h>
#include <qptrqueue.h>

class KURL;
class QCString;
template <typename T> class QMemArray;
typedef QMemArray<char> QByteArray;

namespace KioSMTP {
  class Response;
  class TransactionState;
  class Command;
}

class SMTPProtocol : public KIO::TCPSlaveBase {
  friend class KioSMTP::Command;
public:
  SMTPProtocol(const QCString & pool, const QCString & app, bool useSSL);
  virtual ~ SMTPProtocol();

  virtual void setHost(const QString & host, int port,
                       const QString & user, const QString & pass);

  virtual void special(const QByteArray & aData);
  virtual void put(const KURL & url, int permissions, bool overwrite,
                   bool resume);
  virtual void stat(const KURL & url);
  virtual void openConnection();
  virtual void closeConnection();

protected:

  bool smtp_open(const QString& fakeHostname = QString::null);

  /** Closes the connection. If @p nice is true (default), then QUIT
      is sent and it's reponse waited for. */
  void smtp_close( bool nice=true );

  /** Execute command @p cmd */
  bool execute( KioSMTP::Command * cmd, KioSMTP::TransactionState * ts=0 );
  /** Execute a command of type @p type */
  bool execute( int type, KioSMTP::TransactionState * ts=0 );
  /** Execute the queued commands. If something goes horribly wrong
      (sending command oline fails, getting response fails or some
      command raises the failedFatally() flag in @p ts, shuts down the
      connection with <code>smtp_close( false )</code>. If The
      transaction fails gracefully (<code>ts->failed()</code> is
      true), issues a RSET command.

      @return true if transaction succeeded, false otherwise.
  **/
  bool executeQueuedCommands( KioSMTP::TransactionState * ts );

  /** Parse a single response from the server. Single- vs. multiline
      responses are correctly detected.

      @param ok if not 0, returns whether response parsing was
                successful. Don't confuse this with negative responses
                (e.g. 5xx), which you can check for using
                @ref Response::isNegative()
      @return the @ref Response object representing the server response.
  **/
  KioSMTP::Response getResponse( bool * ok );

  bool authenticate();
  void parseFeatures( const KioSMTP::Response & ehloResponse );

  bool sendCommandLine( const QCString & cmd );
  QCString collectPipelineCommands( KioSMTP::TransactionState * ts );
  bool batchProcessResponses( KioSMTP::TransactionState * ts );

  /** This is a pure convenience wrapper around
      @ref KioSMTP::Capabilities::have() */
  bool haveCapability( const char * cap ) const {
    return mCapabilities.have( cap );
  }

  /** @return true is pipelining is available and allowed by metadata */
  bool canPipelineCommands() const {
    return haveCapability("PIPELINING") && metaData("pipelining") != "off" ;
  }

  /** Wrapper around getsockopt(..., SO_SNDBUF,...) */
  unsigned int sendBufferSize() const;

  /** This is a pure convenience wrapper around
      @ref KioSMTP::Capabilities::createSpecialResponse */
  QString createSpecialResponse() const {
    return mCapabilities.createSpecialResponse( usingTLS() || haveCapability( "STARTTLS" ) );
  }

  void queueCommand( KioSMTP::Command * command ) {
    mPendingCommandQueue.enqueue( command );
  }
  void queueCommand( int type );

  unsigned short m_iOldPort;
  bool m_opened;
  QString m_sServer, m_sOldServer;
  QString m_sUser, m_sOldUser;
  QString m_sPass, m_sOldPass;
  QString m_hostname;

  KioSMTP::Capabilities mCapabilities;

  typedef QPtrQueue<KioSMTP::Command> CommandQueue;
  CommandQueue mPendingCommandQueue;
  CommandQueue mSentCommandQueue;
};

#endif // _SMTP_H